Skip to content

Commit 6f50726

Browse files
committed
tests: include detailed trace logs with --write-junit-xml upon failure
The JUnit XML format lends itself to be presented in a powerful UI, where you can drill down to the information you are interested in very quickly. For test failures, this usually means that you want to see the detailed trace of the failing tests. With Travis CI, we passed the `--verbose-log` option to get those traces. However, that seems excessive, as we do not need/use the logs in almost all of those cases: only when a test fails do we have a way to include the trace. So let's do something different when using Azure DevOps: let's run all the tests with `--quiet` first, and only if a failure is encountered, try to trace the commands as they are executed. Of course, we cannot turn on `--verbose-log` after the fact. So let's just re-run the test with all the same options, adding `--verbose-log`. And then munging the output file into the JUnit XML on the fly. Note: there is an off chance that re-running the test in verbose mode "fixes" the failures (and this does happen from time to time!). That is a possibility we should be able to live with. Ideally, we would label this as "Passed upon rerun", and Azure Pipelines even know about that outcome, but it is not available when using the JUnit XML format for now: https://github.com/Microsoft/azure-pipelines-agent/blob/master/src/Agent.Worker/TestResults/JunitResultReader.cs Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6e1215f commit 6f50726

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

t/helper/test-path-utils.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,27 @@ int cmd__path_utils(int argc, const char **argv)
303303
return !!res;
304304
}
305305

306+
if (argc == 4 && !strcmp(argv[1], "skip-n-bytes")) {
307+
int fd = open(argv[2], O_RDONLY), offset = atoi(argv[3]);
308+
char buffer[65536];
309+
310+
if (fd < 0)
311+
die_errno("could not open '%s'", argv[2]);
312+
if (lseek(fd, offset, SEEK_SET) < 0)
313+
die_errno("could not skip %d bytes", offset);
314+
for (;;) {
315+
ssize_t count = read(fd, buffer, sizeof(buffer));
316+
if (count < 0)
317+
die_errno("could not read '%s'", argv[2]);
318+
if (!count)
319+
break;
320+
if (write(1, buffer, count) < 0)
321+
die_errno("could not write to stdout");
322+
}
323+
close(fd);
324+
return 0;
325+
}
326+
306327
fprintf(stderr, "%s: unknown function name: %s\n", argv[0],
307328
argv[1] ? argv[1] : "(there was none)");
308329
return 1;

t/test-lib.sh

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,8 +503,19 @@ test_failure_ () {
503503
junit_insert="<failure message=\"not ok $test_count -"
504504
junit_insert="$junit_insert $(xml_attr_encode "$1")\">"
505505
junit_insert="$junit_insert $(xml_attr_encode \
506-
"$(printf '%s\n' "$@" | sed 1d)")"
506+
"$(if test -n "$GIT_TEST_TEE_OUTPUT_FILE"
507+
then
508+
test-tool path-utils skip-n-bytes \
509+
"$GIT_TEST_TEE_OUTPUT_FILE" $GIT_TEST_TEE_OFFSET
510+
else
511+
printf '%s\n' "$@" | sed 1d
512+
fi)")"
507513
junit_insert="$junit_insert</failure>"
514+
if test -n "$GIT_TEST_TEE_OUTPUT_FILE"
515+
then
516+
junit_insert="$junit_insert<system-err>$(xml_attr_encode \
517+
"$(cat "$GIT_TEST_TEE_OUTPUT_FILE")")</system-err>"
518+
fi
508519
write_junit_xml_testcase "$1" " $junit_insert"
509520
fi
510521
test_failure=$(($test_failure + 1))
@@ -795,6 +806,11 @@ test_finish_ () {
795806
echo >&3 ""
796807
maybe_teardown_valgrind
797808
maybe_teardown_verbose
809+
if test -n "$GIT_TEST_TEE_OFFSET"
810+
then
811+
GIT_TEST_TEE_OFFSET=$(test-tool path-utils file-size \
812+
"$GIT_TEST_TEE_OUTPUT_FILE")
813+
fi
798814
}
799815

800816
test_skip () {
@@ -1153,6 +1169,10 @@ then
11531169
date +%Y-%m-%dT%H:%M:%S)\""
11541170
write_junit_xml --truncate "<testsuites>" " <testsuite $junit_attrs>"
11551171
junit_suite_start=$(test-tool date getnanos)
1172+
if test -n "$GIT_TEST_TEE_OUTPUT_FILE"
1173+
then
1174+
GIT_TEST_TEE_OFFSET=0
1175+
fi
11561176
fi
11571177

11581178
# Provide an implementation of the 'yes' utility

0 commit comments

Comments
 (0)