test: add test_expect_equal_file_nonempty

A common bug in tests is that the code used to generate the EXPECTED
file fails, generating no output. When the code generating the OUTPUT
file fails in the same way, the test passes, even though there is a
failure being hidden. Add a new test function that guards against
this.
This commit is contained in:
David Bremner 2021-08-04 07:42:32 -03:00
parent 78045301e7
commit 474a7f8e65

View file

@ -434,6 +434,26 @@ test_expect_equal_file () {
test_diff_file_ "$1" "$2"
}
# Like test_expect_equal, but takes two filenames. Fails if either is empty
test_expect_equal_file_nonempty () {
exec 1>&6 2>&7 # Restore stdout and stderr
if [ -z "$inside_subtest" ]; then
error "bug in the test script: test_expect_equal_file_nonempty without test_begin_subtest"
fi
inside_subtest=
test "$#" = 2 ||
error "bug in the test script: not 2 parameters to test_expect_equal_file_nonempty"
for file in "$1" "$2"; do
if [ ! -s "$file" ]; then
test_failure_ "Missing or zero length file: $file"
return $?
fi
done
test_diff_file_ "$1" "$2"
}
# Like test_expect_equal, but arguments are JSON expressions to be
# canonicalized before diff'ing. If an argument cannot be parsed, it
# is used unchanged so that there's something to diff against.