Skip to content

Commit 654e780

Browse files
authored
Merge pull request #174 from buildkite-plugins/toote_summary_issue_136
Test summary
2 parents 574131b + 9070da9 commit 654e780

File tree

7 files changed

+229
-104
lines changed

7 files changed

+229
-104
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ The artifact glob path to find the JUnit XML files.
2626

2727
Example: `tmp/junit-*.xml`
2828

29+
### `always-annotate` (optional, boolean)
30+
31+
Forces the creation of the annotation even when no failures or errors are found
32+
2933
### `job-uuid-file-pattern` (optional)
3034
Default: `-(.*).xml`
3135

hooks/command

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ annotation_dir="$(pwd)/$(mktemp -d "junit-annotate-plugin-annotation-tmp.XXXXXXX
1515
annotation_path="${annotation_dir}/annotation.md"
1616
annotation_style="info"
1717
fail_build=0
18+
has_errors=0
19+
create_annotation=0
1820

1921
function cleanup {
2022
rm -rf "${artifacts_dir}"
@@ -54,8 +56,11 @@ exit_code=$?
5456
set -e
5557

5658
if [[ $exit_code -eq 64 ]]; then # special exit code to signal test failures
59+
has_errors=1
60+
create_annotation=1
5761
annotation_style="error"
5862
if [[ "${BUILDKITE_PLUGIN_JUNIT_ANNOTATE_FAIL_BUILD_ON_ERROR:-false}" =~ (true|on|1) ]]; then
63+
echo "--- :boom: Build will fail due to errors being found"
5964
fail_build=1
6065
fi
6166
elif [[ $exit_code -ne 0 ]]; then
@@ -65,21 +70,33 @@ fi
6570

6671
cat "$annotation_path"
6772

68-
if grep -q "<details>" "$annotation_path"; then
73+
if [ $has_errors -eq 0 ]; then
74+
# done in nested if to simplify outer conditions
75+
if [[ "${BUILDKITE_PLUGIN_JUNIT_ANNOTATE_ALWAYS_ANNOTATE:-false}" =~ (true|on|1) ]]; then
76+
echo "Will create annotation anyways"
77+
create_annotation=1
78+
fi
79+
elif ! check_size; then
80+
echo "--- :warning: Failures too large to annotate"
81+
82+
# creating a simplified version of the annotation
83+
mv "${annotation_path}" "${annotation_path}2"
84+
head -4 "${annotation_path}2" >"${annotation_path}"
85+
# || true is to avoid issues if no summary is found
86+
grep '<summary>' "${annotation_path}2" >>"${annotation_path}" || true
87+
6988
if ! check_size; then
70-
echo "--- :warning: Failures too large to annotate"
71-
msg="The failures are too large to create a build annotation. Please inspect the failed JUnit artifacts manually."
72-
echo "$msg"
89+
echo "The failures are too large to create a build annotation. Please inspect the failed JUnit artifacts manually."
90+
create_annotation=0
7391
else
74-
echo "--- :buildkite: Creating annotation"
75-
# shellcheck disable=SC2002
76-
cat "$annotation_path" | buildkite-agent annotate --context "${BUILDKITE_PLUGIN_JUNIT_ANNOTATE_CONTEXT:-junit}" --style "$annotation_style"
92+
echo "The failures are too large to create complete annotation, using a simplified annotation"
7793
fi
7894
fi
7995

80-
if ((fail_build)); then
81-
echo "--- :boom: Failing build due to error"
82-
exit 1
83-
else
84-
exit 0
96+
if [ $create_annotation -ne 0 ]; then
97+
echo "--- :buildkite: Creating annotation"
98+
# shellcheck disable=SC2002
99+
cat "$annotation_path" | buildkite-agent annotate --context "${BUILDKITE_PLUGIN_JUNIT_ANNOTATE_CONTEXT:-junit}" --style "$annotation_style"
85100
fi
101+
102+
exit $fail_build

plugin.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ configuration:
77
properties:
88
artifacts:
99
type: string
10-
job-uuid-file-pattern:
10+
always-annotate:
11+
type: boolean
12+
context:
1113
type: string
1214
failure-format:
1315
type: string
@@ -16,7 +18,7 @@ configuration:
1618
- file
1719
fail-build-on-error:
1820
type: boolean
19-
context:
21+
job-uuid-file-pattern:
2022
type: string
2123
report-slowest:
2224
type: integer

ruby/bin/annotate

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -74,41 +74,33 @@ junit_report_files.sort.each do |file|
7474
end
7575

7676
STDERR.puts "--- ✍️ Preparing annotation"
77-
STDERR.puts "#{testcases} testcases found"
78-
79-
if failures.any?
80-
STDERR.puts "There #{failures.length == 1 ? "is 1 failure/error" : "are #{failures.length} failures/errors" } 😭"
81-
82-
failures_count = failures.select {|f| f.type == :failure }.length
83-
errors_count = failures.select {|f| f.type == :error }.length
84-
puts [
85-
failures_count == 0 ? nil : (failures_count == 1 ? "1 failure" : "#{failures_count} failures"),
86-
errors_count === 0 ? nil : (errors_count == 1 ? "1 error" : "#{errors_count} errors"),
87-
].compact.join(" and ") + ":\n\n"
88-
89-
failures.each do |failure|
90-
puts "<details>"
91-
puts "<summary><code>#{failure.name} in #{failure.unit_name}</code></summary>\n\n"
92-
if failure.message
93-
puts "<p>#{failure.message.chomp.strip}</p>\n\n"
94-
end
95-
if failure.body
96-
puts "<pre><code>#{CGI.escapeHTML(failure.body.chomp.strip)}</code></pre>\n\n"
97-
end
98-
if failure.job
99-
puts "in <a href=\"##{failure.job}\">Job ##{failure.job}</a>"
100-
end
101-
puts "</details>"
102-
puts "" unless failure == failures.last
103-
end
10477

105-
else
106-
STDERR.puts "There were no failures/errors 🙌"
78+
failures_count = failures.select {|f| f.type == :failure }.length
79+
errors_count = failures.select {|f| f.type == :error }.length
80+
81+
puts "Failures: #{failures_count}"
82+
puts "Errors: #{errors_count}"
83+
puts "Total tests: #{testcases}"
84+
85+
failures.each do |failure|
86+
puts ""
87+
puts "<details>"
88+
puts "<summary><code>#{failure.name} in #{failure.unit_name}</code></summary>\n\n"
89+
if failure.message
90+
puts "<p>#{failure.message.chomp.strip}</p>\n\n"
91+
end
92+
if failure.body
93+
puts "<pre><code>#{CGI.escapeHTML(failure.body.chomp.strip)}</code></pre>\n\n"
94+
end
95+
if failure.job
96+
puts "in <a href=\"##{failure.job}\">Job ##{failure.job}</a>"
97+
end
98+
puts "</details>"
10799
end
108100

109101
if report_slowest > 0
110102
STDERR.puts "Reporting slowest tests ⏱"
111-
103+
puts ""
112104
puts "<details>"
113105
puts "<summary>#{report_slowest} slowest tests</summary>\n\n"
114106
puts "<table>"

0 commit comments

Comments
 (0)