2014-05-09 02:39:16 -04:00
|
|
|
require 'spec_helper'
|
2011-03-24 05:25:00 -04:00
|
|
|
|
|
|
|
describe Sinatra::ConfigFile do
|
|
|
|
def config_file(*args, &block)
|
|
|
|
mock_app do
|
|
|
|
register Sinatra::ConfigFile
|
|
|
|
set :root, File.expand_path('../config_file', __FILE__)
|
|
|
|
instance_eval(&block) if block
|
|
|
|
config_file(*args)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should set options from a simple config_file' do
|
|
|
|
config_file 'key_value.yml'
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(settings.foo).to eq('bar')
|
|
|
|
expect(settings.something).to eq(42)
|
2011-03-24 05:25:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should create indifferent hashes' do
|
|
|
|
config_file 'key_value.yml'
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(settings.nested['a']).to eq(1)
|
|
|
|
expect(settings.nested[:a]).to eq(1)
|
2011-03-24 05:25:00 -04:00
|
|
|
end
|
|
|
|
|
2016-04-28 07:53:32 -04:00
|
|
|
it 'should render options in ERB tags when using .yml files' do
|
|
|
|
config_file 'key_value.yml'
|
2016-12-28 10:35:12 -05:00
|
|
|
expect(settings.bar).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
|
2016-04-28 07:53:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should render options in ERB tags when using .yml.erb files' do
|
2012-06-20 03:05:54 -04:00
|
|
|
config_file 'key_value.yml.erb'
|
2016-05-10 10:08:54 -04:00
|
|
|
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)
|
2012-06-12 16:48:53 -04:00
|
|
|
end
|
|
|
|
|
2019-08-26 13:34:47 -04:00
|
|
|
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
|
2016-04-28 07:53:32 -04:00
|
|
|
expect{ config_file 'config.txt' }.to raise_error(Sinatra::ConfigFile::UnsupportedConfigType)
|
|
|
|
end
|
|
|
|
|
2011-03-24 05:25:00 -04:00
|
|
|
it 'should recognize env specific settings per file' do
|
|
|
|
config_file 'with_envs.yml'
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(settings.foo).to eq('test')
|
2011-03-24 05:25:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should recognize env specific settings per setting' do
|
|
|
|
config_file 'with_nested_envs.yml'
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(settings.database[:adapter]).to eq('sqlite')
|
2011-03-24 05:25:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not set present values to nil if the current env is missing' do
|
|
|
|
# first let's check the test is actually working properly
|
|
|
|
config_file('missing_env.yml') { set :foo => 42, :environment => :production }
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(settings.foo).to eq(10)
|
2011-03-24 05:25:00 -04:00
|
|
|
# now test it
|
|
|
|
config_file('missing_env.yml') { set :foo => 42, :environment => :test }
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(settings.foo).to eq(42)
|
2011-03-24 05:25:00 -04:00
|
|
|
end
|
2011-09-29 20:58:23 -04:00
|
|
|
|
|
|
|
it 'should prioritize settings in latter files' do
|
|
|
|
# first let's check the test is actually working properly
|
|
|
|
config_file 'key_value.yml'
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(settings.foo).to eq('bar')
|
2011-09-29 20:58:23 -04:00
|
|
|
# now test it
|
|
|
|
config_file 'key_value_override.yml'
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(settings.foo).to eq('foo')
|
2011-09-29 20:58:23 -04:00
|
|
|
end
|
2017-01-27 21:50:54 -05:00
|
|
|
|
|
|
|
context 'when file contains superfluous environments' do
|
|
|
|
before { config_file 'with_env_defaults.yml' }
|
|
|
|
|
|
|
|
it 'loads settings for the current environment anyway' do
|
|
|
|
expect { settings.foo }.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when file contains defaults' do
|
|
|
|
before { config_file 'with_env_defaults.yml' }
|
|
|
|
|
|
|
|
it 'uses the overridden value' do
|
|
|
|
expect(settings.foo).to eq('test')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'uses the default value' do
|
|
|
|
expect(settings.bar).to eq('baz')
|
|
|
|
end
|
|
|
|
end
|
2011-03-24 05:25:00 -04:00
|
|
|
end
|