File: templates_spec.rb

package info (click to toggle)
ruby-puppet-syntax 4.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 312 kB
  • sloc: ruby: 672; makefile: 6
file content (128 lines) | stat: -rw-r--r-- 3,695 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
require 'spec_helper'

describe PuppetSyntax::Templates do
  let(:subject) { PuppetSyntax::Templates.new }
  let(:conditional_warning_regex) do
    if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.6.0')
      /2: warning: found `= literal' in conditional/
    else
      /2: warning: found = in conditional/
    end
  end

  it 'expects an array of files' do
    expect { subject.check(nil) }.to raise_error(/Expected an array of files/)
  end

  it 'returns nothing from a valid file' do
    files = fixture_templates('pass.erb')
    res = subject.check(files)

    expect(res[:warnings]).to match([])
    expect(res[:errors]).to match([])
  end

  it 'ignores NameErrors from unbound variables' do
    files = fixture_templates('pass_unbound_var.erb')
    res = subject.check(files)

    expect(res[:warnings]).to match([])
    expect(res[:errors]).to match([])
  end

  it 'catches SyntaxError' do
    files = fixture_templates('fail_error.erb')
    res = subject.check(files)

    expect(res[:errors].size).to eq(1)
    expect(res[:errors][0]).to match(/2: syntax error, unexpected/)
  end

  it 'catches Ruby warnings' do
    files = fixture_templates('fail_warning.erb')
    res = subject.check(files)

    expect(res[:warnings].size).to eq(1)
    expect(res[:warnings][0]).to match(conditional_warning_regex)
  end

  it 'reads more than one valid file' do
    files = fixture_templates(['pass.erb', 'pass_unbound_var.erb'])
    res = subject.check(files)

    expect(res[:warnings]).to match([])
    expect(res[:errors]).to match([])
  end

  it 'continues after finding an error in the first file' do
    files = fixture_templates(['fail_error.erb', 'fail_warning.erb'])
    res = subject.check(files)

    expect(res[:warnings].size).to eq(1)
    expect(res[:errors].size).to eq(1)
    expect(res[:errors][0]).to match(/2: syntax error, unexpected/)
    expect(res[:warnings][0]).to match(conditional_warning_regex)
  end

  it 'ignores a TypeError' do
    files = fixture_templates('typeerror_shouldwin.erb')
    res = subject.check(files)

    expect(res[:warnings]).to match([])
    expect(res[:errors]).to match([])
  end

  it 'ignores files without .erb extension' do
    files = fixture_templates('ignore.tpl')
    res = subject.check(files)

    expect(res[:warnings]).to match([])
    expect(res[:errors]).to match([])
  end

  it 'returns nothing from a valid file' do
    files = fixture_templates('pass.epp')
    res = subject.check(files)

    expect(res[:warnings]).to match([])
    expect(res[:errors]).to match([])
  end

  it 'catches SyntaxError' do
    files = fixture_templates('fail_error.epp')
    res = subject.check(files)

    expect(res[:errors].size).to eq(1)
    expect(res[:errors][0]).to match(/This Type-Name has no effect/)
  end

  it 'reads more than one valid file' do
    files = fixture_templates(['pass.epp', 'pass_also.epp'])
    res = subject.check(files)

    expect(res[:warnings]).to match([])
    expect(res[:errors]).to match([])
  end

  it 'continues after finding an error in the first file' do
    files = fixture_templates(['fail_error.epp', 'fail_error_also.epp'])
    res = subject.check(files)

    expect(res[:errors].size).to eq(2)
    expect(res[:errors][0]).to match(/This Type-Name has no effect/)
    expect(res[:errors][1]).to match(/Syntax error at '}' \(file: \S*\/fail_error_also.epp, line: 2, column: 4\)/)
  end

  context "when the 'epp_only' options is set" do
    before do
      PuppetSyntax.epp_only = true
    end

    it 'processes an ERB as EPP and find an error' do
      files = fixture_templates('pass.erb')
      res = subject.check(files)

      expect(res[:errors].size).to eq(1)
    end
  end
end