<commit_before>require "yaml"
require "slave"
require "pathname"
require "test/unit/ui/console/testrunner"

# Just declare the module to prevent deeply nested code below
module Nestor
  module Mappers
    module Rails
    end
  end
end

module Nestor::Mappers::Rails
  module Test
    class Unit
      # Returns the path to the script this {Mapper} uses.
      def self.default_script_path
        Pathname.new(File.dirname(__FILE__) + "/rails_test_unit.rb")
      end

      # Utility method to extract data from a Test::Unit failure.
      #
      # @param failure [Test::Unit::Failure, Test::Unit::Error] The Test::Unit failure or error from which to extract information.
      # @param test_files [Array<String>] The list of files that might have generated this failure.  This is used to detect the file that caused the failure.
      #
      # @return [String, String] Returns the filename and test name as a 2 element Array.
      def self.parse_failure(failure, test_files)
        filename = if failure.respond_to?(:location) then
                     failure.location.map do |loc|
                       filename = loc.split(":", 2).first
                       test_files.detect {|tf| filename.include?(tf)}
                     end.compact.first
                   elsif failure.respond_to?(:exception) then
                     failure.exception.backtrace.map do |loc|
                       filename = loc.split(":", 2).first
                       loc = loc[1..-1] if loc[0,1] == "/"
                       test_files.detect {|tf| filename.include?(tf)}
                     end.compact.first
                   else
                     raise "Unknown object type received as failure: #{failure.inspect} doesn't have #exception or #location methods."
                   end

        test_name = failure.test_name.split("(", 2).first.strip.sub(/\.$/, "")

        [filename, test_name]
      end

      # Logs a message to STDOUT.  This implementation forks, so the #log method also
      # provides the PID of the logger.
      def log(message)
        STDOUT.printf "[%d] %s - %s\n", Process.pid, Time.now.strftime("%H:%M:%S"), message
        STDOUT.flush
      end

      # Runs absolutely all tests as found by walking test/.
      def run_all
        Slave.object do
          log "Run all tests"
          test_files = load_test_files(["test/"])

          ActiveRecord::Base.establish_connection if defined?(ActiveRecord)
          test_runner = ::Nestor::Mappers::Rails::Test::TestRunner.new(nil)
          result = ::Test::Unit::AutoRunner.run(false, nil, []) do |autorunner|
            autorunner.runner = lambda { test_runner }
          end

          # Returns a Hash which the parent process will retrieve
          report(test_runner, test_files)
        end
      end

      # Runs only the named files, and optionally focuses on only a couple of tests<commit_msg>Putting the slash keeps it for all child files, resulting in strings that don't compare equally.<commit_after>require "yaml"
require "slave"
require "pathname"
require "test/unit/ui/console/testrunner"

# Just declare the module to prevent deeply nested code below
module Nestor
  module Mappers
    module Rails
    end
  end
end

module Nestor::Mappers::Rails
  module Test
    class Unit
      # Returns the path to the script this {Mapper} uses.
      def self.default_script_path
        Pathname.new(File.dirname(__FILE__) + "/rails_test_unit.rb")
      end

      # Utility method to extract data from a Test::Unit failure.
      #
      # @param failure [Test::Unit::Failure, Test::Unit::Error] The Test::Unit failure or error from which to extract information.
      # @param test_files [Array<String>] The list of files that might have generated this failure.  This is used to detect the file that caused the failure.
      #
      # @return [String, String] Returns the filename and test name as a 2 element Array.
      def self.parse_failure(failure, test_files)
        filename = if failure.respond_to?(:location) then
                     failure.location.map do |loc|
                       filename = loc.split(":", 2).first
                       test_files.detect {|tf| filename.include?(tf)}
                     end.compact.first
                   elsif failure.respond_to?(:exception) then
                     failure.exception.backtrace.map do |loc|
                       filename = loc.split(":", 2).first
                       loc = loc[1..-1] if loc[0,1] == "/"
                       test_files.detect {|tf| filename.include?(tf)}
                     end.compact.first
                   else
                     raise "Unknown object type received as failure: #{failure.inspect} doesn't have #exception or #location methods."
                   end

        test_name = failure.test_name.split("(", 2).first.strip.sub(/\.$/, "")

        [filename, test_name]
      end

      # Logs a message to STDOUT.  This implementation forks, so the #log method also
      # provides the PID of the logger.
      def log(message)
        STDOUT.printf "[%d] %s - %s\n", Process.pid, Time.now.strftime("%H:%M:%S"), message
        STDOUT.flush
      end

      # Runs absolutely all tests as found by walking test/.
      def run_all
        Slave.object do
          log "Run all tests"
          test_files = load_test_files(["test"])

          ActiveRecord::Base.establish_connection if defined?(ActiveRecord)
          test_runner = ::Nestor::Mappers::Rails::Test::TestRunner.new(nil)
          result = ::Test::Unit::AutoRunner.run(false, nil, []) do |autorunner|
            autorunner.runner = lambda { test_runner }
          end

          # Returns a Hash which the parent process will retrieve
          report(test_runner, test_files)
        end
      end

      # Runs only the named files, and optionally focuses on only a couple of tests
{'fraction_non_alphanumeric': 0.09567585972080354, 'fraction_numerical': 0.002723867892407218, 'mean_word_length': 2.141711229946524, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 5, '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': '958889', 'n_tokens_mistral': 1616, 'n_tokens_neox': 1557, 'n_words': 522}