Skip to content

Commit 7c1ae25

Browse files
authored
Merge pull request #1122 from trevor-vaughan/MODULES-10781_fix_defined_type_defined_with_params
[MODULES-10781] Fix defined type defined_with_params()
2 parents 33ff524 + cf7b302 commit 7c1ae25

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

lib/puppet/parser/functions/defined_with_params.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,19 @@
5454
[findresource(type, title)]
5555
end
5656

57-
resources.compact.each do |resource|
57+
resources.compact.each do |res|
58+
# If you call this from within a defined type, it will find itself
59+
next if res.to_s == resource.to_s
60+
5861
matches = params.map do |key, value|
5962
# eql? avoids bugs caused by monkeypatching in puppet
60-
resource_is_undef = resource[key].eql?(:undef) || resource[key].nil?
63+
res_is_undef = res[key].eql?(:undef) || res[key].nil?
6164
value_is_undef = value.eql?(:undef) || value.nil?
62-
(resource_is_undef && value_is_undef) || (resource[key] == value)
65+
found_match = (res_is_undef && value_is_undef) || (res[key] == value)
66+
67+
Puppet.debug("Matching resource is #{res}") if found_match
68+
69+
found_match
6370
end
6471
ret = params.empty? || !matches.include?(false)
6572

spec/functions/defined_with_params_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,26 @@
9494
}
9595
end
9696

97+
describe 'when called from within a defined type looking for a defined type of the same type' do
98+
let :pre_condition do
99+
<<-PRECOND
100+
define test::deftype(
101+
Optional $port = undef
102+
) {
103+
if defined_with_params(Test::Deftype, { 'port' => $port }) {
104+
fail('Ruh Roh Shaggy')
105+
}
106+
}
107+
108+
test::deftype { 'foo': }
109+
test::deftype { 'bar': port => 200 }
110+
PRECOND
111+
end
112+
113+
# Testing to make sure that the internal logic handles this case via the pre_condition
114+
it { is_expected.to run.with_params('NoOp[noop]', {}).and_return(false) }
115+
end
116+
97117
describe 'when passed a class' do
98118
let :pre_condition do
99119
'class test () { } class { "test": }'

0 commit comments

Comments
 (0)