Skip to content

Commit aeae4ff

Browse files
authored
Merge pull request #48 from serpapi/remove-default-seed
Remove default `seed` value
2 parents bb814e1 + 4115412 commit aeae4ff

File tree

8 files changed

+202
-141
lines changed

8 files changed

+202
-141
lines changed

.github/workflows/snyk_ruby-analysis.yml

Lines changed: 0 additions & 33 deletions
This file was deleted.

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
turbo_tests (2.2.3)
4+
turbo_tests (2.2.4)
55
parallel_tests (>= 3.3.0, < 5)
66
rspec (>= 3.10)
77

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
RSpec.describe "NoMethodError spec" do
2+
it("fails") { expect(nil[:key]).to eql("value") }
3+
end

lib/turbo_tests/cli.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def run
9898
end
9999
end
100100

101-
success = TurboTests::Runner.run(
101+
exitstatus = TurboTests::Runner.run(
102102
formatters: formatters,
103103
tags: tags,
104104
files: @argv.empty? ? ["spec"] : @argv,
@@ -109,11 +109,8 @@ def run
109109
seed: seed
110110
)
111111

112-
if success
113-
exit 0
114-
else
115-
exit 1
116-
end
112+
# From https://github.com/serpapi/turbo_tests/pull/20/
113+
exit exitstatus
117114
end
118115
end
119116
end

lib/turbo_tests/reporter.rb

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ module TurboTests
44
class Reporter
55
attr_writer :load_time
66

7-
def self.from_config(formatter_config, start_time)
8-
reporter = new(start_time)
7+
def self.from_config(formatter_config, start_time, seed, seed_used)
8+
reporter = new(start_time, seed, seed_used)
99

1010
formatter_config.each do |config|
1111
name, outputs = config.values_at(:name, :outputs)
@@ -23,13 +23,15 @@ def self.from_config(formatter_config, start_time)
2323
attr_reader :pending_examples
2424
attr_reader :failed_examples
2525

26-
def initialize(start_time)
26+
def initialize(start_time, seed, seed_used)
2727
@formatters = []
2828
@pending_examples = []
2929
@failed_examples = []
3030
@all_examples = []
3131
@messages = []
3232
@start_time = start_time
33+
@seed = seed
34+
@seed_used = seed_used
3335
@load_time = 0
3436
@errors_outside_of_examples_count = 0
3537
end
@@ -50,6 +52,38 @@ def add(name, outputs)
5052
end
5153
end
5254

55+
# Borrowed from RSpec::Core::Reporter
56+
# https://github.com/rspec/rspec-core/blob/5699fcdc4723087ff6139af55bd155ad9ad61a7b/lib/rspec/core/reporter.rb#L71
57+
def report(example_groups)
58+
start(example_groups)
59+
begin
60+
yield self
61+
ensure
62+
finish
63+
end
64+
end
65+
66+
def start(example_groups, time=RSpec::Core::Time.now)
67+
@start = time
68+
@load_time = (@start - @start_time).to_f
69+
70+
report_number_of_tests(example_groups)
71+
expected_example_count = example_groups.flatten(1).count
72+
73+
delegate_to_formatters(:seed, RSpec::Core::Notifications::SeedNotification.new(@seed, @seed_used))
74+
delegate_to_formatters(:start, RSpec::Core::Notifications::StartNotification.new(expected_example_count, @load_time))
75+
end
76+
77+
def report_number_of_tests(groups)
78+
name = ParallelTests::RSpec::Runner.test_file_name
79+
80+
num_processes = groups.size
81+
num_tests = groups.map(&:size).sum
82+
tests_per_process = (num_processes == 0 ? 0 : num_tests.to_f / num_processes).round
83+
84+
puts "#{num_processes} processes for #{num_tests} #{name}s, ~ #{tests_per_process} #{name}s per process"
85+
end
86+
5387
def group_started(notification)
5488
delegate_to_formatters(:example_group_started, notification)
5589
end
@@ -83,16 +117,18 @@ def message(message)
83117
@messages << message
84118
end
85119

86-
def error_outside_of_examples
120+
def error_outside_of_examples(error_message)
87121
@errors_outside_of_examples_count += 1
122+
message error_message
88123
end
89124

90125
def finish
91-
# SEE: https://bit.ly/2NP87Cz
92-
end_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
126+
end_time = RSpec::Core::Time.now
127+
128+
@duration = end_time - @start_time
129+
delegate_to_formatters :stop, RSpec::Core::Notifications::ExamplesNotification.new(self)
93130

94-
delegate_to_formatters(:start_dump,
95-
RSpec::Core::Notifications::NullNotification)
131+
delegate_to_formatters :start_dump, RSpec::Core::Notifications::NullNotification
96132
delegate_to_formatters(:dump_pending,
97133
RSpec::Core::Notifications::ExamplesNotification.new(
98134
self
@@ -110,13 +146,13 @@ def finish
110146
@load_time,
111147
@errors_outside_of_examples_count
112148
))
113-
delegate_to_formatters(:close,
114-
RSpec::Core::Notifications::NullNotification)
115-
end
116-
117-
def seed_notification(seed, seed_used)
118-
puts RSpec::Core::Notifications::SeedNotification.new(seed, seed_used).fully_formatted
119-
puts
149+
delegate_to_formatters(:seed,
150+
RSpec::Core::Notifications::SeedNotification.new(
151+
@seed,
152+
@seed_used,
153+
))
154+
ensure
155+
delegate_to_formatters :close, RSpec::Core::Notifications::NullNotification
120156
end
121157

122158
protected

lib/turbo_tests/runner.rb

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,19 @@ def self.run(opts = {})
1414
formatters = opts[:formatters]
1515
tags = opts[:tags]
1616

17-
# SEE: https://bit.ly/2NP87Cz
18-
start_time = opts.fetch(:start_time) { Process.clock_gettime(Process::CLOCK_MONOTONIC) }
17+
start_time = opts.fetch(:start_time) { RSpec::Core::Time.now }
1918
runtime_log = opts.fetch(:runtime_log, nil)
2019
verbose = opts.fetch(:verbose, false)
2120
fail_fast = opts.fetch(:fail_fast, nil)
2221
count = opts.fetch(:count, nil)
23-
seed = opts.fetch(:seed) || rand(0xFFFF).to_s
24-
seed_used = !opts[:seed].nil?
22+
seed = opts.fetch(:seed)
23+
seed_used = !seed.nil?
2524

2625
if verbose
2726
warn "VERBOSE"
2827
end
2928

30-
reporter = Reporter.from_config(formatters, start_time)
29+
reporter = Reporter.from_config(formatters, start_time, seed, seed_used)
3130

3231
new(
3332
reporter: reporter,
@@ -38,7 +37,7 @@ def self.run(opts = {})
3837
fail_fast: fail_fast,
3938
count: count,
4039
seed: seed,
41-
seed_used: seed_used
40+
seed_used: seed_used,
4241
).run
4342
end
4443

@@ -50,11 +49,12 @@ def initialize(opts)
5049
@verbose = opts[:verbose]
5150
@fail_fast = opts[:fail_fast]
5251
@count = opts[:count]
52+
@seed = opts[:seed]
53+
@seed_used = opts[:seed_used]
54+
5355
@load_time = 0
5456
@load_count = 0
5557
@failure_count = 0
56-
@seed = opts[:seed]
57-
@seed_used = opts[:seed_used]
5858

5959
@messages = Thread::Queue.new
6060
@threads = []
@@ -87,26 +87,25 @@ def run
8787
setup_tmp_dir
8888

8989
subprocess_opts = {
90-
record_runtime: use_runtime_info
90+
record_runtime: use_runtime_info,
9191
}
9292

93-
report_number_of_tests(tests_in_groups)
94-
95-
@reporter.seed_notification(@seed, @seed_used)
96-
97-
wait_threads = tests_in_groups.map.with_index do |tests, process_id|
98-
start_regular_subprocess(tests, process_id + 1, **subprocess_opts)
99-
end
100-
101-
handle_messages
102-
103-
@reporter.finish
93+
@reporter.report(tests_in_groups) do |reporter|
94+
wait_threads = tests_in_groups.map.with_index do |tests, process_id|
95+
start_regular_subprocess(tests, process_id + 1, **subprocess_opts)
96+
end
10497

105-
@reporter.seed_notification(@seed, @seed_used)
98+
handle_messages
10699

107-
@threads.each(&:join)
100+
@threads.each(&:join)
108101

109-
@reporter.failed_examples.empty? && wait_threads.map(&:value).all?(&:success?)
102+
if @reporter.failed_examples.empty? && wait_threads.map(&:value).all?(&:success?)
103+
0
104+
else
105+
# From https://github.com/serpapi/turbo_tests/pull/20/
106+
wait_threads.map { |thread| thread.value.exitstatus }.max
107+
end
108+
end
110109
end
111110

112111
private
@@ -157,12 +156,18 @@ def start_subprocess(env, extra_args, tests, process_id, record_runtime:)
157156
[]
158157
end
159158

159+
seed_option = if @seed_used
160+
[
161+
"--seed", @seed,
162+
]
163+
else
164+
[]
165+
end
166+
160167
command = [
161168
*command_name,
162169
*extra_args,
163-
"--seed", rand(0xFFFF).to_s,
164-
"--format", "ParallelTests::RSpec::RuntimeLogger",
165-
"--out", @runtime_log,
170+
*seed_option,
166171
"--format", "TurboTests::JsonRowsFormatter",
167172
*record_runtime_options,
168173
*tests,
@@ -254,12 +259,17 @@ def handle_messages
254259
break
255260
end
256261
when "message"
257-
@reporter.message(message[:message])
262+
if message[:message].include?("An error occurred") || message[:message].include?("occurred outside of examples")
263+
@reporter.error_outside_of_examples(message[:message])
264+
@error = true
265+
else
266+
@reporter.message(message[:message])
267+
end
258268
when "seed"
259269
when "close"
260270
when "error"
261-
@reporter.error_outside_of_examples
262-
@error = true
271+
# Do nothing
272+
nil
263273
when "exit"
264274
exited += 1
265275
if exited == @num_processes
@@ -277,15 +287,5 @@ def handle_messages
277287
def fail_fast_met
278288
!@fail_fast.nil? && @failure_count >= @fail_fast
279289
end
280-
281-
def report_number_of_tests(groups)
282-
name = ParallelTests::RSpec::Runner.test_file_name
283-
284-
num_processes = groups.size
285-
num_tests = groups.map(&:size).sum
286-
tests_per_process = (num_processes == 0 ? 0 : num_tests.to_f / num_processes).round
287-
288-
puts "#{num_processes} processes for #{num_tests} #{name}s, ~ #{tests_per_process} #{name}s per process"
289-
end
290290
end
291291
end

lib/turbo_tests/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module TurboTests
2-
VERSION = "2.2.3"
2+
VERSION = "2.2.4"
33
end

0 commit comments

Comments
 (0)