Skip to content

Commit 211f488

Browse files
authored
Merge pull request #580 from github/pluehne/make-test-output-more-pleasant-to-inspect
Make test output more pleasant to inspect
2 parents a7b4c05 + beb4e7d commit 211f488

File tree

4 files changed

+101
-21
lines changed

4 files changed

+101
-21
lines changed

script/cibuild

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,38 @@
22
# Usage: script/cibuild [--no-package]
33
set -e
44

5+
ROOTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )"
6+
TMPDIR="$ROOTDIR/test/tmp"
7+
8+
# Remove possible remnants of previous test runs
9+
rm -rf "${TMPDIR:?}/*"
10+
11+
print_test_results() {
12+
if [ -n "$GITHUB_STEP_SUMMARY" ]; then
13+
echo -e "### Test results\n" >> "$GITHUB_STEP_SUMMARY"
14+
echo "| Test suite | Result | Successful | Failed | Skipped | Duration |" >> "$GITHUB_STEP_SUMMARY"
15+
echo "|---|---|--:|--:|--:|--:|" >> "$GITHUB_STEP_SUMMARY"
16+
sort -V "$TMPDIR/results" >> "$GITHUB_STEP_SUMMARY"
17+
fi
18+
}
19+
520
# Enable verbose logging of ssh commands
621
export GHE_VERBOSE_SSH=true
722

823
if ! find test -name "test-*.sh" -print0 | xargs -0 -n 1 /bin/bash; then
24+
print_test_results
925
exit 1
1026
fi
1127

28+
print_test_results
29+
1230
# Bail out when --no-package given
1331
[ "$1" = "--no-package" ] && exit 0
1432

1533
# files we'll md5sum at the end
1634
pkg_files=
1735

1836
# Build the tarball
19-
ROOTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )"
20-
TMPDIR="$ROOTDIR/test/tmp"
21-
2237
echo "Building tar.gz package ..."
2338
if script/package-tarball 1>$TMPDIR/package-tarball.txt 2>&1; then
2439
pkg_files=$(grep '^Package ' < $TMPDIR/package-tarball.txt | cut -f 2 -d ' ')

test/test-ghe-backup-parallel.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
# ghe-backup command tests run in parallel
33
set -e
44

5+
# Overwrite default test suite name to distinguish it from test-ghe-backup
6+
export GHE_TEST_SUITE_NAME="test-ghe-backup-parallel"
7+
58
export GHE_PARALLEL_ENABLED=yes
69

710
TESTS_DIR="$PWD/$(dirname "$0")"

test/test-ghe-restore-parallel.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ if [[ "$OSTYPE" == "darwin"* ]]; then
88
exit 0
99
fi
1010

11+
# Overwrite default test suite name to distinguish it from test-ghe-restore
12+
export GHE_TEST_SUITE_NAME="test-ghe-restore-parallel"
13+
1114
export GHE_PARALLEL_ENABLED=yes
1215

1316
# use temp dir to fix rsync file issues in parallel execution:

test/testlib.sh

Lines changed: 77 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ PATH="$ROOTDIR/test/bin:$ROOTDIR/bin:$ROOTDIR/share/github-backup-utils:$PATH"
3030
TMPDIR="$ROOTDIR/test/tmp"
3131
TRASHDIR="$TMPDIR/$(basename "$0")-$$"
3232

33+
test_suite_file_name="$(basename "${BASH_SOURCE[1]}")"
34+
test_suite_name="${GHE_TEST_SUITE_NAME:-${test_suite_file_name%.*}}"
35+
results_file="$TMPDIR/results"
36+
test_suite_before_time=$(date '+%s.%3N')
37+
3338
# Set GIT_{AUTHOR,COMMITTER}_{NAME,EMAIL}
3439
# This removes the assumption that a git config that specifies these is present.
3540
export GIT_AUTHOR_NAME=make GIT_AUTHOR_EMAIL=make GIT_COMMITTER_NAME=make GIT_COMMITTER_EMAIL=make
@@ -57,14 +62,52 @@ ghe_remote_version_config "$GHE_TEST_REMOTE_VERSION"
5762
# ghe-restore process groups
5863
unset GHE_SNAPSHOT_TIMESTAMP
5964

65+
# Color definitions for log output
66+
color_reset=$(printf '\e[0m')
67+
# Display commands (lines starting with + in the output) in purple
68+
color_command=$(printf '\e[0;35m')
69+
# Display exit code line in red
70+
color_error_message=$(printf '\e[0;31m')
71+
# Display test suite name in blue
72+
color_test_suite=$(printf '\e[0;34m')
73+
# Display successful tests in bold green
74+
color_pass=$(printf '\e[1;32m')
75+
# Display skipped tests in bold gray
76+
color_skip=$(printf '\e[1;37m')
77+
# Display failed tests in bold red
78+
color_fail=$(printf '\e[1;31m')
79+
6080
# keep track of num tests and failures
6181
tests=0
82+
successes=0
83+
skipped=0
6284
failures=0
6385

