Skip to content

Commit

Permalink
Merge pull request #1122 from trevor-vaughan/MODULES-10781_fix_define…
Browse files Browse the repository at this point in the history
…d_type_defined_with_params

[MODULES-10781] Fix defined type defined_with_params()
  • Loading branch information
david22swan committed Aug 20, 2020
2 parents 33ff524 + cf7b302 commit 7c1ae25
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/puppet/parser/functions/defined_with_params.rb
Expand Up @@ -54,12 +54,19 @@
[findresource(type, title)]
end

resources.compact.each do |resource|
resources.compact.each do |res|
# If you call this from within a defined type, it will find itself
next if res.to_s == resource.to_s

matches = params.map do |key, value|
# eql? avoids bugs caused by monkeypatching in puppet
resource_is_undef = resource[key].eql?(:undef) || resource[key].nil?
res_is_undef = res[key].eql?(:undef) || res[key].nil?
value_is_undef = value.eql?(:undef) || value.nil?
(resource_is_undef && value_is_undef) || (resource[key] == value)
found_match = (res_is_undef && value_is_undef) || (res[key] == value)

Puppet.debug("Matching resource is #{res}") if found_match

found_match
end
ret = params.empty? || !matches.include?(false)

Expand Down
20 changes: 20 additions & 0 deletions spec/functions/defined_with_params_spec.rb
Expand Up @@ -94,6 +94,26 @@
}
end

describe 'when called from within a defined type looking for a defined type of the same type' do
let :pre_condition do
<<-PRECOND
define test::deftype(
Optional $port = undef
) {
if defined_with_params(Test::Deftype, { 'port' => $port }) {
fail('Ruh Roh Shaggy')
}
}
test::deftype { 'foo': }
test::deftype { 'bar': port => 200 }
PRECOND
end

# Testing to make sure that the internal logic handles this case via the pre_condition
it { is_expected.to run.with_params('NoOp[noop]', {}).and_return(false) }
end

describe 'when passed a class' do
let :pre_condition do
'class test () { } class { "test": }'
Expand Down

0 comments on commit 7c1ae25

Please sign in to comment.