From 630d179fc0ff5c75b72cb9dbe8601dcc96fa2ba3 Mon Sep 17 00:00:00 2001 From: Adam Jacob Date: Mon, 12 May 2008 21:41:04 -0700 Subject: [PATCH] Adding has_variable? support, fixing ticket #1177 --- lib/puppet/parser/templatewrapper.rb | 9 +++++ spec/unit/parser/templatewrapper.rb | 57 ++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 0 deletions(-) create mode 100644 spec/unit/parser/templatewrapper.rb 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 -- 1.5.2.5