mirror of
				https://gitea.invidious.io/iv-org/shard-ameba.git
				synced 2024-08-15 00:53:29 +00:00 
			
		
		
		
	Excluded relative path path
This commit is contained in:
		
							parent
							
								
									63eda4e820
								
							
						
					
					
						commit
						eca0d28692
					
				
					 5 changed files with 65 additions and 3 deletions
				
			
		| 
						 | 
					@ -28,5 +28,35 @@ module Ameba::Rule
 | 
				
			||||||
        NoProperties.new.enabled.should be_true
 | 
					        NoProperties.new.enabled.should be_true
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
    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
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -23,5 +23,17 @@ module Ameba
 | 
				
			||||||
        error.message.should eq "Error!"
 | 
					        error.message.should eq "Error!"
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
    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
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -58,6 +58,21 @@ module Ameba::Rule
 | 
				
			||||||
      {{@type}}.class_name
 | 
					      {{@type}}.class_name
 | 
				
			||||||
    end
 | 
					    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
 | 
					    protected def self.class_name
 | 
				
			||||||
      name.gsub("Ameba::Rule::", "")
 | 
					      name.gsub("Ameba::Rule::", "")
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -58,7 +58,7 @@ module Ameba
 | 
				
			||||||
        @formatter.source_started source
 | 
					        @formatter.source_started source
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        @rules.each do |rule|
 | 
					        @rules.each do |rule|
 | 
				
			||||||
          next if rule.excluded.try &.any? &.== source.path
 | 
					          next if rule.excluded?(source)
 | 
				
			||||||
          rule.test(source)
 | 
					          rule.test(source)
 | 
				
			||||||
        end
 | 
					        end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -12,7 +12,7 @@ module Ameba
 | 
				
			||||||
      message : String
 | 
					      message : String
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Path to the source file.
 | 
					    # Path to the source file.
 | 
				
			||||||
    getter path : String?
 | 
					    getter path : String
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Crystal code (content of a source file).
 | 
					    # Crystal code (content of a source file).
 | 
				
			||||||
    getter code : String
 | 
					    getter code : String
 | 
				
			||||||
| 
						 | 
					@ -22,6 +22,7 @@ module Ameba
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @lines : Array(String)?
 | 
					    @lines : Array(String)?
 | 
				
			||||||
    @ast : Crystal::ASTNode?
 | 
					    @ast : Crystal::ASTNode?
 | 
				
			||||||
 | 
					    @fullpath : String?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Creates a new source by `code` and `path`.
 | 
					    # Creates a new source by `code` and `path`.
 | 
				
			||||||
    #
 | 
					    #
 | 
				
			||||||
| 
						 | 
					@ -32,7 +33,7 @@ module Ameba
 | 
				
			||||||
    # Ameba::Source.new File.read(path), path
 | 
					    # Ameba::Source.new File.read(path), path
 | 
				
			||||||
    # ```
 | 
					    # ```
 | 
				
			||||||
    #
 | 
					    #
 | 
				
			||||||
    def initialize(@code : String, @path = nil)
 | 
					    def initialize(@code : String, @path = "")
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Add new error to the list of errors.
 | 
					    # Add new error to the list of errors.
 | 
				
			||||||
| 
						 | 
					@ -99,5 +100,9 @@ module Ameba
 | 
				
			||||||
    def location(l, c)
 | 
					    def location(l, c)
 | 
				
			||||||
      Crystal::Location.new path, l, c
 | 
					      Crystal::Location.new path, l, c
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def fullpath
 | 
				
			||||||
 | 
					      @fullpath ||= File.expand_path @path
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue