From: Junio C Hamano <gitster@pobox.com>
To: shejialuo <shejialuo@gmail.com>
Cc: git@vger.kernel.org, Eric Sunshine <sunshine@sunshineco.com>
Subject: Re: [PATCH 1/1] t9117: prefer test_path_* helper functions
Date: Thu, 29 Feb 2024 21:09:48 -0800 [thread overview]
Message-ID: <xmqqwmqm8rmr.fsf@gitster.g> (raw)
In-Reply-To: <20240301034606.69673-2-shejialuo@gmail.com> (shejialuo@gmail.com's message of "Fri, 1 Mar 2024 11:46:06 +0800")
shejialuo <shejialuo@gmail.com> writes:
> test_expect_success 'basic clone' '
> - test ! -d trunk &&
> + ! test_path_is_dir trunk &&
This is not quite right. Step back and think why we are trying to
use the test_path_* helpers instead of "test [!] -d". What are the
differences between them?
The answer is that, unlike "test [!] -d dir" that is silent whether
"dir" exists or missing, "test_path_is_dir dir" is *not* always
silent. It gives useful messages as necessary. When does it do so?
Here is the definition, from t/test-lib-functions.sh around line
930:
test_path_is_dir () {
test "$#" -ne 1 && BUG "1 param"
if ! test -d "$1"
then
echo "Directory $1 doesn't exist"
false
fi
}
It succeeds silently when "test -d dir" is true, but it complains
loudly when "test -d dir" does not hold. You will be told that the
test is unhappy because "dir" does not exist. That would be easier
to debug than one step among many in &&-chain silently fails.
Now, let's look at the original you rewrote again:
> - test ! -d trunk &&
It says "it is a failure if 'trunk' exists as a directory". If
'trunk' does not exist, it is a very happy state for us. So instead
of silently failing when 'trunk' exists as a directory, you would
want to improve it so that you will get a complaint in such a case,
saying "trunk should *not* exist but it does".
Did you succeed to do so with this rewrite?
> + ! test_path_is_dir trunk &&
The helper "test_path_is_dir" is called with "trunk". As we saw, we
will see complaint when "trunk" does *NOT* exist. When "trunk" does
exist, it will be silent and "test_path_is_dir" will return a success,
which will be inverted with "!" to make it a failure, causing &&-chain
to fail.
So the exit status is not wrong, but it issues a complaint under the
wrong condition. That is not an improvement.
Let's step back one more time. Is the original test happy when
"trunk" existed as a regular file? "test ! -d trunk" says so, but
should it really be? Think.
I suspect that the test is not happy as long as 'trunk' exists,
whether it is a directory or a regular file or a symbolic link.
IOW, it says "I am unhappy if 'trunk' is a directory", but what it
really meant to say was "I am unhappy if there is anything at the
path 'trunk'". IOW, "test ! -e trunk" would be what it really
meant, no?
So the correct rewrite for it would rather be something like
test_path_is_missing trunk &&
instead. This will fail if anything is at path 'trunk', with an
error message saying there shouldn't be anything but there is.
In a peculiar case, which I do not think this one is, a test may
legitimately accept "path" to either (1) exist as long as it is not
a directory, or (2) be missing, as success. In such a case, the
original construct '! test -d path" (or "test ! -d path") would be
appropriate.
But I do not think we have a suitable wrapper to express such a
case, i.e. we do not have a helper like this.
test_path_is_not_dir () {
if test -d "$1"
then
echo "$1 is a directory but it should not be"
false
fi
}
If such a use case were common, we might even do this:
# "test_path_is_dir <dir>" expects <dir> to be a directory.
# "test_path_is_dir ! <dir>" expects <dir> not to be a
# directory.
# In either case, complain only when the expectation is not met.
test_path_is_dir () {
if test "$1" = "!"
then
shift
if test -d "$1"
then
echo "$1 is a directory but it should not be"
return 1
fi
else
if test ! -d "$1"
then
echo "$1 is not a directory"
return 1
fi
fi
true
}
but "we are happy even if path exists as long as it is not a
directory" is a very uncommon thing we want to say in our tests, so
that is why we do not have such a helper function.
HTH.
next prev parent reply other threads:[~2024-03-01 5:10 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-29 15:04 [GSoC][PATCH 0/1] microproject: Use test_path_is_* functions in test scripts shejialuo
2024-02-29 15:04 ` [PATCH 1/1] [GSoC][PATCH] t3070: refactor test -e command shejialuo
2024-02-29 17:58 ` Eric Sunshine
2024-02-29 19:06 ` Junio C Hamano
2024-03-04 9:16 ` [PATCH] SoC 2024: clarify `test_path_is_*` conversion microproject Patrick Steinhardt
2024-03-04 13:42 ` Christian Couder
2024-03-04 17:02 ` Junio C Hamano
2024-03-04 9:17 ` [PATCH 1/1] [GSoC][PATCH] t3070: refactor test -e command Patrick Steinhardt
2024-03-01 2:50 ` shejialuo
2024-03-01 3:46 ` [PATCH V2 0/1] [GSoC][PATCH] t9117: prefer test_path_* helper functions shejialuo
2024-03-01 3:46 ` [PATCH 1/1] " shejialuo
2024-03-01 4:44 ` Eric Sunshine
2024-03-01 11:29 ` shejialuo
2024-03-01 5:09 ` Junio C Hamano [this message]
2024-03-01 11:36 ` shejialuo
2024-03-01 13:03 ` [PATCH v3 0/1] " shejialuo
2024-03-01 13:03 ` [PATCH v3 1/1] [PATCH] " shejialuo
2024-03-04 9:24 ` Patrick Steinhardt
2024-03-04 9:54 ` [PATCH v4 0/1] Change commit message shejialuo
2024-03-04 9:54 ` [PATCH v4 1/1] [PATCH] t9117: prefer test_path_* helper functions shejialuo
2024-03-04 9:59 ` Patrick Steinhardt
2024-03-04 11:45 ` shejialuo
2024-03-04 17:50 ` Junio C Hamano
2024-03-04 17:22 ` Junio C Hamano
2024-03-05 11:42 ` shejialuo
2024-03-04 17:19 ` [PATCH v4 0/1] Change commit message 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
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=xmqqwmqm8rmr.fsf@gitster.g \
--to=gitster@pobox.com \
--cc=git@vger.kernel.org \
--cc=shejialuo@gmail.com \
--cc=sunshine@sunshineco.com \
/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.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
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 read-only IMAP folder(s) and NNTP newsgroup(s).