test-lib-functions: make 'test_cmp_rev' more informative on failure

The 'test_cmp_rev' helper is merely a wrapper around 'test_cmp' checking the output of two 'git rev-parse' commands, which means that its output on failure is not particularly informative, as it's basically two OIDs with a bit of extra clutter of the diff header, but without any indication of which two revisions have caused the failure: --- expect.rev 2018-11-17 14:02:11.569747033 +0000 +++ actual.rev 2018-11-17 14:02:11.569747033 +0000 @@ -1 +1 @@ -d79ce1670bdcb76e6d1da2ae095e890ccb326ae9 +139b20d8e6c5b496de61f033f642d0e3dbff528d It also pollutes the test repo with these two intermediate files, though that doesn't seem to cause any complications in our current tests (meaning that I couldn't find any tests that have to work around the presence of these files by explicitly removing or ignoring them). Enhance 'test_cmp_rev' to provide a more useful output on failure with less clutter: error: two revisions point to different objects: 'HEAD^': d79ce1670bdcb76e6d1da2ae095e890ccb326ae9 'extra': 139b20d8e6c5b496de61f033f642d0e3dbff528d Doing so is more convenient when storing the OIDs outputted by 'git rev-parse' in a local variable each, which, as a bonus, won't pollute the repository with intermediate files. While at it, also ensure that 'test_cmp_rev' is invoked with the right number of parameters, namely two. Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

SZEDER Gábor committed Nov 19, 2018 at 14:28 UTC 30d0b6dccbc08d2146697f4369ac6bc78508a4c9
1 file changed +17 -3
t/test-lib-functions.sh
+17 -3
@@ -854,9 +854,23 @@ test_must_be_empty () {
854
855 # Tests that its two parameters refer to the same revision
856 test_cmp_rev () {
857 - git rev-parse --verify "$1" >expect.rev &&
858 - git rev-parse --verify "$2" >actual.rev &&
859 - test_cmp expect.rev actual.rev
857 + if test $# != 2
858 + then
859 + error "bug in the test script: test_cmp_rev requires two revisions, but got $#"
860 + else
861 + local r1 r2
862 + r1=$(git rev-parse --verify "$1") &&
863 + r2=$(git rev-parse --verify "$2") &&
864 + if test "$r1" != "$r2"
865 + then
866 + cat >&4 <<-EOF
867 + error: two revisions point to different objects:
868 + '$1': $r1
869 + '$2': $r2
870 + EOF
871 + return 1
872 + fi
873 + fi
874 }
875
876 # Print a sequence of integers in increasing order, either with