Skip to content

Commit 98dc688

Browse files
authored
Merge pull request #7 from buildkite-plugins/handle-test-errors
Report on test errors as well
2 parents 009359f + 7e58bfa commit 98dc688

File tree

8 files changed

+109
-19
lines changed

8 files changed

+109
-19
lines changed

ruby/bin/annotate

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ abort("#{junits_dir} does not exist") unless Dir.exist?(junits_dir)
1212
job_pattern = ENV['BUILDKITE_PLUGIN_JUNIT_ANNOTATE_JOB_UUID_FILE_PATTERN']
1313
job_pattern = '-(.*).xml' if !job_pattern || job_pattern.empty?
1414

15-
class Failure < Struct.new(:name, :classname, :body, :job); end
15+
class Failure < Struct.new(:name, :classname, :body, :job, :type)
16+
end
1617

1718
junit_report_files = Dir.glob(File.join(junits_dir, "**", "*"))
18-
all_failures = []
19+
failures = []
1920

2021
junit_report_files.each do |file|
2122
next if File.directory?(file)
@@ -29,31 +30,40 @@ junit_report_files.each do |file|
2930
name = testcase.attributes['name'].to_s
3031
classname = testcase.attributes['classname'].to_s
3132
testcase.elements.each("failure") do |failure|
32-
all_failures << Failure.new(name, classname, failure.text.chomp.strip, job)
33+
failures << Failure.new(name, classname, failure.text, job, :failure)
34+
end
35+
testcase.elements.each("error") do |error|
36+
failures << Failure.new(name, classname, error.text, job, :error)
3337
end
3438
end
3539
end
3640

3741
STDERR.puts "--- ❓ Checking failures"
3842

39-
if all_failures.empty?
40-
STDERR.puts "There were no failures 🙌"
43+
if failures.empty?
44+
STDERR.puts "There were no failures/errors 🙌"
4145
exit 0
4246
else
43-
STDERR.puts "There are #{all_failures.length} failures 😭"
47+
STDERR.puts "There #{failures.length == 1 ? "is 1 failure/error" : "are #{failures.length} failures/errors" } 😭"
4448
end
4549

4650
STDERR.puts "--- ✍️ Preparing annotation"
4751

48-
puts "There were #{all_failures.length} failures:\n\n"
52+
failures_count = failures.select {|f| f.type == :failure }.length
53+
errors_count = failures.select {|f| f.type == :error }.length
54+
55+
puts [
56+
failures_count == 0 ? nil : (failures_count == 1 ? "1 failure" : "#{failures_count} failures"),
57+
errors_count === 0 ? nil : (errors_count == 1 ? "1 error" : "#{errors_count} errors"),
58+
].compact.join(" and ") + ":\n\n"
4959

50-
all_failures.each do |failure|
60+
failures.each do |failure|
5161
puts "<details>"
5262
puts "<summary><code>#{failure.name} in #{failure.classname}</code></summary>\n\n"
53-
puts "<code><pre>#{failure.body}</pre></code>\n\n"
63+
puts "<code><pre>#{failure.body.chomp.strip}</pre></code>\n\n"
5464
if failure.job
5565
puts "in <a href=\"##{failure.job}\">Job ##{failure.job}</a>"
5666
end
5767
puts "</details>"
58-
puts "" unless failure == all_failures.last
68+
puts "" unless failure == failures.last
5969
end

ruby/tests/annotate_test.rb

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,77 @@
33

44
describe "Junit annotate plugin parser" do
55
it "handles no failures" do
6-
output, status = Open3.capture2e("#{__dir__}/../bin/annotate", "#{__dir__}/no-test-errors/")
6+
output, status = Open3.capture2e("#{__dir__}/../bin/annotate", "#{__dir__}/no-test-failures/")
77

88
assert_equal <<~OUTPUT, output
99
Parsing junit-1.xml
1010
Parsing junit-2.xml
1111
--- ❓ Checking failures
12-
There were no failures 🙌
12+
There were no failures/errors 🙌
1313
OUTPUT
1414

1515
assert_equal 0, status.exitstatus
1616
end
1717

1818
it "handles failures across multiple files" do
19-
output, status = Open3.capture2e("#{__dir__}/../bin/annotate", "#{__dir__}/two-test-errors/")
19+
output, status = Open3.capture2e("#{__dir__}/../bin/annotate", "#{__dir__}/two-test-failures/")
2020

