Skip to content

Commit 2e51810

Browse files
(PUP-11751) catch :undefined_variable when defaults defined
Now that scope objects raise errors when variables are not found, interpolation needs to guard against this case when defaults are defined in a lookup and use the default in case a lookup fails.
1 parent 1141388 commit 2e51810

File tree

2 files changed

+10
-18
lines changed

2 files changed

+10
-18
lines changed

lib/puppet/pops/lookup/interpolation.rb

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,16 @@ def interpolate_method(method_key)
9090
lookup_invocation.report_found_in_overrides(root_key, ovr[root_key])
9191
else
9292
scope = lookup_invocation.scope
93-
val = scope[root_key]
94-
if val.nil? && !nil_in_scope?(scope, root_key)
95-
defaults = lookup_invocation.default_values
96-
if defaults.include?(root_key)
97-
lookup_invocation.report_found_in_defaults(root_key, defaults[root_key])
98-
else
99-
nil
100-
end
93+
val = nil
94+
if (default_val = lookup_invocation.default_values[root_key])
95+
catch(:undefined_variable) { val = scope[root_key] }
96+
else
97+
val = scope[root_key]
98+
end
99+
if val.nil? && default_val.nil?
100+
nil
101+
elsif val.nil?
102+
lookup_invocation.report_found_in_defaults(root_key, default_val)
101103
else
102104
lookup_invocation.report_found(root_key, val)
103105
end
@@ -125,15 +127,6 @@ def interpolate_method(method_key)
125127
interpolate_method
126128
end
127129

128-
# Because the semantics of Puppet::Parser::Scope#include? differs from Hash#include?
129-
def nil_in_scope?(scope, key)
130-
if scope.is_a?(Hash)
131-
scope.include?(key)
132-
else
133-
scope.exist?(key)
134-
end
135-
end
136-
137130
def get_method_and_data(data, allow_methods)
138131
match = data.match(/^(\w+)\((?:["]([^"]+)["]|[']([^']+)['])\)$/)
139132
if match

spec/unit/functions/lookup_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2420,7 +2420,6 @@ def uri_test_func(options, context)
24202420
end
24212421

24222422
it 'defaults are used when data is not found in scope interpolations' do
2423-
pending('See PUP-11751')
24242423
expect(lookup('mod_a::interpolate_scope_xd', { 'default_values_hash' => defaults })).to eql('-- value scope_xd (from default) --')
24252424
end
24262425

0 commit comments

Comments
 (0)