[Pkg-puppet-devel] [SCM] Puppet packaging for Debian branch, master, updated. 90c4a6028cf6ebd530af736ce08f1b413698b162

Micah Anderson micah at riseup.net
Sat May 31 17:15:32 UTC 2008


The following commit has been merged in the master branch:
commit 15987ee486ed9643146a75cd575edfacaea08972
Author: Adam Jacob <adam at hjksolutions.com>
Date:   Mon May 12 21:41:04 2008 -0700

    Adding has_variable? support, fixing ticket #1177

diff --git a/lib/puppet/parser/templatewrapper.rb b/lib/puppet/parser/templatewrapper.rb
index 7a8f741..4f044be 100644
--- a/lib/puppet/parser/templatewrapper.rb
+++ b/lib/puppet/parser/templatewrapper.rb
@@ -19,6 +19,15 @@ class Puppet::Parser::TemplateWrapper
             @scope.parser.watch_file(@file)
         end
     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
+        true
+      else
+        false
+      end
+    end
 
     # Ruby treats variables like methods, so we can cheat here and
     # trap missing vars like they were missing methods.
diff --git a/spec/unit/parser/templatewrapper.rb b/spec/unit/parser/templatewrapper.rb
new file mode 100644
index 0000000..76fed39
--- /dev/null
+++ b/spec/unit/parser/templatewrapper.rb
@@ -0,0 +1,57 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../spec_helper'
+
+describe Puppet::Parser::TemplateWrapper do
+  before(:each) do
+    compiler = stub('compiler', :environment => "foo")
+    parser = stub('parser', :watch_file => true)
+    @scope = stub('scope', :compiler => compiler, :parser => parser)
+    @file = "fake_template"
+    Puppet::Module.stubs(:find_template).returns("/tmp/fake_template")
+    FileTest.stubs(:exists?).returns("true")
+    @tw = Puppet::Parser::TemplateWrapper.new(@scope, @file)
+  end
+  
+  it "should create a new object TemplateWrapper from a scope and a file" 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)
+  end
+  
+  it "should turn in to a string like template[name]" do
+    @tw.to_s.should eql("template[/tmp/fake_template]")
+  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.result.should eql("woot!")
+  end
+  
+  it "should return the contents of a variable if called as via method_missing" do
+    @scope.expects(:lookupvar).with("chicken", false).returns("is good")
+    tw = Puppet::Parser::TemplateWrapper.new(@scope, @file)
+    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)    
+  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.has_variable?("chicken").should eql(true)
+  end
+  
+  it "should allow you to check whether a variable is not defiend with has_variable?" do
+    @scope.expects(:lookupvar).with("chicken", false).returns(:undefined)
+    tw = Puppet::Parser::TemplateWrapper.new(@scope, @file)
+    tw.has_variable?("chicken").should eql(false)
+  end
+end
\ No newline at end of file

-- 
Puppet packaging for Debian



More information about the Pkg-puppet-devel mailing list