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

yield_control hardening #1167

Merged
merged 2 commits into from Mar 18, 2020
Merged

Conversation

marcandre
Copy link
Contributor

@marcandre marcandre commented Mar 15, 2020

This fixes two misuses of yield_control.

The following resulted in strange failures

expect{ .. }.to yield_control.at_least(:trice)

Worse, the following doesn't test what is thought to be tested:

expect{ .. }.to yield_control.at_least(2).at_most(4).times

This PR fixes both.

@marcandre marcandre changed the title Yield control hardening yield_control hardening Mar 15, 2020
Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch.
Looks good. Thanks!

spec/rspec/matchers/built_in/yield_spec.rb Outdated Show resolved Hide resolved
lib/rspec/matchers/built_in/yield.rb Outdated Show resolved Hide resolved
Copy link
Member

@JonRowe JonRowe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I think this needs implementing properly soon but if you don't have the bandwidth to do that these tweaks shall suffice.

lib/rspec/matchers/built_in/yield.rb Outdated Show resolved Hide resolved
lib/rspec/matchers/built_in/yield.rb Outdated Show resolved Hide resolved
lib/rspec/matchers/built_in/yield.rb Outdated Show resolved Hide resolved
spec/rspec/matchers/built_in/yield_spec.rb Outdated Show resolved Hide resolved
…t_most

Currently, `exactly(:trice)` would always provide a failure with message 'expected given block to yield control', even if control was yielded.

`at_least(:trice)` yields a cryptic error 'ArgumentError: comparison of Integer with nil failedinstead of an error as it should'.

Some unused code was detected and removed.
@marcandre marcandre force-pushed the yield_control_hardening branch 3 times, most recently from ad7ec3b to 90aac3f Compare March 17, 2020 22:40
In particular, it would be reasonable to expect yield_control.at_least(1).at_most(4).times to work

This also simplifies the failure message in case no count constraint was called.
@@ -67,6 +67,22 @@ def each_arg(*args, &block)
expect(val).to be_nil
end

it 'raises an error if given an invalid count argument' do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for clarity, do you mind to add examples for a failure message like

expect {
  expect { |b| 0.times.each(&b) }.to yield_control.at_least(:once)
}.to fail_with(/expected given block to yield control at least once but did not yield/)

expect {
  expect { |b| 2.times.each(&b) }.to yield_control.at_most(:once)
}.to fail_with(/expected given block to yield control at most once but yielded twice/)

expect {
  expect { |b| 1.times.each(&b) }.to yield_control.at_least(:twice)
}.to fail_with(/expected given block to yield control at least twice but yielded once/)

expect {
  expect { |b| 0.times.each(&b) }.to yield_control
}.to fail_with(/expected given block to yield control but did not yield/)

@JonRowe JonRowe merged commit 5f44bcf into rspec:master Mar 18, 2020
JonRowe added a commit that referenced this pull request Mar 18, 2020
@JonRowe
Copy link
Member

JonRowe commented Mar 18, 2020

Thanks!

JonRowe pushed a commit that referenced this pull request May 8, 2020
Prevent invalid argument given to yield_control.exactly / at_least / at_most

Previously `exactly(:trice)` would always provide a failure with message 'expected given block to yield control', even if control was yielded.

`at_least(:trice)` yields a cryptic error 'ArgumentError: comparison of Integer with nil failedinstead of an error as it should'.
JonRowe added a commit that referenced this pull request May 8, 2020
JonRowe pushed a commit that referenced this pull request May 8, 2020
Prevent invalid argument given to yield_control.exactly / at_least / at_most

Previously `exactly(:trice)` would always provide a failure with message 'expected given block to yield control', even if control was yielded.

`at_least(:trice)` yields a cryptic error 'ArgumentError: comparison of Integer with nil failedinstead of an error as it should'.
JonRowe added a commit that referenced this pull request May 8, 2020
yujinakayama pushed a commit to yujinakayama/rspec-monorepo that referenced this pull request Oct 6, 2021
Prevent invalid argument given to yield_control.exactly / at_least / at_most

Previously `exactly(:trice)` would always provide a failure with message 'expected given block to yield control', even if control was yielded.

`at_least(:trice)` yields a cryptic error 'ArgumentError: comparison of Integer with nil failedinstead of an error as it should'.

---
This commit was imported from rspec/rspec-expectations@96bad96.
yujinakayama pushed a commit to yujinakayama/rspec-monorepo that referenced this pull request Oct 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants