From: Junio C Hamano <gitster@pobox.com>
To: "Štěpán Němec" <stepnem@smrk.net>
Cc: git@vger.kernel.org
Subject: Re: [PATCH] test-lib: make sure TEST_DIRECTORY has no trailing slash
Date: Tue, 03 Oct 2023 14:21:51 -0700 [thread overview]
Message-ID: <xmqqwmw3wgeo.fsf@gitster.g> (raw)
In-Reply-To: <20231003082323.3002663-1-stepnem@smrk.net> ("Štěpán Němec"'s message of "Tue, 3 Oct 2023 10:23:23 +0200")
Štěpán Němec <stepnem@smrk.net> writes:
> Turns out having `pwd` (which TEST_DIRECTORY defaults to) print $PWD
> with a trailing slash isn't very difficult, in my case it went something
> like
>
> ; tmux new-window -c ~/src/git/t/
> [in the new window]
> ; sh ./t0000-basic.sh
> PANIC: Running in a /home/stepnem/src/git/t/ that doesn't end in '/t'?
> ; pwd
> /home/stepnem/src/git/t/
>
> (tmux(1) apparently sets PWD in the environment in addition to calling
> chdir(2), which seems enough to make at least some shells preserve the
> trailing slash in `pwd` output.)
>
> Strip the trailing slash, if present, to prevent bailing out with the
> PANIC message in such cases.
>
> Signed-off-by: Štěpán Němec <stepnem@smrk.net>
> ---
> t/test-lib.sh | 1 +
> 1 file changed, 1 insertion(+)
>
> diff --git a/t/test-lib.sh b/t/test-lib.sh
> index 1656c9eed006..3b6f1a17e349 100644
> --- a/t/test-lib.sh
> +++ b/t/test-lib.sh
> @@ -35,6 +35,7 @@ else
> # needing to exist.
> TEST_DIRECTORY=$(cd "$TEST_DIRECTORY" && pwd) || exit 1
> fi
> +TEST_DIRECTORY="${TEST_DIRECTORY%/}"
> if test -z "$TEST_OUTPUT_DIRECTORY"
> then
> # Similarly, override this to store the test-results subdir
>
> base-commit: d0e8084c65cbf949038ae4cc344ac2c2efd77415
While this would certainly squelch the particular test on TEST_DIRECTORY
I am not sure if a safer fix would be to fix your PWD when it has a
trailing slash. Your tests with this patch may be taking unintended
branch when they do something like
if test "$TRASH_DIRECTORY" = "$(pwd)"
then
...
fi
as TRASH_DIRECTORY, whose default is derived from TEST_DIRECTORY and
thanks to your fix it now does not have an extra slash in it, does
not have a trailing slash but your $(pwd) or $PWD would have the
trailing slash, simply because the patch only fixes TEST_DIRECTORY
without fixing PWD.
I wonder if this would be a safer alternative, or is it doing too
much more than what is necessary?
t/test-lib.sh | 31 +++++++++++++++++++------------
1 file changed, 19 insertions(+), 12 deletions(-)
diff --git c/t/test-lib.sh w/t/test-lib.sh
index 1656c9eed0..6ec42eab0d 100644
--- c/t/test-lib.sh
+++ w/t/test-lib.sh
@@ -22,19 +22,26 @@ then
# ensure that TEST_DIRECTORY is an absolute path so that it
# is valid even if the current working directory is changed
TEST_DIRECTORY=$(pwd)
-else
- # The TEST_DIRECTORY will always be the path to the "t"
- # directory in the git.git checkout. This is overridden by
- # e.g. t/lib-subtest.sh, but only because its $(pwd) is
- # different. Those tests still set "$TEST_DIRECTORY" to the
- # same path.
- #
- # See use of "$GIT_BUILD_DIR" and "$TEST_DIRECTORY" below for
- # hard assumptions about "$GIT_BUILD_DIR/t" existing and being
- # the "$TEST_DIRECTORY", and e.g. "$TEST_DIRECTORY/helper"
- # needing to exist.
- TEST_DIRECTORY=$(cd "$TEST_DIRECTORY" && pwd) || exit 1
fi
+
+# The TEST_DIRECTORY will always be the path to the "t"
+# directory in the git.git checkout. This is overridden by
+# e.g. t/lib-subtest.sh, but only because its $(pwd) is
+# different. Those tests still set "$TEST_DIRECTORY" to the
+# same path.
+#
+# See use of "$GIT_BUILD_DIR" and "$TEST_DIRECTORY" below for
+# hard assumptions about "$GIT_BUILD_DIR/t" existing and being
+# the "$TEST_DIRECTORY", and e.g. "$TEST_DIRECTORY/helper"
+# needing to exist.
+#
+# Also, apparently a shell spawned in some tricky way can be
+# talked into keeping a slash at the end of its $PWD.
+#
+# All of the above can be worked around by doing an extra chdir
+# and asking where we ended up to be.
+TEST_DIRECTORY=$(cd "$TEST_DIRECTORY/." && pwd) || exit 1
+
if test -z "$TEST_OUTPUT_DIRECTORY"
then
# Similarly, override this to store the test-results subdir
next prev parent reply other threads:[~2023-10-03 21:21 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-03 8:23 [PATCH] test-lib: make sure TEST_DIRECTORY has no trailing slash Štěpán Němec
2023-10-03 21:21 ` Junio C Hamano [this message]
2023-10-03 21:57 ` Junio C Hamano
2023-10-04 9:34 ` Štěpán Němec
2023-10-04 16:19 ` Junio C Hamano
2023-10-04 17:01 ` Štěpán Němec
2023-10-04 17:15 ` Junio C Hamano
2023-10-04 17:40 ` Štěpán Němec
2023-10-04 18:24 ` Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=xmqqwmw3wgeo.fsf@gitster.g \
--to=gitster@pobox.com \
--cc=git@vger.kernel.org \
--cc=stepnem@smrk.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).