class KnapsackPro::TestCaseMergers::RSpecMerger

Public Instance Methods

call() click to toggle source
# File lib/knapsack_pro/test_case_mergers/rspec_merger.rb, line 6
def call
  all_test_files_hash = {}
  merged_test_file_examples_hash = {}

  test_files.each do |test_file|
    path = test_file.fetch('path')
    test_file_path = extract_test_file_path(path)

    if rspec_id_path?(path)
      merged_test_file_examples_hash[test_file_path] ||= 0.0
      merged_test_file_examples_hash[test_file_path] += test_file.fetch('time_execution')
    else
      all_test_files_hash[test_file_path] = test_file.fetch('time_execution')
    end
  end

  merged_test_file_examples_hash.each do |path, time_execution|
    all_test_files_hash[path] = [time_execution, all_test_files_hash[path]].compact.max
  end

  merged_test_files = []
  all_test_files_hash.each do |path, time_execution|
    merged_test_files << {
      'path' => path,
      'time_execution' => time_execution
    }
  end
  merged_test_files
end

Private Instance Methods

extract_test_file_path(path) click to toggle source

path - can be: test file path: spec/a_spec.rb or test example path: spec/a_spec.rb

# File lib/knapsack_pro/test_case_mergers/rspec_merger.rb, line 41
def extract_test_file_path(path)
  path.gsub(/\.rb\[.+\]$/, '.rb')
end
rspec_id_path?(path) click to toggle source
# File lib/knapsack_pro/test_case_mergers/rspec_merger.rb, line 45
def rspec_id_path?(path)
  path_with_id_regex = /.+_spec\.rb\[.+\]$/

  path&.match?(path_with_id_regex)
end