Merge branch 'jk/test-shell-trace'
authorJunio C Hamano <gitster@pobox.com>
Mon, 20 Oct 2014 19:23:40 +0000 (12:23 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 20 Oct 2014 19:23:40 +0000 (12:23 -0700)
Test scripts were taught to notice "-x" option to show shell trace,
as if the tests were run under "sh -x".

* jk/test-shell-trace:
test-lib.sh: support -x option for shell-tracing
t5304: use helper to report failure of "test foo = bar"
t5304: use test_path_is_* instead of "test -f"

t/README
t/t5304-prune.sh
t/test-lib-functions.sh
t/test-lib.sh
index 52c77ae936d9cb3ecc4ff42a2c4667fca4373577..9952261299e62c88d04954a2a8e738cd6a2adb30 100644 (file)
--- a/t/README
+++ b/t/README
@@ -82,6 +82,12 @@ appropriately before running "make".
        numbers matching <pattern>.  The number matched against is
        simply the running count of the test within the file.
 
+-x::
+       Turn on shell tracing (i.e., `set -x`) during the tests
+       themselves. Implies `--verbose`. Note that this can cause
+       failures in some tests which redirect and test the
+       output of shell functions. Use with caution.
+
 -d::
 --debug::
        This may help the person who is developing a new test.
index 01c6a3fc1dc3ef028cfdaad749e9e68955f00226..e32e46dee10e96a0b0e51df0eeefd343373f662e 100755 (executable)
@@ -13,8 +13,8 @@ add_blob() {
        before=$(git count-objects | sed "s/ .*//") &&
        BLOB=$(echo aleph_0 | git hash-object -w --stdin) &&
        BLOB_FILE=.git/objects/$(echo $BLOB | sed "s/^../&\//") &&
-       test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
-       test -f $BLOB_FILE &&
+       verbose test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
+       test_path_is_file $BLOB_FILE &&
        test-chmtime =+0 $BLOB_FILE
 }
 
@@ -35,9 +35,9 @@ test_expect_success 'prune stale packs' '
        : > .git/objects/tmp_2.pack &&
        test-chmtime =-86501 .git/objects/tmp_1.pack &&
        git prune --expire 1.day &&
-       test -f $orig_pack &&
-       test -f .git/objects/tmp_2.pack &&
-       ! test -f .git/objects/tmp_1.pack
+       test_path_is_file $orig_pack &&
+       test_path_is_file .git/objects/tmp_2.pack &&
+       test_path_is_missing .git/objects/tmp_1.pack
 
 '
 
@@ -45,12 +45,12 @@ test_expect_success 'prune --expire' '
 
        add_blob &&
        git prune --expire=1.hour.ago &&
-       test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
-       test -f $BLOB_FILE &&
+       verbose test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
+       test_path_is_file $BLOB_FILE &&
        test-chmtime =-86500 $BLOB_FILE &&
        git prune --expire 1.day &&
-       test $before = $(git count-objects | sed "s/ .*//") &&
-       ! test -f $BLOB_FILE
+       verbose test $before = $(git count-objects | sed "s/ .*//") &&
+       test_path_is_missing $BLOB_FILE
 
 '
 
@@ -59,12 +59,12 @@ test_expect_success 'gc: implicit prune --expire' '
        add_blob &&
        test-chmtime =-$((2*$week-30)) $BLOB_FILE &&
        git gc &&
-       test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
-       test -f $BLOB_FILE &&
+       verbose test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
+       test_path_is_file $BLOB_FILE &&
        test-chmtime =-$((2*$week+1)) $BLOB_FILE &&
        git gc &&
-       test $before = $(git count-objects | sed "s/ .*//") &&
-       ! test -f $BLOB_FILE
+       verbose test $before = $(git count-objects | sed "s/ .*//") &&
+       test_path_is_missing $BLOB_FILE
 
 '
 
@@ -110,7 +110,7 @@ test_expect_success 'prune: do not prune detached HEAD with no reflog' '
        git commit --allow-empty -m "detached commit" &&
        # verify that there is no reflogs
        # (should be removed and disabled by previous test)
-       test ! -e .git/logs &&
+       test_path_is_missing .git/logs &&
        git prune -n >prune_actual &&
        : >prune_expected &&
        test_cmp prune_actual prune_expected
@@ -144,8 +144,8 @@ test_expect_success 'gc --no-prune' '
        test-chmtime =-$((5001*$day)) $BLOB_FILE &&
        git config gc.pruneExpire 2.days.ago &&
        git gc --no-prune &&
-       test 1 = $(git count-objects | sed "s/ .*//") &&
-       test -f $BLOB_FILE
+       verbose test 1 = $(git count-objects | sed "s/ .*//") &&
+       test_path_is_file $BLOB_FILE
 
 '
 
@@ -153,10 +153,10 @@ test_expect_success 'gc respects gc.pruneExpire' '
 
        git config gc.pruneExpire 5002.days.ago &&
        git gc &&
-       test -f $BLOB_FILE &&
+       test_path_is_file $BLOB_FILE &&
        git config gc.pruneExpire 5000.days.ago &&
        git gc &&
-       test ! -f $BLOB_FILE
+       test_path_is_missing $BLOB_FILE
 
 '
 
@@ -165,9 +165,9 @@ test_expect_success 'gc --prune=<date>' '
        add_blob &&
        test-chmtime =-$((5001*$day)) $BLOB_FILE &&
        git gc --prune=5002.days.ago &&
-       test -f $BLOB_FILE &&
+       test_path_is_file $BLOB_FILE &&
        git gc --prune=5000.days.ago &&
-       test ! -f $BLOB_FILE
+       test_path_is_missing $BLOB_FILE
 
 '
 
@@ -175,9 +175,9 @@ test_expect_success 'gc --prune=never' '
 
        add_blob &&
        git gc --prune=never &&
-       test -f $BLOB_FILE &&
+       test_path_is_file $BLOB_FILE &&
        git gc --prune=now &&
-       test ! -f $BLOB_FILE
+       test_path_is_missing $BLOB_FILE
 
 '
 
@@ -186,10 +186,10 @@ test_expect_success 'gc respects gc.pruneExpire=never' '
        git config gc.pruneExpire never &&
        add_blob &&
        git gc &&
-       test -f $BLOB_FILE &&
+       test_path_is_file $BLOB_FILE &&
        git config gc.pruneExpire now &&
        git gc &&
-       test ! -f $BLOB_FILE
+       test_path_is_missing $BLOB_FILE
 
 '
 
@@ -197,9 +197,9 @@ test_expect_success 'prune --expire=never' '
 
        add_blob &&
        git prune --expire=never &&
-       test -f $BLOB_FILE &&
+       test_path_is_file $BLOB_FILE &&
        git prune &&
-       test ! -f $BLOB_FILE
+       test_path_is_missing $BLOB_FILE
 
 '
 
@@ -209,11 +209,11 @@ test_expect_success 'gc: prune old objects after local clone' '
        git clone --no-hardlinks . aclone &&
        (
                cd aclone &&
-               test 1 = $(git count-objects | sed "s/ .*//") &&
-               test -f $BLOB_FILE &&
+               verbose test 1 = $(git count-objects | sed "s/ .*//") &&
+               test_path_is_file $BLOB_FILE &&
                git gc --prune &&
-               test 0 = $(git count-objects | sed "s/ .*//") &&
-               ! test -f $BLOB_FILE
+               verbose test 0 = $(git count-objects | sed "s/ .*//") &&
+               test_path_is_missing $BLOB_FILE
        )
 '
 
@@ -250,7 +250,7 @@ test_expect_success 'prune .git/shallow' '
        grep $SHA1 .git/shallow &&
        grep $SHA1 out &&
        git prune &&
-       ! test -f .git/shallow
+       test_path_is_missing .git/shallow
 '
 
 test_done
index dafd6ad21a92bac48155af39ebb9a4baaf3c4970..b7957b87bb9602748d6f84eeaaeb3c6820b1142e 100644 (file)
@@ -634,6 +634,15 @@ test_cmp_bin() {
        cmp "$@"
 }
 
+# Call any command "$@" but be more verbose about its
+# failure. This is handy for commands like "test" which do
+# not output anything when they fail.
+verbose () {
+       "$@" && return 0
+       echo >&2 "command failed: $(git rev-parse --sq-quote "$@")"
+       return 1
+}
+
 # Check if the file expected to be empty is indeed empty, and barfs
 # otherwise.
 
index 0f4a67bfc63a989883007911f889ba5aed1c0aac..cf19339ccebd0ee5f55822ebe11386101f358f41 100644 (file)
@@ -233,6 +233,10 @@ do
        --root=*)
                root=$(expr "z$1" : 'z[^=]*=\(.*\)')
                shift ;;