6486
# this runs at process exit
6587
atexit () {
6688
res=$?
6789

90+
test_suite_after_time=$(date '+%s.%3N')
91+
test_suite_elapsed_time=$(echo "scale=3; $test_suite_after_time - $test_suite_before_time" | bc)
92+
93+
# Temporarily redirect stdout output to results file
94+
exec 3<&1
95+
exec 1>>"$results_file"
96+
97+
# Print test summary for this test suite
98+
echo -n "| $test_suite_name | "
99+
100+
if [ "$failures" -eq "0" ]; then
101+
echo -n ":green_circle: passed"
102+
else
103+
echo -n ":red_circle: failed"
104+
fi
105+
106+
printf " | $successes | $failures | $skipped | %.3f s |\\n" "$test_suite_elapsed_time"
107+
108+
# Restore stdout
109+
exec 1<&3
110+
68111
[ -z "$KEEPTRASH" ] && rm -rf "$TRASHDIR"
69112
if [ $failures -gt 0 ]; then
70113
exit 1
@@ -145,36 +188,53 @@ begin_test () {
145188

146189
# allow the subshell to exit non-zero without exiting this process
147190
set -x +e
148-
before_time=$(date '+%s')
191+
before_time=$(date '+%s.%3N')
192+
193+
# Marker to truncate the actual test output later
194+
echo "begin_test_truncate_marker" > /dev/null
149195
}
150196

151-
report_failure () {
152-
msg=$1
153-
desc=$2
154-
failures=$(( failures + 1 ))
155-
printf "test: %-73s $msg\\n" "$desc ..."
156-
(
157-
sed 's/^/ /' <"$TRASHDIR/out" |
158-
grep -a -v -e '^\+ end_test' -e '^+ set +x' - "$TRASHDIR/out" |
159-
sed 's/[+] test_status=/test failed. last command exited with /' |
160-
sed 's/^/ /'
161-
) 1>&2
197+
report_failure_output () {
198+
echo "::group::Output of failed test" 1>&2
199+
# Truncate the test output to exclude testing-related instructions
200+
echo "$(<"$TRASHDIR/out")" \
201+
| sed '0,/begin_test_truncate_marker/d' \
202+
| sed -n '/end_test_truncate_marker/q;p' | head -n -2 \
203+
| sed "s/^\(+.*\)$/${color_command}\1${color_reset}/" \
204+
1>&2
205+
echo -e "\n${color_error_message}Test failed. The last command exited with exit code" \
206+
"$test_status.${color_reset}" 1>&2
207+
echo "::endgroup::" 1>&2
162208
}
163209

164210
# Mark the end of a test.
165211
end_test () {
166212
test_status="${1:-$?}"
167-
after_time=$(date '+%s')
168-
elapsed_time=$((after_time - before_time))
213+
214+
# Marker to truncate the actual test output later
215+
echo "end_test_truncate_marker" > /dev/null
216+
217+
after_time=$(date '+%s.%3N')
218+
elapsed_time=$(echo "scale=3; $after_time - $before_time" | bc)
169219
set +x -e
170220
exec 1>&3 2>&4
171221

172222
if [ "$test_status" -eq 0 ]; then
173-
printf "test: %-65s OK (${elapsed_time}s)\\n" "$test_description ..."
223+
successes=$(( successes + 1 ))
224+
printf "${color_pass}PASS${color_reset}" 1>&2
174225
elif [ "$test_status" -eq 254 ]; then
175-
printf "test: %-65s SKIPPED\\n" "$test_description ..."
226+
skipped=$(( skipped + 1 ))
227+
printf "${color_skip}SKIP${color_reset}" 1>&2
176228
else
177-
report_failure "FAILED (${elapsed_time}s)" "$test_description ..."
229+
failures=$(( failures + 1 ))
230+
printf "${color_fail}FAIL${color_reset}" 1>&2
231+
fi
232+
233+
printf " [%8.3f s] ${color_test_suite}$test_suite_name${color_reset} $test_description\\n" \
234+
"$elapsed_time" 1>&2
235+
236+
if [ "$test_status" -ne 0 ] && [ "$test_status" -ne 254 ]; then
237+
report_failure_output
178238
fi
179239

180240
unset test_description
@@ -408,7 +468,6 @@ setup_minio_test_data() {
408468
bucket="packages"
409469

410470
mkdir -p "$bucket"
411-
echo "an example blob" "$bucket/91dfa09f-1801-4e00-95ee-6b763d7da3e2"
412471
}
413472

414473
cleanup_minio_test_data() {

0 commit comments

Comments
 (0)