Converting the junit reporter to use nokogiri on top of the json reporter output hash

This commit is contained in:
John Kerry 2017-02-02 12:18:05 -05:00 committed by jkerry
parent 9e513621fe
commit 1f92268aa2
2 changed files with 63 additions and 6 deletions

View file

@ -8,7 +8,7 @@ if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.2.2')
end
gem 'ffi', '>= 1.9.14'
gem 'rspec_junit_formatter', '~> 0.2.3'
gem 'nokogiri', '~> 1.6'
group :test do
gem 'bundler', '~> 1.5'
@ -19,7 +19,6 @@ group :test do
gem 'concurrent-ruby', '~> 0.9'
gem 'mocha', '~> 1.1'
gem 'ruby-progressbar', '~> 1.8'
gem 'nokogiri', '~> 1.6'
gem 'webmock', '~> 2.3.2'
end

View file

@ -748,13 +748,71 @@ class InspecRspecCli < InspecRspecJson # rubocop:disable Metrics/ClassLength
end
end
class InspecRspecJUnit < RSpecJUnitFormatter
class InspecRspecJUnit < InspecRspecJson
RSpec::Core::Formatters.register self, :close
def initialize(*args)
super(*args)
#
# This is the last method is invoked through the formatter interface.
# Converts the junit formatter constructed output_hash into nokogiri generated
# XML and writes it to output.
#
def close(_notification)
require 'nokogiri'
xml_output = Nokogiri::XML::Builder.new { |xml|
xml.testsuites do
@output_hash[:profiles].each do |profile|
build_profile_xml(xml, profile)
end
end
}.to_xml
output.puts xml_output
end
def close(_notification)
private
def build_profile_xml(xml, profile)
xml.testsuite(
name: profile[:name],
tests: count_profile_tests(profile),
failed: count_profile_failed_tests(profile),
) do
profile[:controls].each do |control|
build_control_xml(xml, control)
end
end
end
def build_control_xml(xml, control)
return if control[:results].nil?
control[:results].each do |result|
build_result_xml(xml, control, result)
end
end
def build_result_xml(xml, control, result)
test_class = control[:title].nil? ? 'Anonymnous' : control[:id]
xml.testcase(name: result[:code_desc], class: test_class, time: result[:run_time]) do
if result[:status] == 'failed'
xml.failure(message: result[:message])
end
end
end
def count_profile_tests(profile)
profile[:controls].reduce(0) { |acc, elem|
acc + (elem[:results].nil? ? 0 : elem[:results].count)
}
end
def count_profile_failed_tests(profile)
profile[:controls].reduce(0) { |acc, elem|
if elem[:results].nil?
acc
else
acc + elem[:results].reduce(0) { |fail_test_total, test_case|
test_case[:status] == 'failed' ? fail_test_total + 1 : fail_test_total
}
end
}
end
end