Subject: Re: [PATCH 2/4] test-lib: make $GIT_BUILD_DIR an absolute path

From: Junio C Hamano <[email-masked]>
Date: Fri, 18 Feb 2022 15:30:51 -0800

Ævar Arnfjörð Bjarmason  <[email-masked]> writes:

> Change the GIT_BUILD_DIR from a path like "/path/to/build/t/.." to
> "/path/to/build". The "TEST_DIRECTORY" here is already made an
> absolute path a few lines above this.
>
> This will be helpful to LSAN_OPTIONS which will want to strip the
> build directory path from filenames, which we couldn't do if we had a
> "/.." in there.
>
> Signed-off-by: Ævar Arnfjörð Bjarmason <[email-masked]>
> ---
>  t/test-lib.sh | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/t/test-lib.sh b/t/test-lib.sh
> index 3212966a82f..4f523b82ce5 100644
> --- a/t/test-lib.sh
> +++ b/t/test-lib.sh
> @@ -34,7 +34,7 @@ then
>  	# elsewhere
>  	TEST_OUTPUT_DIRECTORY=$TEST_DIRECTORY
>  fi
> -GIT_BUILD_DIR="$TEST_DIRECTORY"/..
> +GIT_BUILD_DIR="${TEST_DIRECTORY%/t}"

This makes perfect sense in the normal case, but the provision the
code that precedes this part has, i.e.

    if test -z "$TEST_DIRECTORY"
    then
            # We allow tests to override this, in case they want to run tests
            # outside of t/, e.g. for running tests on the test library
            # itself.
            TEST_DIRECTORY=$(pwd)
    else
            # ensure that TEST_DIRECTORY is an absolute path so that it
            # is valid even if the current working directory is changed
            TEST_DIRECTORY=$(cd "$TEST_DIRECTORY" && pwd) || exit 1
    fi

to allow TEST_DIRECTORY to be set externally robs the guarantee that
you can sensibly strip "/t" from its tail and expect everything to
work correctly.  The only thing the original requires on such an
externally given TEST_DIRECTORY is that one level above it is usable
as GIT_BUILD_DIR.

IOW,

	GIT_BUILD_DIR="$(cd "$TEST_DIRECTORY/.." && pwd)"

would give you what you want to achieve in either code path, as long
as the original was working correctly for whatever value that is
given to TEST_DIRECTORY externally.

So, perhaps

	if test -z "$TEST_DIRECTORY"
	then
		TEST_DIRECTORY=$(pwd)
		GIT_BUILD_DIR=${TEST_DIRECTORY%/t}
	else
		TEST_DIRECTORY=$(cd "$TEST_DIRECTORY" && pwd) &&
		GIT_BUILD_DIR=$(cd "$TEST_DIRECTORY/.." && pwd)
	fi

or something like that?  I dunno.