t: send verbose test-helper output to fd 4

Test helper functions like test_must_fail may produce messages to stderr when they see a problem. When the tests are run with "--verbose", this ends up on the test script's stderr, and the user can read it. But there's a problem. Some tests record stderr as part of the test, like: test_must_fail git foo 2>output && test_i18ngrep expected.message output In this case the error text goes into "output". This makes the --verbose output less useful (it also means we might accidentally match it in the second, though in practice we tend to produce these messages only on error, so we'd abort the test when the first command fails). Let's instead send this user-facing output directly to descriptor 4, which always points to the original stderr (or /dev/null in non-verbose mode). And it's already forbidden to redirect descriptor 4, since we use it for BASH_XTRACEFD, as explained in 9be795fbce (t5615: avoid re-using descriptor 4, 2017-12-08). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Feb 22, 2018 at 01:48 UTC 03aa3783f258737c02e0b3aba34bb4c9ed8c07dc
1 file changed +10 -10
t/test-lib-functions.sh
+10 -10
@@ -625,22 +625,22 @@ test_must_fail () {
625 exit_code=$?
626 if test $exit_code -eq 0 && ! list_contains "$_test_ok" success
627 then
628 - echo >&2 "test_must_fail: command succeeded: $*"
628 + echo >&4 "test_must_fail: command succeeded: $*"
629 return 1
630 elif test_match_signal 13 $exit_code && list_contains "$_test_ok" sigpipe
631 then
632 return 0
633 elif test $exit_code -gt 129 && test $exit_code -le 192
634 then
635 - echo >&2 "test_must_fail: died by signal $(($exit_code - 128)): $*"
635 + echo >&4 "test_must_fail: died by signal $(($exit_code - 128)): $*"
636 return 1
637 elif test $exit_code -eq 127
638 then
639 - echo >&2 "test_must_fail: command not found: $*"
639 + echo >&4 "test_must_fail: command not found: $*"
640 return 1
641 elif test $exit_code -eq 126
642 then
643 - echo >&2 "test_must_fail: valgrind error: $*"
643 + echo >&4 "test_must_fail: valgrind error: $*"
644 return 1
645 fi
646 return 0
@@ -678,7 +678,7 @@ test_expect_code () {
678 return 0
679 fi
680
681 - echo >&2 "test_expect_code: command exited with $exit_code, we wanted $want_code $*"
681 + echo >&4 "test_expect_code: command exited with $exit_code, we wanted $want_code $*"
682 return 1
683 }
684
@@ -742,18 +742,18 @@ test_i18ngrep () {
742 shift
743 ! grep "$@" && return 0
744
745 - echo >&2 "error: '! grep $@' did find a match in:"
745 + echo >&4 "error: '! grep $@' did find a match in:"
746 else
747 grep "$@" && return 0
748
749 - echo >&2 "error: 'grep $@' didn't find a match in:"
749 + echo >&4 "error: 'grep $@' didn't find a match in:"
750 fi
751
752 if test -s "$last_arg"
753 then
754 - cat >&2 "$last_arg"
754 + cat >&4 "$last_arg"
755 else
756 - echo >&2 "<File '$last_arg' is empty>"
756 + echo >&4 "<File '$last_arg' is empty>"
757 fi
758
759 return 1
@@ -764,7 +764,7 @@ test_i18ngrep () {
764 # not output anything when they fail.
765 verbose () {
766 "$@" && return 0
767 - echo >&2 "command failed: $(git rev-parse --sq-quote "$@")"
767 + echo >&4 "command failed: $(git rev-parse --sq-quote "$@")"
768 return 1
769 }
770