summaryrefslogtreecommitdiff
path: root/spec/unit/pops/issues_spec.rb
blob: 82dce1b448291459a7c81078b91f0bc7ac6c92c2 (plain)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#! /usr/bin/env ruby
require 'spec_helper'
require 'puppet/pops'

describe "Puppet::Pops::Issues" do
  include Puppet::Pops::Issues

  it "should have an issue called NAME_WITH_HYPHEN" do
    x = Puppet::Pops::Issues::NAME_WITH_HYPHEN
    x.class.should == Puppet::Pops::Issues::Issue
    x.issue_code.should == :NAME_WITH_HYPHEN
  end

  it "should should format a message that requires an argument" do
    x = Puppet::Pops::Issues::NAME_WITH_HYPHEN
    x.format(:name => 'Boo-Hoo',
      :label => Puppet::Pops::Model::ModelLabelProvider.new,
      :semantic => "dummy"
      ).should == "A String may not have a name containing a hyphen. The name 'Boo-Hoo' is not legal"
  end

  it "should should format a message that does not require an argument" do
    x = Puppet::Pops::Issues::NOT_TOP_LEVEL
    x.format().should == "Classes, definitions, and nodes may only appear at toplevel or inside other classes"
  end

end

describe "Puppet::Pops::IssueReporter" do

  let(:acceptor) { Puppet::Pops::Validation::Acceptor.new }

  def fake_positioned(number)
    stub("positioned_#{number}", :line => number, :pos => number)
  end

  def diagnostic(severity,  number)
    Puppet::Pops::Validation::Diagnostic.new(
      severity,
      Puppet::Pops::Issues::Issue.new(number) { "#{severity}#{number}" },
      "#{severity}file",
      fake_positioned(number))
  end

  def warning(number)
    diagnostic(:warning, number)
  end

  def deprecation(number)
    diagnostic(:deprecation, number)
  end

  def error(number)
    diagnostic(:error, number)
  end

  context "given warnings" do

    before(:each) do
      acceptor.accept( warning(1) )
      acceptor.accept( deprecation(1) )
    end

    it "emits warnings if told to emit them" do
      Puppet.expects(:warning).twice.with(regexp_matches(/warning1|deprecation1/))
      Puppet::Pops::IssueReporter.assert_and_report(acceptor, { :emit_warnings => true })
    end

    it "does not emit warnings if not told to emit them" do
      Puppet.expects(:warning).never
      Puppet::Pops::IssueReporter.assert_and_report(acceptor, {})
    end

    it "emits no warnings if :max_warnings is 0" do
      acceptor.accept( warning(2) )
      Puppet[:max_warnings] = 0
      Puppet.expects(:warning).once.with(regexp_matches(/deprecation1/))
      Puppet::Pops::IssueReporter.assert_and_report(acceptor, { :emit_warnings => true })
    end

    it "emits no more than 1 warning if :max_warnings is 1" do
      acceptor.accept( warning(2) )
      acceptor.accept( warning(3) )
      Puppet[:max_warnings] = 1
      Puppet.expects(:warning).twice.with(regexp_matches(/warning1|deprecation1/))
      Puppet::Pops::IssueReporter.assert_and_report(acceptor, { :emit_warnings => true })
    end

    it "does not emit more deprecations warnings than the max deprecation warnings" do
      acceptor.accept( deprecation(2) )
      Puppet[:max_deprecations] = 0
      Puppet.expects(:warning).once.with(regexp_matches(/warning1/))
      Puppet::Pops::IssueReporter.assert_and_report(acceptor, { :emit_warnings => true })
    end

    it "does not emit deprecation warnings, but does emit regular warnings if disable_warnings includes deprecations" do
      Puppet[:disable_warnings] = 'deprecations'
      Puppet.expects(:warning).once.with(regexp_matches(/warning1/))
      Puppet::Pops::IssueReporter.assert_and_report(acceptor, { :emit_warnings => true })
    end
  end

  context "given errors" do
    it "logs nothing, but raises the given :message if :emit_errors is repressing error logging" do
      acceptor.accept( error(1) )
      Puppet.expects(:err).never
      expect do
        Puppet::Pops::IssueReporter.assert_and_report(acceptor, { :emit_errors => false, :message => 'special'})
      end.to raise_error(Puppet::ParseError, 'special')
    end

    it "prefixes :message if a single error is raised" do
      acceptor.accept( error(1) )
      Puppet.expects(:err).never
      expect do
        Puppet::Pops::IssueReporter.assert_and_report(acceptor, { :message => 'special'})
      end.to raise_error(Puppet::ParseError, /special error1/)
    end

    it "logs nothing and raises immediately if there is only one error" do
      acceptor.accept( error(1) )
      Puppet.expects(:err).never
      expect do
        Puppet::Pops::IssueReporter.assert_and_report(acceptor, { })
      end.to raise_error(Puppet::ParseError, /error1/)
    end

    it "logs nothing and raises immediately if there are multiple errors but max_errors is 0" do
      acceptor.accept( error(1) )
      acceptor.accept( error(2) )
      Puppet[:max_errors] = 0
      Puppet.expects(:err).never
      expect do
        Puppet::Pops::IssueReporter.assert_and_report(acceptor, { })
      end.to raise_error(Puppet::ParseError, /error1/)
    end

    it "logs the :message if there is more than one allowed error" do
      acceptor.accept( error(1) )
      acceptor.accept( error(2) )
      Puppet.expects(:err).times(3).with(regexp_matches(/error1|error2|special/))
      expect do
        Puppet::Pops::IssueReporter.assert_and_report(acceptor, { :message => 'special'})
      end.to raise_error(Puppet::ParseError, /Giving up/)
    end

    it "emits accumulated errors before raising a 'giving up' message if there are more errors than allowed" do
      acceptor.accept( error(1) )
      acceptor.accept( error(2) )
      acceptor.accept( error(3) )
      Puppet[:max_errors] = 2
      Puppet.expects(:err).times(2).with(regexp_matches(/error1|error2/))
      expect do
        Puppet::Pops::IssueReporter.assert_and_report(acceptor, { })
      end.to raise_error(Puppet::ParseError, /3 errors.*Giving up/)
    end

    it "emits accumulated errors before raising a 'giving up' message if there are multiple errors but fewer than limits" do
      acceptor.accept( error(1) )
      acceptor.accept( error(2) )
      acceptor.accept( error(3) )
      Puppet[:max_errors] = 4
      Puppet.expects(:err).times(3).with(regexp_matches(/error[123]/))
      expect do
        Puppet::Pops::IssueReporter.assert_and_report(acceptor, { })
      end.to raise_error(Puppet::ParseError, /3 errors.*Giving up/)
    end

    it "emits errors regardless of disable_warnings setting" do
      acceptor.accept( error(1) )
      acceptor.accept( error(2) )
      Puppet[:disable_warnings] = 'deprecations'
      Puppet.expects(:err).times(2).with(regexp_matches(/error1|error2/))
      expect do
        Puppet::Pops::IssueReporter.assert_and_report(acceptor, { })
      end.to raise_error(Puppet::ParseError, /Giving up/)
    end
  end

  context "given both" do

    it "logs warnings and errors" do
      acceptor.accept( warning(1) )
      acceptor.accept( error(1) )
      acceptor.accept( error(2) )
      acceptor.accept( error(3) )
      acceptor.accept( deprecation(1) )
      Puppet[:max_errors] = 2
      Puppet.expects(:warning).twice.with(regexp_matches(/warning1|deprecation1/))
      Puppet.expects(:err).times(2).with(regexp_matches(/error[123]/))
      expect do
        Puppet::Pops::IssueReporter.assert_and_report(acceptor, { :emit_warnings => true })
      end.to raise_error(Puppet::ParseError, /3 errors.*2 warnings.*Giving up/)
    end
  end
end