Skip to content

Commit

Permalink
Update Bundler/GemVersion to check for branch, ref, and tag
Browse files Browse the repository at this point in the history
  • Loading branch information
timlkelly authored and bbatsov committed Jun 25, 2021
1 parent 4326083 commit 19390ca
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 31 deletions.
2 changes: 1 addition & 1 deletion changelog/fix_gem_version_not_respecting_tags.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* [#9803](https://github.com/rubocop/rubocop/pull/9803): Fix `Bundler/GemVersion` cop not respecting git tags. ([@tejasbubane][])
* [#9803](https://github.com/rubocop/rubocop/pull/9803): Fix `Bundler/GemVersion` cop not respecting git tags. ([@tejasbubane,@timlkelly][])
46 changes: 29 additions & 17 deletions lib/rubocop/cop/bundler/gem_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
module RuboCop
module Cop
module Bundler
# Enforce that Gem version specifications are either required
# or forbidden.
# Enforce that Gem version specifications or a commit reference (branch,
# ref, or tag) are either required or forbidden.
#
# @example EnforcedStyle: required (default)
# # bad
Expand All @@ -19,6 +19,15 @@ module Bundler
# # good
# gem 'rubocop', '>= 1.5.0', '< 1.10.0'
#
# # good
# gem 'rubocop', branch: 'feature-branch'
#
# # good
# gem 'rubocop', ref: '74b5bfbb2c4b6fd6cdbbc7254bd7084b36e0c85b'
#
# # good
# gem 'rubocop', tag: 'v1.17.0'
#
# @example EnforcedStyle: forbidden
# # good
# gem 'rubocop'
Expand All @@ -32,6 +41,15 @@ module Bundler
# # bad
# gem 'rubocop', '>= 1.5.0', '< 1.10.0'
#
# # bad
# gem 'rubocop', branch: 'feature-branch'
#
# # bad
# gem 'rubocop', ref: '74b5bfbb2c4b6fd6cdbbc7254bd7084b36e0c85b'
#
# # bad
# gem 'rubocop', tag: 'v1.17.0'
#
class GemVersion < Base
include ConfigurableEnforcedStyle
include GemDeclaration
Expand All @@ -45,19 +63,9 @@ class GemVersion < Base
(send nil? :gem <(str #version_specification?) ...>)
PATTERN

# @!method with_git_ref?(node)
def_node_matcher :with_git_ref?, <<~PATTERN
(send nil? :gem <(hash <#git? #tag_ref? ...>) ...>)
PATTERN

# @!method tag_ref?(node)
def_node_matcher :tag_ref?, <<~PATTERN
(pair (sym {:tag :ref}) (str _))
PATTERN

# @!method git?(node)
def_node_matcher :git?, <<~PATTERN
(pair (sym {:git :github :bitbucket}) (str _))
# @!method includes_commit_reference?(node)
def_node_matcher :includes_commit_reference?, <<~PATTERN
(send nil? :gem <(hash <(pair (sym {:branch :ref :tag}) (str _)) ...>) ...>)
PATTERN

def on_send(node)
Expand Down Expand Up @@ -97,11 +105,15 @@ def offense?(node)
end

def required_offense?(node)
required_style? && !includes_version_specification?(node) && !with_git_ref?(node)
return unless required_style?

!includes_version_specification?(node) && !includes_commit_reference?(node)
end

def forbidden_offense?(node)
forbidden_style? && includes_version_specification?(node)
return unless forbidden_style?

includes_version_specification?(node) || includes_commit_reference?(node)
end

def forbidden_style?
Expand Down
22 changes: 9 additions & 13 deletions spec/rubocop/cop/bundler/gem_version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
^^^^^^^^^^^^^ Gem version specification is required.
gem 'rubocop', require: false
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Gem version specification is required.
gem 'rubocop', tag: '1.2.0'
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Gem version specification is required.
RUBY
end

Expand All @@ -26,9 +24,9 @@
gem 'rubocop', '~> 1'
gem 'rubocop', '~> 1.12', require: false
gem 'rubocop', '>= 1.5.0', '< 1.10.0', git: 'https://github.com/rubocop/rubocop'
gem 'rubocop', github: 'rubocop/rubocop', tag: 'v1'
gem 'rubocop', git: 'https://github.com/rubocop/rubocop', ref: 'b3f37bc7f'
gem 'foobar', bitbucket: 'foo/bar', tag: 'v1'
gem 'rubocop', branch: 'feature-branch'
gem 'rubocop', ref: 'b3f37bc7f'
gem 'rubocop', tag: 'v1'
RUBY
end

Expand Down Expand Up @@ -57,6 +55,12 @@
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Gem version specification is forbidden.
gem 'rubocop', '>= 1.5.0', '< 1.10.0', git: 'https://github.com/rubocop/rubocop'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Gem version specification is forbidden.
gem 'rubocop', branch: 'feature-branch'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Gem version specification is forbidden.
gem 'rubocop', ref: 'b3f37bc7f'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Gem version specification is forbidden.
gem 'rubocop', tag: 'v1'
^^^^^^^^^^^^^^^^^^^^^^^^ Gem version specification is forbidden.
RUBY
end

Expand All @@ -72,13 +76,5 @@
gem 'rspec', '~> 3.10'
RUBY
end

it 'does not flag gems using git source with tag or ref' do
expect_no_offenses(<<~RUBY)
gem 'rubocop', github: 'rubocop/rubocop', tag: 'v1'
gem 'rubocop', git: 'https://github.com/rubocop/rubocop', ref: 'b3f37bc7f'
gem 'foobar', bitbucket: 'foo/bar', tag: 'v1'
RUBY
end
end
end

0 comments on commit 19390ca

Please sign in to comment.