Skip to content

Commit ff09af3

Browse files
Thomas Rastgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Thomas Rast <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 517cd55 commit ff09af3

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

t/README

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ appropriately before running "make".
7676
command being run and their output if any are also
7777
output.
7878

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+
7984
--debug::
8085
This may help the person who is developing a new test.
8186
It causes the command defined with test_debug to run.

t/t0000-basic.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,30 @@ test_expect_success 'test --verbose' '
250250
EOF
251251
'
252252

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+
253277
test_set_prereq HAVEIT
254278
haveit=no
255279
test_expect_success HAVEIT 'test runs if prerequisite is satisfied' '

t/test-lib.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ do
184184
help=t; shift ;;
185185
-v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
186186
verbose=t; shift ;;
187+
--verbose-only=*)
188+
verbose_only=$(expr "z$1" : 'z[^=]*=\(.*\)')
189+
shift ;;
187190
-q|--q|--qu|--qui|--quie|--quiet)
188191
# Ignore --quiet under a TAP::Harness. Saying how many tests
189192
# passed without the ok/not ok details is always an error.
@@ -342,6 +345,32 @@ match_pattern_list () {
342345
return 1
343346
}
344347

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+
345374
test_eval_ () {
346375
# This is a separate function because some tests use
347376
# "return" to end a test_expect_success block early.
@@ -371,10 +400,12 @@ test_run_ () {
371400

372401
test_start_ () {
373402
test_count=$(($test_count+1))
403+
maybe_setup_verbose
374404
}
375405

376406
test_finish_ () {
377407
echo >&3 ""
408+
maybe_teardown_verbose
378409
}
379410

380411
test_skip () {

0 commit comments

Comments
 (0)