test-lib-functions: add and use a "test_hook" wrapper
Add a "test_hook" wrapper similar to the existing "test_config"
wrapper added in d960c47a881 (test-lib: add helper functions for
config, 2011-08-17).
This wrapper:
- Will clean up the hook with "test_when_finished", unless --setup is
provided.
- Will error if we clobber a hook, unless --clobber is provided.
- Takes a name like "update" instead of ".git/hooks/update".
- Accepts -C <dir>, like "test_config" and "test_commit".
By using a wrapper we'll be able to easily change all the hook-related
code that assumes that the template-created ".git/hooks" directory is
created by "init", "clone" etc. once another topic follows-up and
changes the test suite to stop creating trash directories using those
templates.
In addition this will make it easy to have the hooks configured using
the "configuration-based hooks" topic, once we get around to
integrating that. I.e. we'll be able to run the tests in a mode where
we sometimes create a .git/hooks/<name>, and other times create a
script in another location, and point the relevant configuration
snippet to it.
Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Ævar Arnfjörð Bjarmason committedMar 17, 2022 at 11:13 UTC7da7f63cf9950d0ecd83579b4ca28f7b2805bf32
13 files changed+97-43
t/t1416-ref-transaction-hooks.sh
+12-14
index 4e1e84a91f..6fca1f08d9 100755--- a/t/t1416-ref-transaction-hooks.sh+++ b/t/t1416-ref-transaction-hooks.sh@@ -16,9 +16,8 @@ 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 &&+ test_hook reference-transaction <<-\EOF && echo "$*" >>actual EOF cat >expect <<-EOF &&@@ -30,9 +29,8 @@ 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 &&+ test_hook reference-transaction <<-\EOF && if test "$1" = prepared then exit 1@@ -43,9 +41,9 @@ 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" &&+ test_when_finished "rm actual" && git reset --hard PRE &&- write_script .git/hooks/reference-transaction <<-\EOF &&+ test_hook reference-transaction <<-\EOF && if test "$1" = prepared then while read -r line@@ -66,9 +64,9 @@ 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" &&+ test_when_finished "rm actual" && git reset --hard PRE &&- write_script .git/hooks/reference-transaction <<-\EOF &&+ test_hook reference-transaction <<-\EOF && if test "$1" = committed then while read -r line@@ -86,9 +84,9 @@ 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" &&+ test_when_finished "rm actual" && git reset --hard PRE &&- write_script .git/hooks/reference-transaction <<-\EOF &&+ test_hook reference-transaction <<-\EOF && if test "$1" = aborted then while read -r line@@ -115,11 +113,11 @@ test_expect_success 'interleaving hook calls succeed' ' git init --bare target-repo.git &&- write_script target-repo.git/hooks/reference-transaction <<-\EOF &&+ test_hook -C target-repo.git reference-transaction <<-\EOF && echo $0 "$@" >>actual EOF- write_script target-repo.git/hooks/update <<-\EOF &&+ test_hook -C target-repo.git update <<-\EOF && echo $0 "$@" >>actual EOF@@ -140,7 +138,7 @@ test_expect_success 'hook does not get called on packing refs' ' # Pack references first such that we are in a known state. git pack-refs --all &&- write_script .git/hooks/reference-transaction <<-\EOF &&+ test_hook reference-transaction <<-\EOF && echo "$@" >>actual cat >>actual EOF@@ -166,7 +164,7 @@ test_expect_success 'deleting packed ref calls hook once' ' git update-ref refs/heads/to-be-deleted $POST_OID && git pack-refs --all &&- write_script .git/hooks/reference-transaction <<-\EOF &&+ test_hook reference-transaction <<-\EOF && echo "$@" >>actual cat >>actual EOF
t/t1800-hook.sh
+8-4
index 29718aa991..93540b1fa1 100755--- a/t/t1800-hook.sh+++ b/t/t1800-hook.sh@@ -27,7 +27,7 @@ test_expect_success 'git hook run: nonexistent hook with --ignore-missing' ' ' test_expect_success 'git hook run: basic' '- write_script .git/hooks/test-hook <<-EOF &&+ test_hook test-hook <<-EOF && echo Test hook EOF@@ -39,7 +39,7 @@ test_expect_success 'git hook run: basic' ' ' test_expect_success 'git hook run: stdout and stderr both write to our stderr' '- write_script .git/hooks/test-hook <<-EOF &&+ test_hook test-hook <<-EOF && echo >&1 Will end up on stderr echo >&2 Will end up on stderr EOF@@ -84,7 +84,7 @@ test_expect_success 'git hook run arg u ments without -- is not allowed' ' ' test_expect_success 'git hook run -- pass arguments' '- write_script .git/hooks/test-hook <<-\EOF &&+ test_hook test-hook <<-\EOF && echo $1 echo $2 EOF@@ -99,7 +99,7 @@ test_expect_success 'git hook run -- pass arguments' ' ' test_expect_success 'git hook run -- out-of-repo runs excluded' '- write_script .git/hooks/test-hook <<-EOF &&+ test_hook test-hook <<-EOF && echo Test hook EOF@@ -120,6 +120,10 @@ test_expect_success 'git -c core.hooksPath=<PATH> hook run' ' Hook ran four EOF+ test_hook test-hook <<-EOF &&+ echo Test hook+ EOF+ # Test various ways of specifying the path. See also # t1350-config-hooks-path.sh >actual &&
index fffc57120d..4c7c00c94f 100755--- a/t/t7519-status-fsmonitor.sh+++ b/t/t7519-status-fsmonitor.sh@@ -26,7 +26,7 @@ dirty_repo () { } write_integration_script () {- write_script .git/hooks/fsmonitor-test<<-\EOF+ test_hook --setup --clobber fsmonitor-test<<-\EOF if test "$#" -ne 2 then echo "$0: exactly 2 arguments expected"@@ -108,7 +108,7 @@ EOF # test that "update-index --fsmonitor-valid" sets the fsmonitor valid bit test_expect_success 'update-index --fsmonitor-valid" sets the fsmonitor valid bit' '- write_script .git/hooks/fsmonitor-test<<-\EOF &&+ test_hook fsmonitor-test<<-\EOF && printf "last_update_token\0" EOF git update-index --fsmonitor &&@@ -169,7 +169,7 @@ EOF # test that newly added files are marked valid test_expect_success 'newly added files are marked valid' '- write_script .git/hooks/fsmonitor-test<<-\EOF &&+ test_hook --setup --clobber fsmonitor-test<<-\EOF && printf "last_update_token\0" EOF git add new &&@@ -210,7 +210,7 @@ EOF # test that *only* files returned by the integration script get flagged as invalid test_expect_success '*only* files returned by the integration script get flagged as invalid' '- write_script .git/hooks/fsmonitor-test<<-\EOF &&+ test_hook --clobber fsmonitor-test<<-\EOF && printf "last_update_token\0" printf "dir1/modified\0" EOF@@ -231,7 +231,7 @@ test_expect_success 'refresh_index() invalidates fsmonitor cache' ' dirty_repo && write_integration_script && git add . &&- write_script .git/hooks/fsmonitor-test<<-\EOF &&+ test_hook --clobber fsmonitor-test<<-\EOF && EOF git commit -m "to reset" && git reset HEAD~1 &&@@ -280,7 +280,7 @@ do # Make sure it's actually skipping the check for modified and untracked # (if enabled) files unless it is told about them. test_expect_success "status doesn't detect unreported modifications" '- write_script .git/hooks/fsmonitor-test<<-\EOF &&+ test_hook --clobber fsmonitor-test<<-\EOF && printf "last_update_token\0" :>marker EOF@@ -414,14 +414,14 @@ test_expect_success 'status succeeds with sparse index' ' git -C sparse sparse-checkout init --cone --sparse-index && git -C sparse sparse-checkout set dir1 dir2 &&- write_script .git/hooks/fsmonitor-test <<-\EOF &&+ test_hook --clobber fsmonitor-test <<-\EOF && printf "last_update_token\0" EOF git -C full config core.fsmonitor ../.git/hooks/fsmonitor-test && git -C sparse config core.fsmonitor ../.git/hooks/fsmonitor-test && check_sparse_index_behavior ! &&- write_script .git/hooks/fsmonitor-test <<-\EOF &&+ test_hook --clobber fsmonitor-test <<-\EOF && printf "last_update_token\0" printf "dir1/modified\0" EOF@@ -439,7 +439,7 @@ test_expect_success 'status succeeds with sparse index' ' # This one modifies outside the sparse-checkout definition # and hence we expect to expand the sparse-index.- write_script .git/hooks/fsmonitor-test <<-\EOF &&+ test_hook --clobber fsmonitor-test <<-\EOF && printf "last_update_token\0" printf "dir1a/modified\0" EOF
t/test-lib-functions.sh
+52
index 0f439c99d6..6c9c61c79c 100644--- a/t/test-lib-functions.sh+++ b/t/test-lib-functions.sh@@ -551,6 +551,58 @@ write_script () { chmod +x "$1" }+# Usage: test_hook [options] <hook-name> <<-\EOF+#+# -C <dir>:+# Run all git commands in directory <dir>+# --setup+# Setup a hook for subsequent tests, i.e. don't remove it in a+# "test_when_finished"+# --clobber+# Overwrite an existing <hook-name>, if it exists. Implies+# --setup (i.e. the "test_when_finished" is assumed to have been+# set up already).+test_hook () {+ setup= &&+ clobber= &&+ indir= &&+ while test $# != 0+ do+ case "$1" in+ -C)+ indir="$2" &&+ shift+ ;;+ --setup)+ setup=t+ ;;+ --clobber)+ clobber=t+ ;;+ -*)+ BUG "invalid argument: $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" &&+ if test -z "$clobber"+ then+ test_path_is_missing "$hook_file"+ fi &&+ if test -z "$setup$clobber"+ then+ test_when_finished "rm \"$hook_file\""+ fi &&+ 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: #