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



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

def self.default_format
  :write_text
end

.default_target#write



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

def self.default_target
  $stdout
end

.formatsArray<String>



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

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.



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

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


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

def events
  @events ||= {}
end

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

Renders the report as a JUnit XML document.



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

def write_junit(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

  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.



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

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