0001-Adding-has_variable-support-fixing-ticket-1177.patch

Redmine Admin, 05/13/2008 06:44 am

Download (3.9 KB)

 
b/lib/puppet/parser/templatewrapper.rb
19 19
            @scope.parser.watch_file(@file)
20 20
        end
21 21
    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
22 31

  
23 32
    # Ruby treats variables like methods, so we can cheat here and
24 33
    # trap missing vars like they were missing methods.
b/spec/unit/parser/templatewrapper.rb
1
#!/usr/bin/env ruby
2

  
3
require File.dirname(__FILE__) + '/../../spec_helper'
4

  
5
describe 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
57
end
0
-