Skip to content

Commit d572f52

Browse files
sunshinecogitster
authored andcommitted
test_cmp: diagnose incorrect arguments
Under normal circumstances, if a test author misspells a filename passed to test_cmp(), the error is quickly discovered when the test fails unexpectedly due to test_cmp() being unable to find the file. However, if the test is expected to fail, as with test_expect_failure(), a misspelled filename as argument to test_cmp() will go unnoticed since the test will indeed fail, but for the wrong reason. Make it easier for test authors to discover such problems early by sanity-checking the arguments to test_cmp(). To avoid penalizing all clients of test_cmp() in the general case, only check for missing files if the comparison fails. While at it, make test_cmp_bin() sanity-check its arguments, as well. Signed-off-by: Eric Sunshine <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 47ae905 commit d572f52

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

t/test-lib-functions.sh

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,13 @@ test_expect_code () {
905905
# - not all diff versions understand "-u"
906906

907907
test_cmp() {
908-
eval "$GIT_TEST_CMP" '"$@"'
908+
test $# -eq 2 || BUG "test_cmp requires two arguments"
909+
if ! eval "$GIT_TEST_CMP" '"$@"'
910+
then
911+
test "x$1" = x- || test -e "$1" || BUG "test_cmp '$1' missing"
912+
test "x$2" = x- || test -e "$2" || BUG "test_cmp '$2' missing"
913+
return 1
914+
fi
909915
}
910916

911917
# Check that the given config key has the expected value.
@@ -934,7 +940,13 @@ test_cmp_config() {
934940
# test_cmp_bin - helper to compare binary files
935941

936942
test_cmp_bin() {
937-
cmp "$@"
943+
test $# -eq 2 || BUG "test_cmp_bin requires two arguments"
944+
if ! cmp "$@"
945+
then
946+
test "x$1" = x- || test -e "$1" || BUG "test_cmp_bin '$1' missing"
947+
test "x$2" = x- || test -e "$2" || BUG "test_cmp_bin '$2' missing"
948+
return 1
949+
fi
938950
}
939951

940952
# Use this instead of test_cmp to compare files that contain expected and

0 commit comments

Comments
 (0)