Skip to content

Commit 4d9d62f

Browse files
Pavel RoskinJunio C Hamano
authored andcommitted
[PATCH] Trapping exit in tests, using return for errors
I have noticed that "make test" fails without any explanations when the "merge" utility is missing. I don't think tests should be silent in case of failure. It turned out that the particular test was using "exit" to interrupt the test in case of an error. This caused the whole test script to exit. No further tests would be run even if "--immediate" wasn't specified. No error message was printed. This patch does following: All instances of "exit", "exit 1" and "(exit 1)" in tests have been replaced with "return 1". In fact, "(exit 1)" had no effect. File descriptor 5 is duplicated from file descriptor 1. This is needed to print important error messages from tests. New function test_run_() has been introduced. Any "return" in the test would merely cause that function to return without skipping calls to test_failure_() and test_ok_(). The new function also traps "exit" and treats it like a fatal error (in case somebody reintroduces "exit" in the tests). test_expect_failure() and test_expect_success() check both the result of eval and the return value of test_run_(). If the later is not 0, it's always a failure because it indicates the the test didn't complete. Signed-off-by: Pavel Roskin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d9bdd39 commit 4d9d62f

7 files changed

+39
-28
lines changed

t/t1001-read-tree-m-2way.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ test_expect_success \
100100
git-checkout-cache -u -f -q -a &&
101101
git-update-cache --add yomin &&
102102
read_tree_twoway $treeH $treeM &&
103-
git-ls-files --stage >4.out || exit
103+
git-ls-files --stage >4.out || return 1
104104
diff -u M.out 4.out >4diff.out
105105
compare_change 4diff.out expected &&
106106
check_cache_at yomin clean'
@@ -114,7 +114,7 @@ test_expect_success \
114114
git-update-cache --add yomin &&
115115
echo yomin yomin >yomin &&
116116
read_tree_twoway $treeH $treeM &&
117-
git-ls-files --stage >5.out || exit
117+
git-ls-files --stage >5.out || return 1
118118
diff -u M.out 5.out >5diff.out
119119
compare_change 5diff.out expected &&
120120
check_cache_at yomin dirty'
@@ -215,7 +215,7 @@ test_expect_success \
215215
echo nitfol nitfol >nitfol &&
216216
git-update-cache --add nitfol &&
217217
read_tree_twoway $treeH $treeM &&
218-
git-ls-files --stage >14.out || exit
218+
git-ls-files --stage >14.out || return 1
219219
diff -u M.out 14.out >14diff.out
220220
compare_change 14diff.out expected &&
221221
check_cache_at nitfol clean'
@@ -229,7 +229,7 @@ test_expect_success \
229229
git-update-cache --add nitfol &&
230230
echo nitfol nitfol nitfol >nitfol &&
231231
read_tree_twoway $treeH $treeM &&
232-
git-ls-files --stage >15.out || exit
232+
git-ls-files --stage >15.out || return 1
233233
diff -u M.out 15.out >15diff.out
234234
compare_change 15diff.out expected &&
235235
check_cache_at nitfol dirty'

t/t1002-read-tree-m-u-2way.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ test_expect_success \
7373
'rm -f .git/index &&
7474
git-update-cache --add yomin &&
7575
git-read-tree -m -u $treeH $treeM &&
76-
git-ls-files --stage >4.out || exit
76+
git-ls-files --stage >4.out || return 1
7777
diff --unified=0 M.out 4.out >4diff.out
7878
compare_change 4diff.out expected &&
7979
check_cache_at yomin clean &&
@@ -90,7 +90,7 @@ test_expect_success \
9090
git-update-cache --add yomin &&
9191
echo yomin yomin >yomin &&
9292
git-read-tree -m -u $treeH $treeM &&
93-
git-ls-files --stage >5.out || exit
93+
git-ls-files --stage >5.out || return 1
9494
diff --unified=0 M.out 5.out >5diff.out
9595
compare_change 5diff.out expected &&
9696
check_cache_at yomin dirty &&
@@ -192,7 +192,7 @@ test_expect_success \
192192
echo nitfol nitfol >nitfol &&
193193
git-update-cache --add nitfol &&
194194
git-read-tree -m -u $treeH $treeM &&
195-
git-ls-files --stage >14.out || exit
195+
git-ls-files --stage >14.out || return 1
196196
diff --unified=0 M.out 14.out >14diff.out
197197
compare_change 14diff.out expected &&
198198
sum bozbar frotz >actual14.sum &&
@@ -212,7 +212,7 @@ test_expect_success \
212212
git-update-cache --add nitfol &&
213213
echo nitfol nitfol nitfol >nitfol &&
214214
git-read-tree -m -u $treeH $treeM &&
215-
git-ls-files --stage >15.out || exit
215+
git-ls-files --stage >15.out || return 1
216216
diff --unified=0 M.out 15.out >15diff.out
217217
compare_change 15diff.out expected &&
218218
check_cache_at nitfol dirty &&

