Class: PDK::Report::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/pdk/report/event.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Event

Initailises a new PDK::Report::Event object.

Options Hash (data):

  • :file (String) — default: see #file
  • :line (Integer) — default: see #line
  • :column (Integer) — default: see #column
  • :source (String) — default: see #source
  • :message (String) — default: see #message
  • :severity (String) — default: see #severity
  • :test (String) — default: see #test
  • :state (Symbol) — default: see #state
  • :trace (Array) — default: see #trace


54
55
56
57
58
# File 'lib/pdk/report/event.rb', line 54

def initialize(data)
  sanitise_data(data).each do |key, value|
    instance_variable_set("@#{key}", value)
  end
end

Instance Attribute Details

#columnInteger (readonly)



16
17
18
# File 'lib/pdk/report/event.rb', line 16

def column
  @column
end

#fileString (readonly)



8
9
10
# File 'lib/pdk/report/event.rb', line 8

def file
  @file
end

#lineInteger (readonly)



12
13
14
# File 'lib/pdk/report/event.rb', line 12

def line
  @line
end

#messageString (readonly)



24
25
26
# File 'lib/pdk/report/event.rb', line 24

def message
  @message
end

#severityString (readonly)



28
29
30
# File 'lib/pdk/report/event.rb', line 28

def severity
  @severity
end

#sourceString (readonly)



20
21
22
# File 'lib/pdk/report/event.rb', line 20

def source
  @source
end

#stateSymbol (readonly)



35
36
37
# File 'lib/pdk/report/event.rb', line 35

def state
  @state
end

#testString (readonly)



31
32
33
# File 'lib/pdk/report/event.rb', line 31

def test
  @test
end

#traceArray (readonly)



38
39
40
# File 'lib/pdk/report/event.rb', line 38

def trace
  @trace
end

Instance Method Details

#error?Boolean

Checks if the event is the result of a test that could not complete due to an error.



71
72
73
# File 'lib/pdk/report/event.rb', line 71

def error?
  state == :error
end

#failure?Boolean

Checks if the event is the result of a failing test.



78
79
80
# File 'lib/pdk/report/event.rb', line 78

def failure?
  state == :failure
end

#pass?Boolean

Checks if the event is the result of a passing test.



63
64
65
# File 'lib/pdk/report/event.rb', line 63

def pass?
  state == :passed
end

#rspec_puppet_coverage?Boolean

Checks if the event stores the result of an rspec-puppet coverage check.

Due to the implementation details of this check, the ‘file` value for this event will always point to the coverage.rb file in rspec-puppet, making it easy to filter out.



99
100
101
102
# File 'lib/pdk/report/event.rb', line 99

def rspec_puppet_coverage?
  @rspec_puppet_coverage_pattern ||= File.join('**', 'lib', 'rspec-puppet', 'coverage.rb')
  source == 'rspec' && PDK::Util::Filesystem.fnmatch?(@rspec_puppet_coverage_pattern, PDK::Util::Filesystem.expand_path(file))
end

#skipped?Boolean

Checks if the event is the result of test that was not run. This includes pending tests (that are run but have an expected failure result).



86
87
88
# File 'lib/pdk/report/event.rb', line 86

def skipped?
  state == :skipped
end

#to_junitREXML::Element

Renders the event as a JUnit XML testcase.



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/pdk/report/event.rb', line 139

def to_junit
  require 'rexml/document'

  testcase = REXML::Element.new('testcase')
  testcase.attributes['classname'] = [source, test].compact.join('.')
  testcase.attributes['name'] = [file, line, column].compact.join(':')
  testcase.attributes['time'] = 0

  if failure?
    failure = REXML::Element.new('failure')
    failure.attributes['type'] = severity
    failure.attributes['message'] = message
    failure.text = to_text
    testcase.elements << failure
  elsif skipped?
    testcase.add_element('skipped')
  end

  testcase
end

#to_textString

Renders the event in a clang style text format.



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
# File 'lib/pdk/report/event.rb', line 107

def to_text
  return message if rspec_puppet_coverage?

  location = [file, line, column].compact.join(':')
  location = nil if location.empty?

  # TODO: maybe add trace
  if source == 'rspec'
    header = [severity, source, location, message].compact.join(': ')
    result = [header, "  #{test}"]
    context = context_lines
    unless context.nil?
      result << '  Failure/Error:'
      result.concat(context)
      result << "\n"
    end

    result.compact.join("\n")
  else
    output = ['pdk']
    output << "(#{severity.upcase}):" unless severity.nil?
    output << "#{source}:" unless source.nil?
    output << message unless message.nil?
    output << "(#{location})" unless location.nil?

    output.join(' ')
  end
end