tests: let --immediate and --write-junit-xml play well together

When the `--immediate` option is in effect, any test failure will immediately exit the test script. Together with `--write-junit-xml`, we will want the JUnit-style `.xml` file to be finalized (and not leave the XML incomplete). Let's make it so. This comes in particularly handy when trying to debug via Azure Pipelines, where the JUnit-style XML is consumed to present the test results in an informative and helpful way. While at it, also handle the `error()` code path. The only remaining code path that sets `GIT_EXIT_OK` happens whenever the trash directory could not be set up, i.e. long before the JUnit XML was written, therefore we should _not_ try to finalize that XML in that case. It is tempting to change the `immediate` code path to just hand off to `error`, simplifying the code in the process. That would, however, result in a change of behavior (an additional error message) in the test suite, which is outside of the purview of the current patch series: its goal is to allow building Git with Visual Studio and testing it with a portable version of Git for Windows. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Oct 4, 2019 at 08:09 UTC ab7d854abaada90febf5b2e0839dd662cb0c8d10
1 file changed +22 -16
t/test-lib.sh
+22 -16
@@ -567,6 +567,7 @@ export TERM
567
568 error () {
569 say_color error "error: $*"
570 + finalize_junit_xml
571 GIT_EXIT_OK=t
572 exit 1
573 }
@@ -695,7 +696,7 @@ test_failure_ () {
696 say_color error "not ok $test_count - $1"
697 shift
698 printf '%s\n' "$*" | sed -e 's/^/# /'
698 - test "$immediate" = "" || { GIT_EXIT_OK=t; exit 1; }
699 + test "$immediate" = "" || { finalize_junit_xml; GIT_EXIT_OK=t; exit 1; }
700 }
701
702 test_known_broken_ok_ () {
@@ -1063,6 +1064,25 @@ write_junit_xml_testcase () {
1064 junit_have_testcase=t
1065 }
1066
1067 +finalize_junit_xml () {
1068 + if test -n "$write_junit_xml" && test -n "$junit_xml_path"
1069 + then
1070 + test -n "$junit_have_testcase" || {
1071 + junit_start=$(test-tool date getnanos)
1072 + write_junit_xml_testcase "all tests skipped"
1073 + }
1074 +
1075 + # adjust the overall time
1076 + junit_time=$(test-tool date getnanos $junit_suite_start)
1077 + sed "s/<testsuite [^>]*/& time=\"$junit_time\"/" \
1078 + <"$junit_xml_path" >"$junit_xml_path.new"
1079 + mv "$junit_xml_path.new" "$junit_xml_path"
1080 +
1081 + write_junit_xml " </testsuite>" "</testsuites>"
1082 + write_junit_xml=
1083 + fi
1084 +}
1085 +
1086 test_atexit_cleanup=:
1087 test_atexit_handler () {
1088 # In a succeeding test script 'test_atexit_handler' is invoked
@@ -1085,21 +1105,7 @@ test_done () {
1105 # removed, so the commands can access pidfiles and socket files.
1106 test_atexit_handler
1107
1088 - if test -n "$write_junit_xml" && test -n "$junit_xml_path"
1089 - then
1090 - test -n "$junit_have_testcase" || {
1091 - junit_start=$(test-tool date getnanos)
1092 - write_junit_xml_testcase "all tests skipped"
1093 - }
1094 -
1095 - # adjust the overall time
1096 - junit_time=$(test-tool date getnanos $junit_suite_start)
1097 - sed "s/<testsuite [^>]*/& time=\"$junit_time\"/" \
1098 - <"$junit_xml_path" >"$junit_xml_path.new"
1099 - mv "$junit_xml_path.new" "$junit_xml_path"
1100 -
1101 - write_junit_xml " </testsuite>" "</testsuites>"
1102 - fi
1108 + finalize_junit_xml
1109
1110 if test -z "$HARNESS_ACTIVE"
1111 then