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

[PATCH v3 1/2] t5403:introduce check_post_checkout helper function

From
Deveshi Dwivedi <deveshigurgaon@gmail.com>
Date
Jan 12, 2026, 16:36 UTC
Message-ID
<20260112163643.231-2-deveshigurgaon@gmail.com>
In-Reply-To
<20260112163643.231-1-deveshigurgaon@gmail.com>

The test file repeatedly uses the same four-line pattern to validate post-checkout hook arguments: read the args file, then test each of the three values individually.

Introduce a check_post_checkout helper function that encapsulates this pattern. This patch does not change test behavior; it prepares the code for improvement in the next step.

Additionally, the 'post-checkout hook is triggered by clone' test is improved to validate the hook arguments (old ref, new ref, and flag) rather than just checking that the hook file was created.

Signed-off-by: Deveshi Dwivedi <deveshigurgaon@gmail.com>
---
 t/t5403-post-checkout-hook.sh | 49 ++++++++++++++++++++---------------
 1 file changed, 28 insertions(+), 21 deletions(-)
diff --git a/t/t5403-post-checkout-hook.sh b/t/t5403-post-checkout-hook.sh
index 1462e3365b..7bdea25107 100755
--- a/t/t5403-post-checkout-hook.sh
+++ b/t/t5403-post-checkout-hook.sh
@@ -9,6 +9,17 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
 . ./test-lib.sh
 
+# Usage: check_post_checkout <file> <old-ref> <new-ref> <flag>
+#
+# Verify that the post-checkout hook arguments in <file> match the expected
+# values: <old-ref> for the previous HEAD, <new-ref> for the new HEAD, and
+# <flag> indicating whether this was a branch checkout (1) or file checkout (0).
+check_post_checkout () {
+	test "$#" = 4 || BUG "check_post_checkout takes 4 args"
+	read old new flag <"$1" &&
+	test "$old" = "$2" && test "$new" = "$3" && test "$flag" = "$4"
+}
+
 test_expect_success setup '
 	test_hook --setup post-checkout <<-\EOF &&
 	echo "$@" >.git/post-checkout.args
@@ -23,29 +34,30 @@ test_expect_success setup '
 test_expect_success 'post-checkout receives the right arguments with HEAD unchanged ' '
 	test_when_finished "rm -f .git/post-checkout.args" &&
 	git checkout main &&
-	read old new flag <.git/post-checkout.args &&
-	test $old = $new && test $flag = 1
+	check_post_checkout .git/post-checkout.args \
+		"$(git rev-parse HEAD)" "$(git rev-parse HEAD)" 1
 '
 
 test_expect_success 'post-checkout args are correct with git checkout -b ' '
 	test_when_finished "rm -f .git/post-checkout.args" &&
 	git checkout -b new1 &&
-	read old new flag <.git/post-checkout.args &&
-	test $old = $new && test $flag = 1
+	check_post_checkout .git/post-checkout.args \
+		"$(git rev-parse HEAD)" "$(git rev-parse HEAD)" 1
 '
 
 test_expect_success 'post-checkout receives the right args with HEAD changed ' '
 	test_when_finished "rm -f .git/post-checkout.args" &&
+	old=$(git rev-parse HEAD) &&
 	git checkout two &&
-	read old new flag <.git/post-checkout.args &&
-	test $old != $new && test $flag = 1
+	check_post_checkout .git/post-checkout.args \
+		"$old" "$(git rev-parse HEAD)" 1
 '
 
 test_expect_success 'post-checkout receives the right args when not switching branches ' '
 	test_when_finished "rm -f .git/post-checkout.args" &&
 	git checkout main -- three.t &&
