[Pkg-puppet-devel] [SCM] Puppet packaging for Debian branch, master, updated. debian/0.24.6-1-356-g5718585

James Turnbull james at lovedthanlost.net
Fri Jan 23 14:21:39 UTC 2009


The following commit has been merged in the master branch:
commit 2c05a0abcb55347c179e66bb0c9d23698e729046
Author: Brice Figureau <brice-puppet at daysofwonder.com>
Date:   Sat Nov 15 13:22:26 2008 +0100

    Move function existance test to parser evaluation
    
    The aim is to let --parseonly succeeds even if the function
    is not (yet) present. This is usefull in commit-hooks and
    for the inline documentation generation system.
    
    Signed-off-by: Brice Figureau <brice-puppet at daysofwonder.com>

diff --git a/lib/puppet/parser/ast/function.rb b/lib/puppet/parser/ast/function.rb
index 192940a..fc3797f 100644
--- a/lib/puppet/parser/ast/function.rb
+++ b/lib/puppet/parser/ast/function.rb
@@ -11,20 +11,9 @@ class Puppet::Parser::AST
         @settor = true
 
         def evaluate(scope)
-            # We don't need to evaluate the name, because it's plaintext
-            args = @arguments.safeevaluate(scope)
-
-            return scope.send("function_" + @name, args)
-        end
-
-        def initialize(hash)
-            @ftype = hash[:ftype] || :rvalue
-            hash.delete(:ftype) if hash.include? :ftype
-
-            super(hash)
 
             # Make sure it's a defined function
-            unless @fname = Puppet::Parser::Functions.function(@name)
+            unless @fname
                 raise Puppet::ParseError, "Unknown function %s" % @name
             end
 
@@ -45,6 +34,21 @@ class Puppet::Parser::AST
                 raise Puppet::DevError, "Invalid function type %s" % @ftype.inspect
             end
 
+
+
+            # We don't need to evaluate the name, because it's plaintext
+            args = @arguments.safeevaluate(scope)
+
+            return scope.send("function_" + @name, args)
+        end
+
+        def initialize(hash)
+            @ftype = hash[:ftype] || :rvalue
+            hash.delete(:ftype) if hash.include? :ftype
+
+            super(hash)
+
+             @fname = Puppet::Parser::Functions.function(@name)
             # Lastly, check the parity
         end
     end
diff --git a/spec/unit/parser/ast/function.rb b/spec/unit/parser/ast/function.rb
new file mode 100644
index 0000000..1542013
--- /dev/null
+++ b/spec/unit/parser/ast/function.rb
@@ -0,0 +1,77 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../../spec_helper'
+
+describe Puppet::Parser::AST::Function do
+    before :each do
+        @scope = mock 'scope'
+    end
+
+    describe "when initializing" do
+        it "should not fail if the function doesn't exist" do
+            Puppet::Parser::Functions.stubs(:function).returns(false)
+
+            lambda{ Puppet::Parser::AST::Function.new :name => "dontexist" }.should_not raise_error(Puppet::ParseError)
+
+        end
+    end
+
+    describe "when evaluating" do
+
+        it "should fail if the function doesn't exist" do
+            Puppet::Parser::Functions.stubs(:function).returns(false)
+            func = Puppet::Parser::AST::Function.new :name => "dontexist"
+
+            lambda{ func.evaluate(@scope) }.should raise_error(Puppet::ParseError)
+        end
+
+        it "should fail if the function is a statement used as rvalue" do
+            Puppet::Parser::Functions.stubs(:function).with("exist").returns(true)
+            Puppet::Parser::Functions.stubs(:rvalue?).with("exist").returns(false)
+
+            func = Puppet::Parser::AST::Function.new :name => "exist", :ftype => :rvalue
+
+            lambda{ func.evaluate(@scope) }.should raise_error(Puppet::ParseError, "Function 'exist' does not return a value")
+        end
+
+        it "should fail if the function is an rvalue used as statement" do
+            Puppet::Parser::Functions.stubs(:function).with("exist").returns(true)
+            Puppet::Parser::Functions.stubs(:rvalue?).with("exist").returns(true)
+
+            func = Puppet::Parser::AST::Function.new :name => "exist", :ftype => :statement
+
+            lambda{ func.evaluate(@scope) }.should raise_error(Puppet::ParseError,"Function 'exist' must be the value of a statement")
+        end
+
+        it "should evaluate its arguments" do
+            argument = stub 'arg'
+            Puppet::Parser::Functions.stubs(:function).with("exist").returns(true)
+            func = Puppet::Parser::AST::Function.new :name => "exist", :ftype => :statement, :arguments => argument
+            @scope.stubs(:function_exist)
+
+            argument.expects(:safeevaluate).with(@scope).returns("argument")
+
+            func.evaluate(@scope)
+        end
+
+        it "should call the underlying ruby function" do
+            argument = stub 'arg', :safeevaluate => "nothing"
+            Puppet::Parser::Functions.stubs(:function).with("exist").returns(true)
+            func = Puppet::Parser::AST::Function.new :name => "exist", :ftype => :statement, :arguments => argument
+
+            @scope.expects(:function_exist).with("nothing")
+
+            func.evaluate(@scope)
+        end
+
+        it "should return the ruby function return for rvalue functions" do
+            argument = stub 'arg', :safeevaluate => "nothing"
+            Puppet::Parser::Functions.stubs(:function).with("exist").returns(true)
+            func = Puppet::Parser::AST::Function.new :name => "exist", :ftype => :statement, :arguments => argument
+            @scope.stubs(:function_exist).with("nothing").returns("returning")
+
+            func.evaluate(@scope).should == "returning"
+        end
+
+    end
+end
diff --git a/test/language/functions.rb b/test/language/functions.rb
index d9a1a56..7e153ec 100755
--- a/test/language/functions.rb
+++ b/test/language/functions.rb
@@ -12,7 +12,7 @@ class TestLangFunctions < Test::Unit::TestCase
     include PuppetTest::ParserTesting
     include PuppetTest::ResourceTesting
     def test_functions
-        assert_raise(Puppet::ParseError) do
+        assert_nothing_raised do
             Puppet::Parser::AST::Function.new(
                 :name => "fakefunction",
                 :arguments => AST::ASTArray.new(
@@ -21,6 +21,16 @@ class TestLangFunctions < Test::Unit::TestCase
             )
         end
 
+        assert_raise(Puppet::ParseError) do
+            func = Puppet::Parser::AST::Function.new(
+                :name => "fakefunction",
+                :arguments => AST::ASTArray.new(
+                    :children => [nameobj("avalue")]
+                )
+            )
+            func.evaluate(mkscope)
+        end
+
         assert_nothing_raised do
             Puppet::Parser::Functions.newfunction(:fakefunction, :type => :rvalue) do |input|
                 return "output %s" % input[0]

-- 
Puppet packaging for Debian



More information about the Pkg-puppet-devel mailing list