lib-gpg: allow tests with GPGSM or GPGSSH prereq first

When the 'GPG' prereq is lazily tested, `mkdir "$GNUPGHOME"` could fail if the "$GNUPGHOME" directory already exists. This can happen if the 'GPGSM' or the 'GPGSSH' prereq has been lazily tested before as they already create "$GNUPGHOME". To allow the GPGSM or the GPGSSH prereq to appear before the GPG prereq in some test scripts, let's refactor the creation and setup of the "$GNUPGHOME"` directory in a new prepare_gnupghome() function that uses `mkdir -p "$GNUPGHOME"`. This will be useful in a following commit. Unfortunately the new prepare_gnupghome() function cannot be used when lazily testing the GPG2 prereq, because that would expose existing, hidden bugs in "t1016-compatObjectFormat.sh", so let's just document that with a NEEDSWORK comment. Helped-by: Todd Zullinger <tmz@pobox.com> Helped-by: Collin Funk <collin.funk1@gmail.com> Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Christian Couder committed Oct 13, 2025 at 10:48 UTC e204a167757d2c3e4914df60bad5cf78b0e6a9bb
1 file changed +20 -4
t/lib-gpg.sh
+20 -4
@@ -9,6 +9,16 @@
9 GNUPGHOME="$(pwd)/gpghome"
10 export GNUPGHOME
11
12 +# All the "test_lazy_prereq GPG*" below should use
13 +# `prepare_gnupghome()` either directly or through a call to
14 +# `test_have_prereq GPG*`. That's because `gpg` and `gpgsm`
15 +# only create the directory specified using "$GNUPGHOME" or
16 +# `--homedir` if it's the default (usually "~/.gnupg").
17 +prepare_gnupghome() {
18 + mkdir -p "$GNUPGHOME" &&
19 + chmod 0700 "$GNUPGHOME"
20 +}
21 +
22 test_lazy_prereq GPG '
23 gpg_version=$(gpg --version 2>&1)
24 test $? != 127 || exit 1
@@ -38,8 +48,7 @@ test_lazy_prereq GPG '
48 # To export ownertrust:
49 # gpg --homedir /tmp/gpghome --export-ownertrust \
50 # > lib-gpg/ownertrust
41 - mkdir "$GNUPGHOME" &&
42 - chmod 0700 "$GNUPGHOME" &&
51 + prepare_gnupghome &&
52 (gpgconf --kill all || : ) &&
53 gpg --homedir "${GNUPGHOME}" --import \
54 "$TEST_DIRECTORY"/lib-gpg/keyring.gpg &&
@@ -63,6 +72,14 @@ test_lazy_prereq GPG2 '
72 ;;
73 *)
74 (gpgconf --kill all || : ) &&
75 +
76 + # NEEDSWORK: prepare_gnupghome() should definitely be
77 + # called here, but it looks like it exposes a
78 + # pre-existing, hidden bug by allowing some tests in
79 + # t1016-compatObjectFormat.sh to run instead of being
80 + # skipped. See:
81 + # https://lore.kernel.org/git/ZoV8b2RvYxLOotSJ@teonanacatl.net/
82 +
83 gpg --homedir "${GNUPGHOME}" --import \
84 "$TEST_DIRECTORY"/lib-gpg/keyring.gpg &&
85 gpg --homedir "${GNUPGHOME}" --import-ownertrust \
@@ -132,8 +149,7 @@ test_lazy_prereq GPGSSH '
149 test $? = 0 || exit 1;
150
151 # Setup some keys and an allowed signers file
135 - mkdir -p "${GNUPGHOME}" &&
136 - chmod 0700 "${GNUPGHOME}" &&
152 + prepare_gnupghome &&
153 (setfacl -k "${GNUPGHOME}" 2>/dev/null || true) &&
154 ssh-keygen -t ed25519 -N "" -C "git ed25519 key" -f "${GPGSSH_KEY_PRIMARY}" >/dev/null &&
155 ssh-keygen -t rsa -b 2048 -N "" -C "git rsa2048 key" -f "${GPGSSH_KEY_SECONDARY}" >/dev/null &&