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

Brice Figureau brice-puppet at daysofwonder.com
Fri Jan 23 14:21:48 UTC 2009


The following commit has been merged in the master branch:
commit cc45c435b7f62f83f0d0cd4b952a5c05ccfaaac9
Author: Brice Figureau <brice-puppet at daysofwonder.com>
Date:   Sun Nov 23 00:01:04 2008 +0100

    Fix #1741 - refactor TemplateWrapper, test for template function

diff --git a/lib/puppet/parser/functions/template.rb b/lib/puppet/parser/functions/template.rb
index e62c3b3..2eaace1 100644
--- a/lib/puppet/parser/functions/template.rb
+++ b/lib/puppet/parser/functions/template.rb
@@ -9,10 +9,11 @@ Puppet::Parser::Functions::newfunction(:template, :type => :rvalue, :doc =>
             # Use a wrapper, so the template can't get access to the full
             # Scope object.
             debug "Retrieving template %s" % file
-            wrapper = Puppet::Parser::TemplateWrapper.new(self, file)
 
+            wrapper = Puppet::Parser::TemplateWrapper.new(self)
+            wrapper.file = file
             begin
-                wrapper.result()
+                wrapper.result
             rescue => detail
                 raise Puppet::ParseError,
                     "Failed to parse template %s: %s" %
diff --git a/lib/puppet/parser/templatewrapper.rb b/lib/puppet/parser/templatewrapper.rb
index 036f660..55c7745 100644
--- a/lib/puppet/parser/templatewrapper.rb
+++ b/lib/puppet/parser/templatewrapper.rb
@@ -1,33 +1,18 @@
 # A simple wrapper for templates, so they don't have full access to
 # the scope objects.
 class Puppet::Parser::TemplateWrapper
-    attr_accessor :scope, :file
+    attr_accessor :scope, :file, :string
     include Puppet::Util
     Puppet::Util.logmethods(self)
 
-    def initialize(scope, filename)
+    def initialize(scope)
         @__scope__ = scope
-        @__file__ = Puppet::Module::find_template(filename, scope.compiler.environment)
-
-        unless FileTest.exists?(file)
-            raise Puppet::ParseError,
-                "Could not find template %s" % file
-        end
-
-        # We'll only ever not have a parser in testing, but, eh.
-        if scope.parser
-            scope.parser.watch_file(file)
-        end
     end
 
     def scope
         @__scope__
     end
 
-    def file
-        @__file__
-    end
-
     # Should return true if a variable is defined, false if it is not
     def has_variable?(name)
         if scope.lookupvar(name.to_s, false) != :undefined
@@ -77,11 +62,34 @@ class Puppet::Parser::TemplateWrapper
         end
     end
 
-    def result
+    def file=(filename)
+        @file = Puppet::Module::find_template(filename, scope.compiler.environment)
+
+        unless FileTest.exists?(file)
+            raise Puppet::ParseError,
+                "Could not find template %s" % file
+        end
+
+        # We'll only ever not have a parser in testing, but, eh.
+        if scope.parser
+            scope.parser.watch_file(file)
+        end
+
+        @string = File.read(file)
+    end
+
+    def result(string = nil)
+        if string
+            self.string = string
+            template_source = "inline template"
+        else
+            template_source = file
+        end
+
         # Expose all the variables in our scope as instance variables of the
         # current object, making it possible to access them without conflict
         # to the regular methods.
-        benchmark(:debug, "Bound template variables for #{file}") do
+        benchmark(:debug, "Bound template variables for #{template_source}") do
             scope.to_hash.each { |name, value| 
                 if name.kind_of?(String)
                     realname = name.gsub(/[^\w]/, "_")
@@ -93,8 +101,8 @@ class Puppet::Parser::TemplateWrapper
         end
 
         result = nil
-        benchmark(:debug, "Interpolated template #{file}") do
-            template = ERB.new(File.read(file), 0, "-")
+        benchmark(:debug, "Interpolated template #{template_source}") do
+            template = ERB.new(self.string, 0, "-")
             result = template.result(binding)
         end
 
@@ -102,7 +110,7 @@ class Puppet::Parser::TemplateWrapper
     end
 
     def to_s
-        "template[%s]" % file
+        "template[%s]" % (file ? file : "inline")
     end
 end
 
diff --git a/spec/unit/parser/functions/template.rb b/spec/unit/parser/functions/template.rb
new file mode 100644
index 0000000..8fc64d0
--- /dev/null
+++ b/spec/unit/parser/functions/template.rb
@@ -0,0 +1,62 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../../spec_helper'
+
+describe "the template function" do
+
+    before :each do
+        @scope = Puppet::Parser::Scope.new()
+    end
+
+    it "should exist" do
+        Puppet::Parser::Functions.function("template").should == "function_template"
+    end
+
+    it "should create a TemplateWrapper when called" do
+        tw = stub_everything 'template_wrapper'
+
+        Puppet::Parser::TemplateWrapper.expects(:new).returns(tw)
+
+        @scope.function_template("test")
+    end
+
+    it "should give the template filename to the TemplateWrapper" do
+        tw = stub_everything 'template_wrapper'
+        Puppet::Parser::TemplateWrapper.stubs(:new).returns(tw)
+
+        tw.expects(:file=).with("test")
+
+        @scope.function_template("test")
+    end
+
+    it "should return what TemplateWrapper.result returns" do
+        tw = stub_everything 'template_wrapper'
+        Puppet::Parser::TemplateWrapper.stubs(:new).returns(tw)
+        tw.stubs(:file=).with("test")
+
+        tw.expects(:result).returns("template contents evaluated")
+
+        @scope.function_template("test").should == "template contents evaluated"
+    end
+
+    it "should concatenate template wrapper outputs for multiple templates" do
+        tw1 = stub_everything "template_wrapper1"
+        tw2 = stub_everything "template_wrapper2"
+        Puppet::Parser::TemplateWrapper.stubs(:new).returns(tw1,tw2)
+        tw1.stubs(:file=).with("1")
+        tw2.stubs(:file=).with("2")
+        tw1.stubs(:result).returns("result1")
+        tw2.stubs(:result).returns("result2")
+
+        @scope.function_template(["1","2"]).should == "result1result2"
+    end
+
+    it "should raise an error if the template raises an error" do
+        tw = stub_everything 'template_wrapper'
+        Puppet::Parser::TemplateWrapper.stubs(:new).returns(tw)
+        tw.stubs(:result).raises
+
+        lambda { @scope.function_template("1") }.should raise_error(Puppet::ParseError)
+    end
+
+end
\ No newline at end of file
diff --git a/spec/unit/parser/templatewrapper.rb b/spec/unit/parser/templatewrapper.rb
index 5327762..fd9efa8 100755
--- a/spec/unit/parser/templatewrapper.rb
+++ b/spec/unit/parser/templatewrapper.rb
@@ -10,99 +10,118 @@ describe Puppet::Parser::TemplateWrapper do
         @file = "fake_template"
         Puppet::Module.stubs(:find_template).returns("/tmp/fake_template")
         FileTest.stubs(:exists?).returns("true")
-        @tw = Puppet::Parser::TemplateWrapper.new(@scope, @file)
+        File.stubs(:read).with("/tmp/fake_template").returns("template content")
+        @tw = Puppet::Parser::TemplateWrapper.new(@scope)
     end
 
-    it "should create a new object TemplateWrapper from a scope and a file" do
+    it "should create a new object TemplateWrapper from a scope" do
+        tw = Puppet::Parser::TemplateWrapper.new(@scope)
+
+        tw.should be_a_kind_of(Puppet::Parser::TemplateWrapper)
+    end
+
+    it "should check template file existance and read its content" do
         Puppet::Module.expects(:find_template).with("fake_template", "foo").returns("/tmp/fake_template")
         FileTest.expects(:exists?).with("/tmp/fake_template").returns(true)
-        tw = Puppet::Parser::TemplateWrapper.new(@scope, @file)
-        tw.should be_a_kind_of(Puppet::Parser::TemplateWrapper)
+        File.expects(:read).with("/tmp/fake_template").returns("template content")
+
+        @tw.file = @file
     end
 
-    it "should turn into a string like template[name]" do
+    it "should turn into a string like template[name] for file based template" do
+        @tw.file = @file
         @tw.to_s.should eql("template[/tmp/fake_template]")
     end
 
+    it "should turn into a string like template[inline] for string-based template" do
+        @tw.to_s.should eql("template[inline]")
+    end
+
     it "should return the processed template contents with a call to result" do
         template_mock = mock("template", :result => "woot!")
         File.expects(:read).with("/tmp/fake_template").returns("template contents")
         ERB.expects(:new).with("template contents", 0, "-").returns(template_mock)
+
+        @tw.file = @file
         @tw.result.should eql("woot!")
     end
 
+    it "should return the processed template contents with a call to result and a string" do
+        template_mock = mock("template", :result => "woot!")
+        ERB.expects(:new).with("template contents", 0, "-").returns(template_mock)
+
+        @tw.result("template contents").should eql("woot!")
+    end
+
     it "should return the contents of a variable if called via method_missing" do
         @scope.expects(:lookupvar).with("chicken", false).returns("is good")
-        tw = Puppet::Parser::TemplateWrapper.new(@scope, @file)
+        tw = Puppet::Parser::TemplateWrapper.new(@scope)
         tw.chicken.should eql("is good")
     end
 
     it "should throw an exception if a variable is called via method_missing and it does not exist" do
         @scope.expects(:lookupvar).with("chicken", false).returns(:undefined)
-        tw = Puppet::Parser::TemplateWrapper.new(@scope, @file)
-        lambda { tw.chicken }.should raise_error(Puppet::ParseError)        
+        tw = Puppet::Parser::TemplateWrapper.new(@scope)
+        lambda { tw.chicken }.should raise_error(Puppet::ParseError)
     end
 
     it "should allow you to check whether a variable is defined with has_variable?" do
         @scope.expects(:lookupvar).with("chicken", false).returns("is good")
-        tw = Puppet::Parser::TemplateWrapper.new(@scope, @file)
+        tw = Puppet::Parser::TemplateWrapper.new(@scope)
         tw.has_variable?("chicken").should eql(true)
     end
 
     it "should allow you to check whether a variable is not defined with has_variable?" do
         @scope.expects(:lookupvar).with("chicken", false).returns(:undefined)
-        tw = Puppet::Parser::TemplateWrapper.new(@scope, @file)
+        tw = Puppet::Parser::TemplateWrapper.new(@scope)
         tw.has_variable?("chicken").should eql(false)
     end
 
     it "should allow you to retrieve the defined classes with classes" do
         catalog = mock 'catalog', :classes => ["class1", "class2"]
         @scope.expects(:catalog).returns( catalog )
-        tw = Puppet::Parser::TemplateWrapper.new(@scope, @file)
+        tw = Puppet::Parser::TemplateWrapper.new(@scope)
         tw.classes().should == ["class1", "class2"]
     end
 
     it "should allow you to retrieve all the tags with all_tags" do
         catalog = mock 'catalog', :tags => ["tag1", "tag2"]
         @scope.expects(:catalog).returns( catalog )
-        tw = Puppet::Parser::TemplateWrapper.new(@scope, @file)
+        tw = Puppet::Parser::TemplateWrapper.new(@scope)
         tw.all_tags().should == ["tag1","tag2"]
     end
 
     it "should allow you to retrieve the tags defined in the current scope" do
         @scope.expects(:tags).returns( ["tag1", "tag2"] )
-        tw = Puppet::Parser::TemplateWrapper.new(@scope, @file)
+        tw = Puppet::Parser::TemplateWrapper.new(@scope)
         tw.tags().should == ["tag1","tag2"]
     end
 
     it "should set all of the scope's variables as instance variables" do
         template_mock = mock("template", :result => "woot!")
-        File.expects(:read).with("/tmp/fake_template").returns("template contents")
         ERB.expects(:new).with("template contents", 0, "-").returns(template_mock)
 
         @scope.expects(:to_hash).returns("one" => "foo")
-        @tw.result
+        @tw.result("template contents")
 
         @tw.instance_variable_get("@one").should == "foo"
      end
 
      it "should not error out if one of the variables is a symbol" do
         template_mock = mock("template", :result => "woot!")
-        File.expects(:read).with("/tmp/fake_template").returns("template contents")
         ERB.expects(:new).with("template contents", 0, "-").returns(template_mock)
 
         @scope.expects(:to_hash).returns(:_timestamp => "1234")
-        @tw.result
+        @tw.result("template contents")
      end
 
      %w{! . ; :}.each do |badchar|
        it "should translate #{badchar} to _ when setting the instance variables" do
         template_mock = mock("template", :result => "woot!")
-        File.expects(:read).with("/tmp/fake_template").returns("template contents")
         ERB.expects(:new).with("template contents", 0, "-").returns(template_mock)
 
         @scope.expects(:to_hash).returns("one#{badchar}" => "foo")
-        @tw.result
+        @tw.result("template contents")
 
         @tw.instance_variable_get("@one_").should == "foo"
       end

-- 
Puppet packaging for Debian



More information about the Pkg-puppet-devel mailing list