2017-11-14 07:24:36 +00:00
|
|
|
require "../spec_helper"
|
|
|
|
|
|
|
|
module Ameba
|
|
|
|
describe Config do
|
2023-07-25 09:00:16 +00:00
|
|
|
config_sample = "spec/fixtures/config.yml"
|
2017-11-14 07:24:36 +00:00
|
|
|
|
2018-01-25 14:40:32 +00:00
|
|
|
it "should have a list of available formatters" do
|
|
|
|
Config::AVAILABLE_FORMATTERS.should_not be_nil
|
|
|
|
end
|
|
|
|
|
2019-12-28 18:59:41 +00:00
|
|
|
describe ".new" do
|
2023-08-04 20:48:35 +00:00
|
|
|
context "when config is empty" do
|
|
|
|
it "loads default globs" do
|
|
|
|
yml = YAML.parse "{}"
|
|
|
|
config = Config.new(yml)
|
|
|
|
config.globs.should eq ["**/*.cr"]
|
|
|
|
end
|
|
|
|
|
2023-08-05 18:28:58 +00:00
|
|
|
it "sets !lib as a default glob when there are .cr files in lib" do
|
2023-08-04 20:48:35 +00:00
|
|
|
File.touch "lib/shard.cr"
|
|
|
|
yml = YAML.parse "{}"
|
|
|
|
config = Config.new(yml)
|
2023-08-05 15:15:50 +00:00
|
|
|
config.globs.should eq ["**/*.cr", "!lib"]
|
2023-08-05 18:28:58 +00:00
|
|
|
ensure
|
2023-08-04 20:48:35 +00:00
|
|
|
File.delete "lib/shard.cr"
|
|
|
|
end
|
2019-12-28 18:59:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "initializes globs as string" do
|
|
|
|
yml = YAML.parse <<-CONFIG
|
|
|
|
---
|
|
|
|
Globs: src/*.cr
|
|
|
|
CONFIG
|
|
|
|
config = Config.new(yml)
|
2023-11-17 15:37:27 +00:00
|
|
|
config.globs.should eq %w[src/*.cr]
|
2019-12-28 18:59:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "initializes globs as array" do
|
|
|
|
yml = YAML.parse <<-CONFIG
|
|
|
|
---
|
|
|
|
Globs:
|
|
|
|
- "src/*.cr"
|
|
|
|
- "!spec"
|
|
|
|
CONFIG
|
|
|
|
config = Config.new(yml)
|
2023-11-17 15:37:27 +00:00
|
|
|
config.globs.should eq %w[src/*.cr !spec]
|
2019-12-28 18:59:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "raises if Globs has a wrong type" do
|
|
|
|
yml = YAML.parse <<-CONFIG
|
|
|
|
---
|
|
|
|
Globs: 100
|
|
|
|
CONFIG
|
2022-11-14 00:24:29 +00:00
|
|
|
expect_raises(Exception, "Incorrect 'Globs' section in a config file") do
|
|
|
|
Config.new(yml)
|
|
|
|
end
|
2019-12-28 18:59:41 +00:00
|
|
|
end
|
2020-03-22 12:18:07 +00:00
|
|
|
|
|
|
|
it "initializes excluded as string" do
|
|
|
|
yml = YAML.parse <<-CONFIG
|
|
|
|
---
|
|
|
|
Excluded: spec
|
|
|
|
CONFIG
|
|
|
|
config = Config.new(yml)
|
2023-11-17 15:37:27 +00:00
|
|
|
config.excluded.should eq %w[spec]
|
2020-03-22 12:18:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "initializes excluded as array" do
|
|
|
|
yml = YAML.parse <<-CONFIG
|
|
|
|
---
|
|
|
|
Excluded:
|
|
|
|
- spec
|
|
|
|
- lib/*.cr
|
|
|
|
CONFIG
|
|
|
|
config = Config.new(yml)
|
2023-11-17 15:37:27 +00:00
|
|
|
config.excluded.should eq %w[spec lib/*.cr]
|
2020-03-22 12:18:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "raises if Excluded has a wrong type" do
|
|
|
|
yml = YAML.parse <<-CONFIG
|
|
|
|
---
|
|
|
|
Excluded: true
|
|
|
|
CONFIG
|
2022-11-14 00:24:29 +00:00
|
|
|
expect_raises(Exception, "Incorrect 'Excluded' section in a config file") do
|
|
|
|
Config.new(yml)
|
|
|
|
end
|
2020-03-22 12:18:07 +00:00
|
|
|
end
|
2019-12-28 18:59:41 +00:00
|
|
|
end
|
|
|
|
|
2017-11-14 07:24:36 +00:00
|
|
|
describe ".load" do
|
|
|
|
it "loads custom config" do
|
|
|
|
config = Config.load config_sample
|
|
|
|
config.should_not be_nil
|
2019-01-12 21:19:00 +00:00
|
|
|
config.globs.should_not be_nil
|
2017-11-14 07:24:36 +00:00
|
|
|
config.formatter.should_not be_nil
|
|
|
|
end
|
|
|
|
|
2023-07-24 18:06:33 +00:00
|
|
|
it "raises when custom config file doesn't exist" do
|
2023-07-25 07:46:13 +00:00
|
|
|
expect_raises(Exception, "Unable to load config file: Config file does not exist foo.yml") do
|
2023-07-24 18:06:33 +00:00
|
|
|
Config.load "foo.yml"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-14 07:24:36 +00:00
|
|
|
it "loads default config" do
|
|
|
|
config = Config.load
|
|
|
|
config.should_not be_nil
|
2019-01-12 21:19:00 +00:00
|
|
|
config.globs.should_not be_nil
|
2017-11-14 07:24:36 +00:00
|
|
|
config.formatter.should_not be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-12 21:19:00 +00:00
|
|
|
describe "#globs, #globs=" do
|
2017-11-14 07:24:36 +00:00
|
|
|
config = Config.load config_sample
|
|
|
|
|
2019-01-12 21:19:00 +00:00
|
|
|
it "holds source globs" do
|
2023-08-04 20:48:35 +00:00
|
|
|
config.globs.should eq ["**/*.cr"]
|
2017-11-14 07:24:36 +00:00
|
|
|
end
|
|
|
|
|
2019-01-12 21:19:00 +00:00
|
|
|
it "allows to set globs" do
|
|
|
|
config.globs = ["file.cr"]
|
|
|
|
config.globs.should eq ["file.cr"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-22 12:18:07 +00:00
|
|
|
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
|
|
|
|
|
2019-01-12 21:19:00 +00:00
|
|
|
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
|
2021-01-25 01:34:33 +00:00
|
|
|
config.sources.any?(&.fullpath.==(__FILE__)).should be_true
|
2020-03-22 12:18:07 +00:00
|
|
|
end
|
|
|
|
|
2022-11-16 15:55:32 +00:00
|
|
|
it "returns a list of sources matching globs" do
|
2023-11-17 15:37:27 +00:00
|
|
|
config.globs = %w[**/config_spec.cr]
|
2020-03-22 12:18:07 +00:00
|
|
|
config.sources.size.should eq(1)
|
|
|
|
end
|
|
|
|
|
2022-11-22 18:43:52 +00:00
|
|
|
it "returns a list of sources excluding 'Excluded'" do
|
2023-11-17 15:37:27 +00:00
|
|
|
config.excluded = %w[**/config_spec.cr]
|
2021-01-25 01:34:33 +00:00
|
|
|
config.sources.any?(&.fullpath.==(__FILE__)).should be_false
|
2017-11-14 07:24:36 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#formatter, formatter=" do
|
|
|
|
config = Config.load config_sample
|
|
|
|
formatter = DummyFormatter.new
|
|
|
|
|
|
|
|
it "contains default formatter" do
|
|
|
|
config.formatter.should_not be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows to set formatter" do
|
|
|
|
config.formatter = formatter
|
|
|
|
config.formatter.should eq formatter
|
|
|
|
end
|
2018-01-25 14:40:32 +00:00
|
|
|
|
|
|
|
it "allows to set formatter using a name" do
|
|
|
|
config.formatter = :progress
|
|
|
|
config.formatter.should_not be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error if not available formatter is set" do
|
|
|
|
expect_raises(Exception) do
|
|
|
|
config.formatter = :no_such_formatter
|
|
|
|
end
|
|
|
|
end
|
2017-11-14 07:24:36 +00:00
|
|
|
end
|
2017-11-30 21:50:07 +00:00
|
|
|
|
|
|
|
describe "#update_rule" do
|
|
|
|
config = Config.load config_sample
|
|
|
|
|
|
|
|
it "updates enabled property" do
|
2018-02-02 20:11:18 +00:00
|
|
|
name = DummyRule.rule_name
|
2017-11-30 21:50:07 +00:00
|
|
|
config.update_rule name, enabled: false
|
2022-10-28 22:03:39 +00:00
|
|
|
rule = config.rules.find!(&.name.== name)
|
2022-11-22 18:46:38 +00:00
|
|
|
rule.enabled?.should be_false
|
2017-11-30 21:50:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "updates excluded property" do
|
2018-02-02 20:11:18 +00:00
|
|
|
name = DummyRule.rule_name
|
2023-11-17 15:37:27 +00:00
|
|
|
excluded = %w[spec/source.cr]
|
2017-11-30 21:50:07 +00:00
|
|
|
config.update_rule name, excluded: excluded
|
2022-10-28 22:03:39 +00:00
|
|
|
rule = config.rules.find!(&.name.== name)
|
2017-11-30 21:50:07 +00:00
|
|
|
rule.excluded.should eq excluded
|
|
|
|
end
|
|
|
|
end
|
2018-06-18 07:25:06 +00:00
|
|
|
|
|
|
|
describe "#update_rules" do
|
|
|
|
config = Config.load config_sample
|
|
|
|
|
|
|
|
it "updates multiple rules by enabled property" do
|
|
|
|
name = DummyRule.rule_name
|
|
|
|
config.update_rules [name], enabled: false
|
2022-10-28 22:03:39 +00:00
|
|
|
rule = config.rules.find!(&.name.== name)
|
2022-11-22 18:46:38 +00:00
|
|
|
rule.enabled?.should be_false
|
2018-06-18 07:25:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "updates multiple rules by excluded property" do
|
|
|
|
name = DummyRule.rule_name
|
2023-11-17 15:37:27 +00:00
|
|
|
excluded = %w[spec/source.cr]
|
2018-06-18 07:25:06 +00:00
|
|
|
config.update_rules [name], excluded: excluded
|
2022-10-28 22:03:39 +00:00
|
|
|
rule = config.rules.find!(&.name.== name)
|
2018-06-18 07:25:06 +00:00
|
|
|
rule.excluded.should eq excluded
|
|
|
|
end
|
|
|
|
|
|
|
|
it "updates a group of rules by enabled property" do
|
|
|
|
group = DummyRule.group_name
|
|
|
|
config.update_rules [group], enabled: false
|
2022-10-28 22:03:39 +00:00
|
|
|
rule = config.rules.find!(&.name.== DummyRule.rule_name)
|
2022-11-22 18:46:38 +00:00
|
|
|
rule.enabled?.should be_false
|
2018-06-18 07:25:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "updates a group by excluded property" do
|
|
|
|
name = DummyRule.group_name
|
2023-11-17 15:37:27 +00:00
|
|
|
excluded = %w[spec/source.cr]
|
2018-06-18 07:25:06 +00:00
|
|
|
config.update_rules [name], excluded: excluded
|
2022-10-28 22:03:39 +00:00
|
|
|
rule = config.rules.find!(&.name.== DummyRule.rule_name)
|
2018-06-18 07:25:06 +00:00
|
|
|
rule.excluded.should eq excluded
|
|
|
|
end
|
|
|
|
end
|
2017-11-14 07:24:36 +00:00
|
|
|
end
|
|
|
|
end
|