tests: change "mkdir -p && write_script" to use "test_hook"

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>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
maint
Ævar Arnfjörð Bjarmason 2022-03-17 11:13:14 +01:00 committed by Junio C Hamano
parent 60a8a6bf6c
commit bef805b7d8
9 changed files with 27 additions and 60 deletions

View File

@ -12,8 +12,7 @@ then
fi fi


test_expect_success 'setup' ' test_expect_success 'setup' '
mkdir -p "$TRASH_DIRECTORY/.git/hooks" && test_hook --setup pre-commit <<-\EOF
write_script "$TRASH_DIRECTORY/.git/hooks/pre-commit" <<-\EOF
echo $HOBBES >&2 echo $HOBBES >&2
EOF EOF
' '

View File

@ -6,11 +6,11 @@ test_description='Test the core.hooksPath configuration variable'


test_expect_success 'set up a pre-commit hook in core.hooksPath' ' test_expect_success 'set up a pre-commit hook in core.hooksPath' '
>actual && >actual &&
mkdir -p .git/custom-hooks .git/hooks && mkdir -p .git/custom-hooks &&
write_script .git/custom-hooks/pre-commit <<-\EOF && write_script .git/custom-hooks/pre-commit <<-\EOF &&
echo CUSTOM >>actual echo CUSTOM >>actual
EOF EOF
write_script .git/hooks/pre-commit <<-\EOF test_hook --setup pre-commit <<-\EOF
echo NORMAL >>actual echo NORMAL >>actual
EOF EOF
' '

View File

@ -619,9 +619,7 @@ test_expect_success 'rebase a detached HEAD' '
' '


test_expect_success 'rebase a commit violating pre-commit' ' test_expect_success 'rebase a commit violating pre-commit' '

test_hook pre-commit <<-\EOF &&
mkdir -p .git/hooks &&
write_script .git/hooks/pre-commit <<-\EOF &&
test -z "$(git diff --cached --check)" test -z "$(git diff --cached --check)"
EOF EOF
echo "monde! " >> file1 && 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' ' test_expect_success 'rebase with a file named HEAD in worktree' '

