Class: KnapsackPro::Runners::Queue::MinitestRunner

Inherits:
BaseRunner
  • Object
show all
Defined in:
lib/knapsack_pro/runners/queue/minitest_runner.rb

Constant Summary

Constants inherited from BaseRunner

BaseRunner::TERMINATION_SIGNALS, BaseRunner::TerminationError

Class Method Summary collapse

Methods inherited from BaseRunner

child_status, handle_signal!, #initialize, set_terminate_process, #test_dir, #test_file_paths

Constructor Details

This class inherits a constructor from KnapsackPro::Runners::Queue::BaseRunner

Class Method Details

.minitest_run(runner, test_file_paths, args) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/knapsack_pro/runners/queue/minitest_runner.rb', line 104

def self.minitest_run(runner, test_file_paths, args)
  test_file_paths.each do |test_file_path|
    relative_test_file_path = "./#{test_file_path}"

    if File.exist?(relative_test_file_path)
      require relative_test_file_path
    else
      KnapsackPro.logger.warn("Skip loading the #{relative_test_file_path} test file path because it does not exist on the disk. Most likely, the test file path should not be loaded. The test file path could have been recorded during the previous CI build when the knapsack_pro gem could not attribute the execution time of a test to a correct test file path. For instance, you have shared examples in your test suite, and the knapsack_pro gem could not correctly determine for which test file path they were executed. In such a case, the test file path should not be loaded because the actual test cases will be executed by loading a correct test file path. You can ignore this warning.")
    end
  end

  # duplicate args because Minitest modifies args
  result = ::Minitest.run(args.dup)

  ::Minitest::Runnable.reset

  result
end

.run(args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/knapsack_pro/runners/queue/minitest_runner.rb', line 7

def self.run(args)
  require 'minitest'

  # Avoid installing `at_exit` since we are calling `Minitest.run` ourselves.
  # Without this, Minitest would run again (autorun) after `Minitest.run`.
  ::Minitest.class_variable_set('@@installed_at_exit', true)

  ENV['KNAPSACK_PRO_TEST_SUITE_TOKEN'] = KnapsackPro::Config::Env.test_suite_token_minitest
  ENV['KNAPSACK_PRO_QUEUE_MODE_ENABLED'] = 'true'
  ENV['KNAPSACK_PRO_QUEUE_ID'] = KnapsackPro::Config::EnvGenerator.set_queue_id

  adapter_class = KnapsackPro::Adapters::MinitestAdapter
  KnapsackPro::Config::Env.set_test_runner_adapter(adapter_class)
  runner = new(adapter_class)

  # Add test_dir to load path to make work:
  #   require 'test_helper'
  # in test files.
  $LOAD_PATH.unshift(runner.test_dir)

  cli_args = (args || '').split

  accumulator = {
    status: :next,
    runner: runner,
    can_initialize_queue: true,
    args: cli_args,
    exitstatus: 0,
    node_test_file_paths: [],
  }
  while accumulator[:status] == :next
    handle_signal!
    accumulator = run_tests(accumulator)
  end

  Kernel.exit(accumulator[:exitstatus])
rescue KnapsackPro::QueueAllocator::FallbackModeError
  exit_code = KnapsackPro::Config::Env.fallback_mode_error_exit_code
  Kernel.exit(exit_code)
end

.run_tests(accumulator) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/knapsack_pro/runners/queue/minitest_runner.rb', line 48

def self.run_tests(accumulator)
  runner = accumulator.fetch(:runner)
  can_initialize_queue = accumulator.fetch(:can_initialize_queue)
  args = accumulator.fetch(:args)
  exitstatus = accumulator.fetch(:exitstatus)
  node_test_file_paths = accumulator.fetch(:node_test_file_paths)

  test_file_paths = runner.test_file_paths(
    can_initialize_queue: can_initialize_queue,
    executed_test_files: node_test_file_paths
  )

  if test_file_paths.empty?
    unless node_test_file_paths.empty?
      KnapsackPro::Adapters::MinitestAdapter.verify_bind_method_called
    end

    KnapsackPro::Hooks::Queue.call_after_queue

    KnapsackPro::Report.save_node_queue_to_api

    return {
      status: :completed,
      exitstatus: exitstatus,
    }
  else
    subset_queue_id = KnapsackPro::Config::EnvGenerator.set_subset_queue_id
    ENV['KNAPSACK_PRO_SUBSET_QUEUE_ID'] = subset_queue_id

    KnapsackPro.tracker.reset!
    KnapsackPro.tracker.set_prerun_tests(test_file_paths)

    KnapsackPro::Hooks::Queue.call_before_subset_queue

    node_test_file_paths += test_file_paths

    result = minitest_run(runner, test_file_paths, args)
    exitstatus = 1 unless result

    KnapsackPro::Hooks::Queue.call_after_subset_queue

    KnapsackPro::Report.save_subset_queue_to_file

    return {
      status: :next,
      runner: runner,
      can_initialize_queue: false,
      args: args,
      exitstatus: exitstatus,
      node_test_file_paths: node_test_file_paths,
    }
  end
end