git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Junio C Hamano <gitster@pobox.com>
Date
Oct 4, 2023, 16:19 UTC
Message-ID
<xmqqbkdes6lf.fsf@gitster.g>
In-Reply-To
<20231004113413+0200.161419-stepnem@smrk.net>
Štěpán Němec <stepnem@smrk.net> 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
 
Previous: Štěpán NěmecNext: Štěpán Němec
Message 5 of 9 in “test-lib: make sure TEST_DIRECTORY has no trailing slash”
  1. test-lib: make sure TEST_DIRECTORY has no trailing slashŠtěpán Němec, Oct 3, 2023
  2. Junio C HamanoOct 3, 2023
  3. Junio C HamanoOct 3, 2023
  4. Štěpán NěmecOct 4, 2023
  5. Junio C HamanoOct 4, 2023
  6. Štěpán NěmecOct 4, 2023
  7. Junio C HamanoOct 4, 2023
  8. Štěpán NěmecOct 4, 2023
  9. Junio C HamanoOct 4, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.