diff options
author | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2007-01-26 22:15:49 +0000 |
---|---|---|
committer | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2007-01-26 22:15:49 +0000 |
commit | 37acfb9bc8675f33aef159f298e35e3e43998c4f (patch) | |
tree | 1d46a52011169b71fa09fa358fd63aecb9ba0008 | |
parent | 2db68781a11de7b135d0abc332ed144c4e4b71c3 (diff) | |
download | puppet-37acfb9bc8675f33aef159f298e35e3e43998c4f.tar.gz puppet-37acfb9bc8675f33aef159f298e35e3e43998c4f.tar.xz puppet-37acfb9bc8675f33aef159f298e35e3e43998c4f.zip |
Fixing #442. You can now do: defined(File[...]) to see if a resource is defined.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@2097 980ebf18-57e1-0310-9a29-db15c13687c0
-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 |