Re: [PATCH 02/12] t: prepare `test_must_fail ()` for `set -e`
On Mon, Apr 13, 2026 at 09:33:59AM -0700, Junio C Hamano wrote:
Show 41 quoted lines
> Patrick Steinhardt <ps@pks.im> writes:
>
> > The helper function `test_must_fail ()` executes a specific Git command
> > that may or may not fail in a specific way. This is done by executing
> > the command in question and then comparing its exit code against a set
> > of conditions.
> >
> > This works, but once we run our test suite with `set -e` we may bail out
> > of `test_must_fail ()` early in case the command actually fails, even
> > though we expect it to fail. Prepare for this change by marking the
> > command part of a condition.
> >
> > Signed-off-by: Patrick Steinhardt <ps@pks.im>
> > ---
> > t/test-lib-functions.sh | 8 ++++++--
> > 1 file changed, 6 insertions(+), 2 deletions(-)
> >
> > diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh
> > index f3af10fb7e..f8bc77619b 100644
> > --- a/t/test-lib-functions.sh
> > +++ b/t/test-lib-functions.sh
> > @@ -1195,8 +1195,12 @@ test_must_fail () {
> > echo >&7 "test_must_fail: only 'git' is allowed: $*"
> > return 1
> > fi
> > - "$@" 2>&7
> > - exit_code=$?
> > + if "$@" 2>&7
> > + then
> > + exit_code=0
> > + else
> > + exit_code=$?
> > + fi
>
> This is obvious and clear. Alternatively
>
> exit_code=0; "$@" 2>&7 || exit_code=$?
>
> would be more assuring to readers who (with less fluency in Bourne
> shells) wonder, upon seeing the else clause, how far "$?" traveled
> and it still holds the status of "$@".Fair enough, will change.
Patrick