Class: Puppet::Util::Assertion::Reporter

Inherits:
Object
  • Object
show all
Includes:
Printer::Styler
Defined in:
lib/puppet/util/assertion/reporter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeReporter

Returns a new instance of Reporter.



10
11
12
13
# File 'lib/puppet/util/assertion/reporter.rb', line 10

def initialize
  @evaluated = 0
  @failed = 0
end

Instance Attribute Details

#evaluatedObject (readonly)

Returns the value of attribute evaluated.



8
9
10
# File 'lib/puppet/util/assertion/reporter.rb', line 8

def evaluated
  @evaluated
end

#failedObject (readonly)

Returns the value of attribute failed.



8
9
10
# File 'lib/puppet/util/assertion/reporter.rb', line 8

def failed
  @failed
end

Instance Method Details

#<<(assertion) ⇒ Object

Given an assertion resource, evaluate it for success, and on failure, call the appropriate method responsible to render the message, and increment the counter(s).



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/puppet/util/assertion/reporter.rb', line 18

def <<(assertion)
  count

  # If ensure is present, and the resource is not in the catalog
  if assertion[:ensure] == 'present' and not assertion[:subject].catalog
    fail
    expected_present(assertion)
    return

  # If ensure is absent, and the resource is in the catalog
  elsif assertion[:ensure] == 'absent' and assertion[:subject].catalog
    fail
    expected_absent(assertion)
    return
  end

  if assertion.provider.failed?
    fail
    inequal_value(assertion)
    return
  end

end

#countObject



126
127
128
# File 'lib/puppet/util/assertion/reporter.rb', line 126

def count
  @evaluated += 1
end

#expected_absent(assertion) ⇒ Object

Print the appropriate error message when an assertion’s subject is found in the catalog but was intended to be absent.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/puppet/util/assertion/reporter.rb', line 69

def expected_absent(assertion)
  # Shim the value of failed into the
  # local scope in order to access it
  # from the style proc.
  failed = @failed

  style do
    red      "#{failed}) Assertion #{assertion[:name]} failed on #{assertion[:subject].to_s}"
    newline
    blue     "  Subject was expected to be absent from the catalog, but was present"
    newline
    newline
  end
end

#expected_present(assertion) ⇒ Object

Print the appropriate error message when an assertion’s subject is not found in the catalog.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/puppet/util/assertion/reporter.rb', line 86

def expected_present(assertion)
  # Shim the value of failed into the
  # local scope in order to access it
  # from the style proc.
  failed = @failed

  style do
    red      "#{failed}) Assertion #{assertion[:name]} failed on #{assertion[:subject].to_s}"
    newline
    blue     "  Subject was expected to be present in the catalog, but was absent"
    newline
    newline
  end
end

#failObject



130
131
132
# File 'lib/puppet/util/assertion/reporter.rb', line 130

def fail
  @failed += 1
end

#inequal_value(assertion) ⇒ Object

Pretty print the results of an assertion to the console



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/puppet/util/assertion/reporter.rb', line 102

def inequal_value(assertion)
  # Shim the value of failed into the
  # local scope in order to access it
  # from the style proc.
  failed = @failed

  style do
    red      "#{failed}) Assertion #{assertion[:name]} failed on #{assertion[:subject].to_s}"
    newline

    yellow   "  On line #{assertion[:subject].line} of #{assertion.provider.relative_path}"
    newline

    blue     "  Wanted: "
    white    assertion.provider.wanted.to_s
    newline

    blue     "  Got:    "
    white    assertion.provider.got.to_s
    newline
    newline
  end
end


58
59
60
61
62
63
64
# File 'lib/puppet/util/assertion/reporter.rb', line 58

def print_error(err)
  fail #Mark an assertion so the application exits 1
  style do
    red err.message
    newline
  end
end

Print the summary of evaluated assertions



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/puppet/util/assertion/reporter.rb', line 43

def print_footer
  # Shim the reporter into the local scope
  reporter = self

  style do
    if reporter.evaluated == 1
      yellow "Evaluated 1 assertion"
      newline
    else
      yellow "Evaluated #{reporter.evaluated} assertions"
      newline
    end
  end
end