-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
1,035 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,86 @@ | ||
require 'rexml/document' | ||
require 'time' | ||
require 'pdk/report/event' | ||
|
||
module PDK | ||
class Report | ||
attr_reader :format | ||
attr_reader :path | ||
|
||
def initialize(path, format = nil) | ||
@path = path | ||
@format = format || self.class.default_format | ||
end | ||
|
||
# @return [Array<String>] the list of supported report formats. | ||
def self.formats | ||
@report_formats ||= %w[junit text].freeze | ||
end | ||
|
||
# @return [Symbol] the method name of the default report format. | ||
def self.default_format | ||
'text' | ||
:to_text | ||
end | ||
|
||
# @return [#write] the default target to write the report to. | ||
def self.default_target | ||
'stdout' # TODO: actually write to stdout | ||
$stdout | ||
end | ||
|
||
def write(text) | ||
if @format == 'junit' | ||
report = prepare_junit(text) | ||
elsif @format == 'text' | ||
report = prepare_text(text) | ||
end | ||
# 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). | ||
# | ||
# @example accessing events from the puppet-lint validator | ||
# report = PDK::Report.new | ||
# report.events['puppet-lint'] | ||
# | ||
# @return [Hash{String=>Array<PDK::Report::Event>}] the events stored in | ||
# the repuort. | ||
def events | ||
@events ||= {} | ||
end | ||
|
||
File.open(@path, 'a') { |f| f.write(report) } | ||
# Create a new PDK::Report::Event from a hash of values and add it to the | ||
# report. | ||
# | ||
# @param data [Hash] (see PDK::Report::Event#initialize) | ||
def add_event(data) | ||
(events[data[:source]] ||= []) << PDK::Report::Event.new(data) | ||
end | ||
|
||
def prepare_junit(text) | ||
"junit: #{text}" | ||
# Renders the report as a JUnit XML document. | ||
# | ||
# @param target [#write] an IO object that the report will be written to. | ||
# Defaults to PDK::Report.default_target. | ||
def to_junit(target = self.class.default_target) | ||
document = REXML::Document.new | ||
document << REXML::XMLDecl.new | ||
testsuites = REXML::Element.new('testsuites') | ||
|
||
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['time'] = 0 | ||
testsuite.attributes['timestamp'] = Time.now.xmlschema | ||
testcases.each { |r| testsuite.elements << r.to_junit } | ||
|
||
testsuites.elements << testsuite | ||
end | ||
|
||
document.elements << testsuites | ||
document.write(target, 2) | ||
end | ||
|
||
def prepare_text(text) | ||
"text: #{text}" | ||
# 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. | ||
# | ||
# @param target [#write] an IO object that the report will be written to. | ||
# Defaults to PDK::Report.default_target. | ||
def to_text(target = self.class.default_target) | ||
events.each do |_tool, tool_events| | ||
tool_events.each do |event| | ||
target.puts(event.to_text) unless event.pass? | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.