[PATCH] test_must_be_empty: make sure the file exists, not just empty

Subsystems: the rest

STALE3135d

3 messages, 3 authors, 2018-02-27 · open the first message on its own page

[PATCH] test_must_be_empty: make sure the file exists, not just empty

From: Junio C Hamano <hidden>
Date: 2018-02-27 21:27:36

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 <redacted>
---
 t/test-lib-functions.sh | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh
index 37eb34044a..6cfbee60e4 100644
--- a/t/test-lib-functions.sh
+++ b/t/test-lib-functions.sh
@@ -772,7 +772,11 @@ verbose () {
 # 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"
-- 
2.16.2-325-g2fc74f41c5

Re: [PATCH] test_must_be_empty: make sure the file exists, not just empty

From: Stefan Beller <hidden>
Date: 2018-02-27 21:42:46

On Tue, Feb 27, 2018 at 1:27 PM, Junio C Hamano [off-list ref] wrote:
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 <redacted>
Reviewed-by: Stefan Beller <redacted>

Re: [PATCH] test_must_be_empty: make sure the file exists, not just empty

From: Jeff King <hidden>
Date: 2018-02-27 22:08:29

On Tue, Feb 27, 2018 at 01:27:29PM -0800, Junio C Hamano wrote:
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 <redacted>
This seems like a huge and obvious improvement to me. I'm amazed it
hasn't come up before (and that this doesn't reveal any existing typos
like the one you showed).

-Peff
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help