test-lib-functions: keep user's debugger config files and TERM in 'debug'

The 'debug' function in test-lib-functions.sh is used to invoke a debugger at a specific line in a test. It inherits the value of HOME and TERM set by 'test-lib.sh': HOME="$TRASH_DIRECTORY" and TERM=dumb. Changing the value of HOME means that any customization configured in a developers' debugger configuration file (like $HOME/.gdbinit or $HOME/.lldbinit) are not available in the debugger invoked by 'test_pause'. Changing the value of TERM to 'dumb' means that colored output is disabled in the debugger. To make the debugging experience with 'debug' more pleasant, leverage the variable USER_HOME, added in the previous commit, to copy a developer's ~/.gdbinit and ~/.lldbinit to the test HOME. We do not set HOME to USER_HOME as in 'test_pause' to avoid user configuration in $USER_HOME/.gitconfig from interfering with the command being debugged. Also, add a flag to launch the debugger with the original value of TERM, and add the same warning as for 'test_pause'. Helped-by: Carlo Marcelo Arenas Belón <carenas@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:39 UTC 01c381037c30e9351e439d367983f88bdd3415ba
2 files changed +50 -16
t/README
+4 -2
@@ -800,10 +800,12 @@ see test-lib-functions.sh for the full list and their options.
800 argument. This is primarily meant for use during the
801 development of a new test script.
802
803 - - debug <git-command>
803 + - debug [options] <git-command>
804
805 Run a git command inside a debugger. This is primarily meant for
806 - 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.
809
810 - test_done
811
t/test-lib-functions.sh
+46 -14
@@ -190,25 +190,57 @@ test_pause () {
190 # Wrap git with a debugger. Adding this to a command can make it easier
191 # to understand what is going on in a failing test.
192 #
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 +#
203 # Examples:
204 # debug git checkout master
205 # debug --debugger=nemiver git $ARGS
206 # debug -d "valgrind --tool=memcheck --track-origins=yes" git $ARGS
207 debug () {
198 - case "$1" in
199 - -d)
200 - GIT_DEBUGGER="$2" &&
201 - shift 2
202 - ;;
203 - --debugger=*)
204 - GIT_DEBUGGER="${1#*=}" &&
205 - shift 1
206 - ;;
207 - *)
208 - GIT_DEBUGGER=1
209 - ;;
210 - esac &&
211 - 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
244 }
245
246 # Usage: test_commit [options] <message> [<file> [<contents> [<tag>]]]