-	read old new flag <.git/post-checkout.args &&
-	test $old = $new && test $flag = 0
+	check_post_checkout .git/post-checkout.args \
+		"$(git rev-parse HEAD)" "$(git rev-parse HEAD)" 0
 '
 
 test_rebase () {
@@ -55,10 +67,8 @@ test_rebase () {
 		git checkout -B rebase-test main &&
 		rm -f .git/post-checkout.args &&
 		git rebase $args rebase-on-me &&
-		read old new flag <.git/post-checkout.args &&
-		test_cmp_rev main $old &&
-		test_cmp_rev rebase-on-me $new &&
-		test $flag = 1
+		check_post_checkout .git/post-checkout.args \
+			"$(git rev-parse main)" "$(git rev-parse rebase-on-me)" 1
 	'
 
 	test_expect_success "post-checkout is triggered on rebase $args with fast-forward" '
@@ -66,10 +76,8 @@ test_rebase () {
 		git checkout -B ff-rebase-test rebase-on-me^ &&
 		rm -f .git/post-checkout.args &&
 		git rebase $args rebase-on-me &&
-		read old new flag <.git/post-checkout.args &&
-		test_cmp_rev rebase-on-me^ $old &&
-		test_cmp_rev rebase-on-me $new &&
-		test $flag = 1
+		check_post_checkout .git/post-checkout.args \
+			"$(git rev-parse rebase-on-me^)" "$(git rev-parse rebase-on-me)" 1
 	'
 
 	test_expect_success "rebase $args fast-forward branch checkout runs post-checkout hook" '
@@ -79,10 +87,8 @@ test_rebase () {
 		git checkout two  &&
 		rm -f .git/post-checkout.args &&
 		git rebase $args HEAD rebase-fast-forward  &&
-		read old new flag <.git/post-checkout.args &&
-		test_cmp_rev two $old &&
-		test_cmp_rev three $new &&
-		test $flag = 1
+		check_post_checkout .git/post-checkout.args \
+			"$(git rev-parse two)" "$(git rev-parse three)" 1
 	'
 
 	test_expect_success "rebase $args checkout does not remove untracked files" '
@@ -109,7 +115,8 @@ test_expect_success 'post-checkout hook is triggered by clone' '
 	echo "$@" >"$GIT_DIR/post-checkout.args"
 	EOF
 	git clone --template=templates . clone3 &&
-	test_path_is_file clone3/.git/post-checkout.args
+	check_post_checkout clone3/.git/post-checkout.args \
+		"$(test_oid zero)" "$(git -C clone3 rev-parse HEAD)" 1
 '
 
 test_done
-- 
2.52.0.230.gd8af7cadaa
Previous: Deveshi DwivediNext: Deveshi Dwivedi
Message 13 of 14 in “t5403: improve post-checkout hook testing”
  1. 0/2 t5403: improve post-checkout hook testingDeveshi Dwivedi, Jan 11, 2026
  2. 1/2 t5403:introduce check_post_checkout helper functionDeveshi Dwivedi, Jan 11, 2026
  3. Eric SunshineJan 11, 2026
  4. Pushkar SinghJan 11, 2026
  5. Deveshi DwivediJan 12, 2026
  6. t5403: document check_post_checkout helperPushkar Singh, Jan 11, 2026
  7. 2/2 t5403: use test_cmp for post-checkout argument checksDeveshi Dwivedi, Jan 11, 2026
  8. 0/2 t5403: improve post-checkout hook testingDeveshi Dwivedi, Jan 12, 2026
  9. 1/2 t5403:introduce check_post_checkout helper functionDeveshi Dwivedi, Jan 12, 2026
  10. Junio C HamanoJan 12, 2026
  11. 2/2 t5403: use test_cmp for post-checkout argument checksDeveshi Dwivedi, Jan 12, 2026
  12. 0/2 t5403: improve post-checkout hook testingDeveshi Dwivedi, Jan 12, 2026
  13. 1/2 t5403:introduce check_post_checkout helper functionDeveshi Dwivedi, Jan 12, 2026
  14. 2/2 t5403: use test_cmp for post-checkout argument checksDeveshi Dwivedi, Jan 12, 2026

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.