Skip to content

Commit a87eb01

Browse files
committed
Nicer display for rspec failures
1 parent 2b778dd commit a87eb01

26 files changed

+797
-745
lines changed

Diff for: ruby/bin/annotate

+51-30
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,59 @@ junits_dir = ARGV[0]
1111
abort("Usage: annotate <junits-dir>") unless junits_dir
1212
abort("#{junits_dir} does not exist") unless Dir.exist?(junits_dir)
1313

14-
def text_content(element)
14+
class Failure < Struct.new(:element, :testcase, :testsuite)
15+
def title
16+
if rspec?
17+
testcase.attributes['classname']
18+
else
19+
"#{suite_name}##{case_name}"
20+
end
21+
end
22+
23+
def description
24+
if rspec?
25+
CGI.escapeHTML(text.chomp.strip)
26+
else
27+
"#{CGI.escapeHTML(message.chomp.strip)}\n\n#{CGI.escapeHTML(text.chomp.strip)}"
28+
end
29+
end
30+
31+
private
32+
33+
def rspec?
34+
suite_name == "rspec"
35+
end
36+
1537
# Handle mulptiple CDATA/text children elements
16-
text = element.texts().map(&:value).join.strip
17-
if text.empty?
18-
nil
19-
else
20-
text
38+
def text
39+
text = element.texts().map(&:value).join.strip
40+
if text.empty?
41+
nil
42+
else
43+
text
44+
end
2145
end
22-
end
2346

24-
def message_content(element)
2547
# Handle empty attributes
26-
message = element.attributes['message'];
27-
if message.nil? || message.empty?
28-
nil
29-
else
30-
message.to_s
48+
def message
49+
message = element.attributes['message'];
50+
if message.nil? || message.empty?
51+
nil
52+
else
53+
message.to_s
54+
end
3155
end
32-
end
3356

34-
class Failure < Struct.new(:body, :message, :type, :file, :name, :suite_name)
35-
def self.from(element, testcase, testsuite)
36-
new(
37-
text_content(element),
38-
message_content(element),
39-
element.attributes['type'],
40-
testcase.attributes['file'],
41-
testcase.attributes['name'],
42-
testsuite.attributes['name']
43-
)
57+
def file
58+
testcase.attributes['file']
59+
end
60+
61+
def case_name
62+
testcase.attributes['name']
63+
end
64+
65+
def suite_name
66+
testsuite.attributes['name']
4467
end
4568
end
4669

@@ -64,10 +87,10 @@ junit_report_files.sort.each do |file|
6487

6588
testsuite.elements.each("testcase") do |testcase|
6689
testcase.elements.each("failure") do |element|
67-
failures << Failure.from(element, testcase, testsuite)
90+
failures << Failure.new(element, testcase, testsuite)
6891
end
6992
testcase.elements.each("error") do |element|
70-
failures << Failure.from(element, testcase, testsuite)
93+
failures << Failure.new(element, testcase, testsuite)
7194
end
7295
end
7396
end
@@ -88,10 +111,8 @@ STDERR.puts "--- ✍️ Preparing annotation"
88111
puts "Ran **#{stats[:tests]}** tests in **#{stats[:time].round(2)}s**."
89112
failures.each do |failure|
90113
puts "<details>"
91-
puts "<summary><code>#{failure.suite_name}##{failure.name}</code></summary>\n\n"
92-
if failure.body
93-
puts "<pre class=\"term\"><code>#{CGI.escapeHTML(failure.message.chomp.strip)}\n\n#{CGI.escapeHTML(failure.body.chomp.strip)}</code></pre>\n\n"
94-
end
114+
puts "<summary><code>#{failure.title}</code></summary>\n\n"
115+
puts "<pre class=\"term\"><code>#{failure.description}</code></pre>\n\n"
95116
puts "</details>"
96117
puts "" unless failure == failures.last
97118
end

0 commit comments

Comments
 (0)