diff --git a/lib/rubocop/cop/cop.rb b/lib/rubocop/cop/cop.rb index a8d4757aad0b..31bc3be65479 100644 --- a/lib/rubocop/cop/cop.rb +++ b/lib/rubocop/cop/cop.rb @@ -211,8 +211,9 @@ def cop_name alias name cop_name def relevant_file?(file) - file_name_matches_any?(file, 'Include', true) && - !file_name_matches_any?(file, 'Exclude', false) + file == RuboCop::AST::ProcessedSource::STRING_SOURCE_NAME || + file_name_matches_any?(file, 'Include', true) && + !file_name_matches_any?(file, 'Exclude', false) end def excluded_file?(file) diff --git a/spec/rubocop/cop/bundler/duplicated_gem_spec.rb b/spec/rubocop/cop/bundler/duplicated_gem_spec.rb index b3e65e9ef712..a8f5785b449a 100644 --- a/spec/rubocop/cop/bundler/duplicated_gem_spec.rb +++ b/spec/rubocop/cop/bundler/duplicated_gem_spec.rb @@ -5,7 +5,7 @@ context 'when investigating Ruby files' do it 'does not register any offenses' do - expect_no_offenses(<<~RUBY) + expect_no_offenses(<<~RUBY, 'foo.rb') # cop will not read these contents gem('rubocop') gem('rubocop') diff --git a/spec/rubocop/cop/bundler/gem_comment_spec.rb b/spec/rubocop/cop/bundler/gem_comment_spec.rb index 9d964d39006a..e195eb87f5d1 100644 --- a/spec/rubocop/cop/bundler/gem_comment_spec.rb +++ b/spec/rubocop/cop/bundler/gem_comment_spec.rb @@ -10,7 +10,7 @@ context 'when investigating Ruby files' do it 'does not register any offenses' do - expect_no_offenses(<<~RUBY) + expect_no_offenses(<<~RUBY, 'foo.rb') gem('rubocop') RUBY end diff --git a/spec/rubocop/cop/bundler/insecure_protocol_source_spec.rb b/spec/rubocop/cop/bundler/insecure_protocol_source_spec.rb index 2433a5cfdced..07b26672ef68 100644 --- a/spec/rubocop/cop/bundler/insecure_protocol_source_spec.rb +++ b/spec/rubocop/cop/bundler/insecure_protocol_source_spec.rb @@ -1,10 +1,6 @@ # frozen_string_literal: true -RSpec.describe RuboCop::Cop::Bundler::InsecureProtocolSource do - subject(:cop) { described_class.new(config) } - - let(:config) { RuboCop::Config.new } - +RSpec.describe RuboCop::Cop::Bundler::InsecureProtocolSource, :config do it 'registers an offense when using `source :gemcutter`' do expect_offense(<<~RUBY) source :gemcutter diff --git a/spec/rubocop/cop/bundler/ordered_gems_spec.rb b/spec/rubocop/cop/bundler/ordered_gems_spec.rb index 7d800adf7458..3d907036b8c8 100644 --- a/spec/rubocop/cop/bundler/ordered_gems_spec.rb +++ b/spec/rubocop/cop/bundler/ordered_gems_spec.rb @@ -3,8 +3,7 @@ RSpec.describe RuboCop::Cop::Bundler::OrderedGems, :config do let(:cop_config) do { - 'TreatCommentsAsGroupSeparators' => treat_comments_as_group_separators, - 'Include' => nil + 'TreatCommentsAsGroupSeparators' => treat_comments_as_group_separators } end let(:treat_comments_as_group_separators) { false } diff --git a/spec/rubocop/cop/cop_spec.rb b/spec/rubocop/cop/cop_spec.rb index 4c23369ef09a..eae00b5965ea 100644 --- a/spec/rubocop/cop/cop_spec.rb +++ b/spec/rubocop/cop/cop_spec.rb @@ -304,6 +304,30 @@ end end + describe '#relevant_file?' do + subject { cop.relevant_file?(file) } + + let(:cop_config) { { 'Include' => ['foo.rb'] } } + + context 'when the file matches the Include configuration' do + let(:file) { 'foo.rb' } + + it { is_expected.to be(true) } + end + + context 'when the file doesn\'t match the Include configuration' do + let(:file) { 'bar.rb' } + + it { is_expected.to be(false) } + end + + context 'when the file is an anonymous source' do + let(:file) { '(string)' } + + it { is_expected.to be(true) } + end + end + describe '#safe_autocorrect?' do subject { cop.safe_autocorrect? } diff --git a/spec/rubocop/cop/gemspec/duplicated_assignment_spec.rb b/spec/rubocop/cop/gemspec/duplicated_assignment_spec.rb index 2ec9e2ee6790..7cfc938af484 100644 --- a/spec/rubocop/cop/gemspec/duplicated_assignment_spec.rb +++ b/spec/rubocop/cop/gemspec/duplicated_assignment_spec.rb @@ -1,10 +1,6 @@ # frozen_string_literal: true -RSpec.describe RuboCop::Cop::Gemspec::DuplicatedAssignment do - subject(:cop) { described_class.new(config) } - - let(:config) { RuboCop::Config.new } - +RSpec.describe RuboCop::Cop::Gemspec::DuplicatedAssignment, :config do it 'registers an offense when using `name=` twice' do expect_offense(<<~RUBY) Gem::Specification.new do |spec| diff --git a/spec/rubocop/cop/gemspec/ordered_dependencies_spec.rb b/spec/rubocop/cop/gemspec/ordered_dependencies_spec.rb index a43490b48bf5..c306da17bc51 100644 --- a/spec/rubocop/cop/gemspec/ordered_dependencies_spec.rb +++ b/spec/rubocop/cop/gemspec/ordered_dependencies_spec.rb @@ -3,8 +3,7 @@ RSpec.describe RuboCop::Cop::Gemspec::OrderedDependencies, :config do let(:cop_config) do { - 'TreatCommentsAsGroupSeparators' => treat_comments_as_group_separators, - 'Include' => nil + 'TreatCommentsAsGroupSeparators' => treat_comments_as_group_separators } end let(:treat_comments_as_group_separators) { false } diff --git a/spec/rubocop/cop/gemspec/ruby_version_globals_usage_spec.rb b/spec/rubocop/cop/gemspec/ruby_version_globals_usage_spec.rb index 8f6f0e671b82..9107bf5dfaba 100644 --- a/spec/rubocop/cop/gemspec/ruby_version_globals_usage_spec.rb +++ b/spec/rubocop/cop/gemspec/ruby_version_globals_usage_spec.rb @@ -2,7 +2,7 @@ RSpec.describe RuboCop::Cop::Gemspec::RubyVersionGlobalsUsage, :config do it 'registers an offense when using `RUBY_VERSION`' do - expect_offense(<<~RUBY, '/path/to/foo.gemspec') + expect_offense(<<~RUBY) Gem::Specification.new do |spec| RUBY_VERSION ^^^^^^^^^^^^ Do not use `RUBY_VERSION` in gemspec file. diff --git a/spec/rubocop/cop/layout/rescue_ensure_alignment_spec.rb b/spec/rubocop/cop/layout/rescue_ensure_alignment_spec.rb index 2b4afb4f3d13..0c2c7961175b 100644 --- a/spec/rubocop/cop/layout/rescue_ensure_alignment_spec.rb +++ b/spec/rubocop/cop/layout/rescue_ensure_alignment_spec.rb @@ -616,7 +616,7 @@ def foo end it 'processes excluded files with issue' do - expect_no_offenses(<<~RUBY) + expect_no_offenses(<<~RUBY, 'foo.rb') begin foo rescue diff --git a/spec/rubocop/cop/style/rescue_modifier_spec.rb b/spec/rubocop/cop/style/rescue_modifier_spec.rb index 38e9d51b5a52..faec91e19996 100644 --- a/spec/rubocop/cop/style/rescue_modifier_spec.rb +++ b/spec/rubocop/cop/style/rescue_modifier_spec.rb @@ -232,7 +232,7 @@ def foo end it 'processes excluded files with issue' do - expect_no_offenses('foo rescue bar') + expect_no_offenses('foo rescue bar', 'foo.rb') end end end