Class: InspecPlugins::JUnitReporter::Reporter

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run_data_schema_constraintsObject



3
4
5
# File 'lib/plugins/inspec-reporter-junit/lib/inspec-reporter-junit/reporter.rb', line 3

def self.run_data_schema_constraints
  "~> 0.0"
end

Instance Method Details

#build_profile_xml(profile) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/plugins/inspec-reporter-junit/lib/inspec-reporter-junit/reporter.rb', line 25

def build_profile_xml(profile)
  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("failed", count_profile_failed_tests(profile))
  profile_xml.add_attribute("failures", count_profile_failed_tests(profile))

  profile.controls.each do |control|
    control.results.each do |result|
      profile_xml.add(build_result_xml(profile.name, control, result))
    end
  end

  profile_xml
end

#build_result_xml(profile_name, control, result) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/plugins/inspec-reporter-junit/lib/inspec-reporter-junit/reporter.rb', line 41

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}")
  result_xml.add_attribute("target", run_data.platform.target.nil? ? "" : run_data.platform.target.to_s)
  result_xml.add_attribute("time", result.run_time)

  if result.status == "failed"
    failure_element = REXML::Element.new("failure")
    failure_element.add_attribute("message", result[:message])
    result_xml.add(failure_element)
  elsif result.status == "skipped"
    result_xml.add_element("skipped")
  end

  result_xml
end

#count_profile_failed_tests(profile) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/plugins/inspec-reporter-junit/lib/inspec-reporter-junit/reporter.rb', line 65

def count_profile_failed_tests(profile)
  profile.controls.reduce(0) do |acc, elem|
    acc + elem.results.reduce(0) do |fail_test_total, test_case|
      test_case.status == "failed" ? fail_test_total + 1 : fail_test_total
    end
  end
end

#count_profile_tests(profile) ⇒ Object



59
60
61
62
63
# File 'lib/plugins/inspec-reporter-junit/lib/inspec-reporter-junit/reporter.rb', line 59

def count_profile_tests(profile)
  profile.controls.reduce(0) do |acc, elem|
    acc + elem.results.count
  end
end

#renderObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/plugins/inspec-reporter-junit/lib/inspec-reporter-junit/reporter.rb', line 7

def render
  require "rexml/document"
  xml_output = REXML::Document.new
  xml_output.add(REXML::XMLDecl.new)

  testsuites = REXML::Element.new("testsuites")
  xml_output.add(testsuites)

  run_data.profiles.each do |profile|
    testsuites.add(build_profile_xml(profile))
  end

  formatter = REXML::Formatters::Pretty.new
  formatter.compact = true
  output(formatter.write(xml_output.xml_decl, ""))
  output(formatter.write(xml_output.root, ""))
end