@matheusbsbs / matheus-bagliano / commits / 2c9e125b27
Fork 0

t: teach test_cmp_rev to accept ! for not-equals

In the case where we are using test_cmp_rev() to report not-equals, we write `! test_cmp_rev`. However, since test_cmp_rev() contains r1=$(git rev-parse --verify "$1") && r2=$(git rev-parse --verify "$2") && `! test_cmp_rev` will succeed if any of the rev-parses fail. This behavior is not desired. We want the rev-parses to _always_ be successful. Rewrite test_cmp_rev() to optionally accept "!" as the first argument to do a not-equals comparison. Rewrite `! test_cmp_rev` to `test_cmp_rev !` in all tests to take advantage of this new functionality. Also, rewrite the rev-parse logic to end with a `|| return 1` instead of &&-chaining into the rev-comparison logic. This makes it obvious to future readers that we explicitly intend on returning early if either of the rev-parses fail. Signed-off-by: Denton Liu <liu.denton@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Denton Liu committed Nov 12, 2019 at 15:07 UTC 2c9e125b2727c33dccdba1b2a46f462b77e10bed
8 files changed +25 -14
t/t2400-worktree-add.sh
+2 -2
@@ -438,7 +438,7 @@ test_expect_success 'git worktree add does not match remote' '
438 cd foo &&
439 test_must_fail git config "branch.foo.remote" &&
440 test_must_fail git config "branch.foo.merge" &&
441 - ! test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
441 + test_cmp_rev ! refs/remotes/repo_a/foo refs/heads/foo
442 )
443 '
444
@@ -483,7 +483,7 @@ test_expect_success 'git worktree --no-guess-remote option overrides config' '
483 cd foo &&
484 test_must_fail git config "branch.foo.remote" &&
485 test_must_fail git config "branch.foo.merge" &&
486 - ! test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
486 + test_cmp_rev ! refs/remotes/repo_a/foo refs/heads/foo
487 )
488 '
489
t/t3400-rebase.sh
+1 -1
@@ -64,7 +64,7 @@ test_expect_success 'rebase sets ORIG_HEAD to pre-rebase state' '
64 pre="$(git rev-parse --verify HEAD)" &&
65 git rebase master &&
66 test_cmp_rev "$pre" ORIG_HEAD &&
67 - ! test_cmp_rev "$pre" HEAD
67 + test_cmp_rev ! "$pre" HEAD
68 '
69
70 test_expect_success 'rebase, with <onto> and <upstream> specified as :/quuxery' '
t/t3421-rebase-topology-linear.sh
+3 -3
@@ -61,7 +61,7 @@ test_run_rebase () {
61 test_expect_$result "rebase $* -f rewrites even if upstream is an ancestor" "
62 reset_rebase &&
63 git rebase $* -f b e &&
64 - ! test_cmp_rev e HEAD &&
64 + test_cmp_rev ! e HEAD &&
65 test_cmp_rev b HEAD~2 &&
66 test_linear_range 'd e' b..
67 "
@@ -78,7 +78,7 @@ test_run_rebase () {
78 test_expect_$result "rebase $* -f rewrites even if remote upstream is an ancestor" "
79 reset_rebase &&
80 git rebase $* -f branch-b branch-e &&
81 - ! test_cmp_rev branch-e origin/branch-e &&
81 + test_cmp_rev ! branch-e origin/branch-e &&
82 test_cmp_rev branch-b HEAD~2 &&
83 test_linear_range 'd e' branch-b..
84 "
@@ -368,7 +368,7 @@ test_run_rebase () {
368 test_expect_$result "rebase $* -f --root on linear history causes re-write" "
369 reset_rebase &&
370 git rebase $* -f --root c &&
371 - ! test_cmp_rev a HEAD~2 &&
371 + test_cmp_rev ! a HEAD~2 &&
372 test_linear_range 'a b c' HEAD
373 "
374 }
t/t3430-rebase-merges.sh
+1 -1
@@ -346,7 +346,7 @@ test_expect_success 'A root commit can be a cousin, treat it that way' '
346 git merge --allow-unrelated-histories khnum &&
347 test_tick &&
348 git rebase -f -r HEAD^ &&
349 - ! test_cmp_rev HEAD^2 khnum &&
349 + test_cmp_rev ! HEAD^2 khnum &&
350 test_cmp_graph HEAD^.. <<-\EOF &&
351 * Merge branch '\''khnum'\'' into asherah
352 |\
t/t3432-rebase-fast-forward.sh
+1 -1
@@ -64,7 +64,7 @@ test_rebase_same_head_ () {
64 test_cmp_rev \$oldhead \$newhead
65 elif test $cmp = diff
66 then
67 - ! test_cmp_rev \$oldhead \$newhead
67 + test_cmp_rev ! \$oldhead \$newhead
68 fi
69 "
70 }
t/t3501-revert-cherry-pick.sh
+1 -1
@@ -106,7 +106,7 @@ test_expect_success 'cherry-pick on unborn branch' '
106 rm -rf * &&
107 git cherry-pick initial &&
108 git diff --quiet initial &&
109 - ! test_cmp_rev initial HEAD
109 + test_cmp_rev ! initial HEAD
110 '
111
112 test_expect_success 'cherry-pick "-" to pick from previous branch' '
t/t3508-cherry-pick-many-commits.sh
+1 -1
@@ -5,7 +5,7 @@ test_description='test cherry-picking many commits'
5 . ./test-lib.sh
6
7 check_head_differs_from() {
8 - ! test_cmp_rev HEAD "$1"
8 + test_cmp_rev ! HEAD "$1"
9 }
10
11 check_head_equals() {
t/test-lib-functions.sh
+15 -4
@@ -1012,19 +1012,30 @@ test_must_be_empty () {
1012 fi
1013 }
1014
1015 -# Tests that its two parameters refer to the same revision
1015 +# Tests that its two parameters refer to the same revision, or if '!' is
1016 +# provided first, that its other two parameters refer to different
1017 +# revisions.
1018 test_cmp_rev () {
1019 + local op='=' wrong_result=different
1020 +
1021 + if test $# -ge 1 && test "x$1" = 'x!'
1022 + then
1023 + op='!='
1024 + wrong_result='the same'
1025 + shift
1026 + fi
1027 if test $# != 2
1028 then
1029 error "bug in the test script: test_cmp_rev requires two revisions, but got $#"
1030 else
1031 local r1 r2
1032 r1=$(git rev-parse --verify "$1") &&
1023 - r2=$(git rev-parse --verify "$2") &&
1024 - if test "$r1" != "$r2"
1033 + r2=$(git rev-parse --verify "$2") || return 1
1034 +
1035 + if ! test "$r1" "$op" "$r2"
1036 then
1037 cat >&4 <<-EOF
1027 - error: two revisions point to different objects:
1038 + error: two revisions point to $wrong_result objects:
1039 '$1': $r1
1040 '$2': $r2
1041 EOF