Sha256: 0a9654100ff090ddd102c97ca4026d7d69784c8da12d55df80d0d8a029bb3269

Contents?: true

Size: 1.37 KB

Versions: 11

Compression:

Stored size: 1.37 KB

Contents

module KnapsackPro
  class SlowTestFileDeterminer
    TIME_THRESHOLD_PER_CI_NODE = 0.7 # 70%
    REPORT_DIR = "#{KnapsackPro::Config::Env::TMP_DIR}/slow_test_file_determiner"

    # test_files: { 'path' => 'a_spec.rb', 'time_execution' => 0.0 }
    # time_execution: of build distribution (total time of CI build run)
    def self.call(test_files, time_execution)
      time_threshold = (time_execution / KnapsackPro::Config::Env.ci_node_total) * TIME_THRESHOLD_PER_CI_NODE

      test_files.select do |test_file|
        test_file.fetch('time_execution') >= time_threshold
      end
    end

    def self.save_to_json_report(test_files)
      FileUtils.mkdir_p(REPORT_DIR)
      File.write(report_path, test_files.to_json)
    end

    def self.read_from_json_report
      raise 'Report with slow test files was not generated yet. If you have enabled split by test cases https://github.com/KnapsackPro/knapsack_pro-ruby#split-test-files-by-test-cases and you see this error it means that your tests accidentally cleaned up tmp/knapsack_pro directory. Please do not remove this directory during tests runtime!' unless File.exists?(report_path)
      slow_test_files_json_report = File.read(report_path)
      JSON.parse(slow_test_files_json_report)
    end

    private

    def self.report_path
      "#{REPORT_DIR}/slow_test_files_node_#{KnapsackPro::Config::Env.ci_node_index}.json"
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
knapsack_pro-3.0.0 lib/knapsack_pro/slow_test_file_determiner.rb
knapsack_pro-2.18.2 lib/knapsack_pro/slow_test_file_determiner.rb
knapsack_pro-2.18.1 lib/knapsack_pro/slow_test_file_determiner.rb
knapsack_pro-2.18.0 lib/knapsack_pro/slow_test_file_determiner.rb
knapsack_pro-2.17.0 lib/knapsack_pro/slow_test_file_determiner.rb
knapsack_pro-2.16.0 lib/knapsack_pro/slow_test_file_determiner.rb
knapsack_pro-2.15.0 lib/knapsack_pro/slow_test_file_determiner.rb
knapsack_pro-2.14.0 lib/knapsack_pro/slow_test_file_determiner.rb
knapsack_pro-2.13.0 lib/knapsack_pro/slow_test_file_determiner.rb
knapsack_pro-2.12.0 lib/knapsack_pro/slow_test_file_determiner.rb
knapsack_pro-2.11.0 lib/knapsack_pro/slow_test_file_determiner.rb