-
Notifications
You must be signed in to change notification settings - Fork 2
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
Warn if with_them
is used before where
is defined
#9
base: main
Are you sure you want to change the base?
Conversation
Marking as "Draft" for now as this idea and the implementation has to be tested at GitLab. |
lib/rspec/parameterized/core.rb
Outdated
@@ -39,6 +39,10 @@ def initialize(arg_names, &block) | |||
# end | |||
# | |||
def where(*args, &b) | |||
if @parameter | |||
format = ->(b) { b.source_location&.join(':') || '?' } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
where
can be called without a block so the source location would be missing.
Edit: Perhaps use caller
in those cases or always use caller
? 🤔
We'd need to store caller
in @parameter
as well then 🤷
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On the other hand, redefining where
is OKish and used at GitLab like this:
context "foo" do
where(...)
with_them do
it 'allow' do
end
end
where(...)
with_them do
it 'disallow' do
end
end
Removing this warning again.
with_them
is used before where
is defined
In https://gitlab.com/gitlab-org/gitlab/-/issues/430870#note_1637281194 I found some cases where describe 'foo' do
where(foo: [1, 2, 3])
describe 'bar' do
with_them do
it('works') {}
end
end
describe 'bar2' do
with_them do
it('works') {}
end
end
end This ☝️ gives |
@@ -79,6 +79,8 @@ def with_them(*args, &b) | |||
@parameter ||= nil | |||
|
|||
if @parameter.nil? | |||
warn "#{b&.source_location&.join(':') || caller[0]}: `where` not defined." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if with_them
makes sense to be called without a block 🤷
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Happy to add specs once the concept is accepted.
👋 @sue445 Please excuse the noise but I've made this PR ready for review again 😅 Do you mind checking? 🙏 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@splattael Thank you for patch. I understood your issue.
However, in rspec the behavior generally does not change depending on the order in which blocks are defined, so this change (even if it is a warning) would not be in line with rspec behavior.
For example, the following two both have the same definition and the same behavior.
describe "sum" do
subject { a + b }
let(:a) { 1 }
let(:b) { 2 }
it { should eq 3 }
end
describe "sum" do
it { should eq 3 }
let(:b) { 2 }
let(:a) { 1 }
subject { a + b }
end
Therefore, I think we should only treat it as a warning when where
is not defined in same scope as with_them
.
I agree 👍 I was wondering how we can achieve this 🤷 Currently, describe 'foo' do
with_them do # How do we know that `where` will follow in the same scope?
...
end
where(...)
end |
This PR emits a warning if
with_them
is used beforewhere
is defined.See #8
Impact
On this repository we are now seeing 3 warnings:
They relate to
rspec-parameterized-core/spec/rspec/parameterized/core_spec.rb
Lines 264 to 304 in 94ed779
However, emitting a warning in those cases would also cover the following case:
Full output