Skip to content

Commit add5240

Browse files
phil-blaingitster
authored andcommitted
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 <[email protected]> Signed-off-by: Philippe Blain <[email protected]> Acked-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0aa496b commit add5240

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

t/README

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,8 @@ Test harness library
753753
--------------------
754754

755755
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.
757758

758759
- test_expect_success [<prereq>] <message> <script>
759760

@@ -989,7 +990,7 @@ library for your script to use.
989990
EOF
990991

991992

992-
- test_pause
993+
- test_pause [options]
993994

994995
This command is useful for writing and debugging tests and must be
995996
removed before submitting. It halts the execution of the test and

t/test-lib-functions.sh

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,54 @@ test_tick () {
137137
# Stop execution and start a shell. This is useful for debugging tests.
138138
#
139139
# 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
140159

141160
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
143188
}
144189

145190
# Wrap git with a debugger. Adding this to a command can make it easier

t/test-lib.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -585,8 +585,9 @@ else
585585
}
586586
fi
587587

588+
USER_TERM="$TERM"
588589
TERM=dumb
589-
export TERM
590+
export TERM USER_TERM
590591

591592
error () {
592593
say_color error "error: $*"
@@ -1380,9 +1381,10 @@ then
13801381
fi
13811382

13821383
# Last-minute variable setup
1384+
USER_HOME="$HOME"
13831385
HOME="$TRASH_DIRECTORY"
13841386
GNUPGHOME="$HOME/gnupg-home-not-used"
1385-
export HOME GNUPGHOME
1387+
export HOME GNUPGHOME USER_HOME
13861388

13871389
# Test repository
13881390
rm -fr "$TRASH_DIRECTORY" || {

0 commit comments

Comments
 (0)