Skip to content

Commit

Permalink
Merge pull request #25 from ohbarye/improve-error-handle
Browse files Browse the repository at this point in the history
Improve error handle
  • Loading branch information
ohbarye authored May 6, 2024
2 parents 42c7ff5 + b0ff10a commit eb61288
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 42 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## [Unreleased]

## [0.4.0] - 2024-05-06

- Allow to use RSpec::Matchers for `worker: :none, :thread, :process` also.
- Make error message short to keep focusing on failure causes.
- Fix a bug for a case when parameters cannot be passed to a test block correctly.

## [0.3.0] - 2024-04-21

- Add experimental_ractor_rspec_integration mode. Be careful, it's quite experimental.
Expand Down
9 changes: 6 additions & 3 deletions lib/pbt/check/runner_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ def run_it_in_sequential(property, runner)
begin
property.run(val)
runner.handle_result(c)
rescue => e
# Catch all exceptions including RSpec's ExpectationNotMet (It inherits Exception).
rescue Exception => e # standard:disable Lint/RescueException:
c.exception = e
runner.handle_result(c)
break # Ignore the rest of the cases. Just pick up the first failure.
Expand Down Expand Up @@ -162,7 +163,8 @@ def run_it_in_threads(property, runner)
Parallel.map_with_index(runner, in_threads: Parallel.processor_count) do |val, index|
Case.new(val:, index:).tap do |c|
property.run(val)
rescue => e
# Catch all exceptions including RSpec's ExpectationNotMet (It inherits Exception).
rescue Exception => e # standard:disable Lint/RescueException:
c.exception = e
# It's possible to break this loop here by raising `Parallel::Break`.
# But if it raises, we cannot fetch all cases' result. So this loop continues until the end.
Expand All @@ -183,7 +185,8 @@ def run_it_in_processes(property, runner)
Parallel.map_with_index(runner, in_processes: Parallel.processor_count) do |val, index|
Case.new(val:, index:).tap do |c|
property.run(val)
rescue => e
# Catch all exceptions including RSpec's ExpectationNotMet (It inherits Exception).
rescue Exception => e # standard:disable Lint/RescueException:
c.exception = e
# It's possible to break this loop here by raising `Parallel::Break`.
# But if it raises, we cannot fetch all cases' result. So this loop continues until the end.
Expand Down
20 changes: 5 additions & 15 deletions lib/pbt/reporter/run_details_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ def report_run_details
message << error_backtrace
message << if @run_details.verbose
verbose_details
else
hint
end

raise PropertyFailure, message.join("\n") if message.size > 0
Expand All @@ -34,17 +32,17 @@ def report_run_details
def format_error_message
<<~MSG.chomp
Property failed after #{@run_details.num_runs} test(s)
{ seed: #{@run_details.seed} }
Counterexample: #{@run_details.counterexample}
Shrunk #{@run_details.num_shrinks} time(s)
Got #{@run_details.error_instance.class}: #{@run_details.error_message}
seed: #{@run_details.seed}
counterexample: #{@run_details.counterexample}
Shrunk #{@run_details.num_shrinks} time(s)
Got #{@run_details.error_instance.class}: #{@run_details.error_message}
MSG
end

def error_backtrace
return "" if @run_details.error_instance.backtrace_locations.nil? # It can be nil.

i = @run_details.verbose ? -1 : 10
i = @run_details.verbose ? -1 : 3
" #{@run_details.error_instance.backtrace_locations[..i].join("\n ")}"
end

Expand Down Expand Up @@ -89,14 +87,6 @@ def format_execution_summary

"\nExecution summary:\n#{summary_lines.join("\n")}\n"
end

# @return [String]
def hint
[
"\nHint: Set `verbose: true` in order to check the list of all failing values encountered during the run.",
"Hint: Set `seed: #{@run_details.seed}` in order to reproduce the failed test case with the same values."
].join("\n")
end
end
end
end
32 changes: 12 additions & 20 deletions spec/e2e/e2e_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,10 @@
}.to raise_error(Pbt::PropertyFailure) do |e|
expect(e.message).to include <<~MSG.chomp
Property failed after 1 test(s)
{ seed: 6478147390881634219670054323585906496 }
Counterexample: 0
Shrunk 0 time(s)
Got ZeroDivisionError: divided by 0
MSG
expect(e.message).to include <<~MSG.chomp
Hint: Set `verbose: true` in order to check the list of all failing values encountered during the run.
Hint: Set `seed: 6478147390881634219670054323585906496` in order to reproduce the failed test case with the same values.
seed: 6478147390881634219670054323585906496
counterexample: 0
Shrunk 0 time(s)
Got ZeroDivisionError: divided by 0
MSG
end
end
Expand All @@ -105,14 +101,10 @@
}.to raise_error(Pbt::PropertyFailure) do |e|
expect(e.message).to include <<~MSG.chomp
Property failed after 1 test(s)
{ seed: 152305683944880796308915131809827264455 }
Counterexample: [2, 11]
Shrunk 16 time(s)
Got RuntimeError: strings should be sorted as numbers ["11", "2"]
MSG
expect(e.message).to include <<~MSG.chomp
Hint: Set `verbose: true` in order to check the list of all failing values encountered during the run.
Hint: Set `seed: 152305683944880796308915131809827264455` in order to reproduce the failed test case with the same values.
seed: 152305683944880796308915131809827264455
counterexample: [2, 11]
Shrunk 16 time(s)
Got RuntimeError: strings should be sorted as numbers ["11", "2"]
MSG
end
end
Expand All @@ -133,10 +125,10 @@
# common part
expect(e.message).to include <<~MSG
Property failed after 1 test(s)
{ seed: 152305683944880796308915131809827264455 }
Counterexample: [2, 11]
Shrunk 16 time(s)
Got RuntimeError: strings should be sorted as numbers ["11", "2"]
seed: 152305683944880796308915131809827264455
counterexample: [2, 11]
Shrunk 16 time(s)
Got RuntimeError: strings should be sorted as numbers ["11", "2"]
MSG

# verbose part
Expand Down
8 changes: 4 additions & 4 deletions spec/pbt/check/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -507,10 +507,10 @@
}.to raise_error(Pbt::PropertyFailure) do |e|
expect(e.message).to include <<~MSG.chomp
Property failed after 1 test(s)
{ seed: 135479457171118952930684770951487304295 }
Counterexample: 0
Shrunk 21 time(s)
Got RSpec::Expectations::ExpectationNotMetError: expected 0 to be a kind of String
seed: 135479457171118952930684770951487304295
counterexample: 0
Shrunk 21 time(s)
Got RSpec::Expectations::ExpectationNotMetError: expected 0 to be a kind of String
MSG
end
end
Expand Down

0 comments on commit eb61288

Please sign in to comment.