diff options
-rw-r--r-- | lib/puppet/parser/functions.rb | 28 | ||||
-rwxr-xr-x | test/language/functions.rb | 11 |
2 files changed, 32 insertions, 7 deletions
diff --git a/lib/puppet/parser/functions.rb b/lib/puppet/parser/functions.rb index 5f0930c6a..1174d7dad 100644 --- a/lib/puppet/parser/functions.rb +++ b/lib/puppet/parser/functions.rb @@ -151,14 +151,28 @@ module Functions # Test whether a given class or definition is defined newfunction(:defined, :type => :rvalue, :doc => "Determine whether a given - type is defined, either as a native type or a defined type.") do |vals| - # For some reason, it doesn't want me to return from here. - if vals.detect do |val| Puppet::Type.type(val) or finddefine(val) or findclass(val) end - true - else - false + type is defined, either as a native type or a defined type, or whether a resource has been + specified. If you are checking with a resource is defined, use the normal resource + reference syntax, e.g., ``File['/etc/passwd']``.") do |vals| + result = false + vals.each do |val| + case val + when String: + # For some reason, it doesn't want me to return from here. + if Puppet::Type.type(val) or finddefine(val) or findclass(val) + result = true + break + end + when Puppet::Parser::Resource::Reference: + if findresource(val.to_s) + result = true + break + end + else + raise ArgumentError, "Invalid argument of type %s to 'defined'" % val.class + end end - + result end newfunction(:fail, :doc => "Fail with a parse error.") do |vals| diff --git a/test/language/functions.rb b/test/language/functions.rb index 57afa6d41..edb7a3004 100755 --- a/test/language/functions.rb +++ b/test/language/functions.rb @@ -382,6 +382,17 @@ class TestLangFunctions < Test::Unit::TestCase # and make sure multiple falses are still false assert(! scope.function_defined(%w{no otherno stillno}), "Multiple falses were somehow true") + + # Now make sure we can test resources + scope.setresource mkresource(:type => "file", :title => "/tmp/rahness", + :scope => scope, :source => scope.source, + :params => {:owner => "root"}) + + yep = Puppet::Parser::Resource::Reference.new(:type => "file", :title => "/tmp/rahness") + nope = Puppet::Parser::Resource::Reference.new(:type => "file", :title => "/tmp/fooness") + + assert(scope.function_defined([yep]), "valid resource was not considered defined") + assert(! scope.function_defined([nope]), "invalid resource was considered defined") end end |