Skip to content

Commit

Permalink
Merge pull request #257 from Shopify/hm-fix-example-globs
Browse files Browse the repository at this point in the history
Fix '**/vendor/**/*' glob broken in dc7cc25
  • Loading branch information
rafaelfranca authored Jun 16, 2022
2 parents 530ed7d + 1241fbc commit d6e25bc
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/erb_lint/linter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def enabled?
end

def excludes_file?(filename)
@config.excludes_file?(filename)
@config.excludes_file?(filename, @file_loader.base_path)
end

def run(_processed_source)
Expand Down
7 changes: 5 additions & 2 deletions lib/erb_lint/linter_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,12 @@ def to_hash
end
end

def excludes_file?(filename)
def excludes_file?(absolute_filename, base_path)
exclude.any? do |path|
File.fnmatch?(path, filename)
return true if File.fnmatch?(path, absolute_filename)

relative_path = absolute_filename.delete_prefix("#{base_path}/")
File.fnmatch?(path, relative_path)
end
end
end
Expand Down
3 changes: 1 addition & 2 deletions lib/erb_lint/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ def initialize(file_loader, config)
end

def run(processed_source)
relative_filename = processed_source.filename.delete_prefix("#{@file_loader.base_path}/")
@linters
.reject { |linter| linter.excludes_file?(relative_filename) }
.reject { |linter| linter.excludes_file?(processed_source.filename) }
.each do |linter|
linter.run(processed_source)
@offenses.concat(linter.offenses)
Expand Down
16 changes: 14 additions & 2 deletions spec/erb_lint/linter_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,25 @@ class CustomConfig < described_class
describe "#excludes_file?" do
context "when glob matches" do
let(:config_hash) { { exclude: ["vendor/**/*"] } }
subject { linter_config.excludes_file?("vendor/gem/foo.rb") }
subject { linter_config.excludes_file?("/src/vendor/gem/foo.rb", "/src") }
it { expect(subject).to(eq(true)) }
end

context "when glob does not match" do
let(:config_hash) { { exclude: ["vendor/**/*"] } }
subject { linter_config.excludes_file?("app/foo.rb") }
subject { linter_config.excludes_file?("/src/app/foo.rb", "/src") }
it { expect(subject).to(eq(false)) }
end

context "when absolute glob matches" do
let(:config_hash) { { exclude: ["**/vendor/**/*"] } }
subject { linter_config.excludes_file?("/src/vendor/gem/foo.rb", "/src") }
it { expect(subject).to(eq(true)) }
end

context "when absolute glob does not match" do
let(:config_hash) { { exclude: ["**/vendor/**/*"] } }
subject { linter_config.excludes_file?("/src/app/foo.rb", "/src") }
it { expect(subject).to(eq(false)) }
end
end
Expand Down

0 comments on commit d6e25bc

Please sign in to comment.