Class: KnapsackPro::TestCaseDetectors::RSpecTestExampleDetector

Inherits:
Object
  • Object
show all
Defined in:
lib/knapsack_pro/test_case_detectors/rspec_test_example_detector.rb

Instance Method Summary collapse

Instance Method Details

#calculate_slow_id_paths(rspec_args) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/knapsack_pro/test_case_detectors/rspec_test_example_detector.rb', line 29

def calculate_slow_id_paths(rspec_args)
  result = fetch_slow_file_paths(KnapsackPro::OptimizedBuildDistributionFetcher.new)
  return { slow_id_paths: [], test_queue_url: result.fetch(:test_queue_url) } unless result.fetch(:test_queue_url).nil?

  dry_run_to_file(rspec_args, result.fetch(:slow_file_paths))
  { slow_id_paths: slow_id_paths!, test_queue_url: nil }
end

#dry_run_to_file(rspec_args, slow_test_files = fetch_slow_file_paths.fetch(:slow_file_paths)) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/knapsack_pro/test_case_detectors/rspec_test_example_detector.rb', line 6

def dry_run_to_file(rspec_args, slow_test_files = fetch_slow_file_paths.fetch(:slow_file_paths))
  KnapsackPro::Config::TempFiles.ensure_temp_directory_exists!
  FileUtils.mkdir_p(File.dirname(report_path))
  File.delete(report_path) if File.exist?(report_path)
  return File.write(report_path, { examples: [] }.to_json) if slow_test_files.empty?

  KnapsackPro.logger.info("Calculating Split by Test Examples. Analyzing #{slow_test_files.size} slow test files.")
  args = (rspec_args || '').split
  cli_args_without_formatters = KnapsackPro::Adapters::RSpecAdapter.remove_formatters(args)
  cli_args = cli_args_without_formatters + cli_format + [
    '--dry-run',
    '--out', report_path,
    '--default-path', test_dir
  ] + KnapsackPro::TestFilePresenter.paths(slow_test_files)
  exit_code = dry_run(cli_args)
  return if exit_code.zero?

  report.fetch('messages', []).each { |message| puts message }
  command = (['bundle exec rspec'] + cli_args).join(' ')
  KnapsackPro.logger.error("Failed to calculate Split by Test Examples: #{command}")
  exit exit_code
end

#slow_id_paths!Object



37
38
39
40
41
42
43
# File 'lib/knapsack_pro/test_case_detectors/rspec_test_example_detector.rb', line 37

def slow_id_paths!
  raise "No report found at #{report_path}" unless File.exist?(report_path)

  JSON.parse(File.read(report_path))
    .fetch('examples')
    .map { |example| TestFileCleaner.clean(example.fetch('id')) }
end