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 #9580] Add New Cop to Enforce Bundler Gem filename #9903

Merged
merged 4 commits into from Aug 26, 2021

Conversation

gregfletch
Copy link
Contributor

Add a new cop which enforces which bundler gem filename to use. By default, enforces Gemfile and its related Gemfile.lock file. Alternatively, setting RequiresGemfile to false enforces gems.rb and its related gems.locked file.


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

# This cop verifies that a project contains Gemfile or gems.rb file and correct
# associated lock file based on the configuration.
#
# @example RequiresGemfile: true (default)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd suggest "EnforcedStyle" with the options "Gemfile" and "gems.rb". To me at least, that's much easier to understand.

Copy link
Member

Choose a reason for hiding this comment

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

Absolutely agree, and it's more consistent with other cops that way.

class GemFilename < Base
include RangeHelp

MSG_GEMFILE_REQUIRED = 'gems.rb file was found but Gemfile is required.'
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please add backticks around the names of the files, so they are properly colored in the terminal.

file_path = processed_source.file_path
return if expected_gemfile?(file_path)

register_offense(processed_source, file_path)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I see this function uses only the underlying buffer, so I guess you can pass it down directly.

MSG_GEMS_RB_MISMATCHED
end

return if message.nil?
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it's a bit weird that this function might register an offense or not. Perhaps it has to be separated in two for clarity.

MSG_GEMFILE_MISMATCHED
end

add_offense(source_range(source_buffer, 1, 0), message: message)
Copy link
Member

Choose a reason for hiding this comment

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

I believe you should be able to use add_global_offense instead of the two add_offense calls since the offense isn't really related to a line.

# Adds an offense that has no particular location.
# No correction can be applied to global offenses
def add_global_offense(message = nil, severity: nil)
severity = find_severity(nil, severity)
message = find_message(nil, message)
@current_offenses <<
Offense.new(severity, Offense::NO_LOCATION, message, name, :unsupported)
end

Comment on lines 77 to 82
def gemfile_offense?(file_path)
gemfile_required? && %w[gems.rb gems.locked].include?(file_path)
end
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def gemfile_offense?(file_path)
gemfile_required? && %w[gems.rb gems.locked].include?(file_path)
end
def gemfile_offense?(file_path)
gemfile_required? && GEMS_RB_FILES.include?(file_path)
end

Comment on lines 81 to 86
def gems_rb_offense?(file_path)
gems_rb_required? && %w[Gemfile Gemfile.lock].include?(file_path)
end
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def gems_rb_offense?(file_path)
gems_rb_required? && %w[Gemfile Gemfile.lock].include?(file_path)
end
def gems_rb_offense?(file_path)
gems_rb_required? && GEMFILE_FILES.include?(file_path)
end

@dvandersluis
Copy link
Member

dvandersluis commented Jul 22, 2021

The other bundler cops handle gem/lock files that aren't in the root directory, but this doesn't appear like it'll do so given how the file names are matched.

@bbatsov
Copy link
Collaborator

bbatsov commented Aug 19, 2021

@gregfletch Ping :-)

@gregfletch
Copy link
Contributor Author

@gregfletch Ping :-)

Oops, completely forgot about this. Should have time by this weekend to fix up the remaining comments and finish this off. Sorry about that!

Add a new cop which enforces which bundler gem filename to use. By default, enforces Gemfile and its related Gemfile.lock file. Alternatively, setting RequiresGemfile to false enforces gems.rb and its related gems.locked file.
@bbatsov bbatsov merged commit baba941 into rubocop:master Aug 26, 2021
@bbatsov
Copy link
Collaborator

bbatsov commented Aug 26, 2021

Seems we're good to go now. Thanks!

@gregfletch gregfletch deleted the fix/gregfletch/gemfile-cop branch August 26, 2021 12:27
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