[PATCH 10/13] test-lib-functions: add and use a "write_hook" wrapper
From: Ævar Arnfjörð Bjarmason <hidden>
Date: 2021-12-12 20:15:00
Subsystem:
the rest · Maintainer:
Linus Torvalds
Add a "write_hook" wrapper for the common case of "write_script .git/hooks/<NAME>". This also accepts a "-C" option like "test_commit". Let's convert various trivial cases of "write_script" over to it. For now this doesn't have much of an advantage, but in a subsequent commit we'll implicitly create the leading "hooks" directory. This will help us get rid of our --template dependency for "hooks". Signed-off-by: Ævar Arnfjörð Bjarmason <redacted> --- t/t1416-ref-transaction-hooks.sh | 14 +++++++------- t/t5406-remote-rejects.sh | 2 +- t/t5409-colorize-remote-messages.sh | 2 +- t/t5411-proc-receive-hook.sh | 4 ++-- t/t5510-fetch.sh | 6 +++--- t/t5516-fetch-push.sh | 4 ++-- t/t5521-pull-options.sh | 4 ++-- t/t5547-push-quarantine.sh | 4 ++-- t/t5548-push-porcelain.sh | 2 +- t/t6500-gc.sh | 4 ++-- t/test-lib-functions.sh | 26 ++++++++++++++++++++++++++ 11 files changed, 49 insertions(+), 23 deletions(-)
diff --git a/t/t1416-ref-transaction-hooks.sh b/t/t1416-ref-transaction-hooks.sh
index 6c941027a81..4532c90274b 100755
--- a/t/t1416-ref-transaction-hooks.sh
+++ b/t/t1416-ref-transaction-hooks.sh@@ -18,7 +18,7 @@ test_expect_success setup ' test_expect_success 'hook allows updating ref if successful' ' test_when_finished "rm .git/hooks/reference-transaction" && git reset --hard PRE && - write_script .git/hooks/reference-transaction <<-\EOF && + write_hook reference-transaction <<-\EOF && echo "$*" >>actual EOF cat >expect <<-EOF &&
@@ -32,7 +32,7 @@ test_expect_success 'hook allows updating ref if successful' ' test_expect_success 'hook aborts updating ref in prepared state' ' test_when_finished "rm .git/hooks/reference-transaction" && git reset --hard PRE && - write_script .git/hooks/reference-transaction <<-\EOF && + write_hook reference-transaction <<-\EOF && if test "$1" = prepared then exit 1
@@ -45,7 +45,7 @@ test_expect_success 'hook aborts updating ref in prepared state' ' test_expect_success 'hook gets all queued updates in prepared state' ' test_when_finished "rm .git/hooks/reference-transaction actual" && git reset --hard PRE && - write_script .git/hooks/reference-transaction <<-\EOF && + write_hook reference-transaction <<-\EOF && if test "$1" = prepared then while read -r line
@@ -68,7 +68,7 @@ test_expect_success 'hook gets all queued updates in prepared state' ' test_expect_success 'hook gets all queued updates in committed state' ' test_when_finished "rm .git/hooks/reference-transaction actual" && git reset --hard PRE && - write_script .git/hooks/reference-transaction <<-\EOF && + write_hook reference-transaction <<-\EOF && if test "$1" = committed then while read -r line
@@ -88,7 +88,7 @@ test_expect_success 'hook gets all queued updates in committed state' ' test_expect_success 'hook gets all queued updates in aborted state' ' test_when_finished "rm .git/hooks/reference-transaction actual" && git reset --hard PRE && - write_script .git/hooks/reference-transaction <<-\EOF && + write_hook reference-transaction <<-\EOF && if test "$1" = aborted then while read -r line
@@ -115,11 +115,11 @@ test_expect_success 'interleaving hook calls succeed' ' git init --bare target-repo.git && - write_script target-repo.git/hooks/reference-transaction <<-\EOF && + write_hook -C target-repo.git reference-transaction <<-\EOF && echo $0 "$@" >>actual EOF - write_script target-repo.git/hooks/update <<-\EOF && + write_hook -C target-repo.git update <<-\EOF && echo $0 "$@" >>actual EOF
diff --git a/t/t5406-remote-rejects.sh b/t/t5406-remote-rejects.sh
index 5c509db6fc3..c49aeea272c 100755
--- a/t/t5406-remote-rejects.sh
+++ b/t/t5406-remote-rejects.sh@@ -5,7 +5,7 @@ test_description='remote push rejects are reported by client' . ./test-lib.sh test_expect_success 'setup' ' - write_script .git/hooks/update <<-\EOF && + write_hook update <<-\EOF && exit 1 EOF echo 1 >file &&
diff --git a/t/t5409-colorize-remote-messages.sh b/t/t5409-colorize-remote-messages.sh
index 9f1a483f426..1f7cad31442 100755
--- a/t/t5409-colorize-remote-messages.sh
+++ b/t/t5409-colorize-remote-messages.sh@@ -5,7 +5,7 @@ test_description='remote messages are colorized on the client' . ./test-lib.sh test_expect_success 'setup' ' - write_script .git/hooks/update <<-\EOF && + write_hook update <<-\EOF && echo error: error echo ERROR: also highlighted echo hint: hint
diff --git a/t/t5411-proc-receive-hook.sh b/t/t5411-proc-receive-hook.sh
index 98b0e812082..054f4b2efb3 100755
--- a/t/t5411-proc-receive-hook.sh
+++ b/t/t5411-proc-receive-hook.sh@@ -36,7 +36,7 @@ setup_upstream_and_workbench () { TAG=$(git -C workbench rev-parse v123) && # setup pre-receive hook - write_script upstream.git/hooks/pre-receive <<-\EOF && + write_hook -C upstream.git pre-receive <<-\EOF && exec >&2 echo "# pre-receive hook" while read old new ref
@@ -46,7 +46,7 @@ setup_upstream_and_workbench () { EOF # setup post-receive hook - write_script upstream.git/hooks/post-receive <<-\EOF && + write_hook -C upstream.git post-receive <<-\EOF && exec >&2 echo "# post-receive hook" while read old new ref
diff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh
index a0faf0dd949..cdce39e6c76 100755
--- a/t/t5510-fetch.sh
+++ b/t/t5510-fetch.sh@@ -265,7 +265,7 @@ test_expect_success 'fetch --atomic executes a single reference transaction only EOF rm -f atomic/actual && - write_script atomic/.git/hooks/reference-transaction <<-\EOF && + write_hook -C atomic reference-transaction <<-\EOF && ( echo "$*" && cat ) >>actual EOF
@@ -298,7 +298,7 @@ test_expect_success 'fetch --atomic aborts all reference updates if hook aborts' EOF rm -f atomic/actual && - write_script atomic/.git/hooks/reference-transaction <<-\EOF && + write_hook -C atomic/.git reference-transaction <<-\EOF && ( echo "$*" && cat ) >>actual exit 1 EOF
@@ -326,7 +326,7 @@ test_expect_success 'fetch --atomic --append appends to FETCH_HEAD' ' test_line_count = 2 atomic/.git/FETCH_HEAD && cp atomic/.git/FETCH_HEAD expected && - write_script atomic/.git/hooks/reference-transaction <<-\EOF && + write_hook -C atomic reference-transaction <<-\EOF && exit 1 EOF
diff --git a/t/t5516-fetch-push.sh b/t/t5516-fetch-push.sh
index eea191d042f..11458052cb4 100755
--- a/t/t5516-fetch-push.sh
+++ b/t/t5516-fetch-push.sh@@ -1680,7 +1680,7 @@ test_expect_success 'updateInstead with push-to-checkout hook' ' git reset --hard HEAD^^ && git tag initial && git config receive.denyCurrentBranch updateInstead && - write_script .git/hooks/push-to-checkout <<-\EOF + write_hook push-to-checkout <<-\EOF echo >&2 updating from $(git rev-parse HEAD) echo >&2 updating to "$1"
@@ -1739,7 +1739,7 @@ test_expect_success 'updateInstead with push-to-checkout hook' ' ( cd void && git config receive.denyCurrentBranch updateInstead && - write_script .git/hooks/push-to-checkout <<-\EOF + write_hook push-to-checkout <<-\EOF if git rev-parse --quiet --verify HEAD then has_head=yes
diff --git a/t/t5521-pull-options.sh b/t/t5521-pull-options.sh
index 66cfcb09c55..9b30e90383c 100755
--- a/t/t5521-pull-options.sh
+++ b/t/t5521-pull-options.sh@@ -233,7 +233,7 @@ test_expect_success 'git pull --no-verify flag passed to merge' ' git init src && test_commit -C src one && git clone src dst && - write_script dst/.git/hooks/commit-msg <<-\EOF && + write_hook -C dst commit-msg <<-\EOF && false EOF test_commit -C src two &&
@@ -245,7 +245,7 @@ test_expect_success 'git pull --no-verify --verify passed to merge' ' git init src && test_commit -C src one && git clone src dst && - write_script dst/.git/hooks/commit-msg <<-\EOF && + write_hook -C dst commit-msg <<-\EOF && false EOF test_commit -C src two &&
diff --git a/t/t5547-push-quarantine.sh b/t/t5547-push-quarantine.sh
index faaa51ccc56..e0bd469b110 100755
--- a/t/t5547-push-quarantine.sh
+++ b/t/t5547-push-quarantine.sh@@ -5,7 +5,7 @@ test_description='check quarantine of objects during push' test_expect_success 'create picky dest repo' ' git init --bare dest.git && - write_script dest.git/hooks/pre-receive <<-\EOF + write_hook -C dest.git pre-receive <<-\EOF while read old new ref; do test "$(git log -1 --format=%s $new)" = reject && exit 1 done
@@ -60,7 +60,7 @@ test_expect_success 'push to repo path with path separator (colon)' ' test_expect_success 'updating a ref from quarantine is forbidden' ' git init --bare update.git && - write_script update.git/hooks/pre-receive <<-\EOF && + write_hook -C update.git pre-receive <<-\EOF && read old new refname git update-ref refs/heads/unrelated $new exit 1
diff --git a/t/t5548-push-porcelain.sh b/t/t5548-push-porcelain.sh
index f11ff57e549..8997433a992 100755
--- a/t/t5548-push-porcelain.sh
+++ b/t/t5548-push-porcelain.sh@@ -168,7 +168,7 @@ run_git_push_porcelain_output_test() { ' test_expect_success "prepare pre-receive hook ($PROTOCOL)" ' - write_script "$upstream/hooks/pre-receive" <<-EOF + write_hook -C "$upstream" pre-receive <<-EOF exit 1 EOF '
diff --git a/t/t6500-gc.sh b/t/t6500-gc.sh
index c2021267f2c..98cf395806b 100755
--- a/t/t6500-gc.sh
+++ b/t/t6500-gc.sh@@ -103,7 +103,7 @@ test_expect_success 'pre-auto-gc hook can stop auto gc' ' git init pre-auto-gc-hook && ( cd pre-auto-gc-hook && - write_script ".git/hooks/pre-auto-gc" <<-\EOF && + write_hook pre-auto-gc <<-\EOF && echo >&2 no gc for you && exit 1 EOF
@@ -130,7 +130,7 @@ test_expect_success 'pre-auto-gc hook can stop auto gc' ' ( cd pre-auto-gc-hook && - write_script ".git/hooks/pre-auto-gc" <<-\EOF && + write_hook pre-auto-gc <<-\EOF && echo >&2 will gc for you && exit 0 EOF
diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh
index eef2262a360..3786d39ccab 100644
--- a/t/test-lib-functions.sh
+++ b/t/test-lib-functions.sh@@ -551,6 +551,32 @@ write_script () { chmod +x "$1" } +## Usage: write-hook pre-receive +## Usage: write-hook -C some-dir pre-receive +write_hook () { + indir= && + while test $# != 0 + do + case "$1" in + -C) + indir="$2" + shift + ;; + -*) + BUG "invalid write_hook: $1" + ;; + *) + break + ;; + esac && + shift + done && + git_dir=$(git -C "$indir" rev-parse --absolute-git-dir) && + hook_dir="$git_dir/hooks" && + hook_file="$hook_dir/$1" + write_script "$hook_file" +} + # Use test_set_prereq to tell that a particular prerequisite is available. # The prerequisite can later be checked for in two ways: #
--
2.34.1.1020.gb1392dd1877