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

[PATCH v2 09/10] tests: change "mkdir -p && write_script" to use "test_hook"

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Mar 7, 2022, 12:43 UTC
Message-ID
<patch-v2-09.10-121ca23db37-20220307T123909Z-avarab@gmail.com>
In-Reply-To
<cover-v2-00.10-00000000000-20220307T123909Z-avarab@gmail.com>

Change tests that used a "mkdir -p .git/hooks && write_script" pattern to use the new "test_hook" helper instead. The new helper does not create the .git/hooks directory, rather we assume that the default template will do so for us.

An upcoming series[1] will extend "test_hook" to operate in a "--template=" mode, but for now assuming that we have a .git/hooks already is a safe assumption. If that assumption becomes false in the future we'll only need to change 'test_hook", instead of all of these callsites.

1. https://lore.kernel.org/git/cover-00.13-00000000000-20211212T201308Z-avarab@gmail.com/
Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 t/t0029-core-unsetenvvars.sh      |  3 +--
 t/t1350-config-hooks-path.sh      |  4 ++--
 t/t3404-rebase-interactive.sh     | 10 ++--------
 t/t3430-rebase-merges.sh          |  6 +++---
 t/t4150-am.sh                     | 24 ++++++------------------
 t/t5403-post-checkout-hook.sh     |  3 +--
 t/t5534-push-signed.sh            | 26 ++++++++------------------
 t/t7113-post-index-change-hook.sh |  7 +++----
 t/t9001-send-email.sh             |  4 +---
 t/t9800-git-p4-basic.sh           |  5 ++---
 10 files changed, 29 insertions(+), 63 deletions(-)
diff --git a/t/t0029-core-unsetenvvars.sh b/t/t0029-core-unsetenvvars.sh
index b138e1d9cbc..4e8e90dd982 100755
--- a/t/t0029-core-unsetenvvars.sh
+++ b/t/t0029-core-unsetenvvars.sh
@@ -12,8 +12,7 @@ then
 fi
 
 test_expect_success 'setup' '
-	mkdir -p "$TRASH_DIRECTORY/.git/hooks" &&
-	write_script "$TRASH_DIRECTORY/.git/hooks/pre-commit" <<-\EOF
+	test_hook --setup pre-commit <<-\EOF
 	echo $HOBBES >&2
 	EOF
 '
diff --git a/t/t1350-config-hooks-path.sh b/t/t1350-config-hooks-path.sh
index fa9647a7c0b..f6dc83e2aab 100755
--- a/t/t1350-config-hooks-path.sh
+++ b/t/t1350-config-hooks-path.sh
@@ -6,11 +6,11 @@ test_description='Test the core.hooksPath configuration variable'
 
 test_expect_success 'set up a pre-commit hook in core.hooksPath' '
 	>actual &&
-	mkdir -p .git/custom-hooks .git/hooks &&
+	mkdir -p .git/custom-hooks &&
 	write_script .git/custom-hooks/pre-commit <<-\EOF &&
 	echo CUSTOM >>actual
 	EOF
-	write_script .git/hooks/pre-commit <<-\EOF
+	test_hook --setup pre-commit <<-\EOF
 	echo NORMAL >>actual
 	EOF
 '
diff --git a/t/t3404-rebase-interactive.sh b/t/t3404-rebase-interactive.sh
index a38f2da7691..f31afd4a547 100755
--- a/t/t3404-rebase-interactive.sh
+++ b/t/t3404-rebase-interactive.sh
@@ -619,9 +619,7 @@ test_expect_success 'rebase a detached HEAD' '
 '
 
 test_expect_success 'rebase a commit violating pre-commit' '
-
-	mkdir -p .git/hooks &&
-	write_script .git/hooks/pre-commit <<-\EOF &&
+	test_hook pre-commit <<-\EOF &&
 	test -z "$(git diff --cached --check)"
 	EOF
 	echo "monde! " >> file1 &&
@@ -636,8 +634,6 @@ test_expect_success 'rebase a commit violating pre-commit' '
 '
 
 test_expect_success 'rebase with a file named HEAD in worktree' '
