git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v6 01/14] t: teach test_cmp_rev to accept ! for not-equals

From
Denton Liu <liu.denton@gmail.com>
Date
Nov 12, 2019, 23:07 UTC
Message-ID
<0532847787dfd48fbe4b41a4a7d2783748f3bd7f.1573595985.git.liu.denton@gmail.com>
In-Reply-To
<cover.1573595985.git.liu.denton@gmail.com>

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>
---
 t/t2400-worktree-add.sh             |  4 ++--
 t/t3400-rebase.sh                   |  2 +-
 t/t3421-rebase-topology-linear.sh   |  6 +++---
 t/t3430-rebase-merges.sh            |  2 +-
 t/t3432-rebase-fast-forward.sh      |  2 +-
 t/t3501-revert-cherry-pick.sh       |  2 +-
 t/t3508-cherry-pick-many-commits.sh |  2 +-
 t/test-lib-functions.sh             | 20 ++++++++++++++++----
 8 files changed, 26 insertions(+), 14 deletions(-)
diff --git a/t/t2400-worktree-add.sh b/t/t2400-worktree-add.sh
index e819ba741e..52d476979b 100755
--- a/t/t2400-worktree-add.sh
+++ b/t/t2400-worktree-add.sh
@@ -438,7 +438,7 @@ test_expect_success 'git worktree add does not match remote' '
 		cd foo &&
 		test_must_fail git config "branch.foo.remote" &&
 		test_must_fail git config "branch.foo.merge" &&
-		! test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
+		test_cmp_rev ! refs/remotes/repo_a/foo refs/heads/foo
 	)
 '
 
@@ -483,7 +483,7 @@ test_expect_success 'git worktree --no-guess-remote option overrides config' '
 		cd foo &&
 		test_must_fail git config "branch.foo.remote" &&
 		test_must_fail git config "branch.foo.merge" &&
-		! test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
+		test_cmp_rev ! refs/remotes/repo_a/foo refs/heads/foo
 	)
 '
 
diff --git a/t/t3400-rebase.sh b/t/t3400-rebase.sh
index ab18ac5f28..f267f6cd54 100755
--- a/t/t3400-rebase.sh
+++ b/t/t3400-rebase.sh
@@ -64,7 +64,7 @@ test_expect_success 'rebase sets ORIG_HEAD to pre-rebase state' '
 	pre="$(git rev-parse --verify HEAD)" &&
 	git rebase master &&
 	test_cmp_rev "$pre" ORIG_HEAD &&
-	! test_cmp_rev "$pre" HEAD
+	test_cmp_rev ! "$pre" HEAD
 '
 
 test_expect_success 'rebase, with <onto> and <upstream> specified as :/quuxery' '