rm -fr .git/hooks &&
git reset --hard && git reset --hard &&
git checkout -b branch3 A && 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_expect_success 'post-commit hook is called' '
test_when_finished "rm -f .git/hooks/post-commit" &&
>actual && >actual &&
mkdir -p .git/hooks && test_hook post-commit <<-\EOS &&
write_script .git/hooks/post-commit <<-\EOS &&
git rev-parse HEAD >>actual git rev-parse HEAD >>actual
EOS EOS
( (

View File

@ -292,9 +292,9 @@ test_expect_success 'post-rewrite hook and fixups work for merges' '
git commit --fixup HEAD same2.t && git commit --fixup HEAD same2.t &&
fixup="$(git rev-parse HEAD)" && fixup="$(git rev-parse HEAD)" &&


mkdir -p .git/hooks && test_hook post-rewrite <<-\EOF &&
test_when_finished "rm .git/hooks/post-rewrite" && cat >actual
echo "cat >actual" | write_script .git/hooks/post-rewrite && EOF


test_tick && test_tick &&
git rebase -i --autosquash -r HEAD^^^ && git rebase -i --autosquash -r HEAD^^^ &&

View File

@ -315,12 +315,10 @@ test_expect_success 'am --patch-format=hg applies hg patch' '
' '


test_expect_success 'am with applypatch-msg hook' ' test_expect_success 'am with applypatch-msg hook' '
test_when_finished "rm -f .git/hooks/applypatch-msg" &&
rm -fr .git/rebase-apply && rm -fr .git/rebase-apply &&
git reset --hard && git reset --hard &&
git checkout first && git checkout first &&
mkdir -p .git/hooks && test_hook applypatch-msg <<-\EOF &&
write_script .git/hooks/applypatch-msg <<-\EOF &&
cat "$1" >actual-msg && cat "$1" >actual-msg &&
echo hook-message >"$1" echo hook-message >"$1"
EOF EOF
@ -335,12 +333,10 @@ test_expect_success 'am with applypatch-msg hook' '
' '


test_expect_success 'am with failing 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 && rm -fr .git/rebase-apply &&
git reset --hard && git reset --hard &&
git checkout first && git checkout first &&
mkdir -p .git/hooks && test_hook applypatch-msg <<-\EOF &&
write_script .git/hooks/applypatch-msg <<-\EOF &&
exit 1 exit 1
EOF EOF
test_must_fail git am patch1 && 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_expect_success 'am with pre-applypatch hook' '
test_when_finished "rm -f .git/hooks/pre-applypatch" &&
rm -fr .git/rebase-apply && rm -fr .git/rebase-apply &&
git reset --hard && git reset --hard &&
git checkout first && git checkout first &&
mkdir -p .git/hooks && test_hook pre-applypatch <<-\EOF &&
write_script .git/hooks/pre-applypatch <<-\EOF &&
git diff first >diff.actual git diff first >diff.actual
exit 0 exit 0
EOF EOF
@ -368,12 +362,10 @@ test_expect_success 'am with pre-applypatch hook' '
' '


test_expect_success 'am with failing 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 && rm -fr .git/rebase-apply &&
git reset --hard && git reset --hard &&
git checkout first && git checkout first &&
mkdir -p .git/hooks && test_hook pre-applypatch <<-\EOF &&
write_script .git/hooks/pre-applypatch <<-\EOF &&
exit 1 exit 1
EOF EOF
test_must_fail git am patch1 && 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_expect_success 'am with post-applypatch hook' '
test_when_finished "rm -f .git/hooks/post-applypatch" &&
rm -fr .git/rebase-apply && rm -fr .git/rebase-apply &&
git reset --hard && git reset --hard &&
git checkout first && git checkout first &&
mkdir -p .git/hooks && test_hook post-applypatch <<-\EOF &&
write_script .git/hooks/post-applypatch <<-\EOF &&
git rev-parse HEAD >head.actual git rev-parse HEAD >head.actual
git diff second >diff.actual git diff second >diff.actual
exit 0 exit 0
@ -403,12 +393,10 @@ test_expect_success 'am with post-applypatch hook' '
' '


test_expect_success 'am with failing 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 && rm -fr .git/rebase-apply &&
git reset --hard && git reset --hard &&
git checkout first && git checkout first &&
mkdir -p .git/hooks && test_hook post-applypatch <<-\EOF &&
write_script .git/hooks/post-applypatch <<-\EOF &&
git rev-parse HEAD >head.actual git rev-parse HEAD >head.actual
exit 1 exit 1
EOF EOF

View File

@ -10,8 +10,7 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
. ./test-lib.sh . ./test-lib.sh


test_expect_success setup ' test_expect_success setup '
mkdir -p .git/hooks && test_hook --setup post-checkout <<-\EOF &&
write_script .git/hooks/post-checkout <<-\EOF &&
echo "$@" >.git/post-checkout.args echo "$@" >.git/post-checkout.args
EOF EOF
test_commit one && test_commit one &&

View File

@ -35,8 +35,7 @@ test_expect_success setup '


test_expect_success 'unsigned push does not send push certificate' ' test_expect_success 'unsigned push does not send push certificate' '
prepare_dst && prepare_dst &&
mkdir -p dst/.git/hooks && test_hook -C dst post-receive <<-\EOF &&
write_script dst/.git/hooks/post-receive <<-\EOF &&
# discard the update list # discard the update list
cat >/dev/null cat >/dev/null
# record the push certificate # 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' ' test_expect_success 'talking with a receiver without push certificate support' '
prepare_dst && prepare_dst &&
mkdir -p dst/.git/hooks && test_hook -C dst post-receive <<-\EOF &&
write_script dst/.git/hooks/post-receive <<-\EOF &&
# discard the update list # discard the update list
cat >/dev/null cat >/dev/null
# record the push certificate # 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' ' test_expect_success 'push --signed fails with a receiver without push certificate support' '
prepare_dst && prepare_dst &&
mkdir -p dst/.git/hooks &&
test_must_fail git push --signed dst noop ff +noff 2>err && test_must_fail git push --signed dst noop ff +noff 2>err &&
test_i18ngrep "the receiving end does not support" err test_i18ngrep "the receiving end does not support" err
' '


test_expect_success 'push --signed=1 is accepted' ' test_expect_success 'push --signed=1 is accepted' '
prepare_dst && prepare_dst &&
mkdir -p dst/.git/hooks &&
test_must_fail git push --signed=1 dst noop ff +noff 2>err && test_must_fail git push --signed=1 dst noop ff +noff 2>err &&
test_i18ngrep "the receiving end does not support" err test_i18ngrep "the receiving end does not support" err
' '


test_expect_success GPG 'no certificate for a signed push with no update' ' test_expect_success GPG 'no certificate for a signed push with no update' '
prepare_dst && prepare_dst &&
mkdir -p dst/.git/hooks && test_hook -C dst post-receive <<-\EOF &&
write_script dst/.git/hooks/post-receive <<-\EOF &&
if test -n "${GIT_PUSH_CERT-}" if test -n "${GIT_PUSH_CERT-}"
then then
git cat-file blob $GIT_PUSH_CERT >../push-cert 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' ' test_expect_success GPG 'signed push sends push certificate' '
prepare_dst && prepare_dst &&
mkdir -p dst/.git/hooks &&
git -C dst config receive.certnonceseed sekrit && 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 # discard the update list
cat >/dev/null cat >/dev/null
# record the push certificate # 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' ' test_expect_success GPGSSH 'ssh signed push sends push certificate' '
prepare_dst && prepare_dst &&
mkdir -p dst/.git/hooks &&
git -C dst config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" && git -C dst config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
git -C dst config receive.certnonceseed sekrit && 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 # discard the update list
cat >/dev/null cat >/dev/null
# record the push certificate # 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' ' test_expect_success GPG 'fail without key and heed user.signingkey' '
prepare_dst && prepare_dst &&
mkdir -p dst/.git/hooks &&
git -C dst config receive.certnonceseed sekrit && 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 # discard the update list
cat >/dev/null cat >/dev/null
# record the push certificate # 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_expect_success GPGSM 'fail without key and heed user.signingkey x509' '
test_config gpg.format x509 && test_config gpg.format x509 &&
prepare_dst && prepare_dst &&
mkdir -p dst/.git/hooks &&
git -C dst config receive.certnonceseed sekrit && 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 # discard the update list
cat >/dev/null cat >/dev/null
# record the push certificate # 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_expect_success GPGSSH 'fail without key and heed user.signingkey ssh' '
test_config gpg.format ssh && test_config gpg.format ssh &&
prepare_dst && prepare_dst &&
mkdir -p dst/.git/hooks &&
git -C dst config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" && git -C dst config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
git -C dst config receive.certnonceseed sekrit && 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 # discard the update list
cat >/dev/null cat >/dev/null
# record the push certificate # record the push certificate

View File

@ -17,8 +17,7 @@ test_expect_success 'setup' '
' '


test_expect_success 'test status, add, commit, others trigger hook without flags set' ' test_expect_success 'test status, add, commit, others trigger hook without flags set' '
mkdir -p .git/hooks && test_hook post-index-change <<-\EOF &&
write_script .git/hooks/post-index-change <<-\EOF &&
if test "$1" -eq 1; then if test "$1" -eq 1; then
echo "Invalid combination of flags passed to hook; updated_workdir is set." >testfailure echo "Invalid combination of flags passed to hook; updated_workdir is set." >testfailure
exit 1 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' ' 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 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 echo "Invalid combination of flags passed to hook; updated_workdir and updated_skipworktree are both set." >testfailure
exit 1 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' ' 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 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 echo "Invalid combination of flags passed to hook; updated_workdir and updated_skipworktree are both set." >testfailure
exit 1 exit 1

View File

@ -2288,9 +2288,7 @@ test_expect_success $PREREQ 'cmdline in-reply-to used with --no-thread' '
' '


test_expect_success $PREREQ 'invoke hook' ' test_expect_success $PREREQ 'invoke hook' '
mkdir -p .git/hooks && test_hook sendemail-validate <<-\EOF &&

write_script .git/hooks/sendemail-validate <<-\EOF &&
# test that we have the correct environment variable, pwd, and # test that we have the correct environment variable, pwd, and
# argument # argument
case "$GIT_DIR" in case "$GIT_DIR" in