Skip to content

Commit d8bbc1e

Browse files
committed
Update validate_data_hash to return modified hash
Puppet::Pops::Lookup::ModuleDataProvider#validate_data_hash is a method that is supposed to prune a data hash of any keys that are not prefixed with a given module's name. However prior to this commit it incorrectly took the data hash, cloned it, operated on the clone, then returned the unchanged original hash. This commit updates validate_data_hash to instead return the modified hash, and updates the warning message generated when a key is pruned to include the key.
1 parent 1d188e1 commit d8bbc1e

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

lib/puppet/pops/lookup/module_data_provider.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,16 @@ def key_lookup_in_default(key, lookup_invocation, merge)
4747
def validate_data_hash(data_hash)
4848
super
4949
module_prefix = "#{module_name}::"
50-
data_hash.each_key.reduce(data_hash) do |memo, k|
51-
next memo if k == LOOKUP_OPTIONS || k.start_with?(module_prefix)
52-
53-
msg = "#{yield} must use keys qualified with the name of the module"
54-
memo = memo.clone if memo.equal?(data_hash)
55-
memo.delete(k)
56-
Puppet.warning("Module '#{module_name}': #{msg}")
57-
memo
50+
data_hash_to_return = {}
51+
data_hash.keys.each do |k|
52+
if k == LOOKUP_OPTIONS || k.start_with?(module_prefix)
53+
data_hash_to_return[k] = data_hash[k]
54+
else
55+
msg = "#{yield} must use keys qualified with the name of the module"
56+
Puppet.warning("Module '#{module_name}': #{msg}; got #{k}")
57+
end
5858
end
59-
data_hash
59+
data_hash_to_return
6060
end
6161

6262
protected

spec/unit/functions/lookup_fixture_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ def compile_and_get_notifications(code)
377377
expect { compiler.compile }.to raise_error(Puppet::ParseError, /did not find a value for the name 'bad_data::b'/)
378378
end
379379
warnings = logs.select { |log| log.level == :warning }.map { |log| log.message }
380-
expect(warnings).to include("Module 'bad_data': Value returned from deprecated API function 'bad_data::data' must use keys qualified with the name of the module")
380+
expect(warnings).to include("Module 'bad_data': Value returned from deprecated API function 'bad_data::data' must use keys qualified with the name of the module; got b")
381381
end
382382

383383
it 'will succeed finding prefixed keys even when a key in the function provided module data is not prefixed' do
@@ -391,7 +391,7 @@ def compile_and_get_notifications(code)
391391
expect(resources).to include('module_c')
392392
end
393393
warnings = logs.select { |log| log.level == :warning }.map { |log| log.message }
394-
expect(warnings).to include("Module 'bad_data': Value returned from deprecated API function 'bad_data::data' must use keys qualified with the name of the module")
394+
expect(warnings).to include("Module 'bad_data': Value returned from deprecated API function 'bad_data::data' must use keys qualified with the name of the module; got b")
395395
end
396396

397397
it 'will resolve global, environment, and module correctly' do
@@ -422,8 +422,8 @@ def compile_and_get_notifications(code)
422422
Puppet::Util::Log.with_destination(Puppet::Test::LogCollector.new(logs)) do
423423
compiler.compile
424424
end
425-
warnings = logs.select { |log| log.level == :warning }.map { |log| log.message }
426-
expect(warnings).to include("Module 'bad_data': Value returned from deprecated API function 'bad_data::data' must use keys qualified with the name of the module")
425+
warnings = logs.filter_map { |log| log.message if log.level == :warning }
426+
expect(warnings).to include("Module 'bad_data': Value returned from deprecated API function 'bad_data::data' must use keys qualified with the name of the module; got b")
427427
end
428428

429429
it 'a warning will be logged when key in the hiera provided module data is not prefixed' do
@@ -432,8 +432,8 @@ def compile_and_get_notifications(code)
432432
Puppet::Util::Log.with_destination(Puppet::Test::LogCollector.new(logs)) do
433433
compiler.compile
434434
end
435-
warnings = logs.select { |log| log.level == :warning }.map { |log| log.message }
436-
expect(warnings).to include("Module 'hieraprovider': Value returned from data_hash function 'json_data', when using location '#{environmentpath}/production/modules/hieraprovider/data/first.json', must use keys qualified with the name of the module")
435+
warnings = logs.filter_map { |log| log.message if log.level == :warning }
436+
expect(warnings).to include("Module 'hieraprovider': Value returned from data_hash function 'json_data', when using location '#{environmentpath}/production/modules/hieraprovider/data/first.json', must use keys qualified with the name of the module; got test::param_b")
437437
end
438438
end
439439

0 commit comments

Comments
 (0)