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