@matheusbsbs / matheus-bagliano / commits / d572f52a64
Fork 0

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 <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric Sunshine committed Aug 9, 2020 at 13:42 UTC d572f52a64c6a69990f72ad6a09504b9b615d2e4
1 file changed +14 -2
t/test-lib-functions.sh
+14 -2
@@ -905,7 +905,13 @@ test_expect_code () {
905 # - not all diff versions understand "-u"
906
907 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
915 }
916
917 # Check that the given config key has the expected value.
@@ -934,7 +940,13 @@ test_cmp_config() {
940 # test_cmp_bin - helper to compare binary files
941
942 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
950 }
951
952 # Use this instead of test_cmp to compare files that contain expected and