t: update path checks using test_path helpers
PATCH v2 1/1 t: update path checks using test_path helpers
Update old-style shell path checks to use the modern test helpers 'test_path_is_file' and 'test_path_is_dir' for improved readability.
--- t/t2401-worktree-prune.sh | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-)
t/t2401-worktree-prune.sh +17 -17
@@ -24,8 +24,8 @@ test_expect_success 'prune files inside $GIT_DIR/worktrees' ' Removing worktrees/abc: not a valid directory EOF 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 ' test_expect_success 'prune directories without gitdir' '
@@ -36,8 +36,8 @@ Removing worktrees/def: gitdir file does not exist EOF git worktree prune --verbose 2>actual && test_cmp expect actual && - ! test -d .git/worktrees/def && - ! test -d .git/worktrees + ! test_path_is_dir .git/worktrees/def && + ! test_path_is_dir .git/worktrees ' test_expect_success SANITY 'prune directories with unreadable gitdir' '
@@ -47,8 +47,8 @@ test_expect_success SANITY 'prune directories with unreadable gitdir' ' chmod u-r .git/worktrees/def/gitdir && git worktree prune --verbose 2>actual && test_grep "Removing worktrees/def: unable to read gitdir file" actual && - ! test -d .git/worktrees/def && - ! test -d .git/worktrees + ! test_path_is_dir .git/worktrees/def && + ! test_path_is_dir .git/worktrees ' test_expect_success 'prune directories with invalid gitdir' '
@@ -57,8 +57,8 @@ test_expect_success 'prune directories with invalid gitdir' ' : >.git/worktrees/def/gitdir && git worktree prune --verbose 2>actual && test_grep "Removing worktrees/def: invalid gitdir file" actual && - ! test -d .git/worktrees/def && - ! test -d .git/worktrees + ! test_path_is_dir .git/worktrees/def && + ! test_path_is_dir .git/worktrees ' test_expect_success 'prune directories with gitdir pointing to nowhere' '
@@ -67,8 +67,8 @@ test_expect_success 'prune directories with gitdir pointing to nowhere' ' echo "$(pwd)"/nowhere >.git/worktrees/def/gitdir && git worktree prune --verbose 2>actual && test_grep "Removing worktrees/def: gitdir file points to non-existent location" actual && - ! test -d .git/worktrees/def && - ! test -d .git/worktrees + ! test_path_is_dir .git/worktrees/def && + ! test_path_is_dir .git/worktrees ' test_expect_success 'not prune locked checkout' '
@@ -76,23 +76,23 @@ test_expect_success 'not prune locked checkout' ' mkdir -p .git/worktrees/ghi && : >.git/worktrees/ghi/locked && git worktree prune && - test -d .git/worktrees/ghi + test_path_is_dir .git/worktrees/ghi ' test_expect_success 'not prune recent checkouts' ' test_when_finished rm -r .git/worktrees && git worktree add jlm HEAD && - test -d .git/worktrees/jlm && + test_path_is_dir .git/worktrees/jlm && rm -rf jlm && git worktree prune --verbose --expire=2.days.ago && - test -d .git/worktrees/jlm + test_path_is_dir .git/worktrees/jlm ' test_expect_success 'not prune proper checkouts' ' test_when_finished rm -r .git/worktrees && git worktree add --detach "$PWD/nop" main && git worktree prune && - test -d .git/worktrees/nop + test_path_is_dir .git/worktrees/nop ' test_expect_success 'prune duplicate (linked/linked)' '
@@ -103,8 +103,8 @@ test_expect_success 'prune duplicate (linked/linked)' ' mv .git/worktrees/w2/gitdir.new .git/worktrees/w2/gitdir && git worktree prune --verbose 2>actual && test_grep "duplicate entry" actual && - test -d .git/worktrees/w1 && - ! test -d .git/worktrees/w2 + test_path_is_dir .git/worktrees/w1 && + ! test_path_is_dir .git/worktrees/w2 ' test_expect_success 'prune duplicate (main/linked)' '
@@ -116,7 +116,7 @@ test_expect_success 'prune duplicate (main/linked)' ' mv repo wt && git -C wt worktree prune --verbose 2>actual && test_grep "duplicate entry" actual && - ! test -d .git/worktrees/wt + ! test_path_is_dir .git/worktrees/wt ' test_expect_success 'not prune proper worktrees inside linked worktree with relative paths' '
Signature · 1 line
2.34.1
Quoted 1 line · Solly
Subject: Re: [PATCH 1/1] t: update path checks using
test_pathhelpers
Using "t2401:" instead of "t:" would give you a bit more information.
Quoted 3 lines
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 ...
t/t2401-worktree-prune.sh test_expect_success 'prune files inside $GIT_DIR/worktrees' ' · line 28
Quoted 5 lines
t/t2401-worktree-prune.sh +2
-2test_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.
Quoted 2 lines
Hope this helps.
Thanks Junio for the review.
I was on this before the email entered.
Solomon, in addition to this you might also
want to check similar review done by Junio and Eric in [1]
[1]: https://public-inbox.org/git/CAPig+cRfO8t1tdCL6MB4b9XopF3HkZ==hU83AFZ38b-2zsXDjQ@mail.gmail.com/
Quoted 8 lines · Usman Akinyemi
Thanks Junio for the review.
I was on this before the email entered.
Solomon, in addition to this you might also
want to check similar review done by Junio and Eric in [1][1]: https://public-inbox.org/git/CAPig+cRfO8t1tdCL6MB4b9XopF3HkZ==hU83AFZ38b-2zsXDjQ@mail.gmail.com/
Thanks Junio for saying what I was going to say. And thanks Usman for digging up the link to this previous review, thus saving me the effort of doing so.
For what it's worth, these days, I think we usually point people at the lore.kernel.org archive rather than public-inbox.org, so the two reviews in questions can be found at [1] and [2].
[1]: https://lore.kernel.org/git/CAPig+cRfO8t1tdCL6MB4b9XopF3HkZ==hU83AFZ38b-2zsXDjQ@mail.gmail.com/
[2]: https://lore.kernel.org/git/[email protected]/
v2 cover *** t2401: update path checks using test_path helpers ***
In reply to Solly***
Thank you for the timely review.
I applied the corrections and resolved the issues highlighted in the initial patch and used the appropriate test_helpers.
***
Solly (1):
t2401: update path checks using test_path helpers
t/t2401-worktree-prune.sh | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-)
base-commit: 60f3f52f17cceefa5299709b189ce6fe2d181e7b
Signature · 1 line
2.34.1
PATCH v2 1/1 t2401: update path checks using test_path helpers
Update old-style shell path checks to use the modern test helpers 'test_path_is_file' and 'test_path_is_dir' for improved runtime diagnosis.
--- t/t2401-worktree-prune.sh | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-)
t/t2401-worktree-prune.sh +17 -17
@@ -24,8 +24,8 @@ test_expect_success 'prune files inside $GIT_DIR/worktrees' ' Removing worktrees/abc: not a valid directory EOF test_cmp expect actual && - ! test -f .git/worktrees/abc && - ! test -d .git/worktrees + test_path_is_missing .git/worktrees/abc && + test_path_is_missing .git/worktrees ' test_expect_success 'prune directories without gitdir' '
@@ -36,8 +36,8 @@ Removing worktrees/def: gitdir file does not exist EOF git worktree prune --verbose 2>actual && test_cmp expect actual && - ! test -d .git/worktrees/def && - ! test -d .git/worktrees + test_path_is_missing .git/worktrees/def && + test_path_is_missing .git/worktrees ' test_expect_success SANITY 'prune directories with unreadable gitdir' '
@@ -47,8 +47,8 @@ test_expect_success SANITY 'prune directories with unreadable gitdir' ' chmod u-r .git/worktrees/def/gitdir && git worktree prune --verbose 2>actual && test_grep "Removing worktrees/def: unable to read gitdir file" actual && - ! test -d .git/worktrees/def && - ! test -d .git/worktrees + test_path_is_missing .git/worktrees/def && + test_path_is_missing .git/worktrees ' test_expect_success 'prune directories with invalid gitdir' '
@@ -57,8 +57,8 @@ test_expect_success 'prune directories with invalid gitdir' ' : >.git/worktrees/def/gitdir && git worktree prune --verbose 2>actual && test_grep "Removing worktrees/def: invalid gitdir file" actual && - ! test -d .git/worktrees/def && - ! test -d .git/worktrees + test_path_is_missing .git/worktrees/def && + test_path_is_missing .git/worktrees ' test_expect_success 'prune directories with gitdir pointing to nowhere' '
@@ -67,8 +67,8 @@ test_expect_success 'prune directories with gitdir pointing to nowhere' ' echo "$(pwd)"/nowhere >.git/worktrees/def/gitdir && git worktree prune --verbose 2>actual && test_grep "Removing worktrees/def: gitdir file points to non-existent location" actual && - ! test -d .git/worktrees/def && - ! test -d .git/worktrees + test_path_is_missing .git/worktrees/def && + test_path_is_missing .git/worktrees ' test_expect_success 'not prune locked checkout' '
@@ -76,23 +76,23 @@ test_expect_success 'not prune locked checkout' ' mkdir -p .git/worktrees/ghi && : >.git/worktrees/ghi/locked && git worktree prune && - test -d .git/worktrees/ghi + test_path_is_dir .git/worktrees/ghi ' test_expect_success 'not prune recent checkouts' ' test_when_finished rm -r .git/worktrees && git worktree add jlm HEAD && - test -d .git/worktrees/jlm && + test_path_is_dir .git/worktrees/jlm && rm -rf jlm && git worktree prune --verbose --expire=2.days.ago && - test -d .git/worktrees/jlm + test_path_is_dir .git/worktrees/jlm ' test_expect_success 'not prune proper checkouts' ' test_when_finished rm -r .git/worktrees && git worktree add --detach "$PWD/nop" main && git worktree prune && - test -d .git/worktrees/nop + test_path_is_dir .git/worktrees/nop ' test_expect_success 'prune duplicate (linked/linked)' '
@@ -103,8 +103,8 @@ test_expect_success 'prune duplicate (linked/linked)' ' mv .git/worktrees/w2/gitdir.new .git/worktrees/w2/gitdir && git worktree prune --verbose 2>actual && test_grep "duplicate entry" actual && - test -d .git/worktrees/w1 && - ! test -d .git/worktrees/w2 + test_path_is_dir .git/worktrees/w1 && + test_path_is_missing .git/worktrees/w2 ' test_expect_success 'prune duplicate (main/linked)' '
@@ -116,7 +116,7 @@ test_expect_success 'prune duplicate (main/linked)' ' mv repo wt && git -C wt worktree prune --verbose 2>actual && test_grep "duplicate entry" actual && - ! test -d .git/worktrees/wt + test_path_is_missing .git/worktrees/wt ' test_expect_success 'not prune proper worktrees inside linked worktree with relative paths' '
Signature · 1 line
2.34.1
Re: [PATCH v2 1/1] t2401: update path checks using test_path helpers
Quoted 8 lines · Solly
Update old-style shell path checks to use the modern test helpers '
test_path_is_file' and 'test_path_is_dir' for improved runtime diagnosis.Signed-off-by Solly <[email protected]>--- t/t2401-worktree-prune.sh | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-)
Looks good. Will queue. Thanks.