Skip to content

Commit a9b2db3

Browse files
szedergitster
authored andcommitted
test-lib: parse options in a for loop to keep $@ intact
'test-lib.sh' looks for the presence of certain options like '--tee' and '--verbose-log', so it can execute the test script again to save its standard output and error, and to do so it needs the original command line options the test was invoked with. The next patch is about to move the option parsing loop earlier in 'test-lib.sh', but it is implemented using 'shift' in a while loop, effecively destroying "$@" by the end of the option parsing. Not good. As a preparatory step, turn that option parsing loop into a 'for opt in "$@"' loop to preserve "$@" intact while iterating over the options, and taking extra care to handle the '-r' option's required argument (or the lack thereof). Signed-off-by: SZEDER Gábor <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0a97e86 commit a9b2db3

File tree

1 file changed

+42
-36
lines changed

1 file changed

+42
-36
lines changed

t/test-lib.sh

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -264,68 +264,74 @@ test "x$TERM" != "xdumb" && (
264264
) &&
265265
color=t
266266

267-
while test "$#" -ne 0
267+
store_arg_to=
268+
prev_opt=
269+
for opt
268270
do
269-
case "$1" in
271+
if test -n "$store_arg_to"
272+
then
273+
eval $store_arg_to=\$opt
274+
store_arg_to=
275+
prev_opt=
276+
continue
277+
fi
278+
279+
case "$opt" in
270280
-d|--d|--de|--deb|--debu|--debug)
271-
debug=t; shift ;;
281+
debug=t ;;
272282
-i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
273-
immediate=t; shift ;;
283+
immediate=t ;;
274284
-l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests)
275-
GIT_TEST_LONG=t; export GIT_TEST_LONG; shift ;;
285+
GIT_TEST_LONG=t; export GIT_TEST_LONG ;;
276286
-r)
277-
shift; test "$#" -ne 0 || {
278-
echo 'error: -r requires an argument' >&2;
279-
exit 1;
280-
}
281-
run_list=$1; shift ;;
287+
store_arg_to=run_list
288+
;;
282289
--run=*)
283-
run_list=${1#--*=}; shift ;;
290+
run_list=${opt#--*=} ;;
284291
-h|--h|--he|--hel|--help)
285-
help=t; shift ;;
292+
help=t ;;
286293
-v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
287-
verbose=t; shift ;;
294+
verbose=t ;;
288295
--verbose-only=*)
289-
verbose_only=${1#--*=}
290-
shift ;;
296+
verbose_only=${opt#--*=}
297+
;;
291298
-q|--q|--qu|--qui|--quie|--quiet)
292299
# Ignore --quiet under a TAP::Harness. Saying how many tests
293300
# passed without the ok/not ok details is always an error.
294-
test -z "$HARNESS_ACTIVE" && quiet=t; shift ;;
301+
test -z "$HARNESS_ACTIVE" && quiet=t ;;
295302
--with-dashes)
296-
with_dashes=t; shift ;;
303+
with_dashes=t ;;
297304
--no-color)
298-
color=; shift ;;
305+
color= ;;
299306
--va|--val|--valg|--valgr|--valgri|--valgrin|--valgrind)
300-
valgrind=memcheck
301-
shift ;;
307+
valgrind=memcheck ;;
302308
--valgrind=*)
303-
valgrind=${1#--*=}
304-
shift ;;
309+
valgrind=${opt#--*=} ;;
305310
--valgrind-only=*)
306-
valgrind_only=${1#--*=}
307-
shift ;;
311+
valgrind_only=${opt#--*=} ;;
308312
--tee)
309-
shift ;; # was handled already
313+
;; # was handled already
310314
--root=*)
311-
root=${1#--*=}
312-
shift ;;
315+
root=${opt#--*=} ;;
313316
--chain-lint)
314-
GIT_TEST_CHAIN_LINT=1
315-
shift ;;
317+
GIT_TEST_CHAIN_LINT=1 ;;
316318
--no-chain-lint)
317-
GIT_TEST_CHAIN_LINT=0
318-
shift ;;
319+
GIT_TEST_CHAIN_LINT=0 ;;
319320
-x)
320-
trace=t
321-
shift ;;
321+
trace=t ;;
322322
-V|--verbose-log)
323-
verbose_log=t
324-
shift ;;
323+
verbose_log=t ;;
325324
*)
326-
echo "error: unknown test option '$1'" >&2; exit 1 ;;
325+
echo "error: unknown test option '$opt'" >&2; exit 1 ;;
327326
esac
327+
328+
prev_opt=$opt
328329
done
330+
if test -n "$store_arg_to"
331+
then
332+
echo "error: $prev_opt requires an argument" >&2
333+
exit 1
334+
fi
329335

330336
if test -n "$valgrind_only"
331337
then

0 commit comments

Comments
 (0)