-
-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed support for multilevel settings loaded from ENV variables (insp…
- Loading branch information
1 parent
43a822f
commit 8fc7576
Showing
7 changed files
with
186 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module Config | ||
VERSION = '1.2.0' | ||
VERSION = '1.2.1' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
require 'spec_helper' | ||
|
||
describe Config do | ||
|
||
context 'when overriding settings via ENV variables is enabled' do | ||
let(:config) do | ||
Config.load_files "#{fixture_path}/settings.yml", "#{fixture_path}/multilevel.yml" | ||
end | ||
|
||
before :all do | ||
Config.use_env = true | ||
end | ||
|
||
after :all do | ||
Config.use_env = false | ||
end | ||
|
||
after :each do | ||
ENV.clear | ||
|
||
Config.env_prefix = nil | ||
Config.env_separator = '.' | ||
Config.env_converter = nil | ||
Config.env_parse_values = false | ||
end | ||
|
||
it 'should add new setting from ENV variable' do | ||
ENV['Settings.new_var'] = 'value' | ||
|
||
expect(config.new_var).to eq('value') | ||
end | ||
|
||
context 'should override existing setting with a value from ENV variable' do | ||
it 'for a basic values' do | ||
ENV['Settings.size'] = 'overwritten by env' | ||
|
||
expect(config.size).to eq('overwritten by env') | ||
end | ||
|
||
it 'for multilevel sections' do | ||
ENV['Settings.number_of_all_countries'] = '0' | ||
ENV['Settings.world.countries.europe'] = '0' | ||
|
||
expect(config.number_of_all_countries).to eq('0') | ||
expect(config.world.countries.europe).to eq('0') | ||
expect(config.world.countries.australia).to eq(1) | ||
end | ||
end | ||
|
||
context 'and parsing ENV variables is enabled' do | ||
before :each do | ||
Config.env_parse_values = true | ||
end | ||
|
||
it 'should recognize numbers and expose them as integers' do | ||
ENV['Settings.new_var'] = '123' | ||
|
||
expect(config.new_var).to eq(123) | ||
expect(config.new_var.is_a? Integer).to eq(true) | ||
end | ||
|
||
it 'should leave strings intact' do | ||
ENV['Settings.new_var'] = 'foobar' | ||
|
||
expect(config.new_var).to eq('foobar') | ||
expect(config.new_var.is_a? String).to eq(true) | ||
end | ||
end | ||
|
||
context 'and custom ENV variables prefix is defined' do | ||
before :each do | ||
Config.env_prefix = 'MyConfig' | ||
end | ||
|
||
it 'should load variables from the new prefix' do | ||
ENV['MyConfig.new_var'] = 'value' | ||
|
||
expect(config.new_var).to eq('value') | ||
end | ||
|
||
it 'should not load variables from the default prefix' do | ||
ENV['Settings.new_var'] = 'value' | ||
|
||
expect(config.new_var).to eq(nil) | ||
end | ||
|
||
it 'should skip ENV variable when partial prefix match' do | ||
ENV['MyConfigs.new_var'] = 'value' | ||
|
||
expect(config.new_var).to eq(nil) | ||
end | ||
end | ||
|
||
context 'and custom ENV variables separator is defined' do | ||
before :each do | ||
Config.env_separator = '__' | ||
end | ||
|
||
it 'should load variables and correctly recognize the new separator' do | ||
ENV['Settings__new_var'] = 'value' | ||
ENV['Settings__var.with.dot'] = 'value' | ||
ENV['Settings__world__countries__europe'] = '0' | ||
|
||
expect(config.new_var).to eq('value') | ||
expect(config['var.with.dot']).to eq('value') | ||
expect(config.world.countries.europe).to eq('0') | ||
end | ||
|
||
it 'should ignore variables wit default separator' do | ||
ENV['Settings.new_var'] = 'value' | ||
|
||
expect(config.new_var).to eq(nil) | ||
end | ||
end | ||
|
||
context 'and variable names conversion is considered' do | ||
it 'should downcase variable names when :downcase conversion enabled' do | ||
ENV['Settings.NEW_VAR'] = 'value' | ||
Config.env_converter = :downcase | ||
|
||
expect(config.new_var).to eq('value') | ||
end | ||
|
||
it 'should not change variable names by default' do | ||
ENV['Settings.NEW_VAR'] = 'value' | ||
|
||
expect(config.new_var).to eq(nil) | ||
expect(config.NEW_VAR).to eq('value') | ||
end | ||
end | ||
|
||
it 'should always load ENV variables when reloading settings from files' do | ||
ENV['Settings.new_var'] = 'value' | ||
|
||
expect(config.new_var).to eq('value') | ||
|
||
Config.reload! | ||
|
||
expect(config.new_var).to eq('value') | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
world: | ||
capitals: | ||
europe: | ||
germany: 'Berlin' | ||
poland: 'Warsaw' | ||
australia: 'Sydney' | ||
countries: | ||
europe: 50 | ||
australia: 1 | ||
number_of_all_countries: 196 |