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.

Parameters:

  • data (Hash{Symbol=>Object})

    ata [HashSymbol=>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


52
53
54
55
56
# File 'lib/pdk/report/event.rb', line 52

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

Instance Attribute Details

#columnInteger (readonly)

Returns The column number in the line of the file that the event is in reference to.

Returns:

  • (Integer)

    The column number in the line of the file that the event is in reference to.



14
15
16
# File 'lib/pdk/report/event.rb', line 14

def column
  @column
end

#fileString (readonly)

Returns The path to the file that the event is in reference to.

Returns:

  • (String)

    The path to the file that the event is in reference to.



6
7
8
# File 'lib/pdk/report/event.rb', line 6

def file
  @file
end

#lineInteger (readonly)

Returns The line number in the file that the event is in reference to.

Returns:

  • (Integer)

    The line number in the file that the event is in reference to.



10
11
12
# File 'lib/pdk/report/event.rb', line 10

def line
  @line
end

#messageString (readonly)

Returns A freeform String containing a human readable message describing the event.

Returns:

  • (String)

    A freeform String containing a human readable message describing the event.



22
23
24
# File 'lib/pdk/report/event.rb', line 22

def message
  @message
end

#severityString (readonly)

Returns The severity of the event as reported by the underlying tool.

Returns:

  • (String)

    The severity of the event as reported by the underlying tool.



26
27
28
# File 'lib/pdk/report/event.rb', line 26

def severity
  @severity
end

#sourceString (readonly)

Returns The name of the source of the event (usually the name of the validation or testing tool that generated the event).

Returns:

  • (String)

    The name of the source of the event (usually the name of the validation or testing tool that generated the event).



18
19
20
# File 'lib/pdk/report/event.rb', line 18

def source
  @source
end

#stateSymbol (readonly)

Returns The state of the event. :passed, :failure, :error, or :skipped.

Returns:

  • (Symbol)

    The state of the event. :passed, :failure, :error, or :skipped.



33
34
35
# File 'lib/pdk/report/event.rb', line 33

def state
  @state
end

#testString (readonly)

Returns The name of the test that generated the event.

Returns:

  • (String)

    The name of the test that generated the event.



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

def test
  @test
end

#traceArray (readonly)

Returns Array of full stack trace lines associated with event.

Returns:

  • (Array)

    Array of full stack trace lines associated with event



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

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.

Returns:

  • (Boolean)

    true if the test did not complete, otherwise false.



69
70
71
# File 'lib/pdk/report/event.rb', line 69

def error?
  state == :error
end

#failure?Boolean

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

Returns:

  • (Boolean)

    true if the test failed, otherwise false.



76
77
78
# File 'lib/pdk/report/event.rb', line 76

def failure?
  state == :failure
end

#pass?Boolean

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

Returns:

  • (Boolean)

    true if the test passed, otherwise false.



61
62
63
# File 'lib/pdk/report/event.rb', line 61

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.

Returns:

  • (Boolean)

    true if the event contains rspec-puppet coverage results.



97
98
99
100
# File 'lib/pdk/report/event.rb', line 97

def rspec_puppet_coverage?
  @rspec_puppet_coverage_pattern ||= File.join('**', 'lib', 'rspec-puppet', 'coverage.rb')
  source == 'rspec' && File.fnmatch?(@rspec_puppet_coverage_pattern, File.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).

Returns:

  • (Boolean)

    true if the test was skipped, otherwise false.



84
85
86
# File 'lib/pdk/report/event.rb', line 84

def skipped?
  state == :skipped
end

#to_junitREXML::Element

Renders the event as a JUnit XML testcase.

Returns:

  • (REXML::Element)

    The rendered event.



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/pdk/report/event.rb', line 131

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.

Returns:

  • (String)

    The rendered event.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/pdk/report/event.rb', line 105

def to_text
  return message if rspec_puppet_coverage?

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

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

    result.compact.join("\n")
  else
    header
  end
end