test-lib-functions: optionally keep HOME, TERM and SHELL in 'test_pause'

The 'test_pause' function, which is designed to help interactive debugging and exploration of tests, currently inherits the value of HOME and TERM set by 'test-lib.sh': HOME="$TRASH_DIRECTORY" and TERM=dumb. It also invokes the shell defined by TEST_SHELL_PATH, which defaults to /bin/sh (through SHELL_PATH). Changing the value of HOME means that any customization configured in a developers' shell startup files and any Git aliases defined in their global Git configuration file are not available in the shell invoked by 'test_pause'. Changing the value of TERM to 'dumb' means that colored output is disabled for all commands in that shell. Using /bin/sh as the shell invoked by 'test_pause' is not ideal since some platforms (i.e. Debian and derivatives) use Dash as /bin/sh, and this shell is usually compiled without readline support, which makes for a poor interactive command line experience. To make the interactive command line experience in the shell invoked by 'test_pause' more pleasant, save the values of HOME and TERM in USER_HOME and USER_TERM before changing them in test-lib.sh, and add options to 'test_pause' to optionally use these variables to invoke the shell. Also add an option to invoke SHELL instead of TEST_SHELL_PATH, so that developer's interactive shell is used. We use options instead of changing the behaviour unconditionally since these three variables can slightly change command behaviour. Moreover, using the original HOME means commands could overwrite files in a user's home directory. Be explicit about these caveats in the new 'Usage' section in test-lib-functions.sh. Finally, add '[options]' to the test_pause synopsys in t/README, and mention that the full list of helper functions and their options can be found in test-lib-functions.sh. Helped-by: Elijah Newren <newren@gmail.com> Signed-off-by: Philippe Blain <levraiphilippeblain@gmail.com> Acked-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Philippe Blain committed Sep 6, 2021 at 04:38 UTC add5240fa53fef8c9fad9081d927ffb9cbaaeb56
3 files changed +53 -5
t/README
+3 -2
@@ -753,7 +753,8 @@ Test harness library
753 --------------------
754
755 There are a handful helper functions defined in the test harness
756 -library for your script to use.
756 +library for your script to use. Some of them are listed below;
757 +see test-lib-functions.sh for the full list and their options.
758
759 - test_expect_success [<prereq>] <message> <script>
760
@@ -989,7 +990,7 @@ library for your script to use.
990 EOF
991
992
992 - - test_pause
993 + - test_pause [options]
994
995 This command is useful for writing and debugging tests and must be
996 removed before submitting. It halts the execution of the test and
t/test-lib-functions.sh
+46 -1
@@ -137,9 +137,54 @@ test_tick () {
137 # Stop execution and start a shell. This is useful for debugging tests.
138 #
139 # Be sure to remove all invocations of this command before submitting.
140 +# WARNING: the shell invoked by this helper does not have the same environment
141 +# as the one running the tests (shell variables and functions are not
142 +# available, and the options below further modify the environment). As such,
143 +# commands copied from a test script might behave differently than when
144 +# running the test.
145 +#
146 +# Usage: test_pause [options]
147 +# -t
148 +# Use your original TERM instead of test-lib.sh's "dumb".
149 +# This usually restores color output in the invoked shell.
150 +# -s
151 +# Invoke $SHELL instead of $TEST_SHELL_PATH.
152 +# -h
153 +# Use your original HOME instead of test-lib.sh's "$TRASH_DIRECTORY".
154 +# This allows you to use your regular shell environment and Git aliases.
155 +# CAUTION: running commands copied from a test script into the paused shell
156 +# might result in files in your HOME being overwritten.
157 +# -a
158 +# Shortcut for -t -s -h
159
160 test_pause () {
142 - "$TEST_SHELL_PATH" <&6 >&5 2>&7
161 + PAUSE_TERM=$TERM &&
162 + PAUSE_SHELL=$TEST_SHELL_PATH &&
163 + PAUSE_HOME=$HOME &&
164 + while test $# != 0
165 + do
166 + case "$1" in
167 + -t)
168 + PAUSE_TERM="$USER_TERM"
169 + ;;
170 + -s)
171 + PAUSE_SHELL="$SHELL"
172 + ;;
173 + -h)
174 + PAUSE_HOME="$USER_HOME"
175 + ;;
176 + -a)
177 + PAUSE_TERM="$USER_TERM"
178 + PAUSE_SHELL="$SHELL"
179 + PAUSE_HOME="$USER_HOME"
180 + ;;
181 + *)
182 + break
183 + ;;
184 + esac
185 + shift
186 + done &&
187 + TERM="$PAUSE_TERM" HOME="$PAUSE_HOME" "$PAUSE_SHELL" <&6 >&5 2>&7
188 }
189
190 # Wrap git with a debugger. Adding this to a command can make it easier
t/test-lib.sh
+4 -2
@@ -585,8 +585,9 @@ else
585 }
586 fi
587
588 +USER_TERM="$TERM"
589 TERM=dumb
589 -export TERM
590 +export TERM USER_TERM
591
592 error () {
593 say_color error "error: $*"
@@ -1380,9 +1381,10 @@ then
1381 fi
1382
1383 # Last-minute variable setup
1384 +USER_HOME="$HOME"
1385 HOME="$TRASH_DIRECTORY"
1386 GNUPGHOME="$HOME/gnupg-home-not-used"
1385 -export HOME GNUPGHOME
1387 +export HOME GNUPGHOME USER_HOME
1388
1389 # Test repository
1390 rm -fr "$TRASH_DIRECTORY" || {