t/t1005-read-tree-m-2way-emu23.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ test_expect_success \
120120
git-checkout-cache -u -f -q -a &&
121121
git-update-cache --add yomin &&
122122
read_tree_twoway $treeH $treeM &&
123-
git-ls-files --stage >4.out || exit
123+
git-ls-files --stage >4.out || return 1
124124
diff -u M.out 4.out >4diff.out
125125
compare_change 4diff.out expected &&
126126
check_cache_at yomin clean'
@@ -136,7 +136,7 @@ test_expect_success \
136136
git-update-cache --add yomin &&
137137
echo yomin yomin >yomin &&
138138
read_tree_twoway $treeH $treeM &&
139-
git-ls-files --stage >5.out || exit
139+
git-ls-files --stage >5.out || return 1
140140
diff -u M.out 5.out >5diff.out
141141
compare_change 5diff.out expected &&
142142
check_cache_at yomin dirty'
@@ -241,7 +241,7 @@ test_expect_success \
241241
echo nitfol nitfol >nitfol &&
242242
git-update-cache --add nitfol &&
243243
read_tree_twoway $treeH $treeM &&
244-
git-ls-files --stage >14.out || exit
244+
git-ls-files --stage >14.out || return 1
245245
diff -u M.out 14.out >14diff.out
246246
compare_change 14diff.out expected &&
247247
check_cache_at nitfol clean'
@@ -255,7 +255,7 @@ test_expect_success \
255255
git-update-cache --add nitfol &&
256256
echo nitfol nitfol nitfol >nitfol &&
257257
read_tree_twoway $treeH $treeM &&
258-
git-ls-files --stage >15.out || exit
258+
git-ls-files --stage >15.out || return 1
259259
diff -u M.out 15.out >15diff.out
260260
compare_change 15diff.out expected &&
261261
check_cache_at nitfol dirty'
@@ -352,7 +352,7 @@ test_expect_success \
352352
sed -e "s/such as/SUCH AS/" bozbar-old >bozbar &&
353353
git-update-cache --add bozbar &&
354354
read_tree_twoway $treeH $treeM &&
355-
git-ls-files --stage >22.out || exit
355+
git-ls-files --stage >22.out || return 1
356356
diff -u M.out 22.out >22diff.out
357357
compare_change 22diff.out &&
358358
check_cache_at bozbar clean'

t/t4002-diff-basic.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ test_expect_success \
191191
'rm -fr Z [A-Z][A-Z] &&
192192
git-read-tree $tree_A &&
193193
git-checkout-cache -f -a &&
194-
git-read-tree -m $tree_O || (exit 1)
194+
git-read-tree -m $tree_O || return 1
195195
git-update-cache --refresh >/dev/null ;# this can exit non-zero
196196
git-diff-files >.test-a &&
197197
cmp_diff_files_output .test-a .test-recursive-OA'
@@ -201,7 +201,7 @@ test_expect_success \
201201
'rm -fr Z [A-Z][A-Z] &&
202202
git-read-tree $tree_B &&
203203
git-checkout-cache -f -a &&
204-
git-read-tree -m $tree_O || (exit 1)
204+
git-read-tree -m $tree_O || return 1
205205
git-update-cache --refresh >/dev/null ;# this can exit non-zero
206206
git-diff-files >.test-a &&
207207
cmp_diff_files_output .test-a .test-recursive-OB'
@@ -211,7 +211,7 @@ test_expect_success \
211211
'rm -fr Z [A-Z][A-Z] &&
212212
git-read-tree $tree_B &&
213213
git-checkout-cache -f -a &&
214-
git-read-tree -m $tree_A || (exit 1)
214+
git-read-tree -m $tree_A || return 1
215215
git-update-cache --refresh >/dev/null ;# this can exit non-zero
216216
git-diff-files >.test-a &&
217217
cmp_diff_files_output .test-a .test-recursive-AB'