-
-	rm -fr .git/hooks &&
 	git reset --hard &&
 	git checkout -b branch3 A &&
 
@@ -1688,10 +1684,8 @@ test_expect_success 'valid author header when author contains single quote' '
 '
 
 test_expect_success 'post-commit hook is called' '
-	test_when_finished "rm -f .git/hooks/post-commit" &&
 	>actual &&
-	mkdir -p .git/hooks &&
-	write_script .git/hooks/post-commit <<-\EOS &&
+	test_hook post-commit <<-\EOS &&
 	git rev-parse HEAD >>actual
 	EOS
 	(
diff --git a/t/t3430-rebase-merges.sh b/t/t3430-rebase-merges.sh
index 43c82d9a33b..f351701fec2 100755
--- a/t/t3430-rebase-merges.sh
+++ b/t/t3430-rebase-merges.sh
@@ -292,9 +292,9 @@ test_expect_success 'post-rewrite hook and fixups work for merges' '
 	git commit --fixup HEAD same2.t &&
 	fixup="$(git rev-parse HEAD)" &&
 
-	mkdir -p .git/hooks &&
-	test_when_finished "rm .git/hooks/post-rewrite" &&
-	echo "cat >actual" | write_script .git/hooks/post-rewrite &&
+	test_hook post-rewrite <<-\EOF &&
+	cat >actual
+	EOF
 
 	test_tick &&
 	git rebase -i --autosquash -r HEAD^^^ &&
diff --git a/t/t4150-am.sh b/t/t4150-am.sh
index 159fae8d016..cdad4b68807 100755
--- a/t/t4150-am.sh
+++ b/t/t4150-am.sh
@@ -315,12 +315,10 @@ test_expect_success 'am --patch-format=hg applies hg patch' '
 '
 
 test_expect_success 'am with applypatch-msg hook' '
-	test_when_finished "rm -f .git/hooks/applypatch-msg" &&
 	rm -fr .git/rebase-apply &&
 	git reset --hard &&
 	git checkout first &&
-	mkdir -p .git/hooks &&
-	write_script .git/hooks/applypatch-msg <<-\EOF &&
+	test_hook applypatch-msg <<-\EOF &&
 	cat "$1" >actual-msg &&
 	echo hook-message >"$1"
 	EOF
@@ -335,12 +333,10 @@ test_expect_success 'am with applypatch-msg hook' '
 '
 
 test_expect_success 'am with failing applypatch-msg hook' '
-	test_when_finished "rm -f .git/hooks/applypatch-msg" &&
 	rm -fr .git/rebase-apply &&
 	git reset --hard &&
 	git checkout first &&
-	mkdir -p .git/hooks &&
-	write_script .git/hooks/applypatch-msg <<-\EOF &&
+	test_hook applypatch-msg <<-\EOF &&
 	exit 1
 	EOF
 	test_must_fail git am patch1 &&
@@ -350,12 +346,10 @@ test_expect_success 'am with failing applypatch-msg hook' '
 '
 
 test_expect_success 'am with pre-applypatch hook' '
-	test_when_finished "rm -f .git/hooks/pre-applypatch" &&
 	rm -fr .git/rebase-apply &&
 	git reset --hard &&
 	git checkout first &&
-	mkdir -p .git/hooks &&
-	write_script .git/hooks/pre-applypatch <<-\EOF &&
+	test_hook pre-applypatch <<-\EOF &&
 	git diff first >diff.actual
 	exit 0
 	EOF
@@ -368,12 +362,10 @@ test_expect_success 'am with pre-applypatch hook' '
 '
 
 test_expect_success 'am with failing pre-applypatch hook' '
-	test_when_finished "rm -f .git/hooks/pre-applypatch" &&
 	rm -fr .git/rebase-apply &&
 	git reset --hard &&
 	git checkout first &&
-	mkdir -p .git/hooks &&
-	write_script .git/hooks/pre-applypatch <<-\EOF &&
+	test_hook pre-applypatch <<-\EOF &&
 	exit 1
 	EOF
 	test_must_fail git am patch1 &&
@@ -383,12 +375,10 @@ test_expect_success 'am with failing pre-applypatch hook' '
 '
 
 test_expect_success 'am with post-applypatch hook' '
-	test_when_finished "rm -f .git/hooks/post-applypatch" &&
 	rm -fr .git/rebase-apply &&
 	git reset --hard &&
 	git checkout first &&
-	mkdir -p .git/hooks &&
-	write_script .git/hooks/post-applypatch <<-\EOF &&
+	test_hook post-applypatch <<-\EOF &&
 	git rev-parse HEAD >head.actual
 	git diff second >diff.actual
 	exit 0
@@ -403,12 +393,10 @@ test_expect_success 'am with post-applypatch hook' '
 '
 
 test_expect_success 'am with failing post-applypatch hook' '
-	test_when_finished "rm -f .git/hooks/post-applypatch" &&
 	rm -fr .git/rebase-apply &&
 	git reset --hard &&
 	git checkout first &&
-	mkdir -p .git/hooks &&
-	write_script .git/hooks/post-applypatch <<-\EOF &&
+	test_hook post-applypatch <<-\EOF &&
 	git rev-parse HEAD >head.actual
 	exit 1
 	EOF
diff --git a/t/t5403-post-checkout-hook.sh b/t/t5403-post-checkout-hook.sh
index d1181816906..978f240cdac 100755
--- a/t/t5403-post-checkout-hook.sh
+++ b/t/t5403-post-checkout-hook.sh
@@ -10,8 +10,7 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 . ./test-lib.sh
 
 test_expect_success setup '
-	mkdir -p .git/hooks &&
-	write_script .git/hooks/post-checkout <<-\EOF &&
+	test_hook --setup post-checkout <<-\EOF &&
 	echo "$@" >.git/post-checkout.args
 	EOF
 	test_commit one &&
diff --git a/t/t5534-push-signed.sh b/t/t5534-push-signed.sh
index 24d374adbae..7c0a148e73c 100755
--- a/t/t5534-push-signed.sh
+++ b/t/t5534-push-signed.sh
@@ -35,8 +35,7 @@ test_expect_success setup '
 
 test_expect_success 'unsigned push does not send push certificate' '
 	prepare_dst &&
-	mkdir -p dst/.git/hooks &&
-	write_script dst/.git/hooks/post-receive <<-\EOF &&
+	test_hook -C dst post-receive <<-\EOF &&
 	# discard the update list
 	cat >/dev/null
 	# record the push certificate
@@ -52,8 +51,7 @@ test_expect_success 'unsigned push does not send push certificate' '
 
 test_expect_success 'talking with a receiver without push certificate support' '
 	prepare_dst &&
-	mkdir -p dst/.git/hooks &&
-	write_script dst/.git/hooks/post-receive <<-\EOF &&
+	test_hook -C dst post-receive <<-\EOF &&
 	# discard the update list
 	cat >/dev/null
 	# record the push certificate
@@ -69,22 +67,19 @@ test_expect_success 'talking with a receiver without push certificate support' '
 
 test_expect_success 'push --signed fails with a receiver without push certificate support' '
 	prepare_dst &&
-	mkdir -p dst/.git/hooks &&
 	test_must_fail git push --signed dst noop ff +noff 2>err &&
 	test_i18ngrep "the receiving end does not support" err
 '
 
 test_expect_success 'push --signed=1 is accepted' '
 	prepare_dst &&
-	mkdir -p dst/.git/hooks &&
 	test_must_fail git push --signed=1 dst noop ff +noff 2>err &&
 	test_i18ngrep "the receiving end does not support" err
 '
 
 test_expect_success GPG 'no certificate for a signed push with no update' '
 	prepare_dst &&
-	mkdir -p dst/.git/hooks &&
-	write_script dst/.git/hooks/post-receive <<-\EOF &&
+	test_hook -C dst post-receive <<-\EOF &&
 	if test -n "${GIT_PUSH_CERT-}"
 	then
 		git cat-file blob $GIT_PUSH_CERT >../push-cert
@@ -96,9 +91,8 @@ test_expect_success GPG 'no certificate for a signed push with no update' '
 
 test_expect_success GPG 'signed push sends push certificate' '
 	prepare_dst &&
-	mkdir -p dst/.git/hooks &&
 	git -C dst config receive.certnonceseed sekrit &&
-	write_script dst/.git/hooks/post-receive <<-\EOF &&
+	test_hook -C dst post-receive <<-\EOF &&
 	# discard the update list
 	cat >/dev/null
 	# record the push certificate
@@ -139,10 +133,9 @@ test_expect_success GPG 'signed push sends push certificate' '
 
 test_expect_success GPGSSH 'ssh signed push sends push certificate' '
 	prepare_dst &&
-	mkdir -p dst/.git/hooks &&
 	git -C dst config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
 	git -C dst config receive.certnonceseed sekrit &&
-	write_script dst/.git/hooks/post-receive <<-\EOF &&
+	test_hook -C dst post-receive <<-\EOF &&
 	# discard the update list
 	cat >/dev/null
 	# record the push certificate
@@ -223,9 +216,8 @@ test_expect_success GPG 'inconsistent push options in signed push not allowed' '
 
 test_expect_success GPG 'fail without key and heed user.signingkey' '
 	prepare_dst &&
-	mkdir -p dst/.git/hooks &&
 	git -C dst config receive.certnonceseed sekrit &&
-	write_script dst/.git/hooks/post-receive <<-\EOF &&
+	test_hook -C dst post-receive <<-\EOF &&
 	# discard the update list
 	cat >/dev/null
 	# record the push certificate
@@ -273,9 +265,8 @@ test_expect_success GPG 'fail without key and heed user.signingkey' '
 test_expect_success GPGSM 'fail without key and heed user.signingkey x509' '
 	test_config gpg.format x509 &&
 	prepare_dst &&
-	mkdir -p dst/.git/hooks &&
 	git -C dst config receive.certnonceseed sekrit &&
-	write_script dst/.git/hooks/post-receive <<-\EOF &&
+	test_hook -C dst post-receive <<-\EOF &&
 	# discard the update list
 	cat >/dev/null
 	# record the push certificate
@@ -326,10 +317,9 @@ test_expect_success GPGSM 'fail without key and heed user.signingkey x509' '
 test_expect_success GPGSSH 'fail without key and heed user.signingkey ssh' '
 	test_config gpg.format ssh &&
 	prepare_dst &&
-	mkdir -p dst/.git/hooks &&
 	git -C dst config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
 	git -C dst config receive.certnonceseed sekrit &&
-	write_script dst/.git/hooks/post-receive <<-\EOF &&
+	test_hook -C dst post-receive <<-\EOF &&
 	# discard the update list
 	cat >/dev/null
 	# record the push certificate
diff --git a/t/t7113-post-index-change-hook.sh b/t/t7113-post-index-change-hook.sh
index a21781d68a1..58e55a7c779 100755
--- a/t/t7113-post-index-change-hook.sh
+++ b/t/t7113-post-index-change-hook.sh
@@ -17,8 +17,7 @@ test_expect_success 'setup' '
 '
 
 test_expect_success 'test status, add, commit, others trigger hook without flags set' '
-	mkdir -p .git/hooks &&
-	write_script .git/hooks/post-index-change <<-\EOF &&
+	test_hook post-index-change <<-\EOF &&
 		if test "$1" -eq 1; then
 			echo "Invalid combination of flags passed to hook; updated_workdir is set." >testfailure
 			exit 1
@@ -63,7 +62,7 @@ test_expect_success 'test status, add, commit, others trigger hook without flags
 '
 
 test_expect_success 'test checkout and reset trigger the hook' '
-	write_script .git/hooks/post-index-change <<-\EOF &&
+	test_hook post-index-change <<-\EOF &&
 		if test "$1" -eq 1 && test "$2" -eq 1; then
 			echo "Invalid combination of flags passed to hook; updated_workdir and updated_skipworktree are both set." >testfailure
 			exit 1
@@ -106,7 +105,7 @@ test_expect_success 'test checkout and reset trigger the hook' '
 '
 
 test_expect_success 'test reset --mixed and update-index triggers the hook' '
-	write_script .git/hooks/post-index-change <<-\EOF &&
+	test_hook post-index-change <<-\EOF &&
 		if test "$1" -eq 1 && test "$2" -eq 1; then
 			echo "Invalid combination of flags passed to hook; updated_workdir and updated_skipworktree are both set." >testfailure
 			exit 1
diff --git a/t/t9001-send-email.sh b/t/t9001-send-email.sh
index 84d0f40d76a..42694fe5841 100755
--- a/t/t9001-send-email.sh
+++ b/t/t9001-send-email.sh
@@ -2288,9 +2288,7 @@ test_expect_success $PREREQ 'cmdline in-reply-to used with --no-thread' '
 '
 
 test_expect_success $PREREQ 'invoke hook' '
-	mkdir -p .git/hooks &&
-
-	write_script .git/hooks/sendemail-validate <<-\EOF &&
+	test_hook sendemail-validate <<-\EOF &&
 	# test that we have the correct environment variable, pwd, and
 	# argument
 	case "$GIT_DIR" in
diff --git a/t/t9800-git-p4-basic.sh b/t/t9800-git-p4-basic.sh
index 3c1534c94d6..8b30062c0cf 100755
--- a/t/t9800-git-p4-basic.sh
+++ b/t/t9800-git-p4-basic.sh
@@ -279,8 +279,7 @@ test_expect_success 'run hook p4-pre-submit before submit' '
 		git p4 submit --dry-run >out &&
 		grep "Would apply" out
 	) &&
