Puppet: System Administration Automated

Puppet Training Schedule
Next Class July 27-29
New York, New York
Discount before July 1st

Ticket #1177: 0001-Adding-has_variable-support-fixing-ticket-1177.patch

File 0001-Adding-has_variable-support-fixing-ticket-1177.patch, 3.9 kB (added by adamhjk, 1 year ago)

Adds has_variable?() support to Puppet::Parser::Template Wrapper?

  • a/lib/puppet/parser/templatewrapper.rb

    old new  
    1919            @scope.parser.watch_file(@file) 
    2020        end 
    2121    end 
     22     
     23    # Should return true if a variable is defined, false if it is not 
     24    def has_variable?(name) 
     25      if @scope.lookupvar(name.to_s, false) != :undefined 
     26        true 
     27      else 
     28        false 
     29      end 
     30    end 
    2231 
    2332    # Ruby treats variables like methods, so we can cheat here and 
    2433    # trap missing vars like they were missing methods. 
  • /dev/null

    old new  
     1#!/usr/bin/env ruby 
     2 
     3require File.dirname(__FILE__) + '/../../spec_helper' 
     4 
     5describe Puppet::Parser::TemplateWrapper do 
     6  before(:each) do 
     7    compiler = stub('compiler', :environment => "foo") 
     8    parser = stub('parser', :watch_file => true) 
     9    @scope = stub('scope', :compiler => compiler, :parser => parser) 
     10    @file = "fake_template" 
     11    Puppet::Module.stubs(:find_template).returns("/tmp/fake_template") 
     12    FileTest.stubs(:exists?).returns("true") 
     13    @tw = Puppet::Parser::TemplateWrapper.new(@scope, @file) 
     14  end 
     15   
     16  it "should create a new object TemplateWrapper from a scope and a file" do 
     17    Puppet::Module.expects(:find_template).with("fake_template", "foo").returns("/tmp/fake_template") 
     18    FileTest.expects(:exists?).with("/tmp/fake_template").returns(true) 
     19    tw = Puppet::Parser::TemplateWrapper.new(@scope, @file) 
     20    tw.should be_a_kind_of(Puppet::Parser::TemplateWrapper) 
     21  end 
     22   
     23  it "should turn in to a string like template[name]" do 
     24    @tw.to_s.should eql("template[/tmp/fake_template]") 
     25  end 
     26   
     27  it "should return the processed template contents with a call to result" do 
     28    template_mock = mock("template", :result => "woot!") 
     29    File.expects(:read).with("/tmp/fake_template").returns("template contents") 
     30    ERB.expects(:new).with("template contents", 0, "-").returns(template_mock) 
     31    @tw.result.should eql("woot!") 
     32  end 
     33   
     34  it "should return the contents of a variable if called as via method_missing" do 
     35    @scope.expects(:lookupvar).with("chicken", false).returns("is good") 
     36    tw = Puppet::Parser::TemplateWrapper.new(@scope, @file) 
     37    tw.chicken.should eql("is good") 
     38  end 
     39   
     40  it "should throw an exception if a variable is called via method_missing and it does not exist" do 
     41    @scope.expects(:lookupvar).with("chicken", false).returns(:undefined) 
     42    tw = Puppet::Parser::TemplateWrapper.new(@scope, @file) 
     43    lambda { tw.chicken }.should raise_error(Puppet::ParseError)     
     44  end 
     45   
     46  it "should allow you to check whether a variable is defined with has_variable?" do 
     47    @scope.expects(:lookupvar).with("chicken", false).returns("is good") 
     48    tw = Puppet::Parser::TemplateWrapper.new(@scope, @file) 
     49    tw.has_variable?("chicken").should eql(true) 
     50  end 
     51   
     52  it "should allow you to check whether a variable is not defiend with has_variable?" do 
     53    @scope.expects(:lookupvar).with("chicken", false).returns(:undefined) 
     54    tw = Puppet::Parser::TemplateWrapper.new(@scope, @file) 
     55    tw.has_variable?("chicken").should eql(false) 
     56  end 
     57end