Skip to content

Commit

Permalink
Display reproduction command for flakey tests
Browse files Browse the repository at this point in the history
1. Expose the rspec seed.
2. Expose rspec example's location instead of the id.

Closes #32
Closes #45
  • Loading branch information
fragoulis committed Mar 23, 2021
1 parent 0a8ea67 commit 9f6816e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
9 changes: 8 additions & 1 deletion lib/rspecq/formatters/failure_recorder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ def message(n)
def example_failed(notification)
example = notification.example

rerun_cmd = "bin/rspec --seed #{RSpec.configuration.seed} #{example.location_rerun_argument}"

if @queue.requeue_job(example.id, @max_requeues)

# Save the rerun command for later. It will be used if this is
# a flaky test for more user-friendly reporting.
@queue.save_rerun_command(example.id, rerun_cmd)

# HACK: try to avoid picking the job we just requeued; we want it
# to be picked up by a different worker
sleep 0.5
Expand All @@ -44,7 +51,7 @@ def example_failed(notification)
msg = presenter.fully_formatted(nil, @colorizer)
msg << "\n"
msg << @colorizer.wrap(
"bin/rspec --seed #{RSpec.configuration.seed} #{example.location_rerun_argument}",
rerun_cmd,
RSpec.configuration.failure_color
)

Expand Down
8 changes: 8 additions & 0 deletions lib/rspecq/queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ def requeue_job(job, max_requeues)
)
end

def save_rerun_command(job, cmd)
@redis.hset(key("job_metadata"), job, cmd)
end

def rerun_command(job)
@redis.hget(key("job_metadata"), job)
end

def record_example_failure(example_id, message)
@redis.hset(key_failures, example_id, message)
end
Expand Down
9 changes: 7 additions & 2 deletions lib/rspecq/reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def report

@queue.record_build_time(tests_duration)

flaky_jobs = @queue.flaky_jobs
flaky_jobs = @queue.flaky_jobs.map { |job| @queue.rerun_command(job) }

puts summary(@queue.example_failures, @queue.non_example_errors,
flaky_jobs, humanize_duration(tests_duration))
Expand Down Expand Up @@ -107,7 +107,12 @@ def summary(failures, errors, flaky_jobs, duration)
if !flaky_jobs.empty?
summary << "\n\n"
summary << "Flaky jobs detected (count=#{flaky_jobs.count}):\n"
flaky_jobs.each { |j| summary << " #{j}\n" }
flaky_jobs.each do |j|
summary << RSpec::Core::Formatters::ConsoleCodes.wrap(
"#{j}\n",
RSpec.configuration.pending_color
)
end
end

summary
Expand Down
4 changes: 2 additions & 2 deletions test/test_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ def test_failing_suite

def test_flakey_suite
build_id = rand_id
exec_build("flakey_suite", "", build_id: build_id)
exec_build("flakey_suite", "--seed 1234", build_id: build_id)
output = exec_reporter(build_id: build_id)

assert_match "Flaky jobs detected", output
assert_match "./spec/foo_spec.rb[1:1]", output
assert_match "bin/rspec --seed 1234 ./spec/foo_spec.rb:2", output
refute_match "Failed examples", output
end
end

0 comments on commit 9f6816e

Please sign in to comment.