Skip to content

Commit

Permalink
Merge branch 'jk/test-shell-trace'
Browse files Browse the repository at this point in the history
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"
  • Loading branch information
Junio C Hamano committed Oct 20, 2014
2 parents 6459cf8 + a136f6d commit f9a2fd3
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 35 deletions.
6 changes: 6 additions & 0 deletions t/README
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
62 changes: 31 additions & 31 deletions t/t5304-prune.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -35,22 +35,22 @@ 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
'

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
'

Expand All @@ -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
'

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -144,19 +144,19 @@ 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
'

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
'

Expand All @@ -165,19 +165,19 @@ 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
'

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
'

Expand All @@ -186,20 +186,20 @@ 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
'

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
'

Expand All @@ -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
)
'

Expand Down Expand Up @@ -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
9 changes: 9 additions & 0 deletions t/test-lib-functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
42 changes: 38 additions & 4 deletions t/test-lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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_ () {
Expand All @@ -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"
Expand Down

0 comments on commit f9a2fd3

Please sign in to comment.