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

From: Junio C Hamano <[email-masked]>
Date: Wed, 04 Oct 2023 09:19:40 -0700

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

> Yes, actually, AFAICT just $(cd . && pwd) fixes things (and saves a few
> syscalls), and I agree this is a much better approach than my naive fix.

Actually I was still being silly.  We sometimes do

	val=$(cd there && do something there)

so that we can get the output from a command in a different
directory _without_ having to move our current directory.  But the
point of this current topic is that we _need_ to convince the shell
that the path to our current directory is a canonical one without
trailing slash, so my silly 'cd "$(pwd)/."' (or your "cd .") should
be done outside the command expansion, or the canonicalized $PWD will
only appear inside the $() and the next reference to $(pwd) or $PWD
in the test script will still give the path with the trailing slash,
that is textually different from $TEST_DIRECTORY.

I wonder if this works better for you.  We would be sure that $PWD
and $TEST_DIRECTORY (when the latter is not imported from the
environment) are the same, so "your cwd that does not end with /t
and has a trailing slash after it" would be gone.  Any $PWD or $(pwd)
the tests refer to later in the step will also lack the unwanted
trailing slash.   As long as "cd ." is sufficient to cause the shell
reexamine and canonicalize the $PWD, that is.

Thanks.

 t/test-lib.sh | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git c/t/test-lib.sh w/t/test-lib.sh
index 1656c9eed0..a7045e028c 100644
--- c/t/test-lib.sh
+++ w/t/test-lib.sh
@@ -19,6 +19,11 @@
 # t/ subdirectory and are run in 'trash directory' subdirectory.
 if test -z "$TEST_DIRECTORY"
 then
+	# It is reported that some shells spawned in tricky ways can
+	# give $PWD with a trailing slash. An explicit chdir hopefully
+	# would wake them out of their hallucination.
+	cd .
+
 	# ensure that TEST_DIRECTORY is an absolute path so that it
 	# is valid even if the current working directory is changed
 	TEST_DIRECTORY=$(pwd)
@@ -626,7 +631,6 @@ fi
 # Protect ourselves from common misconfiguration to export
 # CDPATH into the environment
 unset CDPATH
-
 unset GREP_OPTIONS
 unset UNZIP