Skip to content

Commit 9dbf20e

Browse files
avargitster
authored andcommitted
test-lib: correct and assert TEST_DIRECTORY overriding
Correct a misleading comment added by me in 62f5390 (test-lib: Allow overriding of TEST_DIRECTORY, 2010-08-19), and add an assertion that TEST_DIRECTORY cannot point to any directory except the "t" directory in the top-level of git.git. This assertion is in effect not new, since we'd already die if that wasn't the case[1], but it and the updated commentary help to make that clearer. The existing comments were also on the wrong arms of the "if". I.e. the "allow tests to override this" was on the "test -z" arm. That came about due to a combination of 62f5390 and 85176d7 (test-lib.sh: convert $TEST_DIRECTORY to an absolute path, 2013-11-17). Those earlier comments could be read as allowing the "$TEST_DIRECTORY" to be some path outside of t/. As explained in the updated comment that's impossible, rather it was meant for *tests* that ran outside of t/, i.e. the "t0000-basic.sh" tests that use "lib-subtest.sh". Those tests have a different working directory, but they set the "TEST_DIRECTORY" to the same path for bootstrapping. The comments now reflect that, and further comment on why we have a hard dependency on this. 1. https://lore.kernel.org/git/[email protected]/ Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 66c1a56 commit 9dbf20e

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

t/test-lib.sh

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,20 @@
1919
# t/ subdirectory and are run in 'trash directory' subdirectory.
2020
if test -z "$TEST_DIRECTORY"
2121
then
22-
# We allow tests to override this, in case they want to run tests
23-
# outside of t/, e.g. for running tests on the test library
24-
# itself.
25-
TEST_DIRECTORY=$(pwd)
26-
else
2722
# ensure that TEST_DIRECTORY is an absolute path so that it
2823
# is valid even if the current working directory is changed
24+
TEST_DIRECTORY=$(pwd)
25+
else
26+
# The TEST_DIRECTORY will always be the path to the "t"
27+
# directory in the git.git checkout. This is overridden by
28+
# e.g. t/lib-subtest.sh, but only because its $(pwd) is
29+
# different. Those tests still set "$TEST_DIRECTORY" to the
30+
# same path.
31+
#
32+
# See use of "$GIT_BUILD_DIR" and "$TEST_DIRECTORY" below for
33+
# hard assumptions about "$GIT_BUILD_DIR/t" existing and being
34+
# the "$TEST_DIRECTORY", and e.g. "$TEST_DIRECTORY/helper"
35+
# needing to exist.
2936
TEST_DIRECTORY=$(cd "$TEST_DIRECTORY" && pwd) || exit 1
3037
fi
3138
if test -z "$TEST_OUTPUT_DIRECTORY"
@@ -35,6 +42,11 @@ then
3542
TEST_OUTPUT_DIRECTORY=$TEST_DIRECTORY
3643
fi
3744
GIT_BUILD_DIR="$TEST_DIRECTORY"/..
45+
if test "$TEST_DIRECTORY" = "${TEST_DIRECTORY%/t}"
46+
then
47+
echo "PANIC: Running in a $TEST_DIRECTORY that doesn't end in '/t'?" >&2
48+
exit 1
49+
fi
3850

3951
# Prepend a string to a VAR using an arbitrary ":" delimiter, not
4052
# adding the delimiter if VAR or VALUE is empty. I.e. a generalized:

0 commit comments

Comments
 (0)