Skip to content

Commit

Permalink
Merge pull request #174 from buildkite-plugins/toote_summary_issue_136
Browse files Browse the repository at this point in the history
Test summary
  • Loading branch information
pzeballos authored Oct 17, 2022
2 parents 574131b + 9070da9 commit 654e780
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 104 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ The artifact glob path to find the JUnit XML files.

Example: `tmp/junit-*.xml`

### `always-annotate` (optional, boolean)

Forces the creation of the annotation even when no failures or errors are found

### `job-uuid-file-pattern` (optional)
Default: `-(.*).xml`

Expand Down
41 changes: 29 additions & 12 deletions hooks/command
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ annotation_dir="$(pwd)/$(mktemp -d "junit-annotate-plugin-annotation-tmp.XXXXXXX
annotation_path="${annotation_dir}/annotation.md"
annotation_style="info"
fail_build=0
has_errors=0
create_annotation=0

function cleanup {
rm -rf "${artifacts_dir}"
Expand Down Expand Up @@ -54,8 +56,11 @@ exit_code=$?
set -e

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

cat "$annotation_path"

if grep -q "<details>" "$annotation_path"; then
if [ $has_errors -eq 0 ]; then
# done in nested if to simplify outer conditions
if [[ "${BUILDKITE_PLUGIN_JUNIT_ANNOTATE_ALWAYS_ANNOTATE:-false}" =~ (true|on|1) ]]; then
echo "Will create annotation anyways"
create_annotation=1
fi
elif ! check_size; then
echo "--- :warning: Failures too large to annotate"

# creating a simplified version of the annotation
mv "${annotation_path}" "${annotation_path}2"
head -4 "${annotation_path}2" >"${annotation_path}"
# || true is to avoid issues if no summary is found
grep '<summary>' "${annotation_path}2" >>"${annotation_path}" || true

if ! check_size; then
echo "--- :warning: Failures too large to annotate"
msg="The failures are too large to create a build annotation. Please inspect the failed JUnit artifacts manually."
echo "$msg"
echo "The failures are too large to create a build annotation. Please inspect the failed JUnit artifacts manually."
create_annotation=0
else
echo "--- :buildkite: Creating annotation"
# shellcheck disable=SC2002
cat "$annotation_path" | buildkite-agent annotate --context "${BUILDKITE_PLUGIN_JUNIT_ANNOTATE_CONTEXT:-junit}" --style "$annotation_style"
echo "The failures are too large to create complete annotation, using a simplified annotation"
fi
fi

if ((fail_build)); then
echo "--- :boom: Failing build due to error"
exit 1
else
exit 0
if [ $create_annotation -ne 0 ]; then
echo "--- :buildkite: Creating annotation"
# shellcheck disable=SC2002
cat "$annotation_path" | buildkite-agent annotate --context "${BUILDKITE_PLUGIN_JUNIT_ANNOTATE_CONTEXT:-junit}" --style "$annotation_style"
fi

exit $fail_build
6 changes: 4 additions & 2 deletions plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ configuration:
properties:
artifacts:
type: string
job-uuid-file-pattern:
always-annotate:
type: boolean
context:
type: string
failure-format:
type: string
Expand All @@ -16,7 +18,7 @@ configuration:
- file
fail-build-on-error:
type: boolean
context:
job-uuid-file-pattern:
type: string
report-slowest:
type: integer
Expand Down
52 changes: 22 additions & 30 deletions ruby/bin/annotate
Original file line number Diff line number Diff line change
Expand Up @@ -74,41 +74,33 @@ junit_report_files.sort.each do |file|
end

STDERR.puts "--- ✍️ Preparing annotation"
STDERR.puts "#{testcases} testcases found"

if failures.any?
STDERR.puts "There #{failures.length == 1 ? "is 1 failure/error" : "are #{failures.length} failures/errors" } 😭"

failures_count = failures.select {|f| f.type == :failure }.length
errors_count = failures.select {|f| f.type == :error }.length
puts [
failures_count == 0 ? nil : (failures_count == 1 ? "1 failure" : "#{failures_count} failures"),
errors_count === 0 ? nil : (errors_count == 1 ? "1 error" : "#{errors_count} errors"),
].compact.join(" and ") + ":\n\n"

failures.each do |failure|
puts "<details>"
puts "<summary><code>#{failure.name} in #{failure.unit_name}</code></summary>\n\n"
if failure.message
puts "<p>#{failure.message.chomp.strip}</p>\n\n"
end
if failure.body
puts "<pre><code>#{CGI.escapeHTML(failure.body.chomp.strip)}</code></pre>\n\n"
end
if failure.job
puts "in <a href=\"##{failure.job}\">Job ##{failure.job}</a>"
end
puts "</details>"
puts "" unless failure == failures.last
end

else
STDERR.puts "There were no failures/errors 🙌"
failures_count = failures.select {|f| f.type == :failure }.length
errors_count = failures.select {|f| f.type == :error }.length

puts "Failures: #{failures_count}"
puts "Errors: #{errors_count}"
puts "Total tests: #{testcases}"

failures.each do |failure|
puts ""
puts "<details>"
puts "<summary><code>#{failure.name} in #{failure.unit_name}</code></summary>\n\n"
if failure.message
puts "<p>#{failure.message.chomp.strip}</p>\n\n"
end
if failure.body
puts "<pre><code>#{CGI.escapeHTML(failure.body.chomp.strip)}</code></pre>\n\n"
end
if failure.job
puts "in <a href=\"##{failure.job}\">Job ##{failure.job}</a>"
end
puts "</details>"
end

if report_slowest > 0
STDERR.puts "Reporting slowest tests ⏱"

puts ""
puts "<details>"
puts "<summary>#{report_slowest} slowest tests</summary>\n\n"
puts "<table>"
Expand Down
Loading

0 comments on commit 654e780

Please sign in to comment.