-	mkdir -p "$git"/.git/hooks &&
-	write_script "$git"/.git/hooks/p4-pre-submit <<-\EOF &&
+	test_hook -C "$git" p4-pre-submit <<-\EOF &&
 	exit 0
 	EOF
 	(
@@ -288,7 +287,7 @@ test_expect_success 'run hook p4-pre-submit before submit' '
 		git p4 submit --dry-run >out &&
 		grep "Would apply" out
 	) &&
-	write_script "$git"/.git/hooks/p4-pre-submit <<-\EOF &&
+	test_hook -C "$git" --clobber p4-pre-submit <<-\EOF &&
 	exit 1
 	EOF
 	(
-- 
2.35.1.1242.gfeba0eae32b
Previous: Ævar Arnfjörð BjarmasonNext: Junio C Hamano
Message 33 of 51 in “tests: add and use a "test_hook" wrapper + hook fixes”
  1. 00/10 tests: add and use a "test_hook" wrapper + hook fixesÆvar Arnfjörð Bjarmason, Mar 2, 2022
  2. 02/10 t5540: don't rely on "hook/post-update.sample"Ævar Arnfjörð Bjarmason, Mar 2, 2022
  3. Junio C HamanoMar 2, 2022
  4. 01/10 hook tests: turn exit code assertions into a loopÆvar Arnfjörð Bjarmason, Mar 2, 2022
  5. Junio C HamanoMar 2, 2022
  6. 03/10 tests: assume the hooks are disabled by defaultÆvar Arnfjörð Bjarmason, Mar 2, 2022
  7. Junio C HamanoMar 2, 2022
  8. 04/10 bugreport tests: tighten up "git bugreport -s hooks" testÆvar Arnfjörð Bjarmason, Mar 2, 2022
  9. Junio C HamanoMar 2, 2022
  10. 05/10 tests: indent and add hook setup to "test_expect_success"Ævar Arnfjörð Bjarmason, Mar 2, 2022
  11. Junio C HamanoMar 2, 2022
  12. 06/10 hook tests: get rid of unnecessary sub-shellsÆvar Arnfjörð Bjarmason, Mar 2, 2022
  13. Junio C HamanoMar 2, 2022
  14. 07/10 fetch+push tests: have tests clean up their own messÆvar Arnfjörð Bjarmason, Mar 2, 2022
  15. Junio C HamanoMar 2, 2022
  16. 10/10 tests: change "cat && chmod +x" to use "test_hook"Ævar Arnfjörð Bjarmason, Mar 2, 2022
  17. Eric SunshineMar 3, 2022
  18. 08/10 test-lib-functions: add and use a "test_hook" wrapperÆvar Arnfjörð Bjarmason, Mar 2, 2022
  19. Junio C HamanoMar 2, 2022
  20. 09/10 tests: change "mkdir -p && write_script" to use "test_hook"Ævar Arnfjörð Bjarmason, Mar 2, 2022
  21. Junio C HamanoMar 2, 2022
  22. Junio C HamanoMar 2, 2022
  23. 00/10 tests: add and use a "test_hook" wrapper + hook fixesÆvar Arnfjörð Bjarmason, Mar 7, 2022
  24. 01/10 hook tests: turn exit code assertions into a loopÆvar Arnfjörð Bjarmason, Mar 7, 2022
  25. 02/10 t5540: don't rely on "hook/post-update.sample"Ævar Arnfjörð Bjarmason, Mar 7, 2022
  26. 03/10 tests: assume the hooks are disabled by defaultÆvar Arnfjörð Bjarmason, Mar 7, 2022
  27. 04/10 bugreport tests: tighten up "git bugreport -s hooks" testÆvar Arnfjörð Bjarmason, Mar 7, 2022
  28. 05/10 tests: indent and add hook setup to "test_expect_success"Ævar Arnfjörð Bjarmason, Mar 7, 2022
  29. 06/10 hook tests: get rid of unnecessary sub-shellsÆvar Arnfjörð Bjarmason, Mar 7, 2022
  30. 08/10 test-lib-functions: add and use a "test_hook" wrapperÆvar Arnfjörð Bjarmason, Mar 7, 2022
  31. 07/10 fetch+push tests: have tests clean up their own messÆvar Arnfjörð Bjarmason, Mar 7, 2022
  32. 10/10 tests: change "cat && chmod +x" to use "test_hook"Ævar Arnfjörð Bjarmason, Mar 7, 2022
  33. 09/10 tests: change "mkdir -p && write_script" to use "test_hook"Ævar Arnfjörð Bjarmason, Mar 7, 2022
  34. Junio C HamanoMar 7, 2022
  35. 00/13 tests: add and use a "test_hook" wrapper + hook fixesÆvar Arnfjörð Bjarmason, Mar 17, 2022
  36. 01/13 test-lib-functions: add and use a "test_hook" wrapperÆvar Arnfjörð Bjarmason, Mar 17, 2022
  37. 03/13 http tests: don't rely on "hook/post-update.sample"Ævar Arnfjörð Bjarmason, Mar 17, 2022
  38. 02/13 hook tests: turn exit code assertions into a loopÆvar Arnfjörð Bjarmason, Mar 17, 2022
  39. 04/13 tests: assume the hooks are disabled by defaultÆvar Arnfjörð Bjarmason, Mar 17, 2022
  40. 05/13 bugreport tests: tighten up "git bugreport -s hooks" testÆvar Arnfjörð Bjarmason, Mar 17, 2022
  41. 06/13 fetch+push tests: use "test_hook" and "test_when_finished" patternÆvar Arnfjörð Bjarmason, Mar 17, 2022
  42. 08/13 tests: change "cat && chmod +x" to use "test_hook"Ævar Arnfjörð Bjarmason, Mar 17, 2022
  43. 10/13 tests: use "test_hook" for misc "mkdir -p" and "chmod" casesÆvar Arnfjörð Bjarmason, Mar 17, 2022
  44. 07/13 gc + p4 tests: use "test_hook", remove sub-shellsÆvar Arnfjörð Bjarmason, Mar 17, 2022
  45. 09/13 tests: change "mkdir -p && write_script" to use "test_hook"Ævar Arnfjörð Bjarmason, Mar 17, 2022
  46. 11/13 tests: extend "test_hook" for "rm" and "chmod -x", convert "$HOOK"Ævar Arnfjörð Bjarmason, Mar 17, 2022
  47. 13/13 http tests: use "test_hook" for "smart" and "dumb" http testsÆvar Arnfjörð Bjarmason, Mar 17, 2022
  48. 12/13 proc-receive hook tests: use "test_hook" instead of "write_script"Ævar Arnfjörð Bjarmason, Mar 17, 2022
  49. Junio C HamanoMar 17, 2022
  50. Ævar Arnfjörð BjarmasonMar 17, 2022
  51. Junio C HamanoMar 17, 2022

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.