]> git.ipfire.org Git - thirdparty/git.git/commitdiff
tests: let --immediate and --write-junit-xml play well together
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Fri, 4 Oct 2019 15:09:34 +0000 (08:09 -0700)
committerJunio C Hamano <gitster@pobox.com>
Sun, 6 Oct 2019 00:07:44 +0000 (09:07 +0900)
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>
t/test-lib.sh

index d1ba33745a24c92411baca54604c853373b9bc39..86b5e6133b7645f8c9b518b70d161989972868e2 100644 (file)
@@ -567,6 +567,7 @@ export TERM
 
 error () {
        say_color error "error: $*"
+       finalize_junit_xml
        GIT_EXIT_OK=t
        exit 1
 }
@@ -695,7 +696,7 @@ test_failure_ () {
        say_color error "not ok $test_count - $1"
        shift
        printf '%s\n' "$*" | sed -e 's/^/#      /'
-       test "$immediate" = "" || { GIT_EXIT_OK=t; exit 1; }
+       test "$immediate" = "" || { finalize_junit_xml; GIT_EXIT_OK=t; exit 1; }
 }
 
 test_known_broken_ok_ () {
@@ -1063,6 +1064,25 @@ write_junit_xml_testcase () {
        junit_have_testcase=t
 }
 
+finalize_junit_xml () {
+       if test -n "$write_junit_xml" && test -n "$junit_xml_path"
+       then
+               test -n "$junit_have_testcase" || {
+                       junit_start=$(test-tool date getnanos)
+                       write_junit_xml_testcase "all tests skipped"
+               }
+
+               # adjust the overall time
+               junit_time=$(test-tool date getnanos $junit_suite_start)
+               sed "s/<testsuite [^>]*/& time=\"$junit_time\"/" \
+                       <"$junit_xml_path" >"$junit_xml_path.new"
+               mv "$junit_xml_path.new" "$junit_xml_path"
+
+               write_junit_xml "  </testsuite>" "</testsuites>"
+               write_junit_xml=
+       fi
+}
+
 test_atexit_cleanup=:
 test_atexit_handler () {
        # In a succeeding test script 'test_atexit_handler' is invoked
@@ -1085,21 +1105,7 @@ test_done () {
        # removed, so the commands can access pidfiles and socket files.
        test_atexit_handler
 
-       if test -n "$write_junit_xml" && test -n "$junit_xml_path"
-       then
-               test -n "$junit_have_testcase" || {
-                       junit_start=$(test-tool date getnanos)
-                       write_junit_xml_testcase "all tests skipped"
-               }
-
-               # adjust the overall time
-               junit_time=$(test-tool date getnanos $junit_suite_start)
-               sed "s/<testsuite [^>]*/& time=\"$junit_time\"/" \
-                       <"$junit_xml_path" >"$junit_xml_path.new"
-               mv "$junit_xml_path.new" "$junit_xml_path"
-
-               write_junit_xml "  </testsuite>" "</testsuites>"
-       fi
+       finalize_junit_xml
 
        if test -z "$HARNESS_ACTIVE"
        then