From 90c8a1db9d6e8f7d2e3e842142ba98f827d42800 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Fri, 8 Dec 2017 05:47:08 -0500 Subject: test-lib: silence "-x" cleanup under bash When the test suite's "-x" option is used with bash, we end up seeing cleanup cruft in the output: $ bash t0001-init.sh -x [...] ++ diff -u expected actual + test_eval_ret_=0 + want_trace + test t = t + test t = t + set +x ok 42 - re-init from a linked worktree This ranges from mildly annoying (for a successful test) to downright confusing (when we say "last command exited with error", but it's really 5 commands back). We normally are able to suppress this cleanup. As the in-code comment explains, we can't convince the shell not to print it, but we can redirect its stderr elsewhere. But since d88785e424 (test-lib: set BASH_XTRACEFD automatically, 2016-05-11), that doesn't hold for bash. It sends the "set -x" output directly to descriptor 4, not to stderr. We can fix this by also redirecting descriptor 4, and paying close attention to which commands redirected and which are not (see the updated comment). Two alternatives I considered and rejected: - unsetting and setting BASH_XTRACEFD; doing so closes the descriptor, which we must avoid - we could keep everything in a single block as before, redirect 4>/dev/null there, but retain 5>&4 as a copy. And then selectively restore 4>&5 for commands which should be allowed to trace. This would work, but the descriptor swapping seems unnecessarily confusing. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano diff --git a/t/test-lib.sh b/t/test-lib.sh index 116bd6a..7914453 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -601,26 +601,40 @@ test_eval_inner_ () { } test_eval_ () { - # We run this block with stderr redirected to avoid extra cruft - # during a "-x" trace. Once in "set -x" mode, we cannot prevent + # If "-x" tracing is in effect, then we want to avoid polluting stderr + # with non-test commands. But once in "set -x" mode, we cannot prevent # the shell from printing the "set +x" to turn it off (nor the saving # of $? before that). But we can make sure that the output goes to # /dev/null. # - # The test itself is run with stderr put back to &4 (so either to - # /dev/null, or to the original stderr if --verbose was used). + # There are a few subtleties here: + # + # - we have to redirect descriptor 4 in addition to 2, to cover + # BASH_XTRACEFD + # + # - the actual eval has to come before the redirection block (since + # it needs to see descriptor 4 to set up its stderr) + # + # - likewise, any error message we print must be outside the block to + # access descriptor 4 + # + # - checking $? has to come immediately after the eval, but it must + # be _inside_ the block to avoid polluting the "set -x" output + # + + test_eval_inner_ "$@" &3 2>&4 { - test_eval_inner_ "$@" &3 2>&4 test_eval_ret_=$? if want_trace then set +x - if test "$test_eval_ret_" != 0 - then - say_color error >&4 "error: last command exited with \$?=$test_eval_ret_" - fi fi - } 2>/dev/null + } 2>/dev/null 4>&2 + + if test "$test_eval_ret_" != 0 && want_trace + then + say_color error >&4 "error: last command exited with \$?=$test_eval_ret_" + fi return $test_eval_ret_ } -- cgit v0.10.2-6-g49f6 From 9be795fbce210dfc19536cfcd4351134a99edf65 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Fri, 8 Dec 2017 05:47:13 -0500 Subject: t5615: avoid re-using descriptor 4 File descriptors 3 and 4 are special in our test suite, as they link back to the test script's original stdout and stderr. Normally this isn't something tests need to worry about: they are free to clobber these descriptors for sub-commands without affecting the overall script. But there's one very special thing about descriptor 4: since d88785e424 (test-lib: set BASH_XTRACEFD automatically, 2016-05-11), we ask bash to output "set -x" output to it by number. This goes to _any_ descriptor 4, even if it no longer points to the place it did when we set BASH_XTRACEFD. But in t5615, we run a shell loop with descriptor 4 redirected. As a result, t5615 works with non-bash shells even with "-x". And it works with bash without "-x". But the combination of "bash t5615-alternate-env.sh -x" gets a test failure (because our "set -x" output pollutes one of the files). We can fix this by using any descriptor _except_ the magical 4. So let's switch arbitrarily to using 5/6 in this loop, not 3/4. Another alternative is to use a different descriptor for BASH_XTRACEFD. But picking an unused one turns out to be hard. Most shells limit us to 9 numbered descriptors. Bash can handle more, but: - while the BASH_XTRACEFD is specific to bash, GIT_TRACE=4 has a similar problem, and would affect all shells - constructs like "999>/dev/null" are synticatically invalid to non-bash shells. So we have to actually bury it inside an eval, which creates more complications. Of the numbers 1-9, you might think that "9" would be less used than "4". But it's not; many of our scripts use descriptors 8 and 9 (probably under the assumption that they are high and therefore unused). The least-used descriptor is currently "7". We could switch to that, but we're just trading one magic number for another. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano diff --git a/t/t5615-alternate-env.sh b/t/t5615-alternate-env.sh index d2d883f..b4905b8 100755 --- a/t/t5615-alternate-env.sh +++ b/t/t5615-alternate-env.sh @@ -7,9 +7,9 @@ check_obj () { alt=$1; shift while read obj expect do - echo "$obj" >&3 && - echo "$obj $expect" >&4 - done 3>input 4>expect && + echo "$obj" >&5 && + echo "$obj $expect" >&6 + done 5>input 6>expect && GIT_ALTERNATE_OBJECT_DIRECTORIES=$alt \ git "$@" cat-file --batch-check='%(objectname) %(objecttype)' \ actual && -- cgit v0.10.2-6-g49f6 From f5ba2de6bc67082d01742ee6ce892fbcff7b97af Mon Sep 17 00:00:00 2001 From: Jeff King Date: Fri, 8 Dec 2017 05:47:17 -0500 Subject: test-lib: make "-x" work with "--verbose-log" The "-x" tracing option implies "--verbose". This is a problem when running under a TAP harness like "prove", where we need to use "--verbose-log" instead. Instead, let's handle this the same way we do for --valgrind, including the recent fix from 88c6e9d31c (test-lib: --valgrind should not override --verbose-log, 2017-09-05). Namely, let's enable --verbose only when we know there isn't a more specific verbosity option indicated. Note that we also have to tweak `want_trace` to turn it on (previously we just lumped $verbose_log in with $verbose, but now we don't necessarily auto-set the latter). Signed-off-by: Jeff King Signed-off-by: Junio C Hamano diff --git a/t/test-lib.sh b/t/test-lib.sh index 7914453..b8dd5e7 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -264,7 +264,6 @@ do shift ;; -x) trace=t - verbose=t shift ;; --verbose-log) verbose_log=t @@ -283,6 +282,11 @@ then test -z "$verbose_log" && verbose=t fi +if test -n "$trace" && test -z "$verbose_log" +then + verbose=t +fi + if test -n "$color" then # Save the color control sequences now rather than run tput @@ -586,7 +590,9 @@ maybe_setup_valgrind () { } want_trace () { - test "$trace" = t && test "$verbose" = t + test "$trace" = t && { + test "$verbose" = t || test "$verbose_log" = t + } } # This is a separate function because some tests use -- cgit v0.10.2-6-g49f6 From 3f824e91c8480f7a236331096d2c4d969f013a83 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Fri, 8 Dec 2017 05:47:22 -0500 Subject: t/Makefile: introduce TEST_SHELL_PATH You may want to run the test suite with a different shell than you use to build Git. For instance, you may build with SHELL_PATH=/bin/sh (because it's faster, or it's what you expect to exist on systems where the build will be used) but want to run the test suite with bash (e.g., since that allows using "-x" reliably across the whole test suite). There's currently no good way to do this. You might think that doing two separate make invocations, like: make && make -C t SHELL_PATH=/bin/bash would work. And it _almost_ does. The second make will see our bash SHELL_PATH, and we'll use that to run the individual test scripts (or tell prove to use it to do so). So far so good. But this breaks down when "--tee" or "--verbose-log" is used. Those options cause the test script to actually re-exec itself using $SHELL_PATH. But wait, wouldn't our second make invocation have set SHELL_PATH correctly in the environment? Yes, but test-lib.sh sources GIT-BUILD-OPTIONS, which we built during the first "make". And that overrides the environment, giving us the original SHELL_PATH again. Let's introduce a new variable that lets you specify a specific shell to be run for the test scripts. Note that we have to touch both the main and t/ Makefiles, since we have to record it in GIT-BUILD-OPTIONS in one, and use it in the latter. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano diff --git a/Makefile b/Makefile index ee9d5eb..ed96453 100644 --- a/Makefile +++ b/Makefile @@ -425,6 +425,10 @@ all:: # # to say "export LESS=FRX (and LV=-c) if the environment variable # LESS (and LV) is not set, respectively". +# +# Define TEST_SHELL_PATH if you want to use a shell besides SHELL_PATH for +# running the test scripts (e.g., bash has better support for "set -x" +# tracing). GIT-VERSION-FILE: FORCE @$(SHELL_PATH) ./GIT-VERSION-GEN @@ -727,6 +731,8 @@ endif export PERL_PATH export PYTHON_PATH +TEST_SHELL_PATH = $(SHELL_PATH) + LIB_FILE = libgit.a XDIFF_LIB = xdiff/lib.a VCSSVN_LIB = vcs-svn/lib.a @@ -1721,6 +1727,7 @@ prefix_SQ = $(subst ','\'',$(prefix)) gitwebdir_SQ = $(subst ','\'',$(gitwebdir)) SHELL_PATH_SQ = $(subst ','\'',$(SHELL_PATH)) +TEST_SHELL_PATH_SQ = $(subst ','\'',$(TEST_SHELL_PATH)) PERL_PATH_SQ = $(subst ','\'',$(PERL_PATH)) PYTHON_PATH_SQ = $(subst ','\'',$(PYTHON_PATH)) TCLTK_PATH_SQ = $(subst ','\'',$(TCLTK_PATH)) @@ -2351,6 +2358,7 @@ GIT-LDFLAGS: FORCE # and the first level quoting from the shell that runs "echo". GIT-BUILD-OPTIONS: FORCE @echo SHELL_PATH=\''$(subst ','\'',$(SHELL_PATH_SQ))'\' >$@+ + @echo TEST_SHELL_PATH=\''$(subst ','\'',$(TEST_SHELL_PATH_SQ))'\' >>$@+ @echo PERL_PATH=\''$(subst ','\'',$(PERL_PATH_SQ))'\' >>$@+ @echo DIFF=\''$(subst ','\'',$(subst ','\'',$(DIFF)))'\' >>$@+ @echo PYTHON_PATH=\''$(subst ','\'',$(PYTHON_PATH_SQ))'\' >>$@+ diff --git a/t/Makefile b/t/Makefile index 1bb06c3..96317a3 100644 --- a/t/Makefile +++ b/t/Makefile @@ -8,6 +8,7 @@ #GIT_TEST_OPTS = --verbose --debug SHELL_PATH ?= $(SHELL) +TEST_SHELL_PATH ?= $(SHELL_PATH) PERL_PATH ?= /usr/bin/perl TAR ?= $(TAR) RM ?= rm -f @@ -23,6 +24,7 @@ endif # Shell quote; SHELL_PATH_SQ = $(subst ','\'',$(SHELL_PATH)) +TEST_SHELL_PATH_SQ = $(subst ','\'',$(TEST_SHELL_PATH)) PERL_PATH_SQ = $(subst ','\'',$(PERL_PATH)) TEST_RESULTS_DIRECTORY_SQ = $(subst ','\'',$(TEST_RESULTS_DIRECTORY)) @@ -42,11 +44,11 @@ failed: test -z "$$failed" || $(MAKE) $$failed prove: pre-clean $(TEST_LINT) - @echo "*** prove ***"; $(PROVE) --exec '$(SHELL_PATH_SQ)' $(GIT_PROVE_OPTS) $(T) :: $(GIT_TEST_OPTS) + @echo "*** prove ***"; $(PROVE) --exec '$(TEST_SHELL_PATH_SQ)' $(GIT_PROVE_OPTS) $(T) :: $(GIT_TEST_OPTS) $(MAKE) clean-except-prove-cache $(T): - @echo "*** $@ ***"; '$(SHELL_PATH_SQ)' $@ $(GIT_TEST_OPTS) + @echo "*** $@ ***"; '$(TEST_SHELL_PATH_SQ)' $@ $(GIT_TEST_OPTS) pre-clean: $(RM) -r '$(TEST_RESULTS_DIRECTORY_SQ)' diff --git a/t/test-lib.sh b/t/test-lib.sh index b8dd5e7..1ae0fc0 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -80,7 +80,7 @@ done,*) # from any previous runs. >"$GIT_TEST_TEE_OUTPUT_FILE" - (GIT_TEST_TEE_STARTED=done ${SHELL_PATH} "$0" "$@" 2>&1; + (GIT_TEST_TEE_STARTED=done ${TEST_SHELL_PATH} "$0" "$@" 2>&1; echo $? >"$BASE.exit") | tee -a "$GIT_TEST_TEE_OUTPUT_FILE" test "$(cat "$BASE.exit")" = 0 exit -- cgit v0.10.2-6-g49f6