From 7690074cab2ee8d952a907c7ea53de4773013809 Mon Sep 17 00:00:00 2001 From: Stuart Frost Date: Fri, 4 Aug 2023 21:48:35 +0100 Subject: [PATCH] Conditionally add !lib to default globs --- spec/ameba/config_spec.cr | 20 +++++++++++++++----- src/ameba/config.cr | 9 ++++++++- src/ameba/glob_utils.cr | 3 +-- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/spec/ameba/config_spec.cr b/spec/ameba/config_spec.cr index 1c1d10b2..844e88fd 100644 --- a/spec/ameba/config_spec.cr +++ b/spec/ameba/config_spec.cr @@ -9,10 +9,20 @@ module Ameba end describe ".new" do - it "loads default globs when config is empty" do - yml = YAML.parse "{}" - config = Config.new(yml) - config.globs.should eq Config::DEFAULT_GLOBS + 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 "{}" + config = Config.new(yml) + config.globs.should eq Config::DEFAULT_GLOBS + File.delete "lib/shard.cr" + end end it "initializes globs as string" do @@ -102,7 +112,7 @@ module Ameba config = Config.load config_sample it "holds source globs" do - config.globs.should eq Config::DEFAULT_GLOBS + config.globs.should eq ["**/*.cr"] end it "allows to set globs" do diff --git a/src/ameba/config.cr b/src/ameba/config.cr index 821fa065..7d4480c1 100644 --- a/src/ameba/config.cr +++ b/src/ameba/config.cr @@ -95,7 +95,7 @@ class Ameba::Config @rules = Rule.rules.map &.new(config).as(Rule::Base) @rule_groups = @rules.group_by &.group @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) self.formatter = formatter_name @@ -239,6 +239,13 @@ class Ameba::Config 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: module RuleConfig # Define rule properties diff --git a/src/ameba/glob_utils.cr b/src/ameba/glob_utils.cr index 5a0e5c72..df4e80bd 100644 --- a/src/ameba/glob_utils.cr +++ b/src/ameba/glob_utils.cr @@ -21,9 +21,8 @@ module Ameba # ``` def expand(globs) globs.flat_map do |glob| - raise ArgumentError.new("No files found matching #{glob}") if Dir[glob].empty? - glob += "/**/*.cr" if File.directory?(glob) + raise ArgumentError.new("No files found matching #{glob}") if Dir[glob].empty? Dir[glob] end.uniq! end