Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions lib/java_buildpack/util/configuration_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,8 @@ def load(identifier, should_log = true)
file = CONFIG_DIRECTORY + "#{identifier}.yml"

if file.exist?
configuration = YAML.load_file(file)
logger.debug { "Configuration from #{file}: #{configuration}" } if should_log

user_provided = ENV[environment_variable_name(identifier)]

if user_provided
YAML.load(user_provided).each do |new_prop|
configuration = do_merge(configuration, new_prop, should_log)
end
logger.debug { "Configuration from #{file} modified with: #{user_provided}" } if should_log
end
configuration = load_configuration(file, user_provided, should_log)
else
logger.debug { "No configuration file #{file} found" } if should_log
end
Expand All @@ -68,6 +59,27 @@ def load(identifier, should_log = true)

private_constant :CONFIG_DIRECTORY, :ENVIRONMENT_VARIABLE_PATTERN

def load_configuration(file, user_provided, should_log)
configuration = YAML.load_file(file)
logger.debug { "Configuration from #{file}: #{configuration}" } if should_log

if user_provided
user_provided_value = YAML.load(user_provided)
if user_provided_value.is_a?(Hash)
configuration = do_merge(configuration, user_provided_value, should_log)
elsif user_provided_value.is_a?(Array)
user_provided_value.each do |new_prop|
configuration = do_merge(configuration, new_prop, should_log)
end
else
fail "User configuration value is not valid: #{user_provided_value}"
end
logger.debug { "Configuration from #{file} modified with: #{user_provided}" } if should_log
end

configuration
end

def do_merge(hash_v1, hash_v2, should_log)
hash_v2.each do |key, value|
if hash_v1.key? key
Expand Down
35 changes: 32 additions & 3 deletions spec/java_buildpack/util/configuration_utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@
before do
allow_any_instance_of(Pathname).to receive(:exist?).and_return(true)
allow(YAML).to receive(:load_file).and_return('foo' => { 'one' => '1', 'two' => 2 },
'bar' => { 'alpha' => { 'one' => 'cat', 'two' => 'dog' } })
'bar' => { 'alpha' => { 'one' => 'cat', 'two' => 'dog' } },
'version' => '1.7.1')
end

it 'load configuration file' do
expect(described_class.load('test')).to eq('foo' => { 'one' => '1', 'two' => 2 },
'bar' => { 'alpha' => { 'one' => 'cat', 'two' => 'dog' } })
'bar' => { 'alpha' => { 'one' => 'cat', 'two' => 'dog' } },
'version' => '1.7.1')
end

context do
Expand All @@ -51,7 +53,34 @@
it 'overlays matching environment variables' do

expect(described_class.load('test')).to eq('foo' => { 'one' => '1', 'two' => 2 },
'bar' => { 'alpha' => { 'one' => 3, 'two' => 'dog' } })
'bar' => { 'alpha' => { 'one' => 3, 'two' => 'dog' } },
'version' => '1.7.1')
end

end

context do

let(:environment) do
{ 'JBP_CONFIG_TEST' => 'version: 1.8.+' }
end

it 'overlays simple matching environment variable' do
expect(described_class.load('test')).to eq('foo' => { 'one' => '1', 'two' => 2 },
'bar' => { 'alpha' => { 'one' => 'cat', 'two' => 'dog' } },
'version' => '1.8.+')
end

end

context do

let(:environment) do
{ 'JBP_CONFIG_TEST' => 'version 1.8.+' }
end

it 'raises an exception when invalid override value is specified' do
expect { described_class.load('test') }.to raise_error(/User configuration value is not valid/)
end

end
Expand Down