test-lib: verbose mode for only tests matching a pattern

With the new --verbose-only=<pattern> option, one can enable --verbose at a per-test granularity. The pattern is matched against the test number, e.g. ./t0000-basic.sh --verbose-only='2[0-2]' to see only the full output of test 20-22, while showing the rest in the one-liner format. As suggested by Jeff King, this takes care to wrap the entire test_expect_* block, but nothing else, in the verbose toggling. We can use the test_start/end functions from the previous commit for the purpose. This is arguably not *too* useful on its own, but makes the next patch easier to follow. Helped-by: Jeff King <peff@peff.net> Signed-off-by: Thomas Rast <trast@inf.ethz.ch> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Thomas Rast committed Jun 23, 2013 at 20:12 UTC ff09af3fb8f5bede0de523723bbd00d6ef2ab71e
3 files changed +60
t/README
+5
@@ -76,6 +76,11 @@ appropriately before running "make".
76 command being run and their output if any are also
77 output.
78
79 +--verbose-only=<pattern>::
80 + Like --verbose, but the effect is limited to tests with
81 + numbers matching <pattern>. The number matched against is
82 + simply the running count of the test within the file.
83 +
84 --debug::
85 This may help the person who is developing a new test.
86 It causes the command defined with test_debug to run.
t/t0000-basic.sh
+24
@@ -250,6 +250,30 @@ test_expect_success 'test --verbose' '
250 EOF
251 '
252
253 +test_expect_success 'test --verbose-only' '
254 + test_must_fail run_sub_test_lib_test \
255 + test-verbose-only-2 "test verbose-only=2" \
256 + --verbose-only=2 <<-\EOF &&
257 + test_expect_success "passing test" true
258 + test_expect_success "test with output" "echo foo"
259 + test_expect_success "failing test" false
260 + test_done
261 + EOF
262 + check_sub_test_lib_test test-verbose-only-2 <<-\EOF
263 + > ok 1 - passing test
264 + > Z
265 + > expecting success: echo foo
266 + > foo
267 + > Z
268 + > ok 2 - test with output
269 + > Z
270 + > not ok 3 - failing test
271 + > # false
272 + > # failed 1 among 3 test(s)
273 + > 1..3
274 + EOF
275 +'
276 +
277 test_set_prereq HAVEIT
278 haveit=no
279 test_expect_success HAVEIT 'test runs if prerequisite is satisfied' '
t/test-lib.sh
+31
@@ -184,6 +184,9 @@ do
184 help=t; shift ;;
185 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
186 verbose=t; shift ;;
187 + --verbose-only=*)
188 + verbose_only=$(expr "z$1" : 'z[^=]*=\(.*\)')
189 + shift ;;
190 -q|--q|--qu|--qui|--quie|--quiet)
191 # Ignore --quiet under a TAP::Harness. Saying how many tests
192 # passed without the ok/not ok details is always an error.
@@ -342,6 +345,32 @@ match_pattern_list () {
345 return 1
346 }
347
348 +maybe_teardown_verbose () {
349 + test -z "$verbose_only" && return
350 + exec 4>/dev/null 3>/dev/null
351 + verbose=
352 +}
353 +
354 +last_verbose=t
355 +maybe_setup_verbose () {
356 + test -z "$verbose_only" && return
357 + if match_pattern_list $test_count $verbose_only
358 + then
359 + exec 4>&2 3>&1
360 + # Emit a delimiting blank line when going from
361 + # non-verbose to verbose. Within verbose mode the
362 + # delimiter is printed by test_expect_*. The choice
363 + # of the initial $last_verbose is such that before
364 + # test 1, we do not print it.
365 + test -z "$last_verbose" && echo >&3 ""
366 + verbose=t
367 + else
368 + exec 4>/dev/null 3>/dev/null
369 + verbose=
370 + fi
371 + last_verbose=$verbose
372 +}
373 +
374 test_eval_ () {
375 # This is a separate function because some tests use
376 # "return" to end a test_expect_success block early.
@@ -371,10 +400,12 @@ test_run_ () {
400
401 test_start_ () {
402 test_count=$(($test_count+1))
403 + maybe_setup_verbose
404 }
405
406 test_finish_ () {
407 echo >&3 ""
408 + maybe_teardown_verbose
409 }
410
411 test_skip () {