Merge pull request #393 from stufro/362-raise-on-invalid-config-file-path

Raise error when passed invalid config file path
This commit is contained in:
Vitalii Elenhaupt 2023-07-26 17:19:44 +03:00 committed by GitHub
commit 8ef588dc6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 3 deletions

View File

@ -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
@ -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

2
spec/fixtures/config.yml vendored Normal file
View File

@ -0,0 +1,2 @@
Lint/ComparisonToBoolean:
Enabled: true

View File

@ -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)