test-lib-functions: add and use a "test_hook" wrapper

Add a "test_hook" wrapper similar to the existing "test_config"
wrapper added in d960c47a88 (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>
This commit is contained in:
Ævar Arnfjörð Bjarmason
2022-03-17 11:13:06 +01:00
committed by Junio C Hamano
parent 74cc1aa55f
commit 7da7f63cf9
13 changed files with 97 additions and 43 deletions

View File

@ -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:
#