Skip to content

Commit f9a2fd3

Browse files
committed
Merge branch 'jk/test-shell-trace'
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"
2 parents 6459cf8 + a136f6d commit f9a2fd3

File tree

4 files changed

+84
-35
lines changed

4 files changed

+84
-35
lines changed

t/README

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ appropriately before running "make".
8282
numbers matching <pattern>. The number matched against is
8383
simply the running count of the test within the file.
8484

85+
-x::
86+
Turn on shell tracing (i.e., `set -x`) during the tests
87+
themselves. Implies `--verbose`. Note that this can cause
88+
failures in some tests which redirect and test the
89+
output of shell functions. Use with caution.
90+
8591
-d::
8692
--debug::
8793
This may help the person who is developing a new test.

t/t5304-prune.sh

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ add_blob() {
1313
before=$(git count-objects | sed "s/ .*//") &&
1414
BLOB=$(echo aleph_0 | git hash-object -w --stdin) &&
1515
BLOB_FILE=.git/objects/$(echo $BLOB | sed "s/^../&\//") &&
16-
test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
17-
test -f $BLOB_FILE &&
16+
verbose test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
17+
test_path_is_file $BLOB_FILE &&
1818
test-chmtime =+0 $BLOB_FILE
1919
}
2020

@@ -35,22 +35,22 @@ test_expect_success 'prune stale packs' '
3535
: > .git/objects/tmp_2.pack &&
3636
test-chmtime =-86501 .git/objects/tmp_1.pack &&
3737
git prune --expire 1.day &&
38-
test -f $orig_pack &&
39-
test -f .git/objects/tmp_2.pack &&
40-
! test -f .git/objects/tmp_1.pack
38+
test_path_is_file $orig_pack &&
39+
test_path_is_file .git/objects/tmp_2.pack &&
40+
test_path_is_missing .git/objects/tmp_1.pack
4141
4242
'
4343

4444
test_expect_success 'prune --expire' '
4545
4646
add_blob &&
4747
git prune --expire=1.hour.ago &&
48-
test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
49-
test -f $BLOB_FILE &&
48+
verbose test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
49+
test_path_is_file $BLOB_FILE &&
5050
test-chmtime =-86500 $BLOB_FILE &&
5151
git prune --expire 1.day &&
52-
test $before = $(git count-objects | sed "s/ .*//") &&
53-
! test -f $BLOB_FILE
52+
verbose test $before = $(git count-objects | sed "s/ .*//") &&
53+
test_path_is_missing $BLOB_FILE
5454
5555
'
5656

@@ -59,12 +59,12 @@ test_expect_success 'gc: implicit prune --expire' '
5959
add_blob &&
6060
test-chmtime =-$((2*$week-30)) $BLOB_FILE &&
6161
git gc &&
62-
test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
63-
test -f $BLOB_FILE &&
62+
verbose test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
63+
test_path_is_file $BLOB_FILE &&
6464
test-chmtime =-$((2*$week+1)) $BLOB_FILE &&
6565
git gc &&
66-
test $before = $(git count-objects | sed "s/ .*//") &&
67-
! test -f $BLOB_FILE
66+
verbose test $before = $(git count-objects | sed "s/ .*//") &&
67+
test_path_is_missing $BLOB_FILE
6868
6969
'
7070

@@ -110,7 +110,7 @@ test_expect_success 'prune: do not prune detached HEAD with no reflog' '
110110
git commit --allow-empty -m "detached commit" &&
111111
# verify that there is no reflogs
112112
# (should be removed and disabled by previous test)
113-
test ! -e .git/logs &&
113+
test_path_is_missing .git/logs &&
114114
git prune -n >prune_actual &&
115115
: >prune_expected &&
116116
test_cmp prune_actual prune_expected
@@ -144,19 +144,19 @@ test_expect_success 'gc --no-prune' '
144144
test-chmtime =-$((5001*$day)) $BLOB_FILE &&
145145
git config gc.pruneExpire 2.days.ago &&
146146
git gc --no-prune &&
147-
test 1 = $(git count-objects | sed "s/ .*//") &&
148-
test -f $BLOB_FILE
147+
verbose test 1 = $(git count-objects | sed "s/ .*//") &&
148+
test_path_is_file $BLOB_FILE
149149
150150
'
151151

152152
test_expect_success 'gc respects gc.pruneExpire' '
153153
154154
git config gc.pruneExpire 5002.days.ago &&
155155
git gc &&
156-
test -f $BLOB_FILE &&
156+
test_path_is_file $BLOB_FILE &&
157157
git config gc.pruneExpire 5000.days.ago &&
158158
git gc &&
159-
test ! -f $BLOB_FILE
159+
test_path_is_missing $BLOB_FILE
160160
161161
'
162162

@@ -165,19 +165,19 @@ test_expect_success 'gc --prune=<date>' '
165165
add_blob &&
166166
test-chmtime =-$((5001*$day)) $BLOB_FILE &&
167167
git gc --prune=5002.days.ago &&
168-
test -f $BLOB_FILE &&
168+
test_path_is_file $BLOB_FILE &&
169169
git gc --prune=5000.days.ago &&
170-
test ! -f $BLOB_FILE
170+
test_path_is_missing $BLOB_FILE
171171
172172
'
173173

