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

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

  # Open a File Object for IO if target is a string containing a filename or path
  target = File.open(target, 'w') if target.is_a? String

  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
  document.write(target, 2)
ensure
  target.close if target.is_a? File
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.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/pdk/report.rb', line 96

def write_text(target = self.class.default_target)
  # Open a File Object for IO if target is a string containing a filename or path
  target = File.open(target, 'w') if target.is_a? String
  coverage_report = nil

  events.each do |_tool, tool_events|
    tool_events.each do |event|
      if event.rspec_puppet_coverage?
        coverage_report = event.to_text
      else
        target.puts(event.to_text) unless event.pass?
      end
    end
  end
ensure
  target.puts "\n#{coverage_report}" if coverage_report
  target.close if target.is_a? File
end