test-lib: parse command line options earlier

'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. It looks for '--valgrind' as well, to set up some Valgrind-specific stuff. These all happen before the actual option parsing loop, and the conditions looking for these options look a bit odd, too. They are not completely correct, either, because in a bogus invocation like './t1234-foo.sh -r --tee' they recognize '--tee', although it should be handled as the required argument of the '-r' option. This patch series will add two more options to look out for early, and, in addition, will have to extract these options' stuck arguments (i.e. '--opt=arg') as well. So let's move the option parsing loop and the couple of related conditions following it earlier in 'test-lib.sh', before the place where the test script is executed again for '--tee' and its friends. 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 8cf5800681a6f7c3324f835673974e39e598d1b5
1 file changed +124 -109
t/test-lib.sh
+124 -109
@@ -71,13 +71,102 @@ then
71 exit 1
72 fi
73
74 +# Parse options while taking care to leave $@ intact, so we will still
75 +# have all the original command line options when executing the test
76 +# script again for '--tee' and '--verbose-log' below.
77 +store_arg_to=
78 +prev_opt=
79 +for opt
80 +do
81 + if test -n "$store_arg_to"
82 + then
83 + eval $store_arg_to=\$opt
84 + store_arg_to=
85 + prev_opt=
86 + continue
87 + fi
88 +
89 + case "$opt" in
90 + -d|--d|--de|--deb|--debu|--debug)
91 + debug=t ;;
92 + -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
93 + immediate=t ;;
94 + -l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests)
95 + GIT_TEST_LONG=t; export GIT_TEST_LONG ;;
96 + -r)
97 + store_arg_to=run_list
98 + ;;
99 + --run=*)
100 + run_list=${opt#--*=} ;;
101 + -h|--h|--he|--hel|--help)
102 + help=t ;;
103 + -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
104 + verbose=t ;;
105 + --verbose-only=*)
106 + verbose_only=${opt#--*=}
107 + ;;
108 + -q|--q|--qu|--qui|--quie|--quiet)
109 + # Ignore --quiet under a TAP::Harness. Saying how many tests
110 + # passed without the ok/not ok details is always an error.
111 + test -z "$HARNESS_ACTIVE" && quiet=t ;;
112 + --with-dashes)
113 + with_dashes=t ;;
114 + --no-color)
115 + color= ;;
116 + --va|--val|--valg|--valgr|--valgri|--valgrin|--valgrind)
117 + valgrind=memcheck
118 + tee=t
119 + ;;
120 + --valgrind=*)
121 + valgrind=${opt#--*=}
122 + tee=t
123 + ;;
124 + --valgrind-only=*)
125 + valgrind_only=${opt#--*=}
126 + tee=t
127 + ;;
128 + --tee)
129 + tee=t ;;
130 + --root=*)
131 + root=${opt#--*=} ;;
132 + --chain-lint)
133 + GIT_TEST_CHAIN_LINT=1 ;;
134 + --no-chain-lint)
135 + GIT_TEST_CHAIN_LINT=0 ;;
136 + -x)
137 + trace=t ;;
138 + -V|--verbose-log)
139 + verbose_log=t
140 + tee=t
141 + ;;
142 + *)
143 + echo "error: unknown test option '$opt'" >&2; exit 1 ;;
144 + esac
145 +
146 + prev_opt=$opt
147 +done
148 +if test -n "$store_arg_to"
149 +then
150 + echo "error: $prev_opt requires an argument" >&2
151 + exit 1
152 +fi
153 +
154 +if test -n "$valgrind_only"
155 +then
156 + test -z "$valgrind" && valgrind=memcheck
157 + test -z "$verbose" && verbose_only="$valgrind_only"
158 +elif test -n "$valgrind"
159 +then
160 + test -z "$verbose_log" && verbose=t
161 +fi
162 +
163 # if --tee was passed, write the output not only to the terminal, but
164 # additionally to the file test-results/$BASENAME.out, too.
76 -case "$GIT_TEST_TEE_STARTED, $* " in
77 -done,*)
78 - # do not redirect again
79 - ;;
80 -*' --tee '*|*' --va'*|*' -V '*|*' --verbose-log '*)
165 +if test "$GIT_TEST_TEE_STARTED" = "done"
166 +then
167 + : # do not redirect again
168 +elif test -n "$tee"
169 +then
170 mkdir -p "$TEST_OUTPUT_DIRECTORY/test-results"
171 BASE="$TEST_OUTPUT_DIRECTORY/test-results/$(basename "$0" .sh)"
172
@@ -94,8 +183,35 @@ done,*)
183 echo $? >"$BASE.exit") | tee -a "$GIT_TEST_TEE_OUTPUT_FILE"
184 test "$(cat "$BASE.exit")" = 0
185 exit
97 - ;;
98 -esac
186 +fi
187 +
188 +if test -n "$trace" && test -n "$test_untraceable"
189 +then
190 + # '-x' tracing requested, but this test script can't be reliably
191 + # traced, unless it is run with a Bash version supporting
192 + # BASH_XTRACEFD (introduced in Bash v4.1).
193 + #
194 + # Perform this version check _after_ the test script was
195 + # potentially re-executed with $TEST_SHELL_PATH for '--tee' or
196 + # '--verbose-log', so the right shell is checked and the
197 + # warning is issued only once.
198 + if test -n "$BASH_VERSION" && eval '
199 + test ${BASH_VERSINFO[0]} -gt 4 || {
200 + test ${BASH_VERSINFO[0]} -eq 4 &&
201 + test ${BASH_VERSINFO[1]} -ge 1
202 + }
203 + '
204 + then
205 + : Executed by a Bash version supporting BASH_XTRACEFD. Good.
206 + else
207 + echo >&2 "warning: ignoring -x; '$0' is untraceable without BASH_XTRACEFD"
208 + trace=
209 + fi
210 +fi
211 +if test -n "$trace" && test -z "$verbose_log"
212 +then
213 + verbose=t
214 +fi
215
216 # For repeatability, reset the environment to known value.
217 # TERM is sanitized below, after saving color control sequences.
@@ -193,7 +309,7 @@ fi
309
310 # Add libc MALLOC and MALLOC_PERTURB test
311 # only if we are not executing the test with valgrind
196 -if expr " $GIT_TEST_OPTS " : ".* --valgrind " >/dev/null ||
312 +if test -n "$valgrind" ||
313 test -n "$TEST_NO_MALLOC_CHECK"
314 then
315 setup_malloc_check () {
@@ -264,107 +380,6 @@ test "x$TERM" != "xdumb" && (
380 ) &&
381 color=t
382
267 -store_arg_to=
268 -prev_opt=
269 -for opt
270 -do
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)
281 - debug=t ;;
282 - -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
283 - immediate=t ;;
284 - -l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests)
285 - GIT_TEST_LONG=t; export GIT_TEST_LONG ;;
286 - -r)
287 - store_arg_to=run_list
288 - ;;
289 - --run=*)
290 - run_list=${opt#--*=} ;;
291 - -h|--h|--he|--hel|--help)
292 - help=t ;;
293 - -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
294 - verbose=t ;;
295 - --verbose-only=*)
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.
301 - test -z "$HARNESS_ACTIVE" && quiet=t ;;
302 - --with-dashes)
303 - with_dashes=t ;;
304 - --no-color)
305 - color= ;;
306 - --va|--val|--valg|--valgr|--valgri|--valgrin|--valgrind)
307 - valgrind=memcheck ;;
308 - --valgrind=*)
309 - valgrind=${opt#--*=} ;;
310 - --valgrind-only=*)
311 - valgrind_only=${opt#--*=} ;;
312 - --tee)
313 - ;; # was handled already
314 - --root=*)
315 - root=${opt#--*=} ;;
316 - --chain-lint)
317 - GIT_TEST_CHAIN_LINT=1 ;;
318 - --no-chain-lint)
319 - GIT_TEST_CHAIN_LINT=0 ;;
320 - -x)
321 - trace=t ;;
322 - -V|--verbose-log)
323 - verbose_log=t ;;
324 - *)
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
338 - test -z "$valgrind" && valgrind=memcheck
339 - test -z "$verbose" && verbose_only="$valgrind_only"
340 -elif test -n "$valgrind"
341 -then
342 - test -z "$verbose_log" && verbose=t
343 -fi
344 -
345 -if test -n "$trace" && test -n "$test_untraceable"
346 -then
347 - # '-x' tracing requested, but this test script can't be reliably
348 - # traced, unless it is run with a Bash version supporting
349 - # BASH_XTRACEFD (introduced in Bash v4.1).
350 - if test -n "$BASH_VERSION" && eval '
351 - test ${BASH_VERSINFO[0]} -gt 4 || {
352 - test ${BASH_VERSINFO[0]} -eq 4 &&
353 - test ${BASH_VERSINFO[1]} -ge 1
354 - }
355 - '
356 - then
357 - : Executed by a Bash version supporting BASH_XTRACEFD. Good.
358 - else
359 - echo >&2 "warning: ignoring -x; '$0' is untraceable without BASH_XTRACEFD"
360 - trace=
361 - fi
362 -fi
363 -if test -n "$trace" && test -z "$verbose_log"
364 -then
365 - verbose=t
366 -fi
367 -
383 if test -n "$color"
384 then
385 # Save the color control sequences now rather than run tput