tests: introduce test_must_fail
When we expect a git command to notice and signal errors, we carelessly wrote in our tests: test_expect_success 'reject bogus request' ' do something && do something else && ! git command ' but a non-zero exit could come from the "git command" segfaulting. A new helper function "tset_must_fail" is introduced and it is meant to be used to make sure the command gracefully fails (iow, dying and exiting with non zero status is counted as a failure to "gracefully fail"). The above example should be written as: test_expect_success 'reject bogus request' ' do something && do something else && test_must_fail git command ' Signed-off-by: Junio C Hamano <gitster@pobox.com>
Junio C Hamano committed
Feb 28, 2008 at 13:09 UTC
7435982102093179474a128648179a44042d8a1c
2 files changed
+21
-4
t/t2008-checkout-subdir.sh
+4
-4
@@ -68,15 +68,15 @@ test_expect_success 'checkout with simple prefix' '
68
'
69
70
test_expect_success 'relative path outside tree should fail' \
71
- '! git checkout HEAD -- ../../Makefile'
71
+ 'test_must_fail git checkout HEAD -- ../../Makefile'
72
73
test_expect_success 'incorrect relative path to file should fail (1)' \
74
- '! git checkout HEAD -- ../file0'
74
+ 'test_must_fail git checkout HEAD -- ../file0'
75
76
test_expect_success 'incorrect relative path should fail (2)' \
77
- '( cd dir1 && ! git checkout HEAD -- ./file0 )'
77
+ '( cd dir1 && test_must_fail git checkout HEAD -- ./file0 )'
78
79
test_expect_success 'incorrect relative path should fail (3)' \
80
- '( cd dir1 && ! git checkout HEAD -- ../../file0 )'
80
+ '( cd dir1 && test_must_fail git checkout HEAD -- ../../file0 )'
81
82
test_done
t/test-lib.sh
+17
@@ -270,6 +270,23 @@ test_expect_code () {
270
echo >&3 ""
271
}
272
273
+# This is not among top-level (test_expect_success | test_expect_failure)
274
+# but is a prefix that can be used in the test script, like:
275
+#
276
+# test_expect_success 'complain and die' '
277
+# do something &&
278
+# do something else &&
279
+# test_must_fail git checkout ../outerspace
280
+# '
281
+#
282
+# Writing this as "! git checkout ../outerspace" is wrong, because
283
+# the failure could be due to a segv. We want a controlled failure.
284
+
285
+test_must_fail () {
286
+ "$@"
287
+ test $? -gt 0 -a $? -le 128
288
+}
289
+
290
# Most tests can use the created repository, but some may need to create more.
291
# Usage: test_create_repo <directory>
292
test_create_repo () {