Skip to content

Commit

Permalink
Merge pull request #48 from varyform/master
Browse files Browse the repository at this point in the history
Retry based on example metadata
  • Loading branch information
michaelglass authored Jun 11, 2018
2 parents 38e8e0a + fe9fbb4 commit 9840048
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/rspec/retry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ def self.setup
config.add_setting :clear_lets_on_failure, :default => true
config.add_setting :display_try_failure_messages, :default => false

# retry based on example metadata
config.add_setting :retry_count_condition, :default => ->(_) { nil }

# If a list of exceptions is provided and 'retry' > 1, we only retry if
# the exception that was raised by the example is NOT in that list. Otherwise
# we ignore the 'retry' value and fail immediately.
Expand Down Expand Up @@ -56,6 +59,7 @@ def retry_count
(
ENV['RSPEC_RETRY_RETRY_COUNT'] ||
ex.metadata[:retry] ||
RSpec.configuration.retry_count_condition.call(ex) ||
RSpec.configuration.default_retry_count
).to_i,
1
Expand Down
9 changes: 9 additions & 0 deletions spec/lib/rspec/retry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ class SharedError < StandardError; end
end
end

context 'with lambda condition' do
before(:all) { set_expectations([false, true]) }

it "should get retry count from condition call", retry_me_once: true do
expect(true).to be(shift_expectation)
expect(count).to eq(2)
end
end

context 'with :retry => 0' do
after(:all) { @@this_ran_once = nil }
it 'should still run once', retry: 0 do
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@
config.around :each, :overridden do |ex|
ex.run_with_retry retry: 3
end

config.retry_count_condition = ->(example) { example.metadata[:retry_me_once] ? 2 : nil }
end

0 comments on commit 9840048

Please sign in to comment.