From e1b51f62a5934d09ea8d4e7d30e9210b72730ebe Mon Sep 17 00:00:00 2001 From: Vitalii Elenhaupt Date: Tue, 29 May 2018 13:19:00 +0300 Subject: [PATCH] Exclude file pattern match closes #61 --- spec/ameba/base_spec.cr | 4 ++-- spec/ameba/source_spec.cr | 12 ++++++++++++ src/ameba/rule/base.cr | 4 ++-- src/ameba/source.cr | 5 +++++ 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/spec/ameba/base_spec.cr b/spec/ameba/base_spec.cr index 03562837..c5427374 100644 --- a/spec/ameba/base_spec.cr +++ b/spec/ameba/base_spec.cr @@ -55,10 +55,10 @@ module Ameba::Rule rule.excluded?(Source.new "", "source.cr").should be_true end - pending "returns true if source matches the wildcard" do + it "returns true if source matches the wildcard" do rule = DummyRule.new rule.excluded = %w(**/*.cr) - rule.excluded?(Source.new "", "source.cr").should be_true + rule.excluded?(Source.new "", __FILE__).should be_true end it "returns false if source does not match the wildcard" do diff --git a/spec/ameba/source_spec.cr b/spec/ameba/source_spec.cr index d6f6c1bf..830c3f5f 100644 --- a/spec/ameba/source_spec.cr +++ b/spec/ameba/source_spec.cr @@ -35,5 +35,17 @@ module Ameba s.fullpath.should_not be_nil end end + + describe "#matches_path?" do + it "returns true if source's path is matched" do + s = Source.new "", "source.cr" + s.matches_path?("source.cr").should be_true + end + + it "returns false if source's path is not matched" do + s = Source.new "", "source.cr" + s.matches_path?("new_source.cr").should be_false + end + end end end diff --git a/src/ameba/rule/base.cr b/src/ameba/rule/base.cr index 1e2d9355..8a8d9404 100644 --- a/src/ameba/rule/base.cr +++ b/src/ameba/rule/base.cr @@ -75,8 +75,8 @@ module Ameba::Rule # def excluded?(source) excluded.try &.any? do |path| - # TODO: file pattern match - source.path == path || source.fullpath == File.expand_path(path) + source.matches_path?(path) || + Dir.glob(path).any? { |glob| source.matches_path? glob } end end diff --git a/src/ameba/source.cr b/src/ameba/source.cr index d706ccfb..3184b05b 100644 --- a/src/ameba/source.cr +++ b/src/ameba/source.cr @@ -111,5 +111,10 @@ module Ameba def fullpath @fullpath ||= File.expand_path @path end + + # Returns true if *filepath* matches the source's path, false if it does not. + def matches_path?(filepath) + path == filepath || path == File.expand_path(filepath) + end end end