From 517cd55fd51ebf4c6c20597055ff191fefe5dda0 Mon Sep 17 00:00:00 2001 From: Thomas Rast Date: Sun, 23 Jun 2013 20:12:55 +0200 Subject: test-lib: self-test that --verbose works t0000 contains some light self-tests of test-lib.sh, but --verbose was not covered. Add a test. The only catch is that the presence of a test harness influences the output (specifically, the presence of some empty lines). So we need to unset TEST_HARNESS or set it to a known value. Leaving it unset leads to spurious test failures in the final summary, which come from the subtest. So we always set it. Signed-off-by: Thomas Rast Signed-off-by: Junio C Hamano --- t/t0000-basic.sh | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) (limited to 't/t0000-basic.sh') diff --git a/t/t0000-basic.sh b/t/t0000-basic.sh index cefe33d6d1..6439cec910 100755 --- a/t/t0000-basic.sh +++ b/t/t0000-basic.sh @@ -47,8 +47,13 @@ test_expect_failure 'pretend we have a known breakage' ' run_sub_test_lib_test () { name="$1" descr="$2" # stdin is the body of the test code + shift 2 mkdir "$name" && ( + # Pretend we're a test harness. This prevents + # test-lib from writing the counts to a file that will + # later be summarized, showing spurious "failed" tests + export HARNESS_ACTIVE=t && cd "$name" && cat >"$name.sh" <<-EOF && #!$SHELL_PATH @@ -65,7 +70,7 @@ run_sub_test_lib_test () { cat >>"$name.sh" && chmod +x "$name.sh" && export TEST_DIRECTORY && - ./"$name.sh" >out 2>err + ./"$name.sh" "$@" >out 2>err ) } @@ -215,6 +220,36 @@ test_expect_success 'pretend we have a mix of all possible results' " EOF " +test_expect_success 'test --verbose' ' + test_must_fail run_sub_test_lib_test \ + test-verbose "test verbose" --verbose <<-\EOF && + test_expect_success "passing test" true + test_expect_success "test with output" "echo foo" + test_expect_success "failing test" false + test_done + EOF + mv test-verbose/out test-verbose/out+ + grep -v "^Initialized empty" test-verbose/out+ >test-verbose/out && + check_sub_test_lib_test test-verbose <<-\EOF + > expecting success: true + > Z + > ok 1 - passing test + > Z + > expecting success: echo foo + > foo + > Z + > ok 2 - test with output + > Z + > expecting success: false + > Z + > not ok 3 - failing test + > # false + > Z + > # failed 1 among 3 test(s) + > 1..3 + EOF +' + test_set_prereq HAVEIT haveit=no test_expect_success HAVEIT 'test runs if prerequisite is satisfied' ' -- cgit v1.3-5-g9baa From ff09af3fb8f5bede0de523723bbd00d6ef2ab71e Mon Sep 17 00:00:00 2001 From: Thomas Rast Date: Sun, 23 Jun 2013 20:12:56 +0200 Subject: test-lib: verbose mode for only tests matching a pattern With the new --verbose-only= 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 Signed-off-by: Thomas Rast Signed-off-by: Junio C Hamano --- t/README | 5 +++++ t/t0000-basic.sh | 24 ++++++++++++++++++++++++ t/test-lib.sh | 31 +++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) (limited to 't/t0000-basic.sh') diff --git a/t/README b/t/README index e669bb31b9..9c8f9b19fd 100644 --- a/t/README +++ b/t/README @@ -76,6 +76,11 @@ appropriately before running "make". command being run and their output if any are also output. +--verbose-only=:: + Like --verbose, but the effect is limited to tests with + numbers matching . The number matched against is + simply the running count of the test within the file. + --debug:: This may help the person who is developing a new test. It causes the command defined with test_debug to run. diff --git a/t/t0000-basic.sh b/t/t0000-basic.sh index 6439cec910..4f1844f782 100755 --- a/t/t0000-basic.sh +++ b/t/t0000-basic.sh @@ -250,6 +250,30 @@ test_expect_success 'test --verbose' ' EOF ' +test_expect_success 'test --verbose-only' ' + test_must_fail run_sub_test_lib_test \ + test-verbose-only-2 "test verbose-only=2" \ + --verbose-only=2 <<-\EOF && + test_expect_success "passing test" true + test_expect_success "test with output" "echo foo" + test_expect_success "failing test" false + test_done + EOF + check_sub_test_lib_test test-verbose-only-2 <<-\EOF + > ok 1 - passing test + > Z + > expecting success: echo foo + > foo + > Z + > ok 2 - test with output + > Z + > not ok 3 - failing test + > # false + > # failed 1 among 3 test(s) + > 1..3 + EOF +' + test_set_prereq HAVEIT haveit=no test_expect_success HAVEIT 'test runs if prerequisite is satisfied' ' diff --git a/t/test-lib.sh b/t/test-lib.sh index 432248dfb8..95df832ed9 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -184,6 +184,9 @@ do help=t; shift ;; -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose) verbose=t; shift ;; + --verbose-only=*) + verbose_only=$(expr "z$1" : 'z[^=]*=\(.*\)') + shift ;; -q|--q|--qu|--qui|--quie|--quiet) # Ignore --quiet under a TAP::Harness. Saying how many tests # passed without the ok/not ok details is always an error. @@ -342,6 +345,32 @@ match_pattern_list () { return 1 } +maybe_teardown_verbose () { + test -z "$verbose_only" && return + exec 4>/dev/null 3>/dev/null + verbose= +} + +last_verbose=t +maybe_setup_verbose () { + test -z "$verbose_only" && return + if match_pattern_list $test_count $verbose_only + then + exec 4>&2 3>&1 + # Emit a delimiting blank line when going from + # non-verbose to verbose. Within verbose mode the + # delimiter is printed by test_expect_*. The choice + # of the initial $last_verbose is such that before + # test 1, we do not print it. + test -z "$last_verbose" && echo >&3 "" + verbose=t + else + exec 4>/dev/null 3>/dev/null + verbose= + fi + last_verbose=$verbose +} + test_eval_ () { # This is a separate function because some tests use # "return" to end a test_expect_success block early. @@ -371,10 +400,12 @@ test_run_ () { test_start_ () { test_count=$(($test_count+1)) + maybe_setup_verbose } test_finish_ () { echo >&3 "" + maybe_teardown_verbose } test_skip () { -- cgit v1.3-5-g9baa