174174
test_expect_success 'gc --prune=never' '
175175
176176
add_blob &&
177177
git gc --prune=never &&
178-
test -f $BLOB_FILE &&
178+
test_path_is_file $BLOB_FILE &&
179179
git gc --prune=now &&
180-
test ! -f $BLOB_FILE
180+
test_path_is_missing $BLOB_FILE
181181
182182
'
183183

@@ -186,20 +186,20 @@ test_expect_success 'gc respects gc.pruneExpire=never' '
186186
git config gc.pruneExpire never &&
187187
add_blob &&
188188
git gc &&
189-
test -f $BLOB_FILE &&
189+
test_path_is_file $BLOB_FILE &&
190190
git config gc.pruneExpire now &&
191191
git gc &&
192-
test ! -f $BLOB_FILE
192+
test_path_is_missing $BLOB_FILE
193193
194194
'
195195

196196
test_expect_success 'prune --expire=never' '
197197
198198
add_blob &&
199199
git prune --expire=never &&
200-
test -f $BLOB_FILE &&
200+
test_path_is_file $BLOB_FILE &&
201201
git prune &&
202-
test ! -f $BLOB_FILE
202+
test_path_is_missing $BLOB_FILE
203203
204204
'
205205

@@ -209,11 +209,11 @@ test_expect_success 'gc: prune old objects after local clone' '
209209
git clone --no-hardlinks . aclone &&
210210
(
211211
cd aclone &&
212-
test 1 = $(git count-objects | sed "s/ .*//") &&
213-
test -f $BLOB_FILE &&
212+
verbose test 1 = $(git count-objects | sed "s/ .*//") &&
213+
test_path_is_file $BLOB_FILE &&
214214
git gc --prune &&
215-
test 0 = $(git count-objects | sed "s/ .*//") &&
216-
! test -f $BLOB_FILE
215+
verbose test 0 = $(git count-objects | sed "s/ .*//") &&
216+
test_path_is_missing $BLOB_FILE
217217
)
218218
'
219219

@@ -250,7 +250,7 @@ test_expect_success 'prune .git/shallow' '
250250
grep $SHA1 .git/shallow &&
251251
grep $SHA1 out &&
252252
git prune &&
253-
! test -f .git/shallow
253+
test_path_is_missing .git/shallow
254254
'
255255

256256
test_done

t/test-lib-functions.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,15 @@ test_cmp_bin() {
634634
cmp "$@"
635635
}
636636

637+
# Call any command "$@" but be more verbose about its
638+
# failure. This is handy for commands like "test" which do
639+
# not output anything when they fail.
640+
verbose () {
641+
"$@" && return 0
642+
echo >&2 "command failed: $(git rev-parse --sq-quote "$@")"
643+
return 1
644+
}
645+
637646
# Check if the file expected to be empty is indeed empty, and barfs
638647
# otherwise.
639648

t/test-lib.sh

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,10 @@ do
233233
--root=*)
234234
root=$(expr "z$1" : 'z[^=]*=\(.*\)')
235235
shift ;;
236+
-x)
237+
trace=t
238+
verbose=t
239+
shift ;;
236240
*)
237241
echo "error: unknown test option '$1'" >&2; exit 1 ;;
238242
esac
@@ -517,10 +521,39 @@ maybe_setup_valgrind () {
517521
fi
518522
}
519523

524+
# This is a separate function because some tests use
525+
# "return" to end a test_expect_success block early
526+
# (and we want to make sure we run any cleanup like
527+
# "set +x").
528+
test_eval_inner_ () {
529+
# Do not add anything extra (including LF) after '$*'
530+
eval "
531+
test \"$trace\" = t && set -x
532+
$*"
533+
}
534+
520535
test_eval_ () {
521-
# This is a separate function because some tests use
522-
# "return" to end a test_expect_success block early.
523-
eval </dev/null >&3 2>&4 "$*"
536+
# We run this block with stderr redirected to avoid extra cruft
537+
# during a "-x" trace. Once in "set -x" mode, we cannot prevent
538+
# the shell from printing the "set +x" to turn it off (nor the saving
539+
# of $? before that). But we can make sure that the output goes to
540+
# /dev/null.
541+
#
542+
# The test itself is run with stderr put back to &4 (so either to
543+
# /dev/null, or to the original stderr if --verbose was used).
544+
{
545+
test_eval_inner_ "$@" </dev/null >&3 2>&4
546+
test_eval_ret_=$?
547+
if test "$trace" = t
548+
then
549+
set +x
550+
if test "$test_eval_ret_" != 0
551+
then
552+
say_color error >&4 "error: last command exited with \$?=$test_eval_ret_"
553+
fi
554+
fi
555+
} 2>/dev/null
556+
return $test_eval_ret_
524557
}
525558

526559
test_run_ () {
@@ -531,7 +564,8 @@ test_run_ () {
531564
eval_ret=$?
532565
teardown_malloc_check
533566

534-
if test -z "$immediate" || test $eval_ret = 0 || test -n "$expecting_failure"
567+
if test -z "$immediate" || test $eval_ret = 0 ||
568+
test -n "$expecting_failure" && test "$test_cleanup" != ":"
535569
then
536570
setup_malloc_check
537571
test_eval_ "$test_cleanup"

0 commit comments

Comments
 (0)