Class: InspecPlugins::JUnitReporter::ReporterV2

Inherits:
Reporter
  • Object
show all
Defined in:
lib/plugins/inspec-reporter-junit/lib/inspec-reporter-junit/reporter.rb

Overview

This is the “Corrected” JUnit reporter. It produces XML which is intended to be valid. It should be used whenever possible.

Instance Method Summary collapse

Methods inherited from Reporter

#count_profile_errored_tests, #count_profile_failed_tests, #count_profile_skipped_tests, #count_profile_tests, #render, run_data_schema_constraints

Instance Method Details

#build_profile_xml(profile, idx) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/plugins/inspec-reporter-junit/lib/inspec-reporter-junit/reporter.rb', line 98

def build_profile_xml(profile, idx)
  profile_xml = REXML::Element.new("testsuite")
  profile_xml.add_attribute("name", profile.name)
  profile_xml.add_attribute("tests", count_profile_tests(profile))
  profile_xml.add_attribute("id", idx + 1)

  # junit2 counts failures and errors separately
  errors = count_profile_errored_tests(profile)
  profile_xml.add_attribute("errors", errors)
  profile_xml.add_attribute("failures", count_profile_failed_tests(profile) - errors)
  profile_xml.add_attribute("skipped", count_profile_skipped_tests(profile))

  profile_xml.add_attribute("hostname", run_data.platform.target.nil? ? "" : run_data.platform.target.to_s)
  # Author of the schema specified 8601, then went on to add
  # a regex that requires no TZ
  profile_xml.add_attribute("timestamp", Time.now.iso8601.slice(0, 19))

  # These are empty but are just here to satisfy the schema
  profile_xml.add_attribute("package", "")
  profile_xml.add(REXML::Element.new("properties"))

  profile_time = 0.0
  profile.controls.each do |control|
    control.results.each do |result|
      profile_time += result.run_time
      profile_xml.add(build_result_xml(profile.name, control, result))
    end
  end
  profile_xml.add_attribute("time", "%.6f" % profile_time)

  profile_xml.add(REXML::Element.new("system-out"))
  profile_xml.add(REXML::Element.new("system-err"))

  profile_xml
end

#build_result_xml(profile_name, control, result) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/plugins/inspec-reporter-junit/lib/inspec-reporter-junit/reporter.rb', line 134

def build_result_xml(profile_name, control, result)
  result_xml = REXML::Element.new("testcase")
  result_xml.add_attribute("name", result.code_desc)
  result_xml.add_attribute("classname", control.title.nil? ? "#{profile_name}.Anonymous" : "#{profile_name}.#{control.id}")

  # <Nokogiri::XML::SyntaxError: 20:0: ERROR: Element 'testcase', attribute 'time': '4.9e-05' is not a valid value of the atomic type 'xs:decimal'.
  # So, we format it.
  result_xml.add_attribute("time", "%.6f" % result.run_time)

  if result.status == "failed"
    failure_element = REXML::Element.new("failure")
    failure_element.add_attribute("message", result.message)
    failure_element.add_attribute("type", result.resource_title&.to_s || "")
    result_xml.add(failure_element)
  elsif result.status == "skipped"
    result_xml.add_element("skipped")
  end

  result_xml
end