Class: TurboTests::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/turbo_tests/runner.rb

Constant Summary collapse

DEFAULT_RUNTIME_LOG =
"tmp/turbo_rspec_runtime.log"
DEFAULT_WORKER_OUTPUT_MODE =
:warnings
WORKER_OUTPUT_MODES =
%i[warnings stream buffered quiet].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**opts) ⇒ Runner

Returns a new instance of Runner.



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/turbo_tests/runner.rb', line 202

def initialize(**opts)
  @formatters = opts[:formatters]
  @reporter = opts[:reporter]
  @files = opts[:files]
  @test_selectors = opts[:test_selectors] || {}
  @tags = opts[:tags]
  @verbose = opts[:verbose]
  @fail_fast = opts[:fail_fast]
  @start_time = opts[:start_time]
  @count = opts[:count]
  @seed = opts[:seed]
  @seed_used = opts[:seed_used]
  @order = opts[:order]
  @nice = opts[:nice]
  @use_runtime_info = opts[:use_runtime_info]

  @load_time = 0
  @load_count = 0
  @failure_count = 0

  # Supports runtime_log as a top level option,
  #   but also nested inside parallel_options
  @runtime_log = opts[:runtime_log] || DEFAULT_RUNTIME_LOG
  @parallel_options = opts.fetch(:parallel_options, {})
  @parallel_options[:runtime_log] ||= @runtime_log
  @record_runtime = true

  @messages = Thread::Queue.new
  @threads = []
  @wait_threads = []
  @exited_process_ids = []
  @worker_output = Hash.new { |hash, process_id| hash[process_id] = {stdout: +"", stderr: +""} }
  @worker_output_mutex = Mutex.new
  @deferred_run_options_messages = Hash.new { |hash, message| hash[message] = [] }
  @error = false
  @print_failed_group = opts[:print_failed_group]
  @worker_output_mode = self.class.normalize_worker_output_mode(opts.fetch(:worker_output, DEFAULT_WORKER_OUTPUT_MODE))
end

Class Method Details

.create(count) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/turbo_tests/runner.rb', line 20

def create(count)
  # We are unable to load parallel tests' tasks in the normal way (top of file)
  # because it requires that the Rails.application instance already be configured
  require "parallel_tests/tasks"

  ENV["PARALLEL_TEST_FIRST_IS_1"] = "true"
  command = ["bundle", "exec", "rake", "db:create", "RAILS_ENV=#{ParallelTests::Tasks.rails_env}"]
  args = {count: count.to_s}
  ParallelTests::Tasks.run_in_parallel(command, args)
end

.generate_seedObject



154
155
156
# File 'lib/turbo_tests/runner.rb', line 154

def generate_seed
  (Random.new_seed % 65_535).to_s
end

.normalize_order(order) ⇒ Object

Raises:

  • (ArgumentError)


146
147
148
149
150
151
152
# File 'lib/turbo_tests/runner.rb', line 146

def normalize_order(order)
  order = order.to_s.strip.downcase
  return "random" if order.empty?
  return order if %w[random defined].include?(order)

  raise ArgumentError, "Unsupported order #{order.inspect}; use random or defined"
end

.normalize_rspec_file_selection(files) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/turbo_tests/runner.rb', line 106

def normalize_rspec_file_selection(files)
  selectors = {}
  files.each do |entry|
    file, selector = split_rspec_location(entry)
    selectors[file] ||= []
    if selector == file
      selectors[file] = [file]
    elsif !selectors[file].include?(file)
      selectors[file] << selector unless selectors[file].include?(selector)
    end
  end

  {files: selectors.keys, selectors: selectors}
end

.normalize_worker_output_mode(mode) ⇒ Object

Raises:

  • (ArgumentError)


95
96
97
98
99
100
101
102
103
104
# File 'lib/turbo_tests/runner.rb', line 95

def normalize_worker_output_mode(mode)
  value = mode.to_s.strip.downcase.tr("-", "_")
  return DEFAULT_WORKER_OUTPUT_MODE if value.empty?

  normalized = value.to_sym
  return normalized if WORKER_OUTPUT_MODES.include?(normalized)

  raise ArgumentError,
    "Unsupported worker output mode #{mode.inspect}; expected one of: #{WORKER_OUTPUT_MODES.join(", ")}"
end

.project_rspec_options(root = Dir.pwd) ⇒ Object



192
193
194
195
196
197
198
199
# File 'lib/turbo_tests/runner.rb', line 192

def project_rspec_options(root = Dir.pwd)
  %w[.rspec .rspec-local].flat_map do |path|
    option_file = File.join(root, path)
    next [] unless File.file?(option_file)

    Shellwords.split(File.read(option_file))
  end
end

.rspec_configured_files_to_runObject



158
159
160
161
162
163
164
165
166
# File 'lib/turbo_tests/runner.rb', line 158

def rspec_configured_files_to_run
  configuration = RSpec::Core::Configuration.new
  RSpec::Core::ConfigurationOptions.new(["spec"]).configure(configuration)
  root = "#{Dir.pwd}/"
  configuration.files_to_run.map do |path|
    path = path.to_s
    path.start_with?(root) ? path[root.length..-1] : path
  end
end

