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

Fix RSpec/NamedSubject when block has no body #1668

Merged
merged 1 commit into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Fix a false negative for `RSpec/Pending` when `it` without body. ([@ydah])
- Add new `RSpec/ReceiveMessages` cop. ([@ydah])
- Add `AllowedIdentifiers` and `AllowedPatterns` configuration option to `RSpec/IndexedLet`. ([@ydah])
- Fix `RSpec/NamedSubject` when block has no body. ([@splattael])

## 2.22.0 (2023-05-06)

Expand Down Expand Up @@ -867,6 +868,7 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
[@seanpdoyle]: /~https://github.com/seanpdoyle
[@sl4vr]: /~https://github.com/sl4vr
[@smcgivern]: /~https://github.com/smcgivern
[@splattael]: /~https://github.com/splattael
[@stephannv]: /~https://github.com/stephannv
[@t3h2mas]: /~https://github.com/t3h2mas
[@tdeo]: /~https://github.com/tdeo
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/named_subject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def nearest_subject(node)
end

def find_subject(block_node)
block_node.body.child_nodes.find { |send_node| subject?(send_node) }
block_node.body&.child_nodes&.find { |send_node| subject?(send_node) }
end
end
end
Expand Down
10 changes: 10 additions & 0 deletions spec/rubocop/cop/rspec/named_subject_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,16 @@ def foo
end
RUBY
end

it 'ignores subject when block has no body' do
expect_no_offenses(<<-RUBY)
it "is a User" do
subject.each do
Copy link
Member

Choose a reason for hiding this comment

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

Is this a real-life example?
How did you find this in the first place?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@pirj Yes, it is in the GitLab code base. See https://gitlab.com/gitlab-org/gitlab/-/issues/249446#note_1461446904

bundle exec rubocop -d --only RSpec/NamedSubject spec/lib/gitlab/github_import/importer/single_endpoint_issue_notes_importer_spec.rb

For example: https://gitlab.com/gitlab-org/gitlab/-/blob/70654c7306d007eb6bdfa970747f682a7f556718/spec/lib/gitlab/github_import/importer/single_endpoint_issue_notes_importer_spec.rb#L40

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In total, I found 38 locations where this RSpec/NamedSubject with EnforcedStyle: named_only would fail because of passing an empty block to subject's method.

spec/lib/api/helpers/authentication_spec.rb:16:6.
spec/lib/gitlab/database/with_lock_retries_outside_transaction_spec.rb:118:12.
spec/lib/gitlab/database/with_lock_retries_outside_transaction_spec.rb:130:12.
spec/lib/gitlab/database/with_lock_retries_outside_transaction_spec.rb:154:8.
spec/lib/gitlab/database/with_lock_retries_outside_transaction_spec.rb:219:4.
spec/lib/gitlab/database/with_lock_retries_outside_transaction_spec.rb:223:4.
spec/lib/gitlab/database/with_lock_retries_outside_transaction_spec.rb:231:4.
spec/lib/gitlab/database/with_lock_retries_outside_transaction_spec.rb:238:4.
spec/lib/gitlab/database/with_lock_retries_spec.rb:118:12.
spec/lib/gitlab/database/with_lock_retries_spec.rb:130:12.
spec/lib/gitlab/database/with_lock_retries_spec.rb:155:10.
spec/lib/gitlab/database/with_lock_retries_spec.rb:166:10.
spec/lib/gitlab/database/with_lock_retries_spec.rb:233:4.
spec/lib/gitlab/database/with_lock_retries_spec.rb:237:4.
spec/lib/gitlab/database/with_lock_retries_spec.rb:245:4.
spec/lib/gitlab/database/with_lock_retries_spec.rb:254:4.
spec/lib/gitlab/database/with_lock_retries_spec.rb:265:4.
spec/lib/gitlab/database/with_lock_retries_spec.rb:271:4.
spec/lib/gitlab/github_import/importer/protected_branches_importer_spec.rb:188:6.
spec/lib/gitlab/github_import/importer/single_endpoint_diff_notes_importer_spec.rb:32:4.
spec/lib/gitlab/github_import/importer/single_endpoint_diff_notes_importer_spec.rb:51:4.
spec/lib/gitlab/github_import/importer/single_endpoint_diff_notes_importer_spec.rb:64:4.
spec/lib/gitlab/github_import/importer/single_endpoint_issue_notes_importer_spec.rb:31:4.
spec/lib/gitlab/github_import/importer/single_endpoint_issue_notes_importer_spec.rb:50:4.
spec/lib/gitlab/github_import/importer/single_endpoint_issue_notes_importer_spec.rb:63:4.
spec/lib/gitlab/github_import/importer/single_endpoint_merge_request_notes_importer_spec.rb:32:4.
spec/lib/gitlab/github_import/importer/single_endpoint_merge_request_notes_importer_spec.rb:51:4.
spec/lib/gitlab/github_import/importer/single_endpoint_merge_request_notes_importer_spec.rb:64:4.
spec/lib/gitlab/sidekiq_logging/structured_logger_spec.rb:390:6.
spec/lib/gitlab/sidekiq_middleware/size_limiter/server_spec.rb:28:2.
spec/lib/mattermost/session_spec.rb:39:6.
spec/lib/mattermost/session_spec.rb:97:8.
spec/services/projects/lfs_pointers/lfs_object_download_list_service_spec.rb:113:12.
spec/services/projects/lfs_pointers/lfs_object_download_list_service_spec.rb:127:8.
spec/services/projects/lfs_pointers/lfs_object_download_list_service_spec.rb:135:8.
spec/services/projects/lfs_pointers/lfs_object_download_list_service_spec.rb:55:8.
spec/services/projects/lfs_pointers/lfs_object_download_list_service_spec.rb:86:8.
spec/services/projects/lfs_pointers/lfs_object_download_list_service_spec.rb:98:10.

# empty body
end
end
RUBY
end
end

context 'when IgnoreSharedExamples is false' do
Expand Down