Skip to content

Commit 477dcad

Browse files
dschogitster
authored andcommitted
tests: do not let lazy prereqs inside test_expect_* turn off tracing
The `test_expect_*` functions use `test_eval_` and so does `test_run_lazy_prereq_`. If tracing is enabled via the `-x` option, `test_eval_` turns on tracing while evaluating the code block, and turns it off directly after it. This is unwanted for nested invocations. One somewhat surprising example of this is when running a test that calls `test_i18ngrep`: that function requires the `C_LOCALE_OUTPUT` prereq, and that prereq is a lazy one, so it is evaluated via `test_eval_`, the command tracing is turned off, and the test case continues to run _without tracing the commands_. Another somewhat surprising example is when one lazy prereq depends on another lazy prereq: the former will call `test_have_prereq` with the latter one, which in turn calls `test_eval_` and -- you guessed it -- tracing (if enabled) will be turned off _before_ returning to evaluating the other lazy prereq. As we will introduce just such a scenario with the GPG, GPGSM and RFC1991 prereqs, let's fix that by introducing a variable that keeps track of the current trace level: nested `test_eval_` calls will increment and then decrement the level, and only when it reaches 0, the tracing will _actually_ be turned off. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 975f45b commit 477dcad

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

t/t0000-basic.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,19 @@ then
833833
exit 1
834834
fi
835835

836+
test_expect_success 'lazy prereqs do not turn off tracing' "
837+
run_sub_test_lib_test lazy-prereq-and-tracing \
838+
'lazy prereqs and -x' -v -x <<-\\EOF &&
839+
test_lazy_prereq LAZY true
840+
841+
test_expect_success lazy 'test_have_prereq LAZY && echo trace'
842+
843+
test_done
844+
EOF
845+
846+
grep 'echo trace' lazy-prereq-and-tracing/err
847+
"
848+
836849
test_expect_success 'tests clean up even on failures' "
837850
run_sub_test_lib_test_err \
838851
failing-cleanup 'Failing tests with cleanup commands' <<-\\EOF &&

t/test-lib.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,7 @@ maybe_setup_valgrind () {
882882
fi
883883
}
884884

885+
trace_level_=0
885886
want_trace () {
886887
test "$trace" = t && {
887888
test "$verbose" = t || test "$verbose_log" = t
@@ -895,7 +896,7 @@ want_trace () {
895896
test_eval_inner_ () {
896897
# Do not add anything extra (including LF) after '$*'
897898
eval "
898-
want_trace && set -x
899+
want_trace && trace_level_=$(($trace_level_+1)) && set -x
899900
$*"
900901
}
901902

@@ -926,7 +927,8 @@ test_eval_ () {
926927
test_eval_ret_=$?
927928
if want_trace
928929
then
929-
set +x
930+
test 1 = $trace_level_ && set +x
931+
trace_level_=$(($trace_level_-1))
930932
fi
931933
} 2>/dev/null 4>&2
932934

0 commit comments

Comments
 (0)