.run(opts = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
# File 'lib/turbo_tests/runner.rb', line 31

def run(opts = {})
  default_file_discovery = !opts.key?(:files) || opts[:files].nil?
  files = default_file_discovery ? rspec_configured_files_to_run : opts[:files]
  file_selection = normalize_rspec_file_selection(files)
  formatters = opts[:formatters]
  tags = opts[:tags]
  parallel_options = opts[:parallel_options] || {}

  start_time = opts.fetch(:start_time) { RSpec::Core::Time.now }
  runtime_log = opts.fetch(:runtime_log, nil) || DEFAULT_RUNTIME_LOG
  example_status_log = opts.fetch(:example_status_log, nil)
  verbose = opts.fetch(:verbose, false)
  fail_fast = opts.fetch(:fail_fast, nil)
  count = opts.fetch(:count, nil)
  order = normalize_order(opts.fetch(:order, nil))
  seed = opts.fetch(:seed, nil)
  seed_used = order != "defined"
  seed = generate_seed if seed_used && seed.nil?
  print_failed_group = opts.fetch(:print_failed_group, false)
  nice = opts.fetch(:nice, false)
  worker_output = normalize_worker_output_mode(
    opts[:worker_output] || ENV["TURBO_TESTS2_WORKER_OUTPUT"]
  )

  use_runtime_info = default_file_discovery
  parallel_options[:runtime_log] ||= runtime_log

  if example_status_log
    runtime_log = runtime_log_from_example_status(example_status_log)
    parallel_options[:runtime_log] = runtime_log
  elsif use_runtime_info
    parallel_options[:runtime_log] ||= runtime_log
  else
    parallel_options[:group_by] ||= :filesize
  end
  parallel_options[:group_by] ||= :filesize if parallel_options[:only_group]

  warn("VERBOSE") if verbose

  reporter = Reporter.from_config(formatters, start_time, seed, seed_used, files, parallel_options)

  new(
    reporter: reporter,
    formatters: formatters,
    start_time: start_time,
    files: file_selection.fetch(:files),
    test_selectors: file_selection.fetch(:selectors),
    tags: tags,
    runtime_log: runtime_log,
    example_status_log: example_status_log,
    verbose: verbose,
    fail_fast: fail_fast,
    count: count,
    seed: seed,
    seed_used: seed_used,
    order: order,
    print_failed_group: print_failed_group,
    use_runtime_info: use_runtime_info,
    parallel_options: parallel_options,
    nice: nice,
    worker_output: worker_output
  ).run
end

.runtime_log_from_example_status(example_status_log) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/turbo_tests/runner.rb', line 131

def runtime_log_from_example_status(example_status_log)
  statuses = RSpec::Core::ExampleStatusPersister.load_from(example_status_log)
  runtimes = statuses.each_with_object(Hash.new(0.0)) do |status, sums|
    next unless status.fetch(:status).match?(/pass/i)

    file_name = RSpec::Core::Example.parse_id(status.fetch(:example_id)).first
    sums[file_name] += status.fetch(:run_time).to_s[/\d+(\.\d+)?/].to_f
  end

  path = File.join("tmp", "turbo_tests2_example_status_runtime.log")
  FileUtils.mkdir_p(File.dirname(path))
  File.write(path, runtimes.sort.map { |file, runtime| "#{file}:#{runtime}" }.join("\n"))
  path
end

.split_rspec_location(entry) ⇒ Object



121
122
123
124
125
126
127
128
129
# File 'lib/turbo_tests/runner.rb', line 121

def split_rspec_location(entry)
  value = entry.to_s
  return [value, value] if File.exist?(value)

  match = value.match(/\A(.+):(\d+)\z/)
  return [value, value] unless match && File.exist?(match[1])

  [match[1], value]
end

.worker_spec_opts(spec_opts) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/turbo_tests/runner.rb', line 168

def worker_spec_opts(spec_opts)
  args = project_rspec_options + Array(spec_opts)
  filtered = []
  skip_next = false

  args.each do |arg|
    if skip_next
      skip_next = false
      next
    end

    case arg
    when "--pattern", "-P", "--default-path"
      skip_next = true
    when /\A--pattern=/, /\A-P.+/, /\A--default-path=/
      next
    else
      filtered << arg
    end
  end

  filtered
end

Instance Method Details

#runObject



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/turbo_tests/runner.rb', line 241

def run
  parallel_tests_options = @parallel_options.reject { |key, _value| key == :only_group }
  tests_with_size = ParallelTests::RSpec::Runner.tests_with_size(@files, parallel_tests_options)
  @num_processes = [
    ParallelTests.determine_number_of_processes(@count),
    tests_with_size.size
  ].min

  if @num_processes.zero?
    @tests_in_groups = []
    @reporter.report([]) { |_reporter| }
    return 0
  end

  tests_in_groups =
    ParallelTests::RSpec::Runner.tests_in_groups(
      @files,
      @num_processes,
      **parallel_tests_options
    )
  tests_in_groups = selected_groups(tests_in_groups) if @parallel_options[:only_group]
  @tests_in_groups = tests_in_groups

  subprocess_opts = {
    record_runtime: @record_runtime
  }

  ParallelTests.with_pid_file do
    exit_status = nil
    report_coverage = false

    @reporter.report(tests_in_groups) do |_reporter|
      old_signal = Signal.trap(:INT) { handle_interrupt }

      @wait_threads = tests_in_groups.map.with_index do |tests, process_id|
        start_regular_subprocess(tests, process_id + 1, **subprocess_opts)
      end.compact
      @interrupt_handled = false

      handle_messages

      @threads.each(&:join)

      report_failed_group(tests_in_groups) if @print_failed_group

      Signal.trap(:INT, old_signal)

      statuses = @wait_threads.map(&:value)

      if @reporter.failed_examples.empty? && statuses.all?(&:success?)
        flush_successful_worker_output
        report_coverage = true
        exit_status = 0
      else
        flush_worker_output unless stream_worker_output?
        # From https://github.com/galtzo-floss/turbo_tests2/pull/20/
        exit_status = statuses.map(&:exitstatus).max
      end
    end

    flush_coverage_summary if report_coverage
    exit_status
  end
end