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.



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/turbo_tests/runner.rb', line 147

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
  @error = false
  @print_failed_group = opts[:print_failed_group]
end

Class Method Details

.create(count) ⇒ Object



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

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



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

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

.normalize_order(order) ⇒ Object

Raises:

  • (ArgumentError)


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

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



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

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

.rspec_configured_files_to_runObject



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

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



26
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
# File 'lib/turbo_tests/runner.rb', line 26

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



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

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



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

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

Instance Method Details

#runObject



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
218
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
# File 'lib/turbo_tests/runner.rb', line 184

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