t9001: use test_when_finished

The confirmation tests in t9001 all save the value of sendemail.confirm, do something to it, then restore it at the end, in a way that breaks the &&-chain (they are not wrong, because they save the $? value, but it fools --chain-lint). Instead, they can all use test_when_finished, and we can even make the code simpler by factoring out the shared lines. Note that we can _almost_ use test_config here, except that: 1. We do not restore the config with test_unconfig, but by setting it back to some prior value. 2. We are not always setting a config variable. Sometimes the change to be undone is unsetting it entirely. We could teach test_config to handle these cases, but it's not worth the complexity for a single call-site. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Mar 20, 2015 at 06:13 UTC 545871bf77da38ec98e66ed35751f42120f1987e
1 file changed +10 -20
t/t9001-send-email.sh
+10 -20
@@ -817,26 +817,25 @@ test_expect_success $PREREQ '--confirm=compose' '
817 test_confirm --confirm=compose --compose
818 '
819
820 -test_expect_success $PREREQ 'confirm by default (due to cc)' '
820 +save_confirm () {
821 CONFIRM=$(git config --get sendemail.confirm) &&
822 + test_when_finished "git config sendemail.confirm ${CONFIRM:-never}"
823 +}
824 +
825 +test_expect_success $PREREQ 'confirm by default (due to cc)' '
826 + save_confirm &&
827 git config --unset sendemail.confirm &&
828 test_confirm
824 - ret="$?"
825 - git config sendemail.confirm ${CONFIRM:-never}
826 - test $ret = "0"
829 '
830
831 test_expect_success $PREREQ 'confirm by default (due to --compose)' '
830 - CONFIRM=$(git config --get sendemail.confirm) &&
832 + save_confirm &&
833 git config --unset sendemail.confirm &&
834 test_confirm --suppress-cc=all --compose
833 - ret="$?"
834 - git config sendemail.confirm ${CONFIRM:-never}
835 - test $ret = "0"
835 '
836
837 test_expect_success $PREREQ 'confirm detects EOF (inform assumes y)' '
839 - CONFIRM=$(git config --get sendemail.confirm) &&
838 + save_confirm &&
839 git config --unset sendemail.confirm &&
840 rm -fr outdir &&
841 git format-patch -2 -o outdir &&
@@ -846,13 +845,10 @@ test_expect_success $PREREQ 'confirm detects EOF (inform assumes y)' '
845 --to=nobody@example.com \
846 --smtp-server="$(pwd)/fake.sendmail" \
847 outdir/*.patch </dev/null
849 - ret="$?"
850 - git config sendemail.confirm ${CONFIRM:-never}
851 - test $ret = "0"
848 '
849
850 test_expect_success $PREREQ 'confirm detects EOF (auto causes failure)' '
855 - CONFIRM=$(git config --get sendemail.confirm) &&
851 + save_confirm &&
852 git config sendemail.confirm auto &&
853 GIT_SEND_EMAIL_NOTTY=1 &&
854 export GIT_SEND_EMAIL_NOTTY &&
@@ -861,13 +857,10 @@ test_expect_success $PREREQ 'confirm detects EOF (auto causes failure)' '
857 --to=nobody@example.com \
858 --smtp-server="$(pwd)/fake.sendmail" \
859 $patches </dev/null
864 - ret="$?"
865 - git config sendemail.confirm ${CONFIRM:-never}
866 - test $ret = "0"
860 '
861
862 test_expect_success $PREREQ 'confirm does not loop forever' '
870 - CONFIRM=$(git config --get sendemail.confirm) &&
863 + save_confirm &&
864 git config sendemail.confirm auto &&
865 GIT_SEND_EMAIL_NOTTY=1 &&
866 export GIT_SEND_EMAIL_NOTTY &&
@@ -876,9 +869,6 @@ test_expect_success $PREREQ 'confirm does not loop forever' '
869 --to=nobody@example.com \
870 --smtp-server="$(pwd)/fake.sendmail" \
871 $patches
879 - ret="$?"
880 - git config sendemail.confirm ${CONFIRM:-never}
881 - test $ret = "0"
872 '
873
874 test_expect_success $PREREQ 'utf8 Cc is rfc2047 encoded' '