<commit_before>require 'torquespec'
require 'rspec/core'
require 'drb'
require 'torquebox-core'

module TorqueSpec
  class Daemon

    include TorqueBox::Injectors

    def initialize(opts={})
      puts "daemon: create opts=#{opts.inspect}"
      dir = opts['pwd'].to_s
      raise "The 'pwd' option must contain a valid directory name" if dir.empty? || !File.exist?(dir)
      @analyzer = inject( 'runtime-injection-analyzer' ) 
      Dir.chdir( dir ) do
        RSpec::Core::Runner.disable_autorun! # avoid a bunch of at_exit finalizer errors
        @options = RSpec::Core::ConfigurationOptions.new( opts['argv'].to_a )
        @options.parse_options

        @configuration = RSpec::configuration
        @world         = RSpec::world

        @options.configure(@configuration)
        @configuration.load_spec_files
        @configuration.configure_mock_framework
        @configuration.configure_expectation_framework
      end
    end

    def start
      puts "daemon: start"
      DRb.start_service("druby://127.0.0.1:#{TorqueSpec.drb_port}", self)
    end

    def stop
      puts "daemon: stop"
      DRb.stop_service
    end

    def run(alien, reporter)
      puts "daemon: run #{alien}"
      simple_name = alien.name.split("::").last
      example_group = @world.example_groups.inject([]) {|all,g| all + g.descendants}.find do |group| 
        group.name.split("::").last == simple_name && group.description == alien.description
      end
      example_group.descendant_filtered_examples.each do |example| 
        @analyzer.analyze_and_inject( example.instance_eval {@example_block} )
      end
      example_group.run( reporter )
    end

    # Intended to extend an RSpec::Core::ExampleGroup
    module Client

      def run(reporter)<commit_msg>I think we get in race conditions when stopping the service on 1.9 resulting in sockets in unpredictable states.<commit_after>require 'torquespec'
require 'rspec/core'
require 'drb'
require 'torquebox-core'

module TorqueSpec
  class Daemon

    include TorqueBox::Injectors

    def initialize(opts={})
      puts "daemon: create opts=#{opts.inspect}"
      dir = opts['pwd'].to_s
      raise "The 'pwd' option must contain a valid directory name" if dir.empty? || !File.exist?(dir)
      @analyzer = inject( 'runtime-injection-analyzer' ) 
      Dir.chdir( dir ) do
        RSpec::Core::Runner.disable_autorun! # avoid a bunch of at_exit finalizer errors
        @options = RSpec::Core::ConfigurationOptions.new( opts['argv'].to_a )
        @options.parse_options

        @configuration = RSpec::configuration
        @world         = RSpec::world

        @options.configure(@configuration)
        @configuration.load_spec_files
        @configuration.configure_mock_framework
        @configuration.configure_expectation_framework
      end
    end

    def start
      puts "daemon: start"
      DRb.start_service("druby://127.0.0.1:#{TorqueSpec.drb_port}", self)
    end

    def stop
      puts "daemon: stop"
      DRb.stop_service
    rescue Exception
      # ignore
    end

    def run(alien, reporter)
      puts "daemon: run #{alien}"
      simple_name = alien.name.split("::").last
      example_group = @world.example_groups.inject([]) {|all,g| all + g.descendants}.find do |group| 
        group.name.split("::").last == simple_name && group.description == alien.description
      end
      example_group.descendant_filtered_examples.each do |example| 
        @analyzer.analyze_and_inject( example.instance_eval {@example_block} )
      end
      example_group.run( reporter )
    end

    # Intended to extend an RSpec::Core::ExampleGroup
    module Client

      def run(reporter)
{'fraction_non_alphanumeric': 0.10989910008181074, 'fraction_numerical': 0.003817834742296155, 'mean_word_length': 3.0, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '481758', 'n_tokens_mistral': 1121, 'n_tokens_neox': 1098, 'n_words': 256}