<commit_before>require 'rspactor'

module RSpactor
  class Runner
    def self.start(options = {})
      run_in = options.delete(:run_in) || Dir.pwd
      new(run_in, options).start
    end
    
    attr_reader :dir, :options, :inspector, :interactor
    
    def initialize(dir, options = {})
      @dir = dir
      @options = options
      read_git_head
    end
    
    def start
      load_dotfile
      puts "** RSpactor is now watching at '#{dir}'"
      Spork.start if options[:spork]
      Celerity.start(dir) if options[:celerity]
      start_interactor
      start_listener
    end
    
    def start_interactor
      @interactor = Interactor.new(self)
      aborted = @interactor.wait_for_enter_key("** Hit <enter> to skip initial spec & cucumber run", 2, false)
      @interactor.start_termination_handler
      unless aborted
        run_all_specs
        run_cucumber_command('~@wip,~@pending', false)
      end
    end
    
    def start_listener
      @inspector = Inspector.new(self)
      
      Listener.new(Inspector::EXTENSIONS) do |files|
        changed_files(files) unless git_head_changed?
      end.run(dir)
    end
    
    def load_dotfile
      dotfile = File.join(ENV['HOME'], '.rspactor')
      if File.exists?(dotfile)
        begin
          Kernel.load dotfile
        rescue => e
          $stderr.puts "Error while loading #{dotfile}: #{e}"
        end
      end
    end
    
    def run_all_specs
      run_spec_command(File.join(dir, 'spec'))
    end
    
    def run_spec_command(paths)
      paths = Array(paths)
      if paths.empty?
        @last_run_failed = nil
      else
        cmd = [ruby_opts, spec_runner, paths, spec_opts].flatten.join(' ')
        @last_run_failed = run_command(cmd)
      end
    end
    
    def run_cucumber_command(tags = '@wip:2', clear = @options[:clear])
      system("clear;") if clear
      puts "** Running all #{tags} tagged features..."
      cmd = [ruby_opts, cucumber_runner, cucumber_opts(tags)].flatten.join(' ')
      @last_run_failed = run_command(cmd)
      # Workaround for killing jruby process when used with celerity and spork
      Celerity.kill_jruby if options[:celerity] && options[:spork]
    end
    
    def last_run_failed?
      @last_run_failed == false
    end
    
    protected
    
    def run_command(cmd)
      system(cmd)
      $?.success?
    end
    
    def changed_files(files)<commit_msg>Do not try to run features if there is no cucumber setup<commit_after>require 'rspactor'

module RSpactor
  class Runner
    def self.start(options = {})
      run_in = options.delete(:run_in) || Dir.pwd
      new(run_in, options).start
    end
    
    attr_reader :dir, :options, :inspector, :interactor
    
    def initialize(dir, options = {})
      @dir = dir
      @options = options
      read_git_head
    end
    
    def start
      load_dotfile
      puts "** RSpactor, now watching at '#{dir}'"
      Spork.start if options[:spork]
      Celerity.start(dir) if options[:celerity]
      start_interactor
      start_listener
    end
    
    def start_interactor
      @interactor = Interactor.new(self)
      aborted = @interactor.wait_for_enter_key("** Hit <enter> to skip initial spec & cucumber run", 2, false)
      @interactor.start_termination_handler
      unless aborted
        run_all_specs
        run_cucumber_command('~@wip,~@pending', false)
      end
    end
    
    def start_listener
      @inspector = Inspector.new(self)
      
      Listener.new(Inspector::EXTENSIONS) do |files|
        changed_files(files) unless git_head_changed?
      end.run(dir)
    end
    
    def load_dotfile
      dotfile = File.join(ENV['HOME'], '.rspactor')
      if File.exists?(dotfile)
        begin
          Kernel.load dotfile
        rescue => e
          $stderr.puts "Error while loading #{dotfile}: #{e}"
        end
      end
    end
    
    def run_all_specs
      run_spec_command(File.join(dir, 'spec'))
    end
    
    def run_spec_command(paths)
      paths = Array(paths)
      if paths.empty?
        @last_run_failed = nil
      else
        cmd = [ruby_opts, spec_runner, paths, spec_opts].flatten.join(' ')
        @last_run_failed = run_command(cmd)
      end
    end
    
    def run_cucumber_command(tags = '@wip:2', clear = @options[:clear])
      return unless File.exist?(File.join(dir, 'features'))
      
      system("clear;") if clear
      puts "** Running all #{tags} tagged features..."
      cmd = [ruby_opts, cucumber_runner, cucumber_opts(tags)].flatten.join(' ')
      @last_run_failed = run_command(cmd)
      # Workaround for killing jruby process when used with celerity and spork
      Celerity.kill_jruby if options[:celerity] && options[:spork]
    end
    
    def last_run_failed?
      @last_run_failed == false
    end
    
    protected
    
    def run_command(cmd)
      system(cmd)
      $?.success?
    end
    
    def changed_files(files)
{'fraction_non_alphanumeric': 0.09995928338762215, 'fraction_numerical': 0.0008143322475570033, 'mean_word_length': 2.365068493150685, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '1422088', 'n_tokens_mistral': 1597, 'n_tokens_neox': 1575, 'n_words': 365}