Commit f57989c2 authored by Robert Speicher's avatar Robert Speicher

Add a spec for our custom GemFetcher cop

parent 75092d96
...@@ -31,8 +31,7 @@ AllCops: ...@@ -31,8 +31,7 @@ AllCops:
- 'lib/gitlab/seeder.rb' - 'lib/gitlab/seeder.rb'
- 'generator_templates/**/*' - 'generator_templates/**/*'
# Style #######################################################################
##################### Style ##################################
# Check indentation of private/protected visibility modifiers. # Check indentation of private/protected visibility modifiers.
Style/AccessModifierIndentation: Style/AccessModifierIndentation:
...@@ -471,7 +470,7 @@ Style/WhileUntilModifier: ...@@ -471,7 +470,7 @@ Style/WhileUntilModifier:
Style/WordArray: Style/WordArray:
Enabled: false Enabled: false
#################### Metrics ################################ # Metrics #####################################################################
# A calculated magnitude based on number of assignments, # A calculated magnitude based on number of assignments,
# branches, and conditions. # branches, and conditions.
...@@ -516,8 +515,7 @@ Metrics/PerceivedComplexity: ...@@ -516,8 +515,7 @@ Metrics/PerceivedComplexity:
Enabled: true Enabled: true
Max: 18 Max: 18
# Lint ########################################################################
#################### Lint ################################
# Checks for useless access modifiers. # Checks for useless access modifiers.
Lint/UselessAccessModifier: Lint/UselessAccessModifier:
...@@ -679,8 +677,7 @@ Lint/UselessSetterCall: ...@@ -679,8 +677,7 @@ Lint/UselessSetterCall:
Lint/Void: Lint/Void:
Enabled: true Enabled: true
# Performance #################################################################
##################### Performance ############################
# Use `casecmp` rather than `downcase ==`. # Use `casecmp` rather than `downcase ==`.
Performance/Casecmp: Performance/Casecmp:
...@@ -718,8 +715,7 @@ Performance/StringReplacement: ...@@ -718,8 +715,7 @@ Performance/StringReplacement:
Performance/TimesMap: Performance/TimesMap:
Enabled: true Enabled: true
# Rails #######################################################################
##################### Rails ##################################
# Enables Rails cops. # Enables Rails cops.
Rails: Rails:
...@@ -767,7 +763,7 @@ Rails/ReadWriteAttribute: ...@@ -767,7 +763,7 @@ Rails/ReadWriteAttribute:
Rails/ScopeArgs: Rails/ScopeArgs:
Enabled: true Enabled: true
##################### RSpec ################################## # RSpec #######################################################################
# Check that instances are not being stubbed globally. # Check that instances are not being stubbed globally.
RSpec/AnyInstance: RSpec/AnyInstance:
...@@ -828,3 +824,9 @@ RSpec/NotToNot: ...@@ -828,3 +824,9 @@ RSpec/NotToNot:
# Prefer using verifying doubles over normal doubles. # Prefer using verifying doubles over normal doubles.
RSpec/VerifiedDoubles: RSpec/VerifiedDoubles:
Enabled: false Enabled: false
# Custom ######################################################################
# Disallow the `git` and `github` arguments in the Gemfile.
GemFetcher:
Enabled: true
module RuboCop module RuboCop
module Cop module Cop
# Cop that checks for all gems specified in the Gemfile, and will # This cop prevents usage of the `git` and `github` arguments to `gem` in a
# alert if any gem is to be fetched not from the RubyGems index. # `Gemfile` in order to avoid additional points of failure beyond
# This enforcement is done so as to minimize external build # rubygems.org.
# dependencies and build times.
class GemFetcher < RuboCop::Cop::Cop class GemFetcher < RuboCop::Cop::Cop
MSG = 'Do not use gems from git repositories, only use gems from RubyGems.' MSG = 'Do not use gems from git repositories, only use gems from RubyGems.'
GIT_KEYS = [:git, :github] GIT_KEYS = [:git, :github]
def on_send(node) def on_send(node)
file_path = node.location.expression.source_buffer.name return unless gemfile?(node)
return unless file_path.end_with?("Gemfile")
func_name = node.children[1] func_name = node.children[1]
return unless func_name == :gem return unless func_name == :gem
...@@ -19,10 +17,21 @@ module RuboCop ...@@ -19,10 +17,21 @@ module RuboCop
node.children.last.each_node(:pair) do |pair| node.children.last.each_node(:pair) do |pair|
key_name = pair.children[0].children[0].to_sym key_name = pair.children[0].children[0].to_sym
if GIT_KEYS.include?(key_name) if GIT_KEYS.include?(key_name)
add_offense(node, :selector) add_offense(node, pair.source_range, MSG)
end end
end end
end end
private
def gemfile?(node)
node
.location
.expression
.source_buffer
.name
.end_with?("Gemfile")
end
end end
end end
end end
require_relative 'cop/migration/add_index' require_relative 'cop/gem_fetcher'
require_relative 'cop/migration/add_column' require_relative 'cop/migration/add_column'
require_relative 'cop/migration/add_column_with_default' require_relative 'cop/migration/add_column_with_default'
require_relative 'cop/gem_fetcher' require_relative 'cop/migration/add_index'
require 'spec_helper'
require 'rubocop'
require 'rubocop/rspec/support'
require_relative '../../../rubocop/cop/gem_fetcher'
describe RuboCop::Cop::GemFetcher do
include CopHelper
subject(:cop) { described_class.new }
context 'in Gemfile' do
before do
allow(cop).to receive(:gemfile?).and_return(true)
end
it 'registers an offense when a gem uses `git`' do
inspect_source(cop, 'gem "foo", git: "https://gitlab.com/foo/bar.git"')
aggregate_failures do
expect(cop.offenses.size).to eq(1)
expect(cop.offenses.map(&:line)).to eq([1])
expect(cop.highlights).to eq(['git: "https://gitlab.com/foo/bar.git"'])
end
end
it 'registers an offense when a gem uses `github`' do
inspect_source(cop, 'gem "foo", github: "foo/bar.git"')
aggregate_failures do
expect(cop.offenses.size).to eq(1)
expect(cop.offenses.map(&:line)).to eq([1])
expect(cop.highlights).to eq(['github: "foo/bar.git"'])
end
end
end
context 'outside of Gemfile' do
it 'registers no offense' do
inspect_source(cop, 'gem "foo", git: "https://gitlab.com/foo/bar.git"')
expect(cop.offenses.size).to eq(0)
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment