class InspecPlugins::JUnitReporter::ReporterV2

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

Public Instance Methods

build_profile_xml(profile, idx) click to toggle source
# 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) click to toggle source
# 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