<commit_before>module Specjour
  require 'specjour/rspec'
  require 'specjour/cucumber'

  class Worker
    include Protocol
    include SocketHelper
    attr_accessor :printer_uri
    attr_reader :project_path, :number, :preload_spec, :preload_feature, :task

    def initialize(options = {})
      ARGV.replace []
      $stdout = StringIO.new if options[:quiet]
      @project_path = options[:project_path]
      @number = options[:number].to_i
      @preload_spec = options[:preload_spec]
      @preload_feature = options[:preload_feature]
      @task = options[:task]
      self.printer_uri = options[:printer_uri]
      set_env_variables
      Dir.chdir(project_path)
      Specjour.load_custom_hooks
    end

    def printer_uri=(val)
      @printer_uri = URI.parse(val)
    end

    def prepare
      load_app
      Configuration.prepare.call
    end

    def run_tests
      load_app
      Configuration.after_fork.call
      run_time = 0

      while test = connection.next_test
        time = Benchmark.realtime do
          run_test test
        end
        run_time += time if test =~ /_spec\.rb$/
      end
      connection.send_message(:rspec_summary=, {:duration => sprintf("%6f", run_time)})
      connection.send_message(:done)
      connection.disconnect
    end

    def start
      send task
    end

    protected

    def connection
      @connection ||= printer_connection
    end

    def load_app
      Rspec::Preloader.load(preload_spec) if preload_spec
      Cucumber::Preloader.load(preload_feature) if preload_feature
    end

    def printer_connection
      Connection.new printer_uri
    end

    def print_status(test)
      status = "[#{ENV['TEST_ENV_NUMBER']}] Running #{test}"
      puts status
      $PROGRAM_NAME = "specjour#{status}"
    end

    def run_test(test)
      print_status(test)
      if test =~ /\.feature$/
        run_feature test
      else
        run_spec test
      end
    end

    def run_feature(feature)
      cli = ::Cucumber::Cli::Main.new(['--format', 'Specjour::Cucumber::DistributedFormatter', feature], connection)
      cli.execute!(::Cucumber::Cli::Main.step_mother)
    end

    def run_spec(spec)
      Specjour::Rspec::Runner.run(spec, connection)
    end

    def set_env_variables
      ENV['RSPEC_COLOR'] = 'true'
      ENV['TEST_ENV_NUMBER'] = number.to_s
    end
  end
end
<commit_msg>Preloading presses on past StandardError<commit_after>module Specjour
  require 'specjour/rspec'
  require 'specjour/cucumber'

  class Worker
    include Protocol
    include SocketHelper
    attr_accessor :printer_uri
    attr_reader :project_path, :number, :preload_spec, :preload_feature, :task

    def initialize(options = {})
      ARGV.replace []
      $stdout = StringIO.new if options[:quiet]
      @project_path = options[:project_path]
      @number = options[:number].to_i
      @preload_spec = options[:preload_spec]
      @preload_feature = options[:preload_feature]
      @task = options[:task]
      self.printer_uri = options[:printer_uri]
      set_env_variables
      Dir.chdir(project_path)
      Specjour.load_custom_hooks
    end

    def printer_uri=(val)
      @printer_uri = URI.parse(val)
    end

    def prepare
      load_app
      Configuration.prepare.call
    end

    def run_tests
      load_app
      Configuration.after_fork.call
      run_time = 0

      while test = connection.next_test
        time = Benchmark.realtime do
          run_test test
        end
        run_time += time if test =~ /_spec\.rb$/
      end
      connection.send_message(:rspec_summary=, {:duration => sprintf("%6f", run_time)})
      connection.send_message(:done)
      connection.disconnect
    end

    def start
      send task
    end

    protected

    def connection
      @connection ||= printer_connection
    end

    def load_app
      Rspec::Preloader.load(preload_spec) if preload_spec
      Cucumber::Preloader.load(preload_feature) if preload_feature
    rescue StandardError => exception
      $stderr.puts "Caught exception: #{exception.class} #{exception.message}\nProceeding..."
    end

    def printer_connection
      Connection.new printer_uri
    end

    def print_status(test)
      status = "[#{ENV['TEST_ENV_NUMBER']}] Running #{test}"
      puts status
      $PROGRAM_NAME = "specjour#{status}"
    end

    def run_test(test)
      print_status(test)
      if test =~ /\.feature$/
        run_feature test
      else
        run_spec test
      end
    end

    def run_feature(feature)
      cli = ::Cucumber::Cli::Main.new(['--format', 'Specjour::Cucumber::DistributedFormatter', feature], connection)
      cli.execute!(::Cucumber::Cli::Main.step_mother)
    end

    def run_spec(spec)
      Specjour::Rspec::Runner.run(spec, connection)
    end

    def set_env_variables
      ENV['RSPEC_COLOR'] = 'true'
      ENV['TEST_ENV_NUMBER'] = number.to_s
    end
  end
end

{'fraction_non_alphanumeric': 0.09682702149437052, 'fraction_numerical': 0.0008188331627430911, 'mean_word_length': 2.6987130961392882, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, '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': '2094088', 'n_tokens_mistral': 1560, 'n_tokens_neox': 1505, 'n_words': 318}