diff --git a/t/t3421-rebase-topology-linear.sh b/t/t3421-rebase-topology-linear.sh
index b847064f91..325072b0a3 100755
--- a/t/t3421-rebase-topology-linear.sh
+++ b/t/t3421-rebase-topology-linear.sh
@@ -61,7 +61,7 @@ test_run_rebase () {
 	test_expect_$result "rebase $* -f rewrites even if upstream is an ancestor" "
 		reset_rebase &&
 		git rebase $* -f b e &&
-		! test_cmp_rev e HEAD &&
+		test_cmp_rev ! e HEAD &&
 		test_cmp_rev b HEAD~2 &&
 		test_linear_range 'd e' b..
 	"
@@ -78,7 +78,7 @@ test_run_rebase () {
 	test_expect_$result "rebase $* -f rewrites even if remote upstream is an ancestor" "
 		reset_rebase &&
 		git rebase $* -f branch-b branch-e &&
-		! test_cmp_rev branch-e origin/branch-e &&
+		test_cmp_rev ! branch-e origin/branch-e &&
 		test_cmp_rev branch-b HEAD~2 &&
 		test_linear_range 'd e' branch-b..
 	"
@@ -368,7 +368,7 @@ test_run_rebase () {
 	test_expect_$result "rebase $* -f --root on linear history causes re-write" "
 		reset_rebase &&
 		git rebase $* -f --root c &&
-		! test_cmp_rev a HEAD~2 &&
+		test_cmp_rev ! a HEAD~2 &&
 		test_linear_range 'a b c' HEAD
 	"
 }
diff --git a/t/t3430-rebase-merges.sh b/t/t3430-rebase-merges.sh
index 9efcf4808a..abbdc26b1b 100755
--- a/t/t3430-rebase-merges.sh
+++ b/t/t3430-rebase-merges.sh
@@ -346,7 +346,7 @@ test_expect_success 'A root commit can be a cousin, treat it that way' '
 	git merge --allow-unrelated-histories khnum &&
 	test_tick &&
 	git rebase -f -r HEAD^ &&
-	! test_cmp_rev HEAD^2 khnum &&
+	test_cmp_rev ! HEAD^2 khnum &&
 	test_cmp_graph HEAD^.. <<-\EOF &&
 	*   Merge branch '\''khnum'\'' into asherah
 	|\
diff --git a/t/t3432-rebase-fast-forward.sh b/t/t3432-rebase-fast-forward.sh
index 034ffc7e76..92f95b57da 100755
--- a/t/t3432-rebase-fast-forward.sh
+++ b/t/t3432-rebase-fast-forward.sh
@@ -64,7 +64,7 @@ test_rebase_same_head_ () {
 			test_cmp_rev \$oldhead \$newhead
 		elif test $cmp = diff
 		then
-			! test_cmp_rev \$oldhead \$newhead
+			test_cmp_rev ! \$oldhead \$newhead
 		fi
 	"
 }
diff --git a/t/t3501-revert-cherry-pick.sh b/t/t3501-revert-cherry-pick.sh
index d1c68af8c5..1c51a9131d 100755
--- a/t/t3501-revert-cherry-pick.sh
+++ b/t/t3501-revert-cherry-pick.sh
@@ -106,7 +106,7 @@ test_expect_success 'cherry-pick on unborn branch' '
 	rm -rf * &&
 	git cherry-pick initial &&
 	git diff --quiet initial &&
-	! test_cmp_rev initial HEAD
+	test_cmp_rev ! initial HEAD
 '
 
 test_expect_success 'cherry-pick "-" to pick from previous branch' '
diff --git a/t/t3508-cherry-pick-many-commits.sh b/t/t3508-cherry-pick-many-commits.sh
index b457333e18..23070a7b73 100755
--- a/t/t3508-cherry-pick-many-commits.sh
+++ b/t/t3508-cherry-pick-many-commits.sh
@@ -5,7 +5,7 @@ test_description='test cherry-picking many commits'
 . ./test-lib.sh
 
 check_head_differs_from() {
-	! test_cmp_rev HEAD "$1"
+	test_cmp_rev ! HEAD "$1"
 }
 
 check_head_equals() {
diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh
index b299ecc326..15f4c96777 100644
--- a/t/test-lib-functions.sh
+++ b/t/test-lib-functions.sh
@@ -1012,19 +1012,31 @@ test_must_be_empty () {
 	fi
 }
 
-# Tests that its two parameters refer to the same revision
+# Tests that its two parameters refer to the same revision, or if '!' is
+# provided first, that its other two parameters refer to different
+# revisions.
 test_cmp_rev () {
+	local op wrong_result
+	op='='
+	wrong_result='different'
+	if test $# -ge 1 && test "x$1" = 'x!'
+	then
+	    op='!='
+	    wrong_result='the same'
+	    shift
+	fi
 	if test $# != 2
 	then
 		error "bug in the test script: test_cmp_rev requires two revisions, but got $#"
 	else
 		local r1 r2
 		r1=$(git rev-parse --verify "$1") &&
-		r2=$(git rev-parse --verify "$2") &&
-		if test "$r1" != "$r2"
+		r2=$(git rev-parse --verify "$2") || return 1
+
+		if ! test "$r1" "$op" "$r2"
 		then
 			cat >&4 <<-EOF
-			error: two revisions point to different objects:
+			error: two revisions point to $wrong_result objects:
 			  '$1': $r1
 			  '$2': $r2
 			EOF
-- 
2.24.0.346.gee0de6d492
Previous: Denton LiuNext: Junio C Hamano
Message 100 of 116 in “t5520: various test cleanup”
  1. 00/12 t5520: various test cleanupDenton Liu, Oct 17, 2019
  2. 01/12 t5520: improve test styleDenton Liu, Oct 17, 2019
  3. 02/12 t5520: use sq for test case namesDenton Liu, Oct 17, 2019
  4. 03/12 t5520: let sed open its own inputDenton Liu, Oct 17, 2019
  5. 05/12 t5520: remove spaces after redirect operatorDenton Liu, Oct 17, 2019
  6. 04/12 t5520: replace test -f with test_path_is_fileDenton Liu, Oct 17, 2019
  7. Eric SunshineOct 17, 2019
  8. 5/12 t5520: replace test -f with test-lib functionsDenton Liu, Oct 17, 2019
  9. Eric SunshineOct 18, 2019
  10. 06/12 t5520: use test_line_count where possibleDenton Liu, Oct 17, 2019
  11. 07/12 t5520: replace test -{n,z} with test-lib functionsDenton Liu, Oct 17, 2019
  12. Eric SunshineOct 17, 2019
  13. 08/12 t5520: use test_cmp_rev where possibleDenton Liu, Oct 17, 2019
  14. Eric SunshineOct 17, 2019
  15. Denton LiuOct 18, 2019
  16. Eric SunshineOct 23, 2019
  17. 09/12 t5520: test single-line files by git with test_cmpDenton Liu, Oct 17, 2019
  18. Eric SunshineOct 17, 2019
  19. 10/12 t5520: don't put git in upstream of pipeDenton Liu, Oct 17, 2019
  20. 11/12 t5520: replace subshell cat comparison with test_cmpDenton Liu, Oct 17, 2019
  21. 12/12 t5520: replace `! git` with `test_must_fail git`Denton Liu, Oct 17, 2019
  22. 00/15 t5520: various test cleanupDenton Liu, Oct 18, 2019
  23. 01/15 t7408: replace `test_must_fail test_path_is_file`Denton Liu, Oct 18, 2019
  24. Johannes SixtOct 19, 2019
  25. 09/15 t5520: replace test -{n,z} with test-lib functionsDenton Liu, Oct 18, 2019
  26. 11/15 t5520: test single-line files by git with test_cmpDenton Liu, Oct 18, 2019
  27. 06/15 t5520: replace test -f with test-lib functionsDenton Liu, Oct 18, 2019
  28. 12/15 t5520: don't put git in upstream of pipeDenton Liu, Oct 18, 2019
  29. 14/15 t5520: remove redundant lines in test casesDenton Liu, Oct 18, 2019
  30. 15/15 t5520: replace `! git` with `test_must_fail git`Denton Liu, Oct 18, 2019
  31. 13/15 t5520: replace subshell cat comparison with test_cmpDenton Liu, Oct 18, 2019
  32. 10/15 t5520: use test_cmp_rev where possibleDenton Liu, Oct 18, 2019
  33. 08/15 t5520: use test_line_count where possibleDenton Liu, Oct 18, 2019
  34. 07/15 t5520: remove spaces after redirect operatorDenton Liu, Oct 18, 2019
  35. 05/15 t5520: let sed open its own inputDenton Liu, Oct 18, 2019
  36. 04/15 t5520: use sq for test case namesDenton Liu, Oct 18, 2019
  37. 03/15 t5520: improve test styleDenton Liu, Oct 18, 2019
  38. 02/15 t: teach test_cmp_rev to accept ! for not-equalsDenton Liu, Oct 18, 2019
  39. SZEDER GáborOct 18, 2019
  40. Denton LiuOct 18, 2019
  41. 00/14 t5520: various test cleanupDenton Liu, Oct 22, 2019
  42. 01/14 t: teach test_cmp_rev to accept ! for not-equalsDenton Liu, Oct 22, 2019
  43. 02/14 t5520: improve test styleDenton Liu, Oct 22, 2019
  44. 03/14 t5520: use sq for test case namesDenton Liu, Oct 22, 2019
  45. 04/14 t5520: let sed open its own inputDenton Liu, Oct 22, 2019
  46. 05/14 t5520: replace test -f with test-lib functionsDenton Liu, Oct 22, 2019
  47. 06/14 t5520: remove spaces after redirect operatorDenton Liu, Oct 22, 2019
  48. 07/14 t5520: use test_line_count where possibleDenton Liu, Oct 22, 2019
  49. 08/14 t5520: replace test -{n,z} with test-lib functionsDenton Liu, Oct 22, 2019
  50. 09/14 t5520: use test_cmp_rev where possibleDenton Liu, Oct 22, 2019
  51. 10/14 t5520: test single-line files by git with test_cmpDenton Liu, Oct 22, 2019
  52. 11/14 t5520: don't put git in upstream of pipeDenton Liu, Oct 22, 2019
  53. 12/14 t5520: replace subshell cat comparison with test_cmpDenton Liu, Oct 22, 2019
  54. 13/14 t5520: remove redundant lines in test casesDenton Liu, Oct 22, 2019
  55. 14/14 t5520: replace `! git` with `test_must_fail git`Denton Liu, Oct 22, 2019
  56. Denton LiuOct 24, 2019
  57. Junio C HamanoOct 25, 2019
  58. Denton LiuNov 4, 2019
  59. Junio C HamanoNov 6, 2019
  60. 00/14 t5520: various test cleanupDenton Liu, Nov 7, 2019
  61. 01/14 t: teach test_cmp_rev to accept ! for not-equalsDenton Liu, Nov 7, 2019
  62. Junio C HamanoNov 8, 2019
  63. Denton LiuNov 8, 2019
  64. Junio C HamanoNov 8, 2019
  65. Denton LiuNov 8, 2019
  66. Junio C HamanoNov 10, 2019
  67. 02/14 t5520: improve test styleDenton Liu, Nov 7, 2019
  68. 03/14 t5520: use sq for test case namesDenton Liu, Nov 7, 2019
  69. 04/14 t5520: let sed open its own inputDenton Liu, Nov 7, 2019
  70. 05/14 t5520: replace test -f with test-lib functionsDenton Liu, Nov 7, 2019
  71. 06/14 t5520: remove spaces after redirect operatorDenton Liu, Nov 7, 2019
  72. 07/14 t5520: use test_line_count where possibleDenton Liu, Nov 7, 2019
  73. 08/14 t5520: replace test -{n,z} with test-lib functionsDenton Liu, Nov 7, 2019
  74. 09/14 t5520: use test_cmp_rev where possibleDenton Liu, Nov 7, 2019
  75. 10/14 t5520: test single-line files by git with test_cmpDenton Liu, Nov 7, 2019
  76. 11/14 t5520: don't put git in upstream of pipeDenton Liu, Nov 7, 2019
  77. 12/14 t5520: replace subshell cat comparison with test_cmpDenton Liu, Nov 7, 2019
  78. 13/14 t5520: remove redundant lines in test casesDenton Liu, Nov 7, 2019
  79. 14/14 t5520: replace `! git` with `test_must_fail git`Denton Liu, Nov 7, 2019
  80. 00/14 t5520: various test cleanupDenton Liu, Nov 12, 2019
  81. 01/14 t: teach test_cmp_rev to accept ! for not-equalsDenton Liu, Nov 12, 2019
  82. Junio C HamanoNov 12, 2019
  83. fixup! t: teach test_cmp_rev to accept ! for not-equalsDenton Liu, Nov 12, 2019
  84. 02/14 t5520: improve test styleDenton Liu, Nov 12, 2019
  85. 03/14 t5520: use sq for test case namesDenton Liu, Nov 12, 2019
  86. 04/14 t5520: let sed open its own inputDenton Liu, Nov 12, 2019
  87. 05/14 t5520: replace test -f with test-lib functionsDenton Liu, Nov 12, 2019
  88. 06/14 t5520: remove spaces after redirect operatorDenton Liu, Nov 12, 2019
  89. 07/14 t5520: use test_line_count where possibleDenton Liu, Nov 12, 2019
  90. 08/14 t5520: replace test -{n,z} with test-lib functionsDenton Liu, Nov 12, 2019
  91. 09/14 t5520: use test_cmp_rev where possibleDenton Liu, Nov 12, 2019
  92. 10/14 t5520: test single-line files by git with test_cmpDenton Liu, Nov 12, 2019
  93. Junio C HamanoNov 12, 2019
  94. Denton LiuNov 12, 2019
  95. 11/14 t5520: don't put git in upstream of pipeDenton Liu, Nov 12, 2019
  96. 12/14 t5520: replace subshell cat comparison with test_cmpDenton Liu, Nov 12, 2019
  97. 13/14 t5520: remove redundant lines in test casesDenton Liu, Nov 12, 2019
  98. 14/14 t5520: replace `! git` with `test_must_fail git`Denton Liu, Nov 12, 2019
  99. 00/14 t5520: various test cleanupDenton Liu, Nov 12, 2019
  100. 01/14 t: teach test_cmp_rev to accept ! for not-equalsDenton Liu, Nov 12, 2019
  101. Junio C HamanoNov 13, 2019
  102. Denton LiuNov 14, 2019
  103. 02/14 t5520: improve test styleDenton Liu, Nov 12, 2019
  104. 03/14 t5520: use sq for test case namesDenton Liu, Nov 12, 2019
  105. 04/14 t5520: let sed open its own inputDenton Liu, Nov 12, 2019
  106. 05/14 t5520: replace test -f with test-lib functionsDenton Liu, Nov 12, 2019
  107. 06/14 t5520: remove spaces after redirect operatorDenton Liu, Nov 12, 2019
  108. 07/14 t5520: use test_line_count where possibleDenton Liu, Nov 12, 2019
  109. 08/14 t5520: replace test -{n,z} with test-lib functionsDenton Liu, Nov 12, 2019
  110. 09/14 t5520: use test_cmp_rev where possibleDenton Liu, Nov 12, 2019
  111. 10/14 t5520: test single-line files by git with test_cmpDenton Liu, Nov 12, 2019
  112. 11/14 t5520: don't put git in upstream of pipeDenton Liu, Nov 12, 2019
  113. 12/14 t5520: replace $(cat ...) comparison with test_cmpDenton Liu, Nov 12, 2019
  114. 13/14 t5520: remove redundant lines in test casesDenton Liu, Nov 12, 2019
  115. 14/14 t5520: replace `! git` with `test_must_fail git`Denton Liu, Nov 12, 2019
  116. Junio C HamanoNov 13, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.