Skip to content

Commit

Permalink
Merge pull request #8028 from eugeneius/always_inspect_anonymous_sources
Browse files Browse the repository at this point in the history
[Fix #8005] Always inspect anonymous sources
  • Loading branch information
koic committed May 25, 2020
2 parents 34763d5 + 6a229ad commit 42ea792
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 21 deletions.
5 changes: 3 additions & 2 deletions lib/rubocop/cop/cop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion spec/rubocop/cop/bundler/duplicated_gem_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion spec/rubocop/cop/bundler/gem_comment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions spec/rubocop/cop/bundler/insecure_protocol_source_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 1 addition & 2 deletions spec/rubocop/cop/bundler/ordered_gems_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
24 changes: 24 additions & 0 deletions spec/rubocop/cop/cop_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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? }

Expand Down
6 changes: 1 addition & 5 deletions spec/rubocop/cop/gemspec/duplicated_assignment_spec.rb
Original file line number Diff line number Diff line change
@@ -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|
Expand Down
3 changes: 1 addition & 2 deletions spec/rubocop/cop/gemspec/ordered_dependencies_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion spec/rubocop/cop/layout/rescue_ensure_alignment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/rubocop/cop/style/rescue_modifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 42ea792

Please sign in to comment.