File: templatewrapper_spec.rb

package info (click to toggle)
puppet 5.5.22-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 21,316 kB
  • sloc: ruby: 254,925; sh: 1,608; xml: 219; makefile: 153; sql: 103
file content (100 lines) | stat: -rw-r--r-- 3,566 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
require 'spec_helper'
require 'puppet/parser/templatewrapper'

describe Puppet::Parser::TemplateWrapper do
  let(:known_resource_types) { Puppet::Resource::TypeCollection.new("env") }
  let(:scope) do
    compiler = Puppet::Parser::Compiler.new(Puppet::Node.new("mynode"))
    allow(compiler.environment).to receive(:known_resource_types).and_return(known_resource_types)
    Puppet::Parser::Scope.new compiler
  end

  let(:tw) { Puppet::Parser::TemplateWrapper.new(scope) }

  it "fails if a template cannot be found" do
    expect(Puppet::Parser::Files).to receive(:find_template).and_return(nil)

    expect { tw.file = "fake_template" }.to raise_error(Puppet::ParseError)
  end

  it "stringifies as template[<filename>] for a file based template" do
    allow(Puppet::Parser::Files).to receive(:find_template).and_return("/tmp/fake_template")
    tw.file = "fake_template"
    expect(tw.to_s).to eql("template[/tmp/fake_template]")
  end

  it "stringifies as template[inline] for a string-based template" do
    expect(tw.to_s).to eql("template[inline]")
  end

  it "reads and evaluates a file-based template" do
    given_a_template_file("fake_template", "template contents")

    tw.file = "fake_template"
    expect(tw.result).to eql("template contents")
  end

  it "provides access to the name of the template via #file" do
    full_file_name = given_a_template_file("fake_template", "<%= file %>")

    tw.file = "fake_template"
    expect(tw.result).to eq(full_file_name)
  end

  it "evaluates a given string as a template" do
    expect(tw.result("template contents")).to eql("template contents")
  end

  it "provides the defined classes with #classes" do
    catalog = double('catalog', :classes => ["class1", "class2"])
    expect(scope).to receive(:catalog).and_return(catalog)
    expect(tw.classes).to eq(["class1", "class2"])
  end

  it "provides all the tags with #all_tags" do
    catalog = double('catalog', :tags => ["tag1", "tag2"])
    expect(scope).to receive(:catalog).and_return(catalog)
    expect(tw.all_tags).to eq(["tag1","tag2"])
  end

  it "provides the tags defined in the current scope with #tags" do
    expect(scope).to receive(:tags).and_return(["tag1", "tag2"])
    expect(tw.tags).to eq(["tag1","tag2"])
  end

  it "raises error on access to removed in-scope variables via method calls" do
    scope["in_scope_variable"] = "is good"
    expect { tw.result("<%= in_scope_variable %>") }.to raise_error(/undefined local variable or method `in_scope_variable'/ )
  end

  it "reports that variable is available when it is in scope" do
    scope["in_scope_variable"] = "is good"
    expect(tw.result("<%= has_variable?('in_scope_variable') %>")).to eq("true")
  end

  it "reports that a variable is not available when it is not in scope" do
    expect(tw.result("<%= has_variable?('not_in_scope_variable') %>")).to eq("false")
  end

  it "provides access to in-scope variables via instance variables" do
    scope["one"] = "foo"
    expect(tw.result("<%= @one %>")).to eq("foo")
  end

  %w{! . ; :}.each do |badchar|
    it "translates #{badchar} to _ in instance variables" do
      scope["one#{badchar}"] = "foo"
      expect(tw.result("<%= @one_ %>")).to eq("foo")
    end
  end

  def given_a_template_file(name, contents)
    full_name = "/full/path/to/#{name}"
    allow(Puppet::Parser::Files).to receive(:find_template).
      with(name, anything()).
      and_return(full_name)
    allow(Puppet::FileSystem).to receive(:read_preserve_line_endings).with(full_name).and_return(contents)

    full_name
  end
end