t/t5300-pack-object.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ test_expect_success \
1616
for i in a b c
1717
do
1818
dd if=/dev/zero bs=4k count=1 | tr "\\0" $i >$i &&
19-
git-update-cache --add $i || exit
19+
git-update-cache --add $i || return 1
2020
done &&
2121
cat c >d && echo foo >>d && git-update-cache --add d &&
2222
tree=`git-write-tree` &&
@@ -29,7 +29,7 @@ test_expect_success \
2929
while read object
3030
do
3131
t=`git-cat-file -t $object` &&
32-
git-cat-file $t $object || exit 1
32+
git-cat-file $t $object || return 1
3333
done <obj-list
3434
} >expect'
3535

@@ -58,7 +58,7 @@ test_expect_success \
5858
do
5959
cmp $path ../.git/$path || {
6060
echo $path differs.
61-
exit 1
61+
return 1
6262
}
6363
done'
6464
cd $TRASH
@@ -88,7 +88,7 @@ test_expect_success \
8888
do
8989
cmp $path ../.git/$path || {
9090
echo $path differs.
91-
exit 1
91+
return 1
9292
}
9393
done'
9494
cd $TRASH
@@ -106,7 +106,7 @@ test_expect_success \
106106
while read object
107107
do
108108
t=`git-cat-file -t $object` &&
109-
git-cat-file $t $object || exit 1
109+
git-cat-file $t $object || return 1
110110
done <obj-list
111111
} >current &&
112112
diff expect current'
@@ -122,7 +122,7 @@ test_expect_success \
122122
while read object
123123
do
124124
t=`git-cat-file -t $object` &&
125-
git-cat-file $t $object || exit 1
125+
git-cat-file $t $object || return 1
126126
done <obj-list
127127
} >current &&
128128
diff expect current'

t/t5400-send-pack.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ test_expect_success setup '
1818
do
1919
sleep 1 &&
2020
commit=$(echo "Commit #$i" | git-commit-tree $tree -p $parent) &&
21-
parent=$commit || exit
21+
parent=$commit || return 1
2222
done &&
2323
echo "$commit" >.git/HEAD &&
2424
git clone -l ./. victim &&
@@ -31,7 +31,7 @@ test_expect_success setup '
3131
do
3232
sleep 1 &&
3333
commit=$(echo "Rebase #$i" | git-commit-tree $tree -p $parent) &&
34-
parent=$commit || exit
34+
parent=$commit || return 1
3535
done &&
3636
echo "$commit" >.git/HEAD &&
3737
echo Rebase &&

t/test-lib.sh

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ do
6363
esac
6464
done
6565

66+
exec 5>&1
6667
if test "$verbose" = "t"
6768
then
6869
exec 4>&2 3>&1
@@ -96,23 +97,33 @@ test_debug () {
9697
test "$debug" = "" || eval "$1"
9798
}
9899

100+
test_run_ () {
101+
trap 'echo >&5 "FATAL: Unexpected exit with code $?"; exit 1' exit
102+
eval >&3 2>&4 "$1"
103+
eval_ret="$?"
104+
trap - exit
105+
return 0
106+
}
107+
99108
test_expect_failure () {
100109
test "$#" = 2 ||
101110
error "bug in the test script: not 2 parameters to test-expect-failure"
102111
say >&3 "expecting failure: $2"
103-
if eval >&3 2>&4 "$2"
112+
test_run_ "$2"
113+
if [ "$?" = 0 -a "$eval_ret" != 0 ]
104114
then
105-
test_failure_ "$@"
106-
else
107115
test_ok_ "$1"
116+
else
117+
test_failure_ "$@"
108118
fi
109119
}
110120

111121
test_expect_success () {
112122
test "$#" = 2 ||
113123
error "bug in the test script: not 2 parameters to test-expect-success"
114124
say >&3 "expecting success: $2"
115-
if eval >&3 2>&4 "$2"
125+
test_run_ "$2"
126+
if [ "$?" = 0 -a "$eval_ret" = 0 ]
116127
then
117128
test_ok_ "$1"
118129
else

0 commit comments

Comments
 (0)