@matheusbsbs / matheus-bagliano / commits / b417ec5f22
Fork 0

tests: turn GPG, GPGSM and RFC1991 into lazy prereqs

The code to set those prereqs is executed completely outside of any `test_eval_` block. As a consequence, its output had to be suppressed so that it does not clutter the output of a regular test script run. Unfortunately, the output *stays* suppressed even when the `--verbose` option is in effect. This hid important output when debugging why the GPG prereq was not enabled in the Windows part of our CI builds. In preparation for fixing that, let's move all of this code into lazy prereqs. The only slightly tricky part is the global environment variable `GNUPGHOME`. Originally, it was configured only when we verified that there is a `gpg` in the `PATH` that we can use. This is now no longer possible, as lazy prereqs are evaluated in a subshell that changes the working directory to a temporary one. Therefore, we simply _always_ set that environment variable: it does not hurt anything because it does not indicate the presence of a working GPG. Side note: it was quite tempting to use a hack that is possible because we do not validate what is passed to `test_lazy_prereq` (and it is therefore possible to "break out" of the lazy_prereq subshell: test_lazy_prereq GPG '...) && GNUPGHOME=... && (...' However, this is rather tricksy hobbitses code, and the current patch is _much_ easier to understand. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Mar 26, 2020 at 15:35 UTC b417ec5f22e05e4320b338cd48b280008314a691
1 file changed +57 -45
t/lib-gpg.sh
+57 -45
@@ -1,12 +1,25 @@
1 -gpg_version=$(gpg --version 2>&1)
2 -if test $? != 127
3 -then
1 +# We always set GNUPGHOME, even if no usable GPG was found, as
2 +#
3 +# - It does not hurt, and
4 +#
5 +# - we cannot set global environment variables in lazy prereqs because they are
6 +# executed in an eval'ed subshell that changes the working directory to a
7 +# temporary one.
8 +
9 +GNUPGHOME="$PWD/gpghome"
10 +export GNUPGHOME
11 +
12 +test_lazy_prereq GPG '
13 + gpg_version=$(gpg --version 2>&1)
14 + test $? != 127 || exit 1
15 +
16 # As said here: http://www.gnupg.org/documentation/faqs.html#q6.19
5 - # the gpg version 1.0.6 didn't parse trust packets correctly, so for
17 + # the gpg version 1.0.6 did not parse trust packets correctly, so for
18 # that version, creation of signed tags using the generated key fails.
19 case "$gpg_version" in
8 - 'gpg (GnuPG) 1.0.6'*)
20 + "gpg (GnuPG) 1.0.6"*)
21 say "Your version of gpg (1.0.6) is too buggy for testing"
22 + exit 1
23 ;;
24 *)
25 # Available key info:
@@ -25,55 +38,54 @@ then
38 # To export ownertrust:
39 # gpg --homedir /tmp/gpghome --export-ownertrust \
40 # > lib-gpg/ownertrust
28 - mkdir ./gpghome &&
29 - chmod 0700 ./gpghome &&
30 - GNUPGHOME="$PWD/gpghome" &&
31 - export GNUPGHOME &&
41 + mkdir "$GNUPGHOME" &&
42 + chmod 0700 "$GNUPGHOME" &&
43 (gpgconf --kill gpg-agent >/dev/null 2>&1 || : ) &&
44 gpg --homedir "${GNUPGHOME}" 2>/dev/null --import \
45 "$TEST_DIRECTORY"/lib-gpg/keyring.gpg &&
46 gpg --homedir "${GNUPGHOME}" 2>/dev/null --import-ownertrust \
47 "$TEST_DIRECTORY"/lib-gpg/ownertrust &&
48 gpg --homedir "${GNUPGHOME}" </dev/null >/dev/null 2>&1 \
38 - --sign -u committer@example.com &&
39 - test_set_prereq GPG &&
40 - # Available key info:
41 - # * see t/lib-gpg/gpgsm-gen-key.in
42 - # To generate new certificate:
43 - # * no passphrase
44 - # gpgsm --homedir /tmp/gpghome/ \
45 - # -o /tmp/gpgsm.crt.user \
46 - # --generate-key \
47 - # --batch t/lib-gpg/gpgsm-gen-key.in
48 - # To import certificate:
49 - # gpgsm --homedir /tmp/gpghome/ \
50 - # --import /tmp/gpgsm.crt.user
51 - # To export into a .p12 we can later import:
52 - # gpgsm --homedir /tmp/gpghome/ \
53 - # -o t/lib-gpg/gpgsm_cert.p12 \
54 - # --export-secret-key-p12 "committer@example.com"
55 - echo | gpgsm --homedir "${GNUPGHOME}" 2>/dev/null \
56 - --passphrase-fd 0 --pinentry-mode loopback \
57 - --import "$TEST_DIRECTORY"/lib-gpg/gpgsm_cert.p12 &&
58 -
59 - gpgsm --homedir "${GNUPGHOME}" 2>/dev/null -K |
60 - grep fingerprint: |
61 - cut -d" " -f4 |
62 - tr -d '\n' >"${GNUPGHOME}/trustlist.txt" &&
63 -
64 - echo " S relax" >>"${GNUPGHOME}/trustlist.txt" &&
65 - echo hello | gpgsm --homedir "${GNUPGHOME}" >/dev/null \
66 - -u committer@example.com -o /dev/null --sign - 2>&1 &&
67 - test_set_prereq GPGSM
49 + --sign -u committer@example.com
50 ;;
51 esac
70 -fi
52 +'
53 +
54 +test_lazy_prereq GPGSM '
55 + test_have_prereq GPG &&
56 + # Available key info:
57 + # * see t/lib-gpg/gpgsm-gen-key.in
58 + # To generate new certificate:
59 + # * no passphrase
60 + # gpgsm --homedir /tmp/gpghome/ \
61 + # -o /tmp/gpgsm.crt.user \
62 + # --generate-key \
63 + # --batch t/lib-gpg/gpgsm-gen-key.in
64 + # To import certificate:
65 + # gpgsm --homedir /tmp/gpghome/ \
66 + # --import /tmp/gpgsm.crt.user
67 + # To export into a .p12 we can later import:
68 + # gpgsm --homedir /tmp/gpghome/ \
69 + # -o t/lib-gpg/gpgsm_cert.p12 \
70 + # --export-secret-key-p12 "committer@example.com"
71 + echo | gpgsm --homedir "${GNUPGHOME}" 2>/dev/null \
72 + --passphrase-fd 0 --pinentry-mode loopback \
73 + --import "$TEST_DIRECTORY"/lib-gpg/gpgsm_cert.p12 &&
74 +
75 + gpgsm --homedir "${GNUPGHOME}" 2>/dev/null -K |
76 + grep fingerprint: |
77 + cut -d" " -f4 |
78 + tr -d "\\n" >"${GNUPGHOME}/trustlist.txt" &&
79 +
80 + echo " S relax" >>"${GNUPGHOME}/trustlist.txt" &&
81 + echo hello | gpgsm --homedir "${GNUPGHOME}" >/dev/null \
82 + -u committer@example.com -o /dev/null --sign - 2>&1
83 +'
84
72 -if test_have_prereq GPG &&
73 - echo | gpg --homedir "${GNUPGHOME}" -b --rfc1991 >/dev/null 2>&1
74 -then
75 - test_set_prereq RFC1991
76 -fi
85 +test_lazy_prereq RFC1991 '
86 + test_have_prereq GPG &&
87 + echo | gpg --homedir "${GNUPGHOME}" -b --rfc1991 >/dev/null 2>&1
88 +'
89
90 sanitize_pgp() {
91 perl -ne '