<commit_before>module Guard
  class KonachaRails
    class Runner
      DEFAULT_OPTIONS = {
        run_all_on_start: true,
        notification: true,
        rails_environment_file: './config/environment'
      }.freeze

      attr_reader :options, :formatter

      def initialize(options = {})
        @options = DEFAULT_OPTIONS.merge(options)

        # Require project's rails environment file to load Konacha configuration.
        require_rails_environment
        raise 'Konacha not loaded' unless defined? ::Konacha

        # Custom formatter to handle multiple runs.
        @formatter = Formatter.new
        ::Konacha.config.formatters = [@formatter]

        # Reusable session to increase performance.
        @session = Capybara::Session.new(::Konacha.driver, Server.new)

        ::Konacha.mode = :runner

        UI.info 'Guard::KonachaRails Initialized'
      end

      def start
        run if options[:run_all_on_start]
      end

      def run(paths = [''])
        formatter.reset

        paths.each do |path|
          if path.empty? or File.exists? real_path path
            UI.info "Guard::KonachaRails running #{specs_description(path)}"
            runner.run konacha_path(path)
          end
        end

        formatter.write_summary
        notify
      rescue => e
        UI.error(e)
      end

      private

      def require_rails_environment
        if @options[:rails_environment_file]
          require @options[:rails_environment_file]
        else
          dir = '.'
          while File.expand_path(dir) != '/' do
            env_file = File.join(dir, 'config/environment.rb')<commit_msg>Print out exception message when one is thrown<commit_after>module Guard
  class KonachaRails
    class Runner
      DEFAULT_OPTIONS = {
        run_all_on_start: true,
        notification: true,
        rails_environment_file: './config/environment'
      }.freeze

      attr_reader :options, :formatter

      def initialize(options = {})
        @options = DEFAULT_OPTIONS.merge(options)

        # Require project's rails environment file to load Konacha configuration.
        require_rails_environment
        raise 'Konacha not loaded' unless defined? ::Konacha

        # Custom formatter to handle multiple runs.
        @formatter = Formatter.new
        ::Konacha.config.formatters = [@formatter]

        # Reusable session to increase performance.
        @session = Capybara::Session.new(::Konacha.driver, Server.new)

        ::Konacha.mode = :runner

        UI.info 'Guard::KonachaRails Initialized'
      end

      def start
        run if options[:run_all_on_start]
      end

      def run(paths = [''])
        formatter.reset

        paths.each do |path|
          if path.empty? or File.exists? real_path path
            UI.info "Guard::KonachaRails running #{specs_description(path)}"
            runner.run konacha_path(path)
          end
        end

        formatter.write_summary
        notify
      rescue => e
        UI.error(e)
        UI.info(e.message)
      end

      private

      def require_rails_environment
        if @options[:rails_environment_file]
          require @options[:rails_environment_file]
        else
          dir = '.'
          while File.expand_path(dir) != '/' do
            env_file = File.join(dir, 'config/environment.rb')
{'fraction_non_alphanumeric': 0.08578284343131373, 'fraction_numerical': 0.0, 'mean_word_length': 2.1551561021759698, 'pattern_counts': {'":': 0, '<': 3, '<?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': '6519261', 'n_tokens_mistral': 976, 'n_tokens_neox': 905, 'n_words': 225}