<commit_before>require 'tempfile'
require 'tmpdir'
require 'minitest/autorun'
require 'rugged'
require 'pp'

module Rugged
  class TestCase < Minitest::Test
    # Set up some isolation for our tests so we don't try to touch any
    # configuration from the user running the tests
    def before_setup
      @configdir ||= begin
        path = Dir.mktmpdir("rugged-config")
        Rugged::Settings['search_path_global'] = path
        Rugged::Settings['search_path_xdg'] = path
        Rugged::Settings['search_path_system'] = path
      end
      super
    end

    # Automatically clean up created fixture repos after each test run
    def after_teardown
      Rugged::TestCase::FixtureRepo.teardown
      FileUtils.remove_entry_secure(@configdir)
      super
    end

    module FixtureRepo
      # Create a new, empty repository.
      def self.empty(*args)
        path = Dir.mktmpdir("rugged-empty")
        ensure_cleanup(path)
        Rugged::Repository.init_at(path, *args)
      end

      # Create a repository based on a rugged fixture repo.
      def self.from_rugged(name, *args)
        path = Dir.mktmpdir("rugged-#{name}")
        ensure_cleanup(path)

        FileUtils.cp_r(File.join(TestCase::TEST_DIR, "fixtures", name, "."), path)

        prepare(path)

        Rugged::Repository.new(path, *args).tap do |repo|
          rewrite_gitmodules(repo) unless repo.bare?
        end
      end

      # Create a repository based on a libgit2 fixture repo.
      def self.from_libgit2(name, *args)
        path = Dir.mktmpdir("rugged-libgit2-#{name}")
        ensure_cleanup(path)

        FileUtils.cp_r(File.join(TestCase::LIBGIT2_FIXTURE_DIR, name, "."), path)

        prepare(path)

        Rugged::Repository.new(path, *args).tap do |repo|
          rewrite_gitmodules(repo) unless repo.bare?
        end
      end

      # Create a repository cloned from another Rugged::Repository instance.
      def self.clone(repository)
        path = Dir.mktmpdir("rugged")
        ensure_cleanup(path)

        `git clone --quiet -- #{repository.path} #{path}`

        Rugged::Repository.new(path)
      end

      def self.prepare(path)
        Dir.chdir(path) do
          File.rename(".gitted", ".git") if File.exist?(".gitted")
          File.rename("gitattributes", ".gitattributes") if File.exist?("gitattributes")
          File.rename("gitignore", ".gitignore") if File.exist?("gitignore")
        end
      end
<commit_msg>Adjust the fixture copies to go a level deeper<commit_after>require 'tempfile'
require 'tmpdir'
require 'minitest/autorun'
require 'rugged'
require 'pp'

module Rugged
  class TestCase < Minitest::Test
    # Set up some isolation for our tests so we don't try to touch any
    # configuration from the user running the tests
    def before_setup
      @configdir ||= begin
        path = Dir.mktmpdir("rugged-config")
        Rugged::Settings['search_path_global'] = path
        Rugged::Settings['search_path_xdg'] = path
        Rugged::Settings['search_path_system'] = path
      end
      super
    end

    # Automatically clean up created fixture repos after each test run
    def after_teardown
      Rugged::TestCase::FixtureRepo.teardown
      FileUtils.remove_entry_secure(@configdir)
      super
    end

    module FixtureRepo
      # Create a new, empty repository.
      def self.empty(*args)
        path = Dir.mktmpdir("rugged-empty")
        ensure_cleanup(path)
        Rugged::Repository.init_at(path, *args)
      end

      # Create a repository based on a rugged fixture repo.
      def self.from_rugged(name, *args)
        path = Dir.mktmpdir("rugged-#{name}")
        ensure_cleanup(path)

        copy_fixture(path, name, File.join(TestCase::TEST_DIR, "fixtures", name, "."), *args)
      end

      # Create a repository based on a libgit2 fixture repo.
      def self.from_libgit2(name, *args)
        path = Dir.mktmpdir("rugged-libgit2-#{name}")
        ensure_cleanup(path)

        copy_fixture(path, name, File.join(TestCase::LIBGIT2_FIXTURE_DIR, name, "."), *args)
      end

      def self.copy_fixture(path, name, fixture_prefix, *args)
        repo_path = File.join(path, name)
        FileUtils.cp_r(fixture_prefix, repo_path)

        prepare(repo_path)

        Rugged::Repository.new(repo_path, *args).tap do |repo|
          rewrite_gitmodules(repo) unless repo.bare?
        end
      end

      # Create a repository cloned from another Rugged::Repository instance.
      def self.clone(repository)
        path = Dir.mktmpdir("rugged")
        ensure_cleanup(path)

        `git clone --quiet -- #{repository.path} #{path}`

        Rugged::Repository.new(path)
      end

      def self.prepare(path)
        Dir.chdir(path) do
          File.rename(".gitted", ".git") if File.exist?(".gitted")
          File.rename("gitattributes", ".gitattributes") if File.exist?("gitattributes")
          File.rename("gitignore", ".gitignore") if File.exist?("gitignore")
        end
      end

{'fraction_non_alphanumeric': 0.11057498994772819, 'fraction_numerical': 0.0016083634901487736, 'mean_word_length': 2.6988847583643123, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1833653', 'n_tokens_mistral': 1531, 'n_tokens_neox': 1506, 'n_words': 368}