Skip to content

Commit

Permalink
Merge pull request #393 from stufro/362-raise-on-invalid-config-file-…
Browse files Browse the repository at this point in the history
…path

Raise error when passed invalid config file path
  • Loading branch information
veelenga authored Jul 26, 2023
2 parents 8c9d234 + 88e0437 commit 8ef588d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
8 changes: 7 additions & 1 deletion spec/ameba/config_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ require "../spec_helper"

module Ameba
describe Config do
config_sample = "config/ameba.yml"
config_sample = "spec/fixtures/config.yml"

it "should have a list of available formatters" do
Config::AVAILABLE_FORMATTERS.should_not be_nil
Expand Down Expand Up @@ -84,6 +84,12 @@ module Ameba
config.formatter.should_not be_nil
end

it "raises when custom config file doesn't exist" do
expect_raises(Exception, "Unable to load config file: Config file does not exist foo.yml") do
Config.load "foo.yml"
end
end

it "loads default config" do
config = Config.load
config.should_not be_nil
Expand Down
2 changes: 2 additions & 0 deletions spec/fixtures/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Lint/ComparisonToBoolean:
Enabled: true
5 changes: 3 additions & 2 deletions src/ameba/config.cr
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,13 @@ class Ameba::Config
end
Config.new YAML.parse(content)
rescue e
raise "Config file is invalid: #{e.message}"
raise "Unable to load config file: #{e.message}"
end

protected def self.read_config(path = nil)
if path
return File.exists?(path) ? File.read(path) : nil
raise ArgumentError.new("Config file does not exist #{path}") unless File.exists?(path)
return File.read(path)
end
each_config_path do |config_path|
return File.read(config_path) if File.exists?(config_path)
Expand Down

0 comments on commit 8ef588d

Please sign in to comment.