From cdc3096798997282a64fd0a4abe0c19fa497706f Mon Sep 17 00:00:00 2001 From: Tejas Bubane Date: Mon, 17 May 2021 20:43:31 +0530 Subject: [PATCH 1/3] [Fix #9803] Fix `Bundler/GemVersion` cop not respecting git tags Closes #9803 --- .../fix_gem_version_not_respecting_tags.md | 1 + lib/rubocop/cop/bundler/gem_version.rb | 26 +++++++++++++++++-- spec/rubocop/cop/bundler/gem_version_spec.rb | 13 ++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 changelog/fix_gem_version_not_respecting_tags.md diff --git a/changelog/fix_gem_version_not_respecting_tags.md b/changelog/fix_gem_version_not_respecting_tags.md new file mode 100644 index 00000000000..0022e4b1d5d --- /dev/null +++ b/changelog/fix_gem_version_not_respecting_tags.md @@ -0,0 +1 @@ +* [#9803](https://github.com/rubocop/rubocop/pull/9803): Fix `Bundler/GemVersion` cop not respecting git tags. ([@tejasbubane][]) diff --git a/lib/rubocop/cop/bundler/gem_version.rb b/lib/rubocop/cop/bundler/gem_version.rb index 6d73edbf303..a18aa945eaf 100644 --- a/lib/rubocop/cop/bundler/gem_version.rb +++ b/lib/rubocop/cop/bundler/gem_version.rb @@ -45,6 +45,21 @@ 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 _)) + PATTERN + def on_send(node) return unless gem_declaration?(node) return if allowed_gem?(node) @@ -78,8 +93,15 @@ def message(range) end def offense?(node) - (required_style? && !includes_version_specification?(node)) || - (forbidden_style? && includes_version_specification?(node)) + required_offense?(node) || forbidden_offense?(node) + end + + def required_offense?(node) + required_style? && !includes_version_specification?(node) && !with_git_ref?(node) + end + + def forbidden_offense?(node) + forbidden_style? && includes_version_specification?(node) end def forbidden_style? diff --git a/spec/rubocop/cop/bundler/gem_version_spec.rb b/spec/rubocop/cop/bundler/gem_version_spec.rb index 840dbd2e68b..7e58913a8ae 100644 --- a/spec/rubocop/cop/bundler/gem_version_spec.rb +++ b/spec/rubocop/cop/bundler/gem_version_spec.rb @@ -15,6 +15,8 @@ ^^^^^^^^^^^^^ 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 @@ -24,6 +26,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' RUBY end @@ -67,5 +72,13 @@ 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 From 5eea3236652413a5ebd80de41347a59e1274137b Mon Sep 17 00:00:00 2001 From: Tim Kelly Date: Sun, 20 Jun 2021 18:54:53 -0500 Subject: [PATCH 2/3] Update Bundler/GemVersion to check for branch, ref, and tag --- .../fix_gem_version_not_respecting_tags.md | 2 +- lib/rubocop/cop/bundler/gem_version.rb | 46 ++++++++++++------- spec/rubocop/cop/bundler/gem_version_spec.rb | 22 ++++----- 3 files changed, 39 insertions(+), 31 deletions(-) diff --git a/changelog/fix_gem_version_not_respecting_tags.md b/changelog/fix_gem_version_not_respecting_tags.md index 0022e4b1d5d..22b4595ce3b 100644 --- a/changelog/fix_gem_version_not_respecting_tags.md +++ b/changelog/fix_gem_version_not_respecting_tags.md @@ -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][]) diff --git a/lib/rubocop/cop/bundler/gem_version.rb b/lib/rubocop/cop/bundler/gem_version.rb index a18aa945eaf..dfe8f2ab645 100644 --- a/lib/rubocop/cop/bundler/gem_version.rb +++ b/lib/rubocop/cop/bundler/gem_version.rb @@ -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 @@ -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' @@ -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 @@ -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) @@ -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? diff --git a/spec/rubocop/cop/bundler/gem_version_spec.rb b/spec/rubocop/cop/bundler/gem_version_spec.rb index 7e58913a8ae..83f0cb12056 100644 --- a/spec/rubocop/cop/bundler/gem_version_spec.rb +++ b/spec/rubocop/cop/bundler/gem_version_spec.rb @@ -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 @@ -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 @@ -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 @@ -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 From d2ad9c036e2e4b5b8767065fa348bbd45d6e9062 Mon Sep 17 00:00:00 2001 From: Tim Kelly Date: Sun, 20 Jun 2021 22:23:06 -0500 Subject: [PATCH 3/3] Update changelog/fix_gem_version_not_respecting_tags.md Co-authored-by: Daniel Vandersluis --- changelog/fix_gem_version_not_respecting_tags.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/fix_gem_version_not_respecting_tags.md b/changelog/fix_gem_version_not_respecting_tags.md index 22b4595ce3b..faecdd74036 100644 --- a/changelog/fix_gem_version_not_respecting_tags.md +++ b/changelog/fix_gem_version_not_respecting_tags.md @@ -1 +1 @@ -* [#9803](https://github.com/rubocop/rubocop/pull/9803): Fix `Bundler/GemVersion` cop not respecting git tags. ([@tejasbubane,@timlkelly][]) +* [#9803](https://github.com/rubocop/rubocop/pull/9803): Fix `Bundler/GemVersion` cop not respecting git tags. ([@tejasbubane][], [@timlkelly][])