Subject: Re: [PATCH] test-lib: make sure TEST_DIRECTORY has no trailing slash

From: Junio C Hamano <[email-masked]>
Date: Wed, 04 Oct 2023 10:15:30 -0700

Štěpán Němec <[email-masked]> writes:

> Yes, this is even simpler and more obvious, although for some reason,
> the subshell version works just as well, i.e., with just
> TEST_DIRECTORY=$(cd . && pwd) (no cd in the parent) in test-lib.sh and
>
> cat <<EOF >./tslash.sh
> #!/bin/sh
> test_description='yada yada'
>
> . ./test-lib.sh
>
> test_expect_success 'check TEST_DIRECTORY for trailing slash' '
>         echo "$TEST_DIRECTORY" &&
>         test "$TEST_DIRECTORY" = "${TEST_DIRECTORY%/}"
> '
>
> test_done
> EOF

If the only thing you checked was if TEST_DIRECTORY has or does not
have a trailing slash, then it is totally understandable how and why
the chdir inside subshell works.  The value $(pwd) in the subshell
returns, that is assigned to TEST_DIRECTORY, is canonicalized.  But
the outer shell still thinks $(pwd) is with trailing slash, and the
above does not test it.

    test_expect_success 'check $PWD for trailing slash' '
            echo "$PWD" &&
            test "$PWD" = "${PWD%/}"
    '

The primary reason why I said I was silly doing it in a subshell was
because I wanted to make sure that any test that refers to $PWD or
$(pwd) later in the code would not get upset the same way with
trailing slash after $PWD or $(pwd).  As long as it is not overdone,
it is a good practice to consider possibilities of similar problems
triggered by the same root cause, which we may not have got bitten
by yet, and preventing it from happening with the same fix.

Thanks.