Ability to excluded globally

This commit is contained in:
Vitalii Elenhaupt 2020-03-22 14:18:07 +02:00
parent bffc46c545
commit 2303bd82ae
No known key found for this signature in database
GPG key ID: CD0BF17825928BC0
2 changed files with 72 additions and 9 deletions

View file

@ -42,6 +42,34 @@ module Ameba
CONFIG
expect_raises(Exception, "incorrect 'Globs' section in a config file") { Config.new(yml) }
end
it "initializes excluded as string" do
yml = YAML.parse <<-CONFIG
---
Excluded: spec
CONFIG
config = Config.new(yml)
config.excluded.should eq %w(spec)
end
it "initializes excluded as array" do
yml = YAML.parse <<-CONFIG
---
Excluded:
- spec
- lib/*.cr
CONFIG
config = Config.new(yml)
config.excluded.should eq %w(spec lib/*.cr)
end
it "raises if Excluded has a wrong type" do
yml = YAML.parse <<-CONFIG
---
Excluded: true
CONFIG
expect_raises(Exception, "incorrect 'Excluded' section in a config file") { Config.new(yml) }
end
end
describe ".load" do
@ -73,12 +101,36 @@ module Ameba
end
end
describe "#excluded, #excluded=" do
config = Config.load config_sample
it "defaults to empty array" do
config.excluded.should be_empty
end
it "allows to set excluded" do
config.excluded = ["spec"]
config.excluded.should eq ["spec"]
end
end
describe "#sources" do
config = Config.load config_sample
it "returns list of sources" do
config.sources.size.should be > 0
config.sources.first.should be_a Source
config.sources.any? { |s| s.fullpath == __FILE__ }.should be_true
end
it "returns a list of sources mathing globs" do
config.globs = %w(**/config_spec.cr)
config.sources.size.should eq(1)
end
it "returns a lisf of sources excluding 'Excluded'" do
config.excluded = %w(**/config_spec.cr)
config.sources.any? { |s| s.fullpath == __FILE__ }.should be_false
end
end

View file

@ -46,6 +46,15 @@ class Ameba::Config
# ```
property globs : Array(String)
# Represents a list of paths to exclude from globs.
# Can have wildcards.
#
# ```
# config = Ameba::Config.load
# config.excluded = ["spec", "src/server/*.cr"]
# ```
property excluded : Array(String)
@rule_groups : Hash(String, Array(Rule::Base))
# Creates a new instance of `Ameba::Config` based on YAML parameters.
@ -54,7 +63,8 @@ class Ameba::Config
protected def initialize(config : YAML::Any)
@rules = Rule.rules.map &.new(config).as(Rule::Base)
@rule_groups = @rules.group_by &.group
@globs = load_globs(config)
@excluded = load_array_section(config, "Excluded")
@globs = load_array_section(config, "Globs", DEFAULT_GLOBS)
self.formatter = load_formatter_name(config)
end
@ -77,17 +87,18 @@ class Ameba::Config
AVAILABLE_FORMATTERS.keys.join("|")
end
# Returns a list of sources.
# Returns a list of sources matching globs and excluded sections.
#
# ```
# config = Ameba::Config.load
# config.sources # => list of default sources
# config.globs = ["**/*.cr"]
# config.excluded = ["spec"]
# config.sources # => list of sources pointing to files found by the wildcards
# ```
#
def sources
find_files_by_globs(globs)
(find_files_by_globs(globs) - find_files_by_globs(excluded))
.map { |path| Source.new File.read(path), path }
end
@ -164,13 +175,13 @@ class Ameba::Config
name ? name.to_s : nil
end
private def load_globs(config)
case globs = config["Globs"]?
when .nil? then DEFAULT_GLOBS
when .as_s? then [globs.as_s]
when .as_a? then globs.as_a.map(&.as_s)
private def load_array_section(config, section_name, default = [] of String)
case value = config[section_name]?
when .nil? then default
when .as_s? then [value.to_s]
when .as_a? then value.as_a.map(&.as_s)
else
raise "incorrect 'Globs' section in a config file"
raise "incorrect '#{section_name}' section in a config files"
end
end