Skip to content

Commit 2b2af95

Browse files
committed
Merge branch 'pb/test-use-user-env'
Teach "test_pause" and "debug" helpers to allow using the HOME and TERM environment variables the user usually uses. * pb/test-use-user-env: test-lib-functions: keep user's debugger config files and TERM in 'debug' test-lib-functions: optionally keep HOME, TERM and SHELL in 'test_pause' test-lib-functions: use 'TEST_SHELL_PATH' in 'test_pause'
2 parents c76fcf3 + 01c3810 commit 2b2af95

File tree

3 files changed

+103
-21
lines changed

3 files changed

+103
-21
lines changed

t/README

Lines changed: 7 additions & 4 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

@@ -799,10 +800,12 @@ library for your script to use.
799800
argument. This is primarily meant for use during the
800801
development of a new test script.
801802

802-
- debug <git-command>
803+
- debug [options] <git-command>
803804

804805
Run a git command inside a debugger. This is primarily meant for
805-
use when debugging a failing test script.
806+
use when debugging a failing test script. With '-t', use your
807+
original TERM instead of test-lib.sh's "dumb", so that your
808+
debugger interface has colors.
806809

807810
- test_done
808811

@@ -989,7 +992,7 @@ library for your script to use.
989992
EOF
990993

991994

992-
- test_pause
995+
- test_pause [options]
993996

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

t/test-lib-functions.sh

Lines changed: 92 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -137,33 +137,110 @@ 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-
"$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
146191
# to understand what is going on in a failing test.
147192
#
193+
# Usage: debug [options] <git command>
194+
# -d <debugger>
195+
# --debugger=<debugger>
196+
# Use <debugger> instead of GDB
197+
# -t
198+
# Use your original TERM instead of test-lib.sh's "dumb".
199+
# This usually restores color output in the debugger.
200+
# WARNING: the command being debugged might behave differently than when
201+
# running the test.
202+
#
148203
# Examples:
149204
# debug git checkout master
150205
# debug --debugger=nemiver git $ARGS
151206
# debug -d "valgrind --tool=memcheck --track-origins=yes" git $ARGS
152207
debug () {
153-
case "$1" in
154-
-d)
155-
GIT_DEBUGGER="$2" &&
156-
shift 2
157-
;;
158-
--debugger=*)
159-
GIT_DEBUGGER="${1#*=}" &&
160-
shift 1
161-
;;
162-
*)
163-
GIT_DEBUGGER=1
164-
;;
165-
esac &&
166-
GIT_DEBUGGER="${GIT_DEBUGGER}" "$@" <&6 >&5 2>&7
208+
GIT_DEBUGGER=1 &&
209+
DEBUG_TERM=$TERM &&
210+
while test $# != 0
211+
do
212+
case "$1" in
213+
-t)
214+
DEBUG_TERM="$USER_TERM"
215+
;;
216+
-d)
217+
GIT_DEBUGGER="$2" &&
218+
shift
219+
;;
220+
--debugger=*)
221+
GIT_DEBUGGER="${1#*=}"
222+
;;
223+
*)
224+
break
225+
;;
226+
esac
227+
shift
228+
done &&
229+
230+
dotfiles=".gdbinit .lldbinit"
231+
232+
for dotfile in $dotfiles
233+
do
234+
dotfile="$USER_HOME/$dotfile" &&
235+
test -f "$dotfile" && cp "$dotfile" "$HOME" || :
236+
done &&
237+
238+
TERM="$DEBUG_TERM" GIT_DEBUGGER="${GIT_DEBUGGER}" "$@" <&6 >&5 2>&7 &&
239+
240+
for dotfile in $dotfiles
241+
do
242+
rm -f "$HOME/$dotfile"
243+
done
167244
}
168245

169246
# Usage: test_commit [options] <message> [<file> [<contents> [<tag>]]]

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: $*"
@@ -1381,9 +1382,10 @@ then
13811382
fi
13821383

13831384
# Last-minute variable setup
1385+
USER_HOME="$HOME"
13841386
HOME="$TRASH_DIRECTORY"
13851387
GNUPGHOME="$HOME/gnupg-home-not-used"
1386-
export HOME GNUPGHOME
1388+
export HOME GNUPGHOME USER_HOME
13871389

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

0 commit comments

Comments
 (0)