<commit_before>unless ENV['ANCHOR_TASK']
  require 'coveralls'
  Coveralls.wear!
end

$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
require 'ttnt'
require 'helpers/git_helper'
require 'helpers/rake_helper'

require 'rugged'
require 'minitest/autorun'

module TTNT
  module TestCase
    class FizzBuzz < Minitest::Test
      FIXTURE_DIR = File.join(__dir__, 'fixtures/fizzbuzz')

      include GitHelper
      include RakeHelper

      def before_setup
        super
        prepare_git_repository
        @pwd = Dir.pwd
        Dir.chdir(@repo.workdir)
      end

      def after_teardown
        Dir.chdir(@pwd)
        FileUtils.remove_entry_secure(@tmpdir)
        super
      end

      private

      def prepare_git_repository
        @tmpdir = Dir.mktmpdir('ttnt_repository')
        @repo = Rugged::Repository.init_at(@tmpdir)
        populate_with_fixtures
        load_rakefile("#{@tmpdir}/Rakefile")
        anchor_and_commit
        make_change_fizz_branch
      end

      def populate_with_fixtures
        copy_fixture('Rakefile', "#{@tmpdir}/Rakefile")
        git_commit_am('Add Rakefile')
        copy_fixture('fizzbuzz.rb', "#{@tmpdir}/fizzbuzz.rb")
        git_commit_am('Add fizzbuzz code')
        copy_fixture('fizz_test.rb', "#{@tmpdir}/fizz_test.rb")
        copy_fixture('buzz_test.rb', "#{@tmpdir}/buzz_test.rb")
        git_commit_am('Add fizzbuzz tests')
      end

      def anchor_and_commit
        @anchored_sha = @repo.head.target_id
        rake('ttnt:test:anchor')
        git_commit_am('Add TTNT generated files')
      end

      def make_change_fizz_branch
        git_checkout_b('change_fizz')
        fizzbuzz_file = "#{@repo.workdir}/fizzbuzz.rb"
        new_content = File.read(fizzbuzz_file)
                          .gsub(/"fizzbuzz"$/, '"fizzbizz"' + "\n" * 10) # diff uglifier
                          .gsub(/"fizz"$/, '"foo"')
        File.write(fizzbuzz_file, new_content)
        git_commit_am('Change fizz code')
        @repo.checkout('master')
      end

      def copy_fixture(src, dest)
        unless File.directory?(File.dirname(dest))
          FileUtils.mkdir_p(File.dirname(dest))
        end
        FileUtils.cp("#{FIXTURE_DIR}/#{src}", dest)
      end

      def capture
        captured_stream     = Tempfile.new("stdout")
        origin_stream       = $stdout.dup
        captured_stream_err = Tempfile.new("stderr")
        origin_stream_err   = $stderr.dup
        $stdout.reopen(captured_stream)
        $stderr.reopen(captured_stream_err)

        yield

        $stdout.rewind
        $stderr.rewind
        return { stdout: captured_stream.read, stderr: captured_stream_err.read }
      ensure
        captured_stream.close
        captured_stream.unlink
        captured_stream_err.close
        captured_stream_err.unlink
        $stdout.reopen(origin_stream)
        $stderr.reopen(origin_stream_err)
      end
    end
  end
end<commit_msg>Create TTNT::TestCase::Base and make it possible to make many test cases<commit_after>unless ENV['ANCHOR_TASK']
  require 'coveralls'
  Coveralls.wear!
end

$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
require 'ttnt'
require 'helpers/git_helper'
require 'helpers/rake_helper'

require 'rugged'
require 'minitest/autorun'

module TTNT
  module TestCase
    class Base < Minitest::Test
      include GitHelper
      include RakeHelper

      def fixture_dir
        raise '`fixture_dir` method is not implemented.'
      end

      def before_setup
        super
        prepare_git_repository
        @pwd = Dir.pwd
        Dir.chdir(@repo.workdir)
      end

      def after_teardown
        Dir.chdir(@pwd)
        FileUtils.remove_entry_secure(@tmpdir)
        super
      end

      private

      def prepare_git_repository
        @tmpdir = Dir.mktmpdir('ttnt_repository')
        @repo = Rugged::Repository.init_at(@tmpdir)
        populate_with_fixtures
        load_rakefile("#{@tmpdir}/Rakefile")
        anchor_and_commit
        after_preparing_git_repository
      end

      def populate_with_fixtures
        Dir.entries(fixture_dir).each do |file|
          next if file.start_with?('.')
          copy_fixture(file, "#{@tmpdir}/#{file}")
          git_commit_am("Add #{file}")
        end
      end

      def anchor_and_commit
        @anchored_sha = @repo.head.target_id
        rake('ttnt:test:anchor')
        git_commit_am('Add TTNT generated files')
      end

      def copy_fixture(src, dest)
        unless File.directory?(File.dirname(dest))
          FileUtils.mkdir_p(File.dirname(dest))
        end
        FileUtils.cp("#{fixture_dir}/#{src}", dest)
      end

      def capture
        captured_stream     = Tempfile.new("stdout")
        origin_stream       = $stdout.dup
        captured_stream_err = Tempfile.new("stderr")
        origin_stream_err   = $stderr.dup
        $stdout.reopen(captured_stream)
        $stderr.reopen(captured_stream_err)

        yield

        $stdout.rewind
        $stderr.rewind
        return { stdout: captured_stream.read, stderr: captured_stream_err.read }
      ensure
        captured_stream.close
        captured_stream.unlink
        captured_stream_err.close
        captured_stream_err.unlink
        $stdout.reopen(origin_stream)
        $stderr.reopen(origin_stream_err)
      end
    end

    class FizzBuzz < Base
      def fixture_dir
        File.join(__dir__, 'fixtures/fizzbuzz')
      end

      private

      def after_preparing_git_repository
        make_change_fizz_branch
      end

      def make_change_fizz_branch
        git_checkout_b('change_fizz')
        fizzbuzz_file = "#{@repo.workdir}/fizzbuzz.rb"
        new_content = File.read(fizzbuzz_file)
                          .gsub(/"fizzbuzz"$/, '"fizzbizz"' + "\n" * 10) # diff uglifier
                          .gsub(/"fizz"$/, '"foo"')
        File.write(fizzbuzz_file, new_content)
        git_commit_am('Change fizz code')
        @repo.checkout('master')
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.10030090270812438, 'fraction_numerical': 0.0006686726847208291, 'mean_word_length': 2.3518207282913166, 'pattern_counts': {'":': 0, '<': 6, '<?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': '2599529', 'n_tokens_mistral': 1960, 'n_tokens_neox': 1931, 'n_words': 326}