2121
assert_equal <<~OUTPUT, output
2222
Parsing junit-1.xml
2323
Parsing junit-2.xml
2424
--- ❓ Checking failures
25-
There are 2 failures 😭
25+
There are 2 failures/errors 😭
2626
--- ✍️ Preparing annotation
27-
There were 2 failures:
27+
2 failures:
28+
29+
<details>
30+
<summary><code>Account#maximum_jobs_added_by_pipeline_changer returns 250 by default in spec.models.account_spec</code></summary>
31+
32+
<code><pre>Failure/Error: expect(account.maximum_jobs_added_by_pipeline_changer).to eql(250)
33+
34+
expected: 250
35+
got: 500
36+
37+
(compared using eql?)
38+
./spec/models/account_spec.rb:78:in `block (3 levels) in <top (required)>'
39+
./spec/support/database.rb:16:in `block (2 levels) in <top (required)>'
40+
./spec/support/log.rb:17:in `run'
41+
./spec/support/log.rb:66:in `block (2 levels) in <top (required)>'</pre></code>
42+
43+
in <a href="#1">Job #1</a>
44+
</details>
45+
46+
<details>
47+
<summary><code>Account#maximum_jobs_added_by_pipeline_changer returns 700 if the account is XYZ in spec.models.account_spec</code></summary>
48+
49+
<code><pre>Failure/Error: expect(account.maximum_jobs_added_by_pipeline_changer).to eql(250)
50+
51+
expected: 700
52+
got: 500
53+
54+
(compared using eql?)
55+
./spec/models/account_spec.rb:78:in `block (3 levels) in <top (required)>'
56+
./spec/support/database.rb:16:in `block (2 levels) in <top (required)>'
57+
./spec/support/log.rb:17:in `run'
58+
./spec/support/log.rb:66:in `block (2 levels) in <top (required)>'</pre></code>
59+
60+
in <a href="#2">Job #2</a>
61+
</details>
62+
OUTPUT
63+
64+
assert_equal 0, status.exitstatus
65+
end
66+
67+
it "handles failures and errors across multiple files" do
68+
output, status = Open3.capture2e("#{__dir__}/../bin/annotate", "#{__dir__}/test-failure-and-error/")
69+
70+
assert_equal <<~OUTPUT, output
71+
Parsing junit-1.xml
72+
Parsing junit-2.xml
73+
--- ❓ Checking failures
74+
There are 2 failures/errors 😭
75+
--- ✍️ Preparing annotation
76+
1 failure and 1 error:
2877
2978
<details>
3079
<summary><code>Account#maximum_jobs_added_by_pipeline_changer returns 250 by default in spec.models.account_spec</code></summary>
@@ -70,9 +119,9 @@
70119
assert_equal <<~OUTPUT, output
71120
Parsing junit-123-456-custom-pattern.xml
72121
--- ❓ Checking failures
73-
There are 1 failures 😭
122+
There is 1 failure/error 😭
74123
--- ✍️ Preparing annotation
75-
There were 1 failures:
124+
1 failure:
76125
77126
<details>
78127
<summary><code>Account#maximum_jobs_added_by_pipeline_changer returns 250 by default in spec.models.account_spec</code></summary>
@@ -102,9 +151,9 @@
102151
Parsing sub-dir/junit-1.xml
103152
Parsing sub-dir/junit-2.xml
104153
--- ❓ Checking failures
105-
There are 2 failures 😭
154+
There are 2 failures/errors 😭
106155
--- ✍️ Preparing annotation
107-
There were 2 failures:
156+
2 failures:
108157
109158
<details>
110159
<summary><code>Account#maximum_jobs_added_by_pipeline_changer returns 250 by default in spec.models.account_spec</code></summary>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<testsuite name="rspec" tests="2" skipped="0" failures="0" errors="1" time="49.290713" timestamp="2018-04-18T23:29:42+00:00" hostname="626d214475e4">
3+
<testcase classname="spec.models.account_spec" name="Account#maximum_jobs_added_by_pipeline_changer returns 500 if the account is ABC" file="./spec/models/account_spec.rb" time="0.020013"/>
4+
<testcase classname="spec.models.account_spec" name="Account#maximum_jobs_added_by_pipeline_changer returns 250 by default" file="./spec/models/account_spec.rb" time="0.967127">
5+
<error message=" expected: 250 got: 500 (compared using eql?) " type="RSpec::Expectations::ExpectationNotMetError">Failure/Error: expect(account.maximum_jobs_added_by_pipeline_changer).to eql(250)
6+
7+
expected: 250
8+
got: 500
9+
10+
(compared using eql?)
11+
./spec/models/account_spec.rb:78:in `block (3 levels) in &lt;top (required)&gt;&apos;
12+
./spec/support/database.rb:16:in `block (2 levels) in &lt;top (required)&gt;&apos;
13+
./spec/support/log.rb:17:in `run&apos;
14+
./spec/support/log.rb:66:in `block (2 levels) in &lt;top (required)&gt;&apos;</error>
15+
</testcase>
16+
</testsuite>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<testsuite name="rspec" tests="1" skipped="0" failures="1" errors="0" time="49.290713" timestamp="2018-04-18T23:29:42+00:00" hostname="626d214475e4">
3+
<testcase classname="spec.models.account_spec" name="Account#maximum_jobs_added_by_pipeline_changer returns 700 if the account is XYZ" file="./spec/models/account_spec.rb" time="0.967127">
4+
<failure message=" expected: 700 got: 500 (compared using eql?) " type="RSpec::Expectations::ExpectationNotMetError">Failure/Error: expect(account.maximum_jobs_added_by_pipeline_changer).to eql(250)
5+
6+
expected: 700
7+
got: 500
8+
9+
(compared using eql?)
10+
./spec/models/account_spec.rb:78:in `block (3 levels) in &lt;top (required)&gt;&apos;
11+
./spec/support/database.rb:16:in `block (2 levels) in &lt;top (required)&gt;&apos;
12+
./spec/support/log.rb:17:in `run&apos;
13+
./spec/support/log.rb:66:in `block (2 levels) in &lt;top (required)&gt;&apos;</failure>
14+
</testcase>
15+
</testsuite>

0 commit comments

Comments
 (0)