Class: TurboTests::Runner

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**opts) ⇒ Runner

Returns a new instance of Runner.



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/turbo_tests/runner.rb', line 181

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] || "tmp/turbo_rspec_runtime.log"
  @parallel_options = opts.fetch(:parallel_options, {})
  @parallel_options[:runtime_log] ||= @runtime_log
  @record_runtime = @parallel_options[:group_by] == :runtime

  @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]
end

Class Method Details

.create(count) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/turbo_tests/runner.rb', line 16

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



133
134
135
# File 'lib/turbo_tests/runner.rb', line 133

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

.normalize_order(order) ⇒ Object

Raises:

  • (ArgumentError)


125
126
127
128
129
130
131
# File 'lib/turbo_tests/runner.rb', line 125

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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/turbo_tests/runner.rb', line 85

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

.project_rspec_options(root = Dir.pwd) ⇒ Object



171
172
173
174
175
176
177
178
# File 'lib/turbo_tests/runner.rb', line 171

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



137
138
139
140
141
142
143
144
145
# File 'lib/turbo_tests/runner.rb', line 137

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



27
28
29
30
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
# File 'lib/turbo_tests/runner.rb', line 27

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)
  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)

  use_runtime_info = default_file_discovery

  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

  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
  ).run
end

.runtime_log_from_example_status(example_status_log) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/turbo_tests/runner.rb', line 110

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



100
101
102
103
104
105
106
107
108
# File 'lib/turbo_tests/runner.rb', line 100

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



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/turbo_tests/runner.rb', line 147

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



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
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
# File 'lib/turbo_tests/runner.rb', line 219

def run
  tests_with_size = ParallelTests::RSpec::Runner.tests_with_size(@files, {})
  @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_options
    )
  @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?)
        report_coverage = true
        exit_status = 0
      else
        flush_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