Conditionally add !lib to default globs

This commit is contained in:
Stuart Frost 2023-08-04 21:48:35 +01:00
parent b2069ea4ff
commit 7690074cab
3 changed files with 24 additions and 8 deletions

View file

@ -9,10 +9,20 @@ module Ameba
end end
describe ".new" do describe ".new" do
it "loads default globs when config is empty" do context "when config is empty" do
it "loads default globs" do
yml = YAML.parse "{}"
config = Config.new(yml)
config.globs.should eq ["**/*.cr"]
end
it "only sets !lib as a default glob when there are .cr files in lib" do
File.touch "lib/shard.cr"
yml = YAML.parse "{}" yml = YAML.parse "{}"
config = Config.new(yml) config = Config.new(yml)
config.globs.should eq Config::DEFAULT_GLOBS config.globs.should eq Config::DEFAULT_GLOBS
File.delete "lib/shard.cr"
end
end end
it "initializes globs as string" do it "initializes globs as string" do
@ -102,7 +112,7 @@ module Ameba
config = Config.load config_sample config = Config.load config_sample
it "holds source globs" do it "holds source globs" do
config.globs.should eq Config::DEFAULT_GLOBS config.globs.should eq ["**/*.cr"]
end end
it "allows to set globs" do it "allows to set globs" do

View file

@ -95,7 +95,7 @@ class Ameba::Config
@rules = Rule.rules.map &.new(config).as(Rule::Base) @rules = Rule.rules.map &.new(config).as(Rule::Base)
@rule_groups = @rules.group_by &.group @rule_groups = @rules.group_by &.group
@excluded = load_array_section(config, "Excluded") @excluded = load_array_section(config, "Excluded")
@globs = load_array_section(config, "Globs", DEFAULT_GLOBS) @globs = load_array_section(config, "Globs", default_globs)
return unless formatter_name = load_formatter_name(config) return unless formatter_name = load_formatter_name(config)
self.formatter = formatter_name self.formatter = formatter_name
@ -239,6 +239,13 @@ class Ameba::Config
end end
end end
private def default_globs
DEFAULT_GLOBS.select do |glob|
!Dir[glob].empty? ||
(glob.starts_with?("!") && !Dir["#{glob[1..-1]}/**/*.cr"].empty?)
end
end
# :nodoc: # :nodoc:
module RuleConfig module RuleConfig
# Define rule properties # Define rule properties

View file

@ -21,9 +21,8 @@ module Ameba
# ``` # ```
def expand(globs) def expand(globs)
globs.flat_map do |glob| globs.flat_map do |glob|
raise ArgumentError.new("No files found matching #{glob}") if Dir[glob].empty?
glob += "/**/*.cr" if File.directory?(glob) glob += "/**/*.cr" if File.directory?(glob)
raise ArgumentError.new("No files found matching #{glob}") if Dir[glob].empty?
Dir[glob] Dir[glob]
end.uniq! end.uniq!
end end