summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/puppet/parser/scope.rb18
-rwxr-xr-xtest/language/functions.rb39
2 files changed, 53 insertions, 4 deletions
diff --git a/lib/puppet/parser/scope.rb b/lib/puppet/parser/scope.rb
index 69c8c06ed..a26ec938d 100644
--- a/lib/puppet/parser/scope.rb
+++ b/lib/puppet/parser/scope.rb
@@ -36,8 +36,13 @@ module Puppet::Parser
end
end
+ # Ruby treats variables like methods, so we can cheat here and
+ # trap missing vars like they were missing methods.
def method_missing(name, *args)
- if value = @scope.lookupvar(name.to_s) and value != :undefined and value != ""
+ # We have to tell lookupvar to return :undefined to us when
+ # appropriate; otherwise it converts to "".
+ value = @scope.lookupvar(name.to_s, false)
+ if value != :undefined
return value
else
# Just throw an error immediately, instead of searching for
@@ -690,11 +695,16 @@ module Puppet::Parser
end
end
- # Look up a variable. The simplest value search we do.
- def lookupvar(name)
+ # Look up a variable. The simplest value search we do. Default to returning
+ # an empty string for missing values, but support returning a constant.
+ def lookupvar(name, usestring = true)
value = lookup("variable", name)
if value == :undefined
- return ""
+ if usestring
+ return ""
+ else
+ return :undefined
+ end
else
return value
end
diff --git a/test/language/functions.rb b/test/language/functions.rb
index b35a1ac4a..d9364f996 100755
--- a/test/language/functions.rb
+++ b/test/language/functions.rb
@@ -241,6 +241,45 @@ class TestLangFunctions < Test::Unit::TestCase
assert(parsedate != newdate, "Parse date did not change")
end
+ def test_template_defined_vars
+ template = tempfile()
+
+ File.open(template, "w") do |f|
+ f.puts "template <%= yayness %>"
+ end
+
+ func = nil
+ assert_nothing_raised do
+ func = Puppet::Parser::AST::Function.new(
+ :name => "template",
+ :ftype => :rvalue,
+ :arguments => AST::ASTArray.new(
+ :children => [stringobj(template)]
+ )
+ )
+ end
+ ast = varobj("output", func)
+
+ {
+ "" => "",
+ false => "false",
+ }.each do |string, value|
+ scope = Puppet::Parser::Scope.new()
+ assert_raise(Puppet::ParseError) do
+ ast.evaluate(:scope => scope)
+ end
+
+ scope.setvar("yayness", string)
+
+ assert_nothing_raised("An empty string was not a valid variable value") do
+ ast.evaluate(:scope => scope)
+ end
+
+ assert_equal("template #{value}\n", scope.lookupvar("output"),
+ "%s did not get evaluated correctly" % string.inspect)
+ end
+ end
+
def test_autoloading_functions
assert_equal(false, Puppet::Parser::Functions.function(:autofunc),
"Got told autofunc already exists")