Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add seed parameter to turbo tests #33

Merged
merged 5 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<div align="center">
<img src="https://user-images.githubusercontent.com/78694043/233910064-87a6d557-1120-42d2-b965-2a9403c6f2f4.svg" width="500" alt="Turbo-Tests">

</div>

<div align="center">
Expand Down Expand Up @@ -96,6 +96,7 @@ Options:
--runtime-log FILE Location of previously recorded test runtimes
-v, --verbose More output
--fail-fast=[N]
--seed SEED Seed for rspec
```

## Development
Expand Down
6 changes: 6 additions & 0 deletions lib/turbo_tests/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def run
runtime_log = nil
verbose = false
fail_fast = nil
seed = nil

OptionParser.new { |opts|
opts.banner = <<~BANNER
Expand Down Expand Up @@ -76,6 +77,10 @@ def run
end
fail_fast = n.nil? || n < 1 ? 1 : n
end

opts.on("--seed SEED", "Seed for rspec") do |s|
seed = s
end
}.parse!(@argv)

requires.each { |f| require(f) }
Expand All @@ -101,6 +106,7 @@ def run
verbose: verbose,
fail_fast: fail_fast,
count: count,
seed: seed
)

if success
Expand Down
5 changes: 5 additions & 0 deletions lib/turbo_tests/reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ def finish
RSpec::Core::Notifications::NullNotification)
end

def seed_notification(seed, seed_used)
puts RSpec::Core::Notifications::SeedNotification.new(seed, seed_used).fully_formatted
puts
end

protected

def delegate_to_formatters(method, *args)
Expand Down
14 changes: 12 additions & 2 deletions lib/turbo_tests/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def self.run(opts = {})
verbose = opts.fetch(:verbose, false)
fail_fast = opts.fetch(:fail_fast, nil)
count = opts.fetch(:count, nil)
seed = opts.fetch(:seed, rand(0xFFFF).to_s)
seed_used = !opts[:seed].nil?

if verbose
STDERR.puts "VERBOSE"
Expand All @@ -34,7 +36,9 @@ def self.run(opts = {})
runtime_log: runtime_log,
verbose: verbose,
fail_fast: fail_fast,
count: count
count: count,
seed: seed,
seed_used: seed_used
).run
end

Expand All @@ -49,6 +53,8 @@ def initialize(opts)
@load_time = 0
@load_count = 0
@failure_count = 0
@seed = opts[:seed]
@seed_used = opts[:seed_used]

@messages = Thread::Queue.new
@threads = []
Expand Down Expand Up @@ -86,6 +92,8 @@ def run

report_number_of_tests(tests_in_groups)

@reporter.seed_notification(@seed, @seed_used)

wait_threads = tests_in_groups.map.with_index do |tests, process_id|
start_regular_subprocess(tests, process_id + 1, **subprocess_opts)
end
Expand All @@ -94,6 +102,8 @@ def run

@reporter.finish

@reporter.seed_notification(@seed, @seed_used)

@threads.each(&:join)

@reporter.failed_examples.empty? && wait_threads.map(&:value).all?(&:success?)
Expand Down Expand Up @@ -150,7 +160,7 @@ def start_subprocess(env, extra_args, tests, process_id, record_runtime:)
command = [
ENV["BUNDLE_BIN_PATH"], "exec", "rspec",
*extra_args,
"--seed", rand(0xFFFF).to_s,
"--seed", @seed,
"--format", "TurboTests::JsonRowsFormatter",
"--out", tmp_filename,
*record_runtime_options,
Expand Down
14 changes: 11 additions & 3 deletions spec/cli_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
RSpec.describe TurboTests::CLI do
subject(:output) { `bundle exec turbo_tests -f d #{fixture}`.strip }
subject(:output) { `bundle exec turbo_tests -f d #{fixture} --seed 1234`.strip }

before { output }

Expand All @@ -8,6 +8,9 @@
%(
1 processes for 1 specs, ~ 1 specs per process

Randomized with seed 1234


An error occurred while loading #{fixture}.
\e[31mFailure/Error: \e[0m\e[1;34m1\e[0m / \e[1;34m0\e[0m\e[0m
\e[31m\e[0m
Expand All @@ -18,14 +21,19 @@
\e[36m# #{fixture}:1:in `<top (required)>'\e[0m
).strip
}
let(:expected_end_of_output) do
"0 examples, 0 failures\n"\
"\n\n"\
"Randomized with seed 1234"
end

let(:fixture) { "./fixtures/rspec/errors_outside_of_examples_spec.rb" }

it "reports" do
expect($?.exitstatus).to eql(1)

expect(output).to start_with(expected_start_of_output)
expect(output).to end_with("0 examples, 0 failures")
expect(output).to end_with(expected_end_of_output)
end
end

Expand Down Expand Up @@ -66,7 +74,7 @@
expect(output).to include(part)
end

expect(output).to end_with("3 examples, 0 failures, 3 pending")
expect(output).to end_with("3 examples, 0 failures, 3 pending\n\n\nRandomized with seed 1234")
end
end

Expand Down