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



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

def self.default_format
  :write_text
end

.default_target#write



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

def self.default_target
  $stdout
end

.formatsArray<String>



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.



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']


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.



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.



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? || event.skipped?
      end
    end
  end

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

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