Excluded relative path path

This commit is contained in:
Vitalii Elenhaupt 2017-12-18 13:06:19 +02:00
parent 63eda4e820
commit eca0d28692
No known key found for this signature in database
GPG key ID: 7558EF3A4056C706
5 changed files with 65 additions and 3 deletions

View file

@ -28,5 +28,35 @@ module Ameba::Rule
NoProperties.new.enabled.should be_true
end
end
describe "#excluded?" do
it "returns false if a rule does no have a list of excluded source" do
DummyRule.new.excluded?(Source.new "", "source.cr").should_not be_true
end
it "returns false if source is not excluded from this rule" do
rule = DummyRule.new
rule.excluded = %w(some_source.cr)
rule.excluded?(Source.new "", "another_source.cr").should_not be_true
end
it "returns true if source is excluded from this rule" do
rule = DummyRule.new
rule.excluded = %w(source.cr)
rule.excluded?(Source.new "", "source.cr").should be_true
end
pending "returns true if source matches the wildcard" do
rule = DummyRule.new
rule.excluded = %w(**/*.cr)
rule.excluded?(Source.new "", "source.cr").should be_true
end
it "returns false if source does not match the wildcard" do
rule = DummyRule.new
rule.excluded = %w(*_spec.cr)
rule.excluded?(Source.new "", "source.cr").should be_false
end
end
end
end

View file

@ -23,5 +23,17 @@ module Ameba
error.message.should eq "Error!"
end
end
describe "#fullpath" do
it "returns a relative path of the source" do
s = Source.new "", "./source_spec.cr"
s.fullpath.should contain "source_spec.cr"
end
it "returns fullpath if path is blank" do
s = Source.new "", ""
s.fullpath.should_not be_nil
end
end
end
end

View file

@ -58,6 +58,21 @@ module Ameba::Rule
{{@type}}.class_name
end
# Checks whether the source is excluded from this rule.
# It searches for a path in `excluded` property which matches
# the one of the given source.
#
# ```
# my_rule.excluded?(source) # => true or false
# ```
#
def excluded?(source)
excluded.try &.any? do |path|
# TODO: file pattern match
source.path == path || source.fullpath == File.expand_path(path)
end
end
protected def self.class_name
name.gsub("Ameba::Rule::", "")
end

View file

@ -58,7 +58,7 @@ module Ameba
@formatter.source_started source
@rules.each do |rule|
next if rule.excluded.try &.any? &.== source.path
next if rule.excluded?(source)
rule.test(source)
end

View file

@ -12,7 +12,7 @@ module Ameba
message : String
# Path to the source file.
getter path : String?
getter path : String
# Crystal code (content of a source file).
getter code : String
@ -22,6 +22,7 @@ module Ameba
@lines : Array(String)?
@ast : Crystal::ASTNode?
@fullpath : String?
# Creates a new source by `code` and `path`.
#
@ -32,7 +33,7 @@ module Ameba
# Ameba::Source.new File.read(path), path
# ```
#
def initialize(@code : String, @path = nil)
def initialize(@code : String, @path = "")
end
# Add new error to the list of errors.
@ -99,5 +100,9 @@ module Ameba
def location(l, c)
Crystal::Location.new path, l, c
end
def fullpath
@fullpath ||= File.expand_path @path
end
end
end