Skip to content

Commit

Permalink
Merge pull request #1564 from EmersonManabuAraki/fix/allow-yaml-files
Browse files Browse the repository at this point in the history
Fix/allow yaml files for Issue #1563
  • Loading branch information
namusyaka authored Sep 6, 2019
2 parents 32d6833 + 1199d64 commit 722353e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
4 changes: 2 additions & 2 deletions sinatra-contrib/lib/sinatra/config_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def config_file(*paths)
Dir.chdir(root || '.') do
paths.each do |pattern|
Dir.glob(pattern) do |file|
raise UnsupportedConfigType unless ['.yml', '.erb'].include?(File.extname(file))
raise UnsupportedConfigType unless ['.yml', '.yaml', '.erb'].include?(File.extname(file))
logger.info "loading config file '#{file}'" if logging? && respond_to?(:logger)
document = ERB.new(IO.read(file)).result
yaml = YAML.load(document)
Expand All @@ -134,7 +134,7 @@ def config_file(*paths)

class UnsupportedConfigType < Exception
def message
'Invalid config file type, use .yml or .yml.erb'
'Invalid config file type, use .yml, .yaml or .erb'
end
end

Expand Down
7 changes: 7 additions & 0 deletions sinatra-contrib/spec/config_file/key_value.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
foo: bar
bar: <%= "bar" %>
something: 42
nested:
a: 1
b: 2
12 changes: 11 additions & 1 deletion sinatra-contrib/spec/config_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,17 @@ def config_file(*args, &block)
expect(settings.nested[:b]).to eq(2)
end

it 'should raise error if config file extension is not .yml or .erb' do
it 'should render options in ERB tags when using .yaml files' do
config_file 'key_value.yaml'
expect(settings.foo).to eq("bar")
expect(settings.something).to eq(42)
expect(settings.nested['a']).to eq(1)
expect(settings.nested[:a]).to eq(1)
expect(settings.nested['b']).to eq(2)
expect(settings.nested[:b]).to eq(2)
end

it 'should raise error if config file extension is not .yml, .yaml or .erb' do
expect{ config_file 'config.txt' }.to raise_error(Sinatra::ConfigFile::UnsupportedConfigType)
end

Expand Down

0 comments on commit 722353e

Please sign in to comment.