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 the option to skip retry #46

Merged
merged 1 commit into from
Jan 26, 2025
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 @@ -39,7 +39,8 @@ Minitest::Retry.use!(
io: $stdout, # Display destination of retry when the message. The default is stdout.
exceptions_to_retry: [], # List of exceptions that will trigger a retry (when empty, all exceptions will).
methods_to_retry: [], # List of methods that will trigger a retry (when empty, all methods will).
classes_to_retry: [] # List of classes that will trigger a retry (when empty, all classes will).
classes_to_retry: [], # List of classes that will trigger a retry (when empty, all classes will).
methods_to_skip: [] # List of methods that will skip a retry (when empty, all methods will retry).
)
```

Expand Down
12 changes: 10 additions & 2 deletions lib/minitest/retry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
module Minitest
module Retry
class << self
def use!(retry_count: 3, io: $stdout, verbose: true, exceptions_to_retry: [], methods_to_retry: [], classes_to_retry: [])
@retry_count, @io, @verbose, @exceptions_to_retry, @methods_to_retry, @classes_to_retry = retry_count, io, verbose, exceptions_to_retry, methods_to_retry, classes_to_retry
def use!(retry_count: 3, io: $stdout, verbose: true, exceptions_to_retry: [], methods_to_retry: [], classes_to_retry: [], methods_to_skip: [])
@retry_count, @io, @verbose, @exceptions_to_retry, @methods_to_retry, @classes_to_retry, @methods_to_skip = retry_count, io, verbose, exceptions_to_retry, methods_to_retry, classes_to_retry, methods_to_skip
@failure_callback, @consistent_failure_callback, @retry_callback = nil, nil, nil
Minitest.prepend(self)
end
Expand Down Expand Up @@ -60,6 +60,10 @@ def retry_callback
@retry_callback
end

def methods_to_skip
@methods_to_skip
end

def failure_to_retry?(failures = [], klass_method_name, klass)
return false if failures.empty?

Expand All @@ -72,6 +76,10 @@ def failure_to_retry?(failures = [], klass_method_name, klass)
return (errors & exceptions_to_retry).any?
end

if methods_to_skip.any?
return !methods_to_skip.include?(klass_method_name)
end

return true if classes_to_retry.empty?
ancestors = klass.ancestors.map(&:to_s)
return classes_to_retry.any? { |class_to_retry| ancestors.include?(class_to_retry) }
Expand Down
26 changes: 26 additions & 0 deletions test/minitest/retry_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,32 @@ def another_fail
end
end

def test_retry_when_method_in_methods_to_skip
capture_stdout do
retry_test = Class.new(Minitest::Test) do
@@counter = 0

class << self
def name
'TestClass'
end
end

def self.counter
@@counter
end
Minitest::Retry.use! methods_to_skip: ["TestClass#fail"]
def fail
@@counter += 1
assert false, 'fail test'
end
end
Minitest::Runnable.run_one_method(retry_test, :fail, self.reporter)

assert_equal 1, retry_test.counter
end
end

def test_run_failure_callback_on_failure
on_failure_block_has_ran = false
test_name, test_class, retry_test, result_in_callback = nil
Expand Down
Loading