summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2007-07-08 23:52:58 +0000
committerluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2007-07-08 23:52:58 +0000
commit4080077639321884cc633928915664e6e1391c2e (patch)
tree15bece5b17737c145813c77c4d801978528c2a28
parent07f05195da27a9e3124d6b987a808c087dce334b (diff)
downloadpuppet-4080077639321884cc633928915664e6e1391c2e.tar.gz
puppet-4080077639321884cc633928915664e6e1391c2e.tar.xz
puppet-4080077639321884cc633928915664e6e1391c2e.zip
The parser now throws an error when a resource reference is created for an unknown type. Also, resource references look up defined types and translate their type accordingly.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@2660 980ebf18-57e1-0310-9a29-db15c13687c0
-rw-r--r--CHANGELOG4
-rw-r--r--lib/puppet/parser/ast/resourceref.rb8
-rwxr-xr-xtest/language/ast/resourceref.rb70
3 files changed, 82 insertions, 0 deletions
diff --git a/CHANGELOG b/CHANGELOG
index d3c95cb03..cf6aaf0a5 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,3 +1,7 @@
+ The parser now throws an error when a resource reference
+ is created for an unknown type. Also, resource references
+ look up defined types and translate their type accordingly. (#706)
+
Hostnames can now be double quoted.
Adding module autoloading (#596) -- you can now 'include' classes
diff --git a/lib/puppet/parser/ast/resourceref.rb b/lib/puppet/parser/ast/resourceref.rb
index b0fe5f6d7..e6d486902 100644
--- a/lib/puppet/parser/ast/resourceref.rb
+++ b/lib/puppet/parser/ast/resourceref.rb
@@ -20,6 +20,14 @@ class Puppet::Parser::AST
# We want a lower-case type.
objtype = @type.downcase
+ if scope.builtintype?(objtype)
+ # nothing
+ elsif dtype = scope.finddefine(objtype)
+ objtype = dtype.classname
+ else
+ raise Puppet::ParseError, "Could not find type %s" % objtype
+ end
+
title = @title.safeevaluate(:scope => scope)
return Puppet::Parser::Resource::Reference.new(
diff --git a/test/language/ast/resourceref.rb b/test/language/ast/resourceref.rb
new file mode 100755
index 000000000..95cf34754
--- /dev/null
+++ b/test/language/ast/resourceref.rb
@@ -0,0 +1,70 @@
+#!/usr/bin/env ruby
+#
+# Created by Luke A. Kanies on 2007-07-8.
+# Copyright (c) 2007. All rights reserved.
+
+$:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/
+
+require 'puppettest'
+require 'puppettest/parsertesting'
+
+class TestASTResourceRef < Test::Unit::TestCase
+ include PuppetTest
+ include PuppetTest::ParserTesting
+ AST = Puppet::Parser::AST
+
+ def newref(type, title)
+ AST::ResourceRef.new(:type => type, :title => AST::String.new(:value => title))
+ end
+
+ def setup
+ super
+ @interp = mkinterp
+ @scope = mkscope :interp => @interp
+ end
+
+ def test_evaluate
+ @interp.newdefine "one::two"
+ @interp.newdefine "one-two"
+ [%w{file /tmp/yay}, %w{one::two three}, %w{one-two three}].each do |type, title|
+ ref = newref(type, title)
+
+ evaled = nil
+ assert_nothing_raised("Could not evaluate resource ref") do
+ evaled = ref.evaluate(:scope => @scope)
+ end
+
+ assert_equal(type, evaled.type, "Type did not translate correctly")
+ assert_equal(title, evaled.title, "Title did not translate correctly")
+ end
+ end
+
+ # Related to #706, make sure resource references correctly translate to qualified types.
+ def test_scoped_references
+ @interp.newdefine "one"
+ @interp.newdefine "one::two"
+ @interp.newdefine "three"
+ twoscope = @scope.newscope(:type => "one", :namespace => "one")
+ assert(twoscope.finddefine("two"), "Could not find 'two' definition")
+ title = "title"
+
+ # First try an unqualified type
+ assert_equal("one::two", newref("two", title).evaluate(:scope => twoscope).type,
+ "Defined type was not made fully qualified")
+
+ # Then try a type that does not need to be qualified
+ assert_equal("one", newref("one", title).evaluate(:scope => twoscope).type,
+ "Unqualified defined type was not handled correctly")
+
+ # Then a builtin type
+ assert_equal("file", newref("file", title).evaluate(:scope => twoscope).type,
+ "Builtin type was not handled correctly")
+
+ # Now try a type that does not exist, which should throw an error.
+ assert_raise(Puppet::ParseError, "Did not fail on a missing type in a resource reference") do
+ newref("nosuchtype", title).evaluate(:scope => twoscope)
+ end
+ end
+end
+
+# $Id$