git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Solly <solobarine@gmail.com>
Cc: git@vger.kernel.org
Subject: Re: [PATCH 1/1] t: update path checks using test_path helpers
Date: Tue, 14 Oct 2025 11:19:13 -0700	[thread overview]
Message-ID: <xmqqa51txs66.fsf@gitster.g> (raw)
In-Reply-To: <20251014161446.6135-1-solobarine@gmail.com> (Solly's message of "Tue, 14 Oct 2025 17:14:46 +0100")

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 ...

>  	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.

  reply	other threads:[~2025-10-14 18:19 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-14 16:14 [PATCH 1/1] t: update path checks using test_path helpers Solly
2025-10-14 18:19 ` Junio C Hamano [this message]
2025-10-14 18:27   ` Usman Akinyemi
2025-10-14 18:47     ` Eric Sunshine
2025-10-15 14:03 ` [PATCH v2 0/1] *** t2401: update path checks using test_path helpers *** Solly
2025-10-15 14:03   ` [PATCH v2 1/1] t2401: update path checks using test_path helpers Solly
2025-10-15 20:38     ` 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=xmqqa51txs66.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=solobarine@gmail.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.
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).