Skip to content

Commit

Permalink
Merge pull request #42 from y-yagi/classes_to_retry
Browse files Browse the repository at this point in the history
Add `classes_to_retry` option
  • Loading branch information
y-yagi authored Sep 15, 2024
2 parents 4566758 + b155ed2 commit 2ec4de9
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.2.3

* Add `classes_to_retry` option #42 [y-yagi]

## 0.2.2

* Add `methods_to_retry` option #33 [edudepetris]
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ Minitest::Retry.use!(
verbose: true, # Whether or not to display the message at the time of retry. The default is true.
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).
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).
)
```

Expand Down
23 changes: 16 additions & 7 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: [])
@retry_count, @io, @verbose, @exceptions_to_retry, @methods_to_retry = retry_count, io, verbose, exceptions_to_retry, methods_to_retry
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
@failure_callback, @consistent_failure_callback, @retry_callback = nil, nil, nil
Minitest.prepend(self)
end
Expand Down Expand Up @@ -44,6 +44,10 @@ def methods_to_retry
@methods_to_retry
end

def classes_to_retry
@classes_to_retry
end

def failure_callback
@failure_callback
end
Expand All @@ -56,16 +60,21 @@ def retry_callback
@retry_callback
end

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

if methods_to_retry.any?
return methods_to_retry.include?(klass_method_name)
end

return true if Minitest::Retry.exceptions_to_retry.empty?
errors = failures.map(&:error).map(&:class)
(errors & Minitest::Retry.exceptions_to_retry).any?
if exceptions_to_retry.any?
errors = failures.map(&:error).map(&:class)
return (errors & exceptions_to_retry).any?
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) }
end
end

Expand All @@ -74,7 +83,7 @@ def run_one_method(klass, method_name)
result = super(klass, method_name)

klass_method_name = "#{klass.name}##{method_name}"
return result unless Minitest::Retry.failure_to_retry?(result.failures, klass_method_name)
return result unless Minitest::Retry.failure_to_retry?(result.failures, klass_method_name, klass)
if !result.skipped?
Minitest::Retry.failure_callback.call(klass, method_name, result) if Minitest::Retry.failure_callback
Minitest::Retry.retry_count.times do |count|
Expand Down
52 changes: 52 additions & 0 deletions test/minitest/retry_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,58 @@ def another_fail
end
end

def test_retry_when_class_in_classes_to_retry
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! classes_to_retry: ["Minitest::Test"]
def fail
@@counter += 1
assert false, 'fail test'
end
end
Minitest::Runnable.run_one_method(retry_test, :fail, self.reporter)

assert_equal 4, retry_test.counter
end
end

def test_donot_retry_when_not_in_classes_to_retry
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! classes_to_retry: ["OtherClass"]
def another_fail
@@counter += 1
assert false, 'fail test'
end
end
Minitest::Runnable.run_one_method(retry_test, :another_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

0 comments on commit 2ec4de9

Please sign in to comment.