diff --git a/spec/ameba/config_spec.cr b/spec/ameba/config_spec.cr index 367a7691..ba8746c4 100644 --- a/spec/ameba/config_spec.cr +++ b/spec/ameba/config_spec.cr @@ -8,6 +8,42 @@ module Ameba Config::AVAILABLE_FORMATTERS.should_not be_nil 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 + end + + it "initializes globs as string" do + yml = YAML.parse <<-CONFIG + --- + Globs: src/*.cr + CONFIG + config = Config.new(yml) + config.globs.should eq %w(src/*.cr) + end + + it "initializes globs as array" do + yml = YAML.parse <<-CONFIG + --- + Globs: + - "src/*.cr" + - "!spec" + CONFIG + config = Config.new(yml) + config.globs.should eq %w(src/*.cr !spec) + end + + it "raises if Globs has a wrong type" do + yml = YAML.parse <<-CONFIG + --- + Globs: 100 + CONFIG + expect_raises(Exception, "incorrect 'Globs' section in a config file") { Config.new(yml) } + end + end + describe ".load" do it "loads custom config" do config = Config.load config_sample @@ -28,7 +64,7 @@ module Ameba config = Config.load config_sample it "holds source globs" do - config.globs.should contain "spec/ameba/config_spec.cr" + config.globs.should eq Config::DEFAULT_GLOBS end it "allows to set globs" do diff --git a/src/ameba/cli/cmd.cr b/src/ameba/cli/cmd.cr index 0344c008..c27bb8dc 100644 --- a/src/ameba/cli/cmd.cr +++ b/src/ameba/cli/cmd.cr @@ -8,7 +8,7 @@ module Ameba::Cli def run(args = ARGV) opts = parse_args args config = Config.load opts.config, opts.colors? - config.globs = opts.globs + config.globs = opts.globs.not_nil! if opts.globs config.severity = opts.fail_level.not_nil! if opts.fail_level configure_formatter(config, opts) diff --git a/src/ameba/config.cr b/src/ameba/config.cr index d82c26af..c231ab32 100644 --- a/src/ameba/config.cr +++ b/src/ameba/config.cr @@ -25,43 +25,16 @@ class Ameba::Config } PATH = ".ameba.yml" + + DEFAULT_GLOBS = %w( + **/*.cr + !lib + ) + setter formatter : Formatter::BaseFormatter? - setter globs : Array(String)? getter rules : Array(Rule::Base) property severity = Severity::Convention - @rule_groups : Hash(String, Array(Rule::Base)) - - # Creates a new instance of `Ameba::Config` based on YAML parameters. - # - # `Config.load` uses this constructor to instantiate new config by YAML file. - protected def initialize(@config : YAML::Any) - @rules = Rule.rules.map &.new(config).as(Rule::Base) - @rule_groups = @rules.group_by &.group - - if @config.as_h? && (name = @config["Formatter"]?.try &.["Name"]?) - self.formatter = name.to_s - end - end - - # Loads YAML configuration file by `path`. - # - # ``` - # config = Ameba::Config.load - # ``` - # - def self.load(path = PATH, colors = true) - Colorize.enabled = colors - content = File.exists?(path) ? File.read path : "" - Config.new YAML.parse(content) - rescue e - raise "Config file is invalid: #{e.message}" - end - - def self.formatter_names - AVAILABLE_FORMATTERS.keys.join("|") - end - # Returns a list of paths (with wildcards) to files. # Represents a list of sources to be inspected. # If globs are not set, it will return default list of files. @@ -71,9 +44,37 @@ class Ameba::Config # config.globs = ["**/*.cr"] # config.globs # ``` + property globs : Array(String) + + @rule_groups : Hash(String, Array(Rule::Base)) + + # Creates a new instance of `Ameba::Config` based on YAML parameters. # - def globs - @globs ||= default_files + # `Config.load` uses this constructor to instantiate new config by YAML file. + 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) + + self.formatter = load_formatter_name(config) + end + + # Loads YAML configuration file by `path`. + # + # ``` + # config = Ameba::Config.load + # ``` + # + def self.load(path = PATH, colors = true) + Colorize.enabled = colors + content = File.exists?(path) ? File.read path : "{}" + Config.new YAML.parse(content) + rescue e + raise "Config file is invalid: #{e.message}" + end + + def self.formatter_names + AVAILABLE_FORMATTERS.keys.join("|") end # Returns a list of sources. @@ -100,7 +101,7 @@ class Ameba::Config # ``` # def formatter - @formatter ||= default_formatter + @formatter ||= Formatter::DotFormatter.new end # Sets formatter by name. @@ -158,12 +159,19 @@ class Ameba::Config end end - private def default_files - Dir["**/*.cr"].reject(&.starts_with? "lib/") + private def load_formatter_name(config) + name = config["Formatter"]?.try &.["Name"]? + name ? name.to_s : nil end - private def default_formatter - Formatter::DotFormatter.new + 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) + else + raise "incorrect 'Globs' section in a config file" + end end # :nodoc: