test_must_be_empty: make sure the file exists, not just empty
authorJunio C Hamano <gitster@pobox.com>
Tue, 27 Feb 2018 21:27:29 +0000 (13:27 -0800)
committerJunio C Hamano <gitster@pobox.com>
Tue, 27 Feb 2018 21:58:43 +0000 (13:58 -0800)
The helper function test_must_be_empty is meant to make sure the
given file is empty, but its implementation is:

if test -s "$1"
then
... not empty, we detected a failure ...
fi

Surely, the file having non-zero size is a sign that the condition
"the file must be empty" is violated, but it misses the case where
the file does not even exist. It is an accident waiting to happen
with a buggy test like this:

git frotz 2>error-message &&
test_must_be_empty errro-message

that won't get caught until you deliberately break 'git frotz' and
notice why the test does not fail.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
t/test-lib-functions.sh
index 1701fe2a06057530d845b91b4fd4cce99b4521a2..d2eaf5ab67269730fd293e2032100cdb34283225 100644 (file)
@@ -718,7 +718,11 @@ verbose () {
 # otherwise.
 
 test_must_be_empty () {
 # otherwise.
 
 test_must_be_empty () {
-       if test -s "$1"
+       if ! test -f "$1"
+       then
+               echo "'$1' is missing"
+               return 1
+       elif test -s "$1"
        then
                echo "'$1' is not empty, it contains:"
                cat "$1"
        then
                echo "'$1' is not empty, it contains:"
                cat "$1"