Skip to content

Commit

Permalink
[Fix rubocop#12113] Fix a false positive for Bundler/DuplicatedGroup
Browse files Browse the repository at this point in the history
Fix rubocop#12113.

This PR fixes a false positive for `Bundler/DuplicatedGroup`
when groups are duplicated but `source`, `git`, `platforms`,
or `path` values are different.
  • Loading branch information
koic committed Aug 14, 2023
1 parent d929f68 commit 5da5286
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12113](https://github.com/rubocop/rubocop/issues/12113): Fix a false positive for `Bundler/DuplicatedGroup` when groups are duplicated but `source`, `git`, `platforms`, or `path` values are different. ([@koic][])
39 changes: 36 additions & 3 deletions lib/rubocop/cop/bundler/duplicated_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@ module Cop
module Bundler
# A Gem group, or a set of groups, should be listed only once in a Gemfile.
#
# For example, if the values of `source`, `git`, `platforms`, or `path`
# surrounding `group` are different, no offense will be registered:
#
# [source,ruby]
# -----
# platforms :ruby do
# group :default do
# gem 'openssl'
# end
# end
#
# platforms :jruby do
# group :default do
# gem 'jruby-openssl'
# end
# end
# -----
#
# @example
# # bad
# group :development do
Expand Down Expand Up @@ -42,6 +60,7 @@ class DuplicatedGroup < Base

MSG = 'Gem group `%<group_name>s` already defined on line ' \
'%<line_of_first_occurrence>d of the Gemfile.'
SOURCE_BLOCK_NAMES = %i[source git platforms path].freeze

# @!method group_declarations(node)
def_node_search :group_declarations, '(send nil? :group ...)'
Expand All @@ -61,11 +80,15 @@ def on_new_investigation
private

def duplicated_group_nodes
groups = group_declarations(processed_source.ast).group_by do |node|
group_attributes(node).sort
group_declarations = group_declarations(processed_source.ast)
group_keys = group_declarations.group_by do |node|
source_key = find_source_key(node)
group_attributes = group_attributes(node).sort.join

"#{source_key}#{group_attributes}"
end

groups.values.select { |nodes| nodes.size > 1 }
group_keys.values.select { |nodes| nodes.size > 1 }
end

def register_offense(node, group_name, line_of_first_occurrence)
Expand All @@ -79,6 +102,16 @@ def register_offense(node, group_name, line_of_first_occurrence)
add_offense(offense_location, message: message)
end

def find_source_key(node)
source_block = node.each_ancestor(:block).find do |block_node|
SOURCE_BLOCK_NAMES.include?(block_node.method_name)
end

return unless source_block

"#{source_block.method_name}#{source_block.send_node.first_argument&.source}"
end

def group_attributes(node)
node.arguments.map do |argument|
if argument.hash_type?
Expand Down
163 changes: 163 additions & 0 deletions spec/rubocop/cop/bundler/duplicated_group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,5 +148,168 @@
RUBY
end
end

context 'and a set of groups is duplicated but `source` URLs are different' do
it 'does not register an offense' do
expect_no_offenses(<<-RUBY, 'Gemfile')
source 'https://rubygems.pkg.github.com/private-org' do
group :development do
gem 'rubocop'
end
end
group :development do
gem 'rubocop-rails'
end
RUBY
end
end

context 'and a set of groups is duplicated and `source` URLs are the same' do
it 'registers an offense' do
expect_offense(<<-RUBY, 'Gemfile')
source 'https://rubygems.pkg.github.com/private-org' do
group :development do
gem 'rubocop'
end
end
source 'https://rubygems.pkg.github.com/private-org' do
group :development do
^^^^^^^^^^^^^^^^^^ Gem group `:development` already defined on line 2 of the Gemfile.
gem 'rubocop-rails'
end
end
group :development do
gem 'rubocop-performance'
end
RUBY
end
end

context 'and a set of groups is duplicated but `git` URLs are different' do
it 'does not register an offense' do
expect_no_offenses(<<-RUBY, 'Gemfile')
git 'https://github.com/rubocop/rubocop.git' do
group :default do
gem 'rubocop'
end
end
git 'https://github.com/rails/rails.git' do
group :default do
gem 'activesupport'
gem 'actionpack'
end
end
RUBY
end
end

context 'and a set of groups is duplicated and `git` URLs are the same' do
it 'registers an offense' do
expect_offense(<<-RUBY, 'Gemfile')
git 'https://github.com/rails/rails.git' do
group :default do
gem 'activesupport'
end
end
git 'https://github.com/rails/rails.git' do
group :default do
^^^^^^^^^^^^^^ Gem group `:default` already defined on line 2 of the Gemfile.
gem 'actionpack'
end
end
RUBY
end
end

context 'and a set of groups is duplicated but `platforms` values are different' do
it 'does not register an offense' do
expect_no_offenses(<<-RUBY, 'Gemfile')
platforms :ruby do
group :default do
gem 'openssl'
end
end
platforms :jruby do
group :default do
gem 'jruby-openssl'
end
end
RUBY
end
end

context 'and a set of groups is duplicated and `platforms` values are the same' do
it 'registers an offense' do
expect_offense(<<-RUBY, 'Gemfile')
platforms :ruby do
group :default do
gem 'ruby-debug'
end
end
platforms :ruby do
group :default do
^^^^^^^^^^^^^^ Gem group `:default` already defined on line 2 of the Gemfile.
gem 'sqlite3'
end
end
RUBY
end
end

context 'and a set of groups is duplicated but `path` values are different' do
it 'does not register an offense' do
expect_no_offenses(<<-RUBY, 'Gemfile')
path 'components_admin' do
group :default do
gem 'admin_ui'
end
end
path 'components_public' do
group :default do
gem 'public_ui'
end
end
RUBY
end
end

context 'and a set of groups is duplicated and `path` values are the same' do
it 'registers an offense' do
expect_offense(<<-RUBY, 'Gemfile')
path 'components' do
group :default do
gem 'admin_ui'
end
end
path 'components' do
group :default do
^^^^^^^^^^^^^^ Gem group `:default` already defined on line 2 of the Gemfile.
gem 'public_ui'
end
end
RUBY
end
end

context 'when `source` URL argument is not given' do
it 'does not crash' do
expect_no_offenses(<<-RUBY, 'Gemfile')
source do
group :development do
gem 'rubocop'
end
end
RUBY
end
end
end
end

0 comments on commit 5da5286

Please sign in to comment.