Re: [PATCH 1/1] t: update path checks using test_path helpers
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Oct 14, 2025, 18:19 UTC
- Message-ID
- <xmqqa51txs66.fsf@gitster.g>
- In-Reply-To
- <20251014161446.6135-1-solobarine@gmail.com>
Solly <solobarine@gmail.com> writes:
> Subject: Re: [PATCH 1/1] t: update path checks using test_path helpers
Using "t2401:" instead of "t:" would give you a bit more information.
> Update old-style shell path checks to use the modern test > helpers 'test_path_is_file' and 'test_path_is_dir' for improved > readability.
This gives a wrong justification.
For readability, "test -f tested-file" is plenty readable. The point of using test_path_is_file and its friends is to get better runtime diagnosis.
Read the helper you are using and understand what it does and why it does it (they are found in t/test-lib-functions.sh):
# debugging-friendly alternatives to "test [-f|-d|-e]"
# The commands test the existence or non-existence of $1
test_path_is_file () {
test "$#" -ne 1 && BUG "1 param"
if ! test -f "$1"
then
echo "File $1 doesn't exist"
false
fi
}And imagine you wrote your test using this, perhaps like this:
test_expect_success 'ensure hello.c exists' '
test_path_is_file hello.c
'What happens when (1) hello.c does exist and (2) hello.c does not exist? If hello.c exists, "if ! test -f" fails, and the control does not go inside of "then" part, hence the helper will succeed silently. If hello.c does not exist, it will say "File hello.c doesn't exist". The person who is running the test will be told why the test failed (and the helper fails because it runs the 'false' after giving the message). And that is the value these helpers offer.
Armed with that knowledge, you can tell that ...
Show 5 quoted lines
> test_cmp expect actual && > - ! test -f .git/worktrees/abc && > - ! test -d .git/worktrees > + ! test_path_is_file .git/worktrees/abc && > + ! test_path_is_dir .git/worktrees
... this is a bad rewrite, right? The original wants to ensure that a file .git/worktrees/abc does not exist. You want the test to be silent when the path is not a file (i.e. "as expected, nothing interesting to see here"), and complain loudly when it *is* a file.
And your "! test_path_is_file .git/worktrees/abc" would not do that, would it? They report loudly in a wrong case!
In this case, both of them should probably become
test_path_is_missing .git/worktrees/abc && test_path_is_missing .git/worktrees
because the _intent_ of the test is NOT that it would be happy as long as .git/worktrees/abc is not a file. Even though the original uses "! test -f", seeing a .git/worktrees/abc directory there is an unexpected outcome of the test (you need to read what is being tested to fully understand this kind of thing).
Hope this helps.