<commit_before>module MiniAutobot
  class Runner

    attr_accessor :options
    @after_hooks = []

    def self.after_run(&blk)
      @after_hooks << blk
    end

    def self.run!(args)
      exit_code = self.run(args)
      @after_hooks.reverse_each(&:call)
      Kernel.exit(exit_code || false)
    end

    def self.run args = []
      Minitest.load_plugins

      @options = Minitest.process_args args

      self.before_run

      reporter = Minitest::CompositeReporter.new
      reporter << Minitest::SummaryReporter.new(@options[:io], @options)
      reporter << Minitest::ProgressReporter.new(@options[:io], @options)

      Minitest.reporter = reporter # this makes it available to plugins
      Minitest.init_plugins @options
      Minitest.reporter = nil # runnables shouldn't depend on the reporter, ever

      reporter.start
      Minitest.__run reporter, @options
      Minitest.parallel_executor.shutdown
      reporter.report

      reporter.passed?
    end

    # before hook where you have parsed @options when loading tests
    def self.before_run
      host_env = @options[:env]
      if host_env.nil?
        # TODO(phu): default host needs to be set in a new env file
        host = 'rent'
        puts "No argument given for option -e \nLoading tests using default host: #{host}"
      else
        host = host_env.split(/_/)[0]
      end
      self.load_tests(host)
    end

    # only load tests you need by specifying env option in command line
    def self.load_tests(host)
      tests_dir_name = 'web_tests'
      tests_dir_full_path = MiniAutobot.root.join(tests_dir_name).to_s
      if Dir.exists? tests_dir_full_path
        $LOAD_PATH << tests_dir_full_path
      else
        puts "Tests directory #{tests_dir_full_path} doesn't exist"
        puts "No test will run."
      end

      Dir.glob("#{tests_dir_name}/#{host}/*.rb") do |f|
        f.sub!(/^#{tests_dir_name}\//, '')
        require f
      end

      # files under subdirectories shouldn't be loaded, eg. archive/
      Dir.glob("#{tests_dir_name}/#{host}/test_cases/*.rb") do |f|
        f.sub!(/^#{tests_dir_name}\//, '')
        require f
      end
    end

  end<commit_msg>[PH][100836724] Load config file tests.yml for tests structure from client repo and use it to decide where to find the tests and which tests to load<commit_after>module MiniAutobot
  class Runner

    attr_accessor :options
    @after_hooks = []

    def self.after_run(&blk)
      @after_hooks << blk
    end

    def self.run!(args)
      exit_code = self.run(args)
      @after_hooks.reverse_each(&:call)
      Kernel.exit(exit_code || false)
    end

    def self.run args = []
      Minitest.load_plugins

      @options = Minitest.process_args args

      self.before_run

      reporter = Minitest::CompositeReporter.new
      reporter << Minitest::SummaryReporter.new(@options[:io], @options)
      reporter << Minitest::ProgressReporter.new(@options[:io], @options)

      Minitest.reporter = reporter # this makes it available to plugins
      Minitest.init_plugins @options
      Minitest.reporter = nil # runnables shouldn't depend on the reporter, ever

      reporter.start
      Minitest.__run reporter, @options
      Minitest.parallel_executor.shutdown
      reporter.report

      reporter.passed?
    end

    # before hook where you have parsed @options when loading tests
    def self.before_run
      host_env = @options[:env]
      if host_env.nil?
        # TODO(phu): default host needs to be set in a user's local env file
        host = 'rent'
        puts "No argument given for option -e \nLoading tests using default host: #{host}"
      else
        host = host_env.split(/_/)[0]
      end
      self.load_tests(host)
    end

    # only load tests you need by specifying env option in command line
    def self.load_tests(host)
      tests_yml_full_path = MiniAutobot.root.join('config', 'tests.yml').to_s
      if File.exist? tests_yml_full_path
        tests_yml = YAML.load_file tests_yml_full_path
        tests_dir_relative_path = tests_yml['tests_dir']['relative_path']
        multi_host_flag = tests_yml['tests_dir']['multi-host']
      else
        puts "Config file #{tests_yml_full_path} doesn't exist"
        puts "mini_autobot doesn't know where your tests are located and how they are structured"
      end

      tests_dir_full_path = MiniAutobot.root.join(tests_dir_relative_path).to_s
      if Dir.exist? tests_dir_full_path
        $LOAD_PATH << tests_dir_full_path
      else
        puts "Tests directory #{tests_dir_full_path} doesn't exist"
        puts "No test will run."
      end

      # load page_objects.rb first
      Dir.glob("#{tests_dir_relative_path}/#{multi_host_flag ? host+'/' : ''}*.rb") do |f|
        f.sub!(/^#{tests_dir_relative_path}\//, '')
        require f
      end

      # files under subdirectories shouldn't be loaded, eg. archive/
      Dir.glob("#{tests_dir_relative_path}/#{multi_host_flag ? host+'/' : ''}test_cases/*.rb") do |f|
        f.sub!(/^#{tests_dir_relative_path}\//, '')
        require f
      end
    end

  end
{'fraction_non_alphanumeric': 0.10080565926508155, 'fraction_numerical': 0.0021615248575358615, 'mean_word_length': 2.6910804931109498, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 3, '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': '486711', 'n_tokens_mistral': 1619, 'n_tokens_neox': 1590, 'n_words': 409}