Utilize Rubocop's Include for BeSuccessMatcher

Use Rubocop's Include
instead of manually checking the matcher in controllers specs.
This commit is contained in:
Vitali Tatarintev 2019-08-26 10:04:54 +02:00
parent b61d26f496
commit 99b27e6951
4 changed files with 16 additions and 43 deletions

View File

@ -265,3 +265,11 @@ RSpec/EnvAssignment:
- 'ee/spec/**/rails_helper.rb'
- 'spec/**/spec_helper.rb'
- 'ee/spec/**/spec_helper.rb'
RSpec/BeSuccessMatcher:
Enabled: true
Include:
- 'spec/controllers/**/*'
- 'ee/spec/controllers/**/*'
- 'spec/support/shared_examples/controllers/**/*'
- 'ee/spec/support/shared_examples/controllers/**/*'
- 'spec/support/controllers/**/*'

View File

@ -41,7 +41,6 @@ module RuboCop
end
def on_send(node)
return unless in_controller_spec?(node)
return unless be_success_usage?(node)
add_offense(node, location: :expression, message: MESSAGE)

View File

@ -2,7 +2,6 @@ module RuboCop
module SpecHelpers
SPEC_HELPERS = %w[fast_spec_helper.rb rails_helper.rb spec_helper.rb].freeze
MIGRATION_SPEC_DIRECTORIES = ['spec/migrations', 'spec/lib/gitlab/background_migration'].freeze
CONTROLLER_SPEC_DIRECTORIES = ['spec/controllers', 'spec/support/shared_examples/controllers'].freeze
# Returns true if the given node originated from the spec directory.
def in_spec?(node)
@ -27,20 +26,5 @@ module RuboCop
in_spec?(node) &&
path.start_with?(*migration_directories)
end
def controller_directories
@controller_directories ||= CONTROLLER_SPEC_DIRECTORIES.map do |dir|
pwd = RuboCop::PathUtil.pwd
[File.join(pwd, dir), File.join(pwd, 'ee', dir)]
end.flatten
end
# Returns true if the given node originated from a controller spec.
def in_controller_spec?(node)
path = node.location.expression.source_buffer.name
in_spec?(node) &&
path.start_with?(*controller_directories)
end
end
end

View File

@ -59,11 +59,6 @@ describe RuboCop::Cop::RSpec::BeSuccessMatcher do
end
end
context 'in a controller spec file' do
before do
allow(cop).to receive(:in_controller_spec?).and_return(true)
end
CODE_EXAMPLES.each do |code_example|
context "using #{code_example[:bad]} call" do
it_behaves_like 'an offensive be_success call', code_example[:bad]
@ -79,16 +74,3 @@ describe RuboCop::Cop::RSpec::BeSuccessMatcher do
end
end
end
context 'outside of a controller spec file' do
CODE_EXAMPLES.each do |code_example|
context "using #{code_example[:bad]} call" do
it 'does not register an offense' do
inspect_source(code_example[:bad])
expect(cop.offenses.size).to eq(0)
end
end
end
end
end