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

yield_control hardening #1167

merged 2 commits into from Mar 18, 2020

Conversation

@marcandre
Copy link
Contributor

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
@marcandre marcandre force-pushed the marcandre:yield_control_hardening branch from 6c23382 to 6e9f6c3 Mar 15, 2020
@pirj
pirj approved these changes Mar 15, 2020
Copy link
Member

pirj left a comment

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
@marcandre marcandre force-pushed the marcandre:yield_control_hardening branch from 6e9f6c3 to b3c7629 Mar 17, 2020
Copy link
Member

JonRowe left a comment

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 marcandre:yield_control_hardening branch 3 times, most recently from ad7ec3b to 90aac3f Mar 17, 2020
@pirj
pirj approved these changes Mar 18, 2020
@pirj pirj requested a review from JonRowe Mar 18, 2020
@marcandre marcandre force-pushed the marcandre:yield_control_hardening branch from 90aac3f to 1341b05 Mar 18, 2020
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.
@marcandre marcandre force-pushed the marcandre:yield_control_hardening branch from 1341b05 to 04efb1f Mar 18, 2020
@@ -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

This comment has been minimized.

Copy link
@pirj

pirj Mar 18, 2020

Member

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
2 checks passed
2 checks passed
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
JonRowe added a commit that referenced this pull request Mar 18, 2020
@JonRowe
Copy link
Member

JonRowe commented Mar 18, 2020

Thanks!

JonRowe added 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 added 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants
You can’t perform that action at this time.