On Mon, Mar 23, 2020 at 01:32:58PM -0400, Jeff King wrote: > In this case if the whole thing were in a "test_lazy_prereq" we would > have gotten all that for free. I don't think the laziness would matter > too much in practice, though it is a little funny that it has side > effects (like setting up $GPGHOME). Having an immediate version like > "test_check_prereq" would make sense to me. > > I don't know if it's worth re-doing this patch (I'll leave that decision > to you). But something to keep in mind when we run into similar > situations (or are writing new prereq code). Actually, it's pretty straight-forward and I think the resulting code is cleaner. Note that we do have to use a real expect_success because of the side effects. That does increment the test counter. IMHO that's not a big deal, but if we're concerned it wouldn't be too hard to add a non-lazy prereq block. Patch is below (I pulled GPGSM into its own block, which involved reindenting; view with "-w"). --- diff --git a/t/lib-gpg.sh b/t/lib-gpg.sh index 11b83b8c24..6aa936e3ac 100755 --- a/t/lib-gpg.sh +++ b/t/lib-gpg.sh @@ -1,13 +1,16 @@ #!/bin/sh -gpg_version=$(gpg --version 2>&1) -if test $? != 127 -then +# This has to happen in a real test and not a lazy_prereq because it +# has side effects (like setting up $GNUPGHOME). +test_expect_success 'set up GPG prereq' ' + test_might_fail gpg --version && + test $? != 127 && + # As said here: http://www.gnupg.org/documentation/faqs.html#q6.19 - # the gpg version 1.0.6 didn't parse trust packets correctly, so for + # the gpg version 1.0.6 did not parse trust packets correctly, so for # that version, creation of signed tags using the generated key fails. case "$gpg_version" in - 'gpg (GnuPG) 1.0.6'*) + "gpg (GnuPG) 1.0.6"*) say "Your version of gpg (1.0.6) is too buggy for testing" ;; *) @@ -31,51 +34,51 @@ then chmod 0700 ./gpghome && GNUPGHOME="$PWD/gpghome" && export GNUPGHOME && - (gpgconf --kill gpg-agent >/dev/null 2>&1 || : ) && - gpg --homedir "${GNUPGHOME}" 2>/dev/null --import \ + (gpgconf --kill gpg-agent || : ) && + gpg --homedir "${GNUPGHOME}" --import \ "$TEST_DIRECTORY"/lib-gpg/keyring.gpg && - gpg --homedir "${GNUPGHOME}" 2>/dev/null --import-ownertrust \ + gpg --homedir "${GNUPGHOME}" --import-ownertrust \ "$TEST_DIRECTORY"/lib-gpg/ownertrust && - gpg --homedir "${GNUPGHOME}" </dev/null >/dev/null 2>&1 \ - --sign -u committer@xxxxxxxxxxx && - test_set_prereq GPG && - # Available key info: - # * see t/lib-gpg/gpgsm-gen-key.in - # To generate new certificate: - # * no passphrase - # gpgsm --homedir /tmp/gpghome/ \ - # -o /tmp/gpgsm.crt.user \ - # --generate-key \ - # --batch t/lib-gpg/gpgsm-gen-key.in - # To import certificate: - # gpgsm --homedir /tmp/gpghome/ \ - # --import /tmp/gpgsm.crt.user - # To export into a .p12 we can later import: - # gpgsm --homedir /tmp/gpghome/ \ - # -o t/lib-gpg/gpgsm_cert.p12 \ - # --export-secret-key-p12 "committer@xxxxxxxxxxx" - echo | gpgsm --homedir "${GNUPGHOME}" 2>/dev/null \ - --passphrase-fd 0 --pinentry-mode loopback \ - --import "$TEST_DIRECTORY"/lib-gpg/gpgsm_cert.p12 && - - gpgsm --homedir "${GNUPGHOME}" 2>/dev/null -K | - grep fingerprint: | - cut -d" " -f4 | - tr -d '\n' >"${GNUPGHOME}/trustlist.txt" && - - echo " S relax" >>"${GNUPGHOME}/trustlist.txt" && - echo hello | gpgsm --homedir "${GNUPGHOME}" >/dev/null \ - -u committer@xxxxxxxxxxx -o /dev/null --sign - 2>&1 && - test_set_prereq GPGSM + gpg --homedir "${GNUPGHOME}" \ + --sign -u committer@xxxxxxxxxxx >/dev/null && + test_set_prereq GPG ;; esac -fi +' + +test_have_prereq GPG && +test_lazy_prereq GPGSM ' + # Available key info: + # * see t/lib-gpg/gpgsm-gen-key.in + # To generate new certificate: + # * no passphrase + # gpgsm --homedir /tmp/gpghome/ \ + # -o /tmp/gpgsm.crt.user \ + # --generate-key \ + # --batch t/lib-gpg/gpgsm-gen-key.in + # To import certificate: + # gpgsm --homedir /tmp/gpghome/ \ + # --import /tmp/gpgsm.crt.user + # To export into a .p12 we can later import: + # gpgsm --homedir /tmp/gpghome/ \ + # -o t/lib-gpg/gpgsm_cert.p12 \ + # --export-secret-key-p12 "committer@xxxxxxxxxxx" + echo | gpgsm --homedir "${GNUPGHOME}" \ + --passphrase-fd 0 --pinentry-mode loopback \ + --import "$TEST_DIRECTORY"/lib-gpg/gpgsm_cert.p12 && + gpgsm --homedir "${GNUPGHOME}" -K | + grep fingerprint: | + cut -d" " -f4 | + tr -d "\\n" >"${GNUPGHOME}/trustlist.txt" && + echo " S relax" >>"${GNUPGHOME}/trustlist.txt" && + echo hello | gpgsm --homedir "${GNUPGHOME}" \ + -u committer@xxxxxxxxxxx -o /dev/null --sign - +' -if test_have_prereq GPG && - echo | gpg --homedir "${GNUPGHOME}" -b --rfc1991 >/dev/null 2>&1 -then - test_set_prereq RFC1991 -fi +test_have_prereq GPG && +test_lazy_prereq RFC1991 ' + echo | gpg --homedir "${GNUPGHOME}" -b --rfc1991 +' sanitize_pgp() { perl -ne '