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

Add RSpec/DuplicatedMetadata cop #1486

Merged
merged 1 commit into from
Nov 21, 2022

Conversation

r7kamura
Copy link
Contributor

@r7kamura r7kamura commented Nov 15, 2022

Looking at RSpec code in some libraries and applications, I see several duplicate metadata. So I think it would be nice to have a Cop that detects such code. What do you think?

For example, the following is the result from rubocop gem:

$ bundle exec rubocop spec
Inspecting 646 files
.................................................................................................................................................................................................................................................................................................................................................................................C....................................................................................................................................................................................................................................................................................

Offenses:

spec/rubocop/cop/style/conditional_assignment_assign_to_condition_spec.rb:3:69: C: [Correctable] RSpec/DuplicatedMetadata: Avoid duplicated metadata.
RSpec.describe RuboCop::Cop::Style::ConditionalAssignment, :config, :config, :config do
                                                                    ^^^^^^^
spec/rubocop/cop/style/conditional_assignment_assign_to_condition_spec.rb:3:78: C: [Correctable] RSpec/DuplicatedMetadata: Avoid duplicated metadata.
RSpec.describe RuboCop::Cop::Style::ConditionalAssignment, :config, :config, :config do
                                                                             ^^^^^^^

646 files inspected, 2 offenses detected, 2 offenses autocorrectable

And the following is from rubocop-rails gem:

$ bundle exec rubocop spec
Inspecting 133 files
................CCC..................................C...............................................................................

Offenses:

spec/rubocop/cop/rails/application_job_spec.rb:3:62: C: [Correctable] RSpec/DuplicatedMetadata: Avoid duplicated metadata.
RSpec.describe RuboCop::Cop::Rails::ApplicationJob, :config, :config do
                                                             ^^^^^^^
spec/rubocop/cop/rails/application_mailer_spec.rb:3:65: C: [Correctable] RSpec/DuplicatedMetadata: Avoid duplicated metadata.
RSpec.describe RuboCop::Cop::Rails::ApplicationMailer, :config, :config do
                                                                ^^^^^^^
spec/rubocop/cop/rails/application_record_spec.rb:3:65: C: [Correctable] RSpec/DuplicatedMetadata: Avoid duplicated metadata.
RSpec.describe RuboCop::Cop::Rails::ApplicationRecord, :config, :config do
                                                                ^^^^^^^
spec/rubocop/cop/rails/http_positional_arguments_spec.rb:3:71: C: [Correctable] RSpec/DuplicatedMetadata: Avoid duplicated metadata.
RSpec.describe RuboCop::Cop::Rails::HttpPositionalArguments, :config, :config do
                                                                      ^^^^^^^

133 files inspected, 4 offenses detected, 4 offenses autocorrectable

Before submitting the PR make sure the following are checked:

  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Updated documentation.
  • Added an entry to the CHANGELOG.md if the new code introduces user-observable changes.
  • The build (bundle exec rake) passes (be sure to run this locally, since it may produce updated documentation that you will need to commit).

If you have created a new cop:

  • Added the new cop to config/default.yml.
  • The cop is configured as Enabled: pending in config/default.yml.
  • The cop is configured as Enabled: true in .rubocop.yml.
  • The cop documents examples of good and bad code.
  • The tests assert both that bad code is reported and that good code is not reported.
  • Set VersionAdded in default/config.yml to the next minor version.

If you have modified an existing cop's configuration options:

  • Set VersionChanged in config/default.yml to the next major version.

@r7kamura r7kamura force-pushed the feature/duplicated-metadata branch from c3490fc to f6032f5 Compare November 15, 2022 22:31
@r7kamura r7kamura force-pushed the feature/duplicated-metadata branch from f6032f5 to 7804a84 Compare November 16, 2022 22:24
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.

Looks good, thank you!
Did a run on real-world-rspec, no errors.

@pirj pirj requested review from Darhazer and bquorning November 17, 2022 19:11
Copy link
Member

@Darhazer Darhazer left a comment

Choose a reason for hiding this comment

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

Not a requirement but just wondering - can it also flag :config, config: true duplication?

I’ll do a full review tomorrow

module Cop
module RSpec
# Helper methods to find RSpec metadata.
module Metadata
Copy link
Member

Choose a reason for hiding this comment

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

❤️

@r7kamura
Copy link
Contributor Author

Not a requirement but just wondering - can it also flag :config, config: true duplication?

It would be nice to support that case as well 👍
It might be good to do that in this Cop, but it is also a little hard to decide which one should be removed in case of such duplicates. How about we achieve that by having a separate Cop that converts config: true to :config? (Is there already one?)

@pirj
Copy link
Member

pirj commented Nov 17, 2022

a separate Cop that converts config: true to :config? (Is there already one?)

There was a PR that initially implemented that, but this part disappeared after a force-push. It remains here in lib/rubocop/cop/rspec/sort_tags.rb around line 187.

@Darhazer
Copy link
Member

I agree that :config vs config: true should be separate cop, and that it partially will resolve the question. (:config, config: false will remain an edge case)
Also I was wondering whether a hash metadata should be handled, but since Ruby itself will warn about the overwritten key, I'd say it's a no-goal for a cop.

@Darhazer
Copy link
Member

@r7kamura can you please rebase to fix the changelog entry 🙇

@r7kamura r7kamura force-pushed the feature/duplicated-metadata branch from 7804a84 to 66f3391 Compare November 21, 2022 12:59
@Darhazer Darhazer merged commit a1d2b46 into rubocop:master Nov 21, 2022
@r7kamura r7kamura deleted the feature/duplicated-metadata branch November 21, 2022 20:22
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.

4 participants