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 <szeder.dev@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

SZEDER Gábor committed Jan 5, 2019 at 02:08 UTC a9b2db379b3be3dcb7540e04f10a1d67c3bc974f
1 file changed +42 -36
t/test-lib.sh
+42 -36
@@ -264,68 +264,74 @@ test "x$TERM" != "xdumb" && (
264 ) &&
265 color=t
266
267 -while test "$#" -ne 0
267 +store_arg_to=
268 +prev_opt=
269 +for opt
270 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
280 -d|--d|--de|--deb|--debu|--debug)
271 - debug=t; shift ;;
281 + debug=t ;;
282 -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
273 - immediate=t; shift ;;
283 + immediate=t ;;
284 -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 ;;
286 -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 + ;;
289 --run=*)
283 - run_list=${1#--*=}; shift ;;
290 + run_list=${opt#--*=} ;;
291 -h|--h|--he|--hel|--help)
285 - help=t; shift ;;
292 + help=t ;;
293 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
287 - verbose=t; shift ;;
294 + verbose=t ;;
295 --verbose-only=*)
289 - verbose_only=${1#--*=}
290 - shift ;;
296 + verbose_only=${opt#--*=}
297 + ;;
298 -q|--q|--qu|--qui|--quie|--quiet)
299 # Ignore --quiet under a TAP::Harness. Saying how many tests
300 # 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 ;;
302 --with-dashes)
296 - with_dashes=t; shift ;;
303 + with_dashes=t ;;
304 --no-color)
298 - color=; shift ;;
305 + color= ;;
306 --va|--val|--valg|--valgr|--valgri|--valgrin|--valgrind)
300 - valgrind=memcheck
301 - shift ;;
307 + valgrind=memcheck ;;
308 --valgrind=*)
303 - valgrind=${1#--*=}
304 - shift ;;
309 + valgrind=${opt#--*=} ;;
310 --valgrind-only=*)
306 - valgrind_only=${1#--*=}
307 - shift ;;
311 + valgrind_only=${opt#--*=} ;;
312 --tee)
309 - shift ;; # was handled already
313 + ;; # was handled already
314 --root=*)
311 - root=${1#--*=}
312 - shift ;;
315 + root=${opt#--*=} ;;
316 --chain-lint)
314 - GIT_TEST_CHAIN_LINT=1
315 - shift ;;
317 + GIT_TEST_CHAIN_LINT=1 ;;
318 --no-chain-lint)
317 - GIT_TEST_CHAIN_LINT=0
318 - shift ;;
319 + GIT_TEST_CHAIN_LINT=0 ;;
320 -x)
320 - trace=t
321 - shift ;;
321 + trace=t ;;
322 -V|--verbose-log)
323 - verbose_log=t
324 - shift ;;
323 + verbose_log=t ;;
324 *)
326 - echo "error: unknown test option '$1'" >&2; exit 1 ;;
325 + echo "error: unknown test option '$opt'" >&2; exit 1 ;;
326 esac
327 +
328 + prev_opt=$opt
329 done
330 +if test -n "$store_arg_to"
331 +then
332 + echo "error: $prev_opt requires an argument" >&2
333 + exit 1
334 +fi
335
336 if test -n "$valgrind_only"
337 then