+       -x)
+               trace=t
+               verbose=t
+               shift ;;
        *)
                echo "error: unknown test option '$1'" >&2; exit 1 ;;
        esac
@@ -517,10 +521,39 @@ maybe_setup_valgrind () {
        fi
 }
 
+# This is a separate function because some tests use
+# "return" to end a test_expect_success block early
+# (and we want to make sure we run any cleanup like
+# "set +x").
+test_eval_inner_ () {
+       # Do not add anything extra (including LF) after '$*'
+       eval "
+               test \"$trace\" = t && set -x
+               $*"
+}
+
 test_eval_ () {
-       # This is a separate function because some tests use
-       # "return" to end a test_expect_success block early.
-       eval </dev/null >&3 2>&4 "$*"
+       # We run this block with stderr redirected to avoid extra cruft
+       # during a "-x" trace. Once in "set -x" mode, we cannot prevent
+       # the shell from printing the "set +x" to turn it off (nor the saving
+       # of $? before that). But we can make sure that the output goes to
+       # /dev/null.
+       #
+       # The test itself is run with stderr put back to &4 (so either to
+       # /dev/null, or to the original stderr if --verbose was used).
+       {
+               test_eval_inner_ "$@" </dev/null >&3 2>&4
+               test_eval_ret_=$?
+               if test "$trace" = t
+               then
+                       set +x
+                       if test "$test_eval_ret_" != 0
+                       then
+                               say_color error >&4 "error: last command exited with \$?=$test_eval_ret_"
+                       fi
+               fi
+       } 2>/dev/null
+       return $test_eval_ret_
 }
 
 test_run_ () {
@@ -531,7 +564,8 @@ test_run_ () {
        eval_ret=$?
        teardown_malloc_check
 
-       if test -z "$immediate" || test $eval_ret = 0 || test -n "$expecting_failure"
+       if test -z "$immediate" || test $eval_ret = 0 ||
+          test -n "$expecting_failure" && test "$test_cleanup" != ":"
        then
                setup_malloc_check
                test_eval_ "$test_cleanup"