Debian Codegraph @

lib/minitest/reporters/junit_reporter.rb

raw
1 # frozen_string_literal: true
2
3 require 'builder'
4 require 'fileutils'
5 require 'pathname'
6 require 'time'
7
8 module Minitest
9 module Reporters
10 # A reporter for writing JUnit test reports
11 # Intended for easy integration with CI servers - tested on JetBrains TeamCity
12 #
13 # Inspired by ci_reporter (see https://github.com/nicksieger/ci_reporter)
14 # Also inspired by Marc Seeger's attempt at producing a JUnitReporter (see https://github.com/rb2k/minitest-reporters/commit/e13d95b5f884453a9c77f62bc5cba3fa1df30ef5)
15 # Also inspired by minitest-ci (see https://github.com/bhenderson/minitest-ci)
16 class JUnitReporter < BaseReporter
17 DEFAULT_REPORTS_DIR = "test/reports"
18
19 attr_reader :reports_path
20
21 def initialize(reports_dir = DEFAULT_REPORTS_DIR, empty = true, options = {})
22 super({})
23 @reports_path = File.absolute_path(ENV.fetch("MINITEST_REPORTERS_REPORTS_DIR", reports_dir))
24 @single_file = options[:single_file]
25 @base_path = options[:base_path] || Dir.pwd
26 @timestamp_report = options[:include_timestamp]
27
28 return unless empty
29
30 puts "Emptying #{@reports_path}"
31 FileUtils.mkdir_p(@reports_path)
32 File.delete(*Dir.glob("#{@reports_path}/TEST-*.xml"))
33 end
34
35 def report
36 super
37
38 puts "Writing XML reports to #{@reports_path}"
39 suites = tests.group_by do |test|
40 test_class(test)
41 end
42
43 if @single_file
44 xml = Builder::XmlMarkup.new(:indent => 2)
45 xml.instruct!
46 xml.testsuites do
47 suites.each do |suite, tests|
48 parse_xml_for(xml, suite, tests)
49 end
50 end
51 File.open(filename_for('minitest'), "w") { |file| file << xml.target! }
52 else
53 suites.each do |suite, tests|
54 xml = Builder::XmlMarkup.new(:indent => 2)
55 xml.instruct!
56 xml.testsuites do
57 parse_xml_for(xml, suite, tests)
58 end
59 File.open(filename_for(suite), "w") { |file| file << xml.target! }
60 end
61 end
62 end
63
64 def get_relative_path(result)
65 file_path = Pathname.new(get_source_location(result).first)
66 base_path = Pathname.new(@base_path)
67
68 if file_path.absolute?
69 file_path.relative_path_from(base_path)
70 else
71 file_path
72 end
73 end
74
75 private
76
77 def get_source_location(result)
78 if result.respond_to? :source_location
79 result.source_location
80 else
81 result.method(result.name).source_location
82 end
83 end
84
85 def parse_xml_for(xml, suite, tests)
86 suite_result = analyze_suite(tests)
87 file_path = get_relative_path(tests.first)
88
89 testsuite_attributes = {
90 :name => suite, :filepath => file_path, :skipped => suite_result[:skip_count],
91 :failures => suite_result[:fail_count], :errors => suite_result[:error_count],
92 :tests => suite_result[:test_count], :assertions => suite_result[:assertion_count],
93 :time => suite_result[:time]
94 }
95 testsuite_attributes[:timestamp] = suite_result[:timestamp] if @timestamp_report
96
97 xml.testsuite(testsuite_attributes) do
98 tests.each do |test|
99 lineno = get_source_location(test).last
100 xml.testcase(
101 :name => test.name, :lineno => lineno, :classname => suite,
102 :assertions => test.assertions, :time => test.time, :file => file_path
103 ) do
104 xml << xml_message_for(test) unless test.passed?
105 xml << xml_attachment_for(test) if test.respond_to?('metadata') && test.metadata[:failure_screenshot_path]
106 end
107 end
108 end
109 end
110
111 def xml_message_for(test)
112 # This is a trick lifted from ci_reporter
113 xml = Builder::XmlMarkup.new(:indent => 2, :margin => 2)
114
115 def xml.trunc!(txt)
116 txt.sub(/\n.*/m, '...')
117 end
118
119 failure = test.failure
120
121 if test.skipped?
122 xml.skipped(:type => failure.error.class.name)
123 elsif test.error?
124 xml.error(:type => failure.error.class.name, :message => xml.trunc!(failure.message)) do
125 xml.text!(message_for(test))
126 end
127 elsif failure
128 xml.failure(:type => failure.error.class.name, :message => xml.trunc!(failure.message)) do
129 xml.text!(message_for(test))
130 end
131 end
132 end
133
134 def message_for(test)
135 suite = test.class
136 name = test.name
137 e = test.failure
138
139 if test.passed?
140 nil
141 elsif test.skipped?
142 "Skipped:\n#{name}(#{suite}) [#{location(e)}]:\n#{e.message}\n"
143 elsif test.failure
144 "Failure:\n#{name}(#{suite}) [#{location(e)}]:\n#{e.message}\n"
145 elsif test.error?
146 "Error:\n#{name}(#{suite}):\n#{e.message}"
147 end
148 end
149
150 def xml_attachment_for(test)
151 xml = Builder::XmlMarkup.new(:indent => 2, :margin => 2)
152
153 xml.tag!('system-out', "[[ATTACHMENT|#{test.metadata[:failure_screenshot_path]}]]")
154 end
155
156 def location(exception)
157 last_before_assertion = ''
158 exception.backtrace.reverse_each do |s|
159 break if s =~ /in .(assert|refute|flunk|pass|fail|raise|must|wont)/
160
161 last_before_assertion = s
162 end
163 last_before_assertion.sub(/:in .*$/, '')
164 end
165
166 def analyze_suite(tests)
167 result = Hash.new(0)
168 result[:time] = 0
169 tests.each do |test|
170 result[:"#{result(test)}_count"] += 1
171 result[:assertion_count] += test.assertions
172 result[:test_count] += 1
173 result[:time] += test.time
174 result[:timestamp] = Time.now.iso8601 if @timestamp_report
175 end
176 result
177 end
178
179 def filename_for(suite)
180 file_counter = 0
181 # restrict max filename length, to be kind to filesystems
182 suite_name = suite.to_s[0..240].gsub(/[^a-zA-Z0-9]+/, '-')
183 filename = "TEST-#{suite_name}.xml"
184 while File.exist?(File.join(@reports_path, filename)) # restrict number of tries, to avoid infinite loops
185 file_counter += 1
186 filename = "TEST-#{suite_name}-#{file_counter}.xml"
187 if file_counter >= 99
188 puts "Too many duplicate files, overwriting earlier report #{filename}"
189 break
190 end
191 end
192 File.join(@reports_path, filename)
193 end
194 end
195 end
196 end