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
Štěpán Němec <stepnem@smrk.net>
Date
Oct 4, 2023, 17:01 UTC
Message-ID
<20231004190140+0200.740775-stepnem@smrk.net>
In-Reply-To
<xmqqbkdes6lf.fsf@gitster.g>

On Wed, 04 Oct 2023 09:19:40 -0700 Junio C. Hamano wrote:

Show 55 quoted lines
> Š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

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

I get
; echo ${TEST_DIRECTORY-unset}
unset
; pwd
/home/stepnem/src/git/t/
; echo $PWD
/home/stepnem/src/git/t/
; sh ./tslash.sh -v
Initialized empty Git repository in /home/stepnem/src/git/t/trash directory.tslash/.git/
expecting success of slash.1 'check TEST_DIRECTORY for trailing slash':
        echo "$TEST_DIRECTORY" &&
        test "$TEST_DIRECTORY" = "${TEST_DIRECTORY%/}"

/home/stepnem/src/git/t ok 1 - check TEST_DIRECTORY for trailing slash

# passed all 1 test(s) 1..1 ; pwd /home/stepnem/src/git/t/ ; echo $PWD /home/stepnem/src/git/t/

[and, needless to say, reverting the changes still triggers the panic:
; sh ./tslash.sh
PANIC: Running in a /home/stepnem/src/git/t/ that doesn't end in '/t'?
]
Go figure...
In any case, for what it's worth, any of your versions is
Tested-by: Štěpán Němec <stepnem@smrk.net>
Thanks,
  Štěpán
Previous: Junio C HamanoNext: Junio C Hamano
Message 6 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.