Class: PDK::Report

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

Defined Under Namespace

Classes: Event

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_formatSymbol

Returns the method name of the default report format.

Returns:

  • (Symbol)

    the method name of the default report format.



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

def self.default_format
  :write_text
end

.default_target#write

Returns the default target to write the report to.

Returns:

  • (#write)

    the default target to write the report to.



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

def self.default_target
  $stdout
end

.formatsArray<String>

Returns the list of supported report formats.

Returns:

  • (Array<String>)

    the list of supported report formats.



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

def self.formats
  @report_formats ||= %w[junit text].freeze
end

Instance Method Details

#add_event(data) ⇒ Object

Create a new PDK::Report::Event from a hash of values and add it to the report.

Parameters:

  • data (Hash)

    (see PDK::Report::Event#initialize)



41
42
43
# File 'lib/pdk/report.rb', line 41

def add_event(data)
  (events[data[:source]] ||= []) << PDK::Report::Event.new(data)
end

#eventsHash{String=>Array<PDK::Report::Event>}

Memoised access to the report event storage hash.

The keys of the Hash are the source names of the Events (see PDK::Report::Event#source).

Examples:

accessing events from the puppet-lint validator

report = PDK::Report.new
report.events['puppet-lint']

Returns:



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

def events
  @events ||= {}
end

#write_junit(target = self.class.default_target) ⇒ Object

Renders the report as a JUnit XML document.

Parameters:

  • target (#write) (defaults to: self.class.default_target)

    an IO object that the report will be written to. Defaults to PDK::Report.default_target.



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

def write_junit(target = self.class.default_target)
  require 'rexml/document'
  require 'time'
  require 'socket'

  document = REXML::Document.new
  document << REXML::XMLDecl.new
  testsuites = REXML::Element.new('testsuites')

  id = 0
  events.each do |testsuite_name, testcases|
    testsuite = REXML::Element.new('testsuite')
    testsuite.attributes['name'] = testsuite_name
    testsuite.attributes['tests'] = testcases.length
    testsuite.attributes['errors'] = testcases.select(&:error?).length
    testsuite.attributes['failures'] = testcases.select(&:failure?).length
    testsuite.attributes['skipped'] = testcases.select(&:skipped?).length
    testsuite.attributes['time'] = 0
    testsuite.attributes['timestamp'] = Time.now.strftime('%Y-%m-%dT%H:%M:%S')
    testsuite.attributes['hostname'] = Socket.gethostname
    testsuite.attributes['id'] = id
    testsuite.attributes['package'] = testsuite_name
    testsuite.add_element('properties')
    testcases.each { |r| testsuite.elements << r.to_junit }
    testsuite.add_element('system-out')
    testsuite.add_element('system-err')

    testsuites.elements << testsuite
    id += 1
  end

  document.elements << testsuites
  report = ''
  document.write(report, 2)

  if target.is_a?(String)
    PDK::Util::Filesystem.write_file(target, report)
  else
    target << report
  end
end

#write_text(target = self.class.default_target) ⇒ Object

Renders the report as plain text.

This report is designed for interactive use by a human and so excludes all passing events in order to be consise.

Parameters:

  • target (#write) (defaults to: self.class.default_target)

    an IO object that the report will be written to. Defaults to PDK::Report.default_target.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/pdk/report.rb', line 98

def write_text(target = self.class.default_target)
  coverage_report = nil
  report = []

  events.each do |_tool, tool_events|
    tool_events.each do |event|
      if event.rspec_puppet_coverage?
        coverage_report = event.to_text
      else
        report << event.to_text unless event.pass?
      end
    end
  end

  report << "\n#{coverage_report}" if coverage_report

  if target.is_a?(String)
    PDK::Util::Filesystem.write_file(target, report.join("\n"))
  else
    target << report.join("\n")
  end
end