Skip to content

Commit 390f0bc

Browse files
jgawornebhale
authored andcommitted
handle simpler and invalid values for config overrides
1 parent 30515fb commit 390f0bc

2 files changed

Lines changed: 54 additions & 13 deletions

File tree

lib/java_buildpack/util/configuration_utils.rb

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,8 @@ def load(identifier, should_log = true)
4242
file = CONFIG_DIRECTORY + "#{identifier}.yml"
4343

4444
if file.exist?
45-
configuration = YAML.load_file(file)
46-
logger.debug { "Configuration from #{file}: #{configuration}" } if should_log
47-
4845
user_provided = ENV[environment_variable_name(identifier)]
49-
50-
if user_provided
51-
YAML.load(user_provided).each do |new_prop|
52-
configuration = do_merge(configuration, new_prop, should_log)
53-
end
54-
logger.debug { "Configuration from #{file} modified with: #{user_provided}" } if should_log
55-
end
46+
configuration = load_configuration(file, user_provided, should_log)
5647
else
5748
logger.debug { "No configuration file #{file} found" } if should_log
5849
end
@@ -68,6 +59,27 @@ def load(identifier, should_log = true)
6859

6960
private_constant :CONFIG_DIRECTORY, :ENVIRONMENT_VARIABLE_PATTERN
7061

62+
def load_configuration(file, user_provided, should_log)
63+
configuration = YAML.load_file(file)
64+
logger.debug { "Configuration from #{file}: #{configuration}" } if should_log
65+
66+
if user_provided
67+
user_provided_value = YAML.load(user_provided)
68+
if user_provided_value.is_a?(Hash)
69+
configuration = do_merge(configuration, user_provided_value, should_log)
70+
elsif user_provided_value.is_a?(Array)
71+
user_provided_value.each do |new_prop|
72+
configuration = do_merge(configuration, new_prop, should_log)
73+
end
74+
else
75+
fail "User configuration value is not valid: #{user_provided_value}"
76+
end
77+
logger.debug { "Configuration from #{file} modified with: #{user_provided}" } if should_log
78+
end
79+
80+
configuration
81+
end
82+
7183
def do_merge(hash_v1, hash_v2, should_log)
7284
hash_v2.each do |key, value|
7385
if hash_v1.key? key

spec/java_buildpack/util/configuration_utils_spec.rb

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@
3434
before do
3535
allow_any_instance_of(Pathname).to receive(:exist?).and_return(true)
3636
allow(YAML).to receive(:load_file).and_return('foo' => { 'one' => '1', 'two' => 2 },
37-
'bar' => { 'alpha' => { 'one' => 'cat', 'two' => 'dog' } })
37+
'bar' => { 'alpha' => { 'one' => 'cat', 'two' => 'dog' } },
38+
'version' => '1.7.1')
3839
end
3940

4041
it 'load configuration file' do
4142
expect(described_class.load('test')).to eq('foo' => { 'one' => '1', 'two' => 2 },
42-
'bar' => { 'alpha' => { 'one' => 'cat', 'two' => 'dog' } })
43+
'bar' => { 'alpha' => { 'one' => 'cat', 'two' => 'dog' } },
44+
'version' => '1.7.1')
4345
end
4446

4547
context do
@@ -51,7 +53,34 @@
5153
it 'overlays matching environment variables' do
5254

5355
expect(described_class.load('test')).to eq('foo' => { 'one' => '1', 'two' => 2 },
54-
'bar' => { 'alpha' => { 'one' => 3, 'two' => 'dog' } })
56+
'bar' => { 'alpha' => { 'one' => 3, 'two' => 'dog' } },
57+
'version' => '1.7.1')
58+
end
59+
60+
end
61+
62+
context do
63+
64+
let(:environment) do
65+
{ 'JBP_CONFIG_TEST' => 'version: 1.8.+' }
66+
end
67+
68+
it 'overlays simple matching environment variable' do
69+
expect(described_class.load('test')).to eq('foo' => { 'one' => '1', 'two' => 2 },
70+
'bar' => { 'alpha' => { 'one' => 'cat', 'two' => 'dog' } },
71+
'version' => '1.8.+')
72+
end
73+
74+
end
75+
76+
context do
77+
78+
let(:environment) do
79+
{ 'JBP_CONFIG_TEST' => 'version 1.8.+' }
80+
end
81+
82+
it 'raises an exception when invalid override value is specified' do
83+
expect { described_class.load('test') }.to raise_error(/User configuration value is not valid/)
5584
end
5685

5786
end

0 commit comments

Comments
 (0)