Skip to content

Commit

Permalink
Merge branch 'release' into github-actions
Browse files Browse the repository at this point in the history
  • Loading branch information
aespinosa authored Feb 12, 2024
2 parents b3c498a + c1d544b commit b528cfc
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 36 deletions.
77 changes: 47 additions & 30 deletions lib/minitest/junit.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'minitest/junit/version'
require 'minitest'
require 'builder'
require 'ox'
require 'socket'
require 'time'

Expand Down Expand Up @@ -28,38 +28,51 @@ def record(result)
end

def report
xml = Builder::XmlMarkup.new(:indent => 2)
xml.testsuite(name: 'minitest',
timestamp: @options[:timestamp],
hostname: @options[:hostname],
tests: @results.count,
skipped: @results.count { |result| result.skipped? },
failures: @results.count { |result| !result.error? && result.failure },
errors: @results.count { |result| result.error? },
time: format_time(@results.inject(0) { |a, e| a += e.time })) do
@results.each { |result| format(result, xml) }
end
@io.puts xml.target!
doc = Ox::Document.new(:version => '1.0')
instruct = Ox::Instruct.new(:xml)
instruct[:version] = '1.0'
instruct[:encoding] = 'UTF-8'
doc << instruct
testsuite = Ox::Element.new('testsuite')
testsuite['name'] = @options[:name] || 'minitest'
testsuite['timestamp'] = @options[:timestamp]
testsuite['hostname'] = @options[:hostname]
testsuite['tests'] = @results.size
testsuite['skipped'] = @results.count(&:skipped?)
testsuite['failures'] = @results.count { |result| !result.error? && result.failure }
testsuite['errors'] = @results.count(&:error?)
testsuite['time'] = format_time(@results.map(&:time).inject(0, :+))
@results.each do |result|
testsuite << format(result)
end

doc << testsuite
@io << Ox.dump(doc)
end

def format(result, parent = nil)
xml = Builder::XmlMarkup.new(:target => parent, :indent => 2)
xml.testcase classname: format_class(result),
name: format_name(result),
time: format_time(result.time),
file: result.source_location.first,
line: result.source_location.last,
assertions: result.assertions do |t|
if result.skipped?
t.skipped message: result
else
result.failures.each do |failure|
type = classify failure
xml.tag! type, format_backtrace(failure), message: failure_message(result)
end
testcase = Ox::Element.new('testcase')
testcase['classname'] = format_class(result)
testcase['name'] = format_name(result)
testcase['time'] = format_time(result.time)
testcase['file'] = relative_to_cwd(result.source_location.first)
testcase['line'] = result.source_location.last
testcase['assertions'] = result.assertions
if result.skipped?
skipped = Ox::Element.new('skipped')
skipped['message'] = result
skipped << ""
testcase << skipped
else
result.failures.each do |failure|
failure_tag = Ox::Element.new(classify(failure))
failure_tag['message'] = result
failure_tag << format_backtrace(failure)
testcase << failure_tag
end
end
xml

testcase
end

private
Expand All @@ -77,12 +90,16 @@ def working_directory
end

def failure_message(result)
"#{result.klass}##{result.name}: #{result.failure.to_s}"
"#{result.klass}##{result.name}: #{result.failure}"
end

def relative_to_cwd(path)
path.sub(working_directory, '.')
end

def format_backtrace(failure)
Minitest.filter_backtrace(failure.backtrace).map do |line|
line.gsub(working_directory, '.')
relative_to_cwd(line)
end.join("\n")
end

Expand Down
4 changes: 2 additions & 2 deletions minitest-junit.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ Gem::Specification.new do |spec|
spec.require_paths = ['lib']

spec.add_dependency 'minitest', '~> 5.11'
spec.add_dependency 'builder', '~> 3.2'
spec.add_dependency 'ox', '~> 2', '>= 2.14.2'

spec.add_development_dependency 'bundler'
spec.add_development_dependency 'nokogiri'
spec.add_development_dependency 'pry-byebug'
spec.add_development_dependency 'rake'
spec.add_development_dependency 'rake', '~> 13'
spec.add_development_dependency 'rubocop', '~> 1'
end
8 changes: 6 additions & 2 deletions test/reporter_test.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
require 'minitest/autorun'
require 'builder'
require 'stringio'
require 'time'
require 'nokogiri'

require 'minitest/junit'

class FakeTestName; end

class ReporterTest < Minitest::Test
def test_no_tests_generates_an_empty_suite
reporter = create_reporter

reporter.report

assert_match(/^<testsuite name="minitest" timestamp="[^"]+" hostname="[^"]+" tests="0" skipped="0" failures="0" errors="0" time="0.000000">\n<\/testsuite>\n$/, reporter.output)
assert_match(
%r{<?xml version="1.0" encoding="UTF-8"\?>\n<testsuite name="minitest" timestamp="[^"]+" hostname="[^"]+" tests="0" skipped="0" failures="0" errors="0" time="0.000000"\/>},
reporter.output
)
end

def test_formats_each_successful_result_with_a_formatter
Expand Down
7 changes: 5 additions & 2 deletions test/testcase_formatter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ def test_all_tests_generate_testcase_tag
test = create_test_result
reporter = create_reporter

assert_match test.name, reporter.format(test).target!
assert_match(
test.name,
reporter.format(test).attributes['name']
)
end

def test_skipped_tests_generates_skipped_tag
Expand All @@ -28,7 +31,7 @@ def test_skipped_tests_generates_skipped_tag

reporter.report

assert_match(/<skipped message="[^<>]+"\/>\n<\/testcase>\n<\/testsuite>\n/, reporter.output)
assert_match(/<skipped message="[^<>]+"\><\/skipped>\n\s+<\/testcase>\n\s*<\/testsuite>\n/, reporter.output)
end

def test_failing_tests_creates_failure_tag
Expand Down

0 comments on commit b528cfc

Please sign in to comment.