git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Eric Sunshine <sunshine@sunshineco.com>
To: shejialuo <shejialuo@gmail.com>
Cc: git@vger.kernel.org, Junio C Hamano <gitster@pobox.com>
Subject: Re: [PATCH 1/1] t9117: prefer test_path_* helper functions
Date: Thu, 29 Feb 2024 23:44:53 -0500	[thread overview]
Message-ID: <CAPig+cRfO8t1tdCL6MB4b9XopF3HkZ==hU83AFZ38b-2zsXDjQ@mail.gmail.com> (raw)
In-Reply-To: <20240301034606.69673-2-shejialuo@gmail.com>

On Thu, Feb 29, 2024 at 10:46 PM shejialuo <shejialuo@gmail.com> wrote:
> test -(e|f|d) does not provide a nice error message when we hit test
> failures, so use test_path_exists, test_path_is_dir and
> test_path_is_file instead.

Thanks for rerolling. t9117 is indeed a better choice[1] than t3070
for the exercise of replacing `test -blah` with `test_path_foo`.

[1]: https://lore.kernel.org/git/CAPig+cR2-6qONkosu7=qEQSJa_fvYuVQ0to47D5qx904zW08Eg@mail.gmail.com/

> Signed-off-by: shejialuo <shejialuo@gmail.com>
> ---
> diff --git a/t/t9117-git-svn-init-clone.sh b/t/t9117-git-svn-init-clone.sh
> @@ -15,39 +15,39 @@ test_expect_success 'setup svnrepo' '
>  test_expect_success 'basic clone' '
> -       test ! -d trunk &&
> +       ! test_path_is_dir trunk &&

Generally speaking, you don't want to use `!` to negate the result of
a `path_is_foo` assertion function. To understand why, take a look at
the definition of `test_path_is_dir`:

    test_path_is_dir () {
        if ! test -d "$1"
        then
            echo "Directory $1 doesn't exist"
            false
        fi
    }

The test in question (t9117: "basic clone") is using `test ! -d` to
assert that the directory `trunk` does not yet exist when the test
begins; indeed, under normal circumstances, this directory should not
yet be present. However, the call to test_path_is_dir() asserts that
the directory _does_ exist, which is the opposite of `test ! -d`, and
complains ("Directory trunk doesn't exist") when it doesn't exist. So,
in the normal and typical case for all the tests in this script,
`test_path_is_dir` is going to be complaining even though the
non-existence of that directory is an expected condition.

Although you make the test pass by using `!` to invert the result of
`test_path_is_dir`, the complaint will nevertheless get lodged, and
may very well be confusing for anyone scrutinizing the output of the
tests when running the script with `-v` or `-x`.

So, `test_path_is_dir` is not a good fit for this case which wants to
assert that the path `trunk` does not yet exist. A better choice for
this particular case would be `test_path_is_missing`.

>         git svn clone "$svnrepo"/project/trunk &&
> -       test -d trunk/.git/svn &&
> -       test -e trunk/foo &&
> +       test_path_is_dir trunk/.git/svn &&
> +       test_path_exists trunk/foo &&

These two changes make sense and the intent directly corresponds to
the original code.

>  test_expect_success 'clone to target directory' '
> -       test ! -d target &&
> +       ! test_path_is_dir target &&
>         git svn clone "$svnrepo"/project/trunk target &&
> -       test -d target/.git/svn &&
> -       test -e target/foo &&
> +       test_path_is_dir target/.git/svn &&
> +       test_path_exists target/foo &&
>         rm -rf target
>         '

What follows is probably beyond the scope of your GSoC microproject,
but there is a bit more of interest to note about these tests.

Rather than asserting some initial condition at the start of the test,
it is more common and more robust simply to _ensure_ that the desired
initial condition holds. So, for instance, instead of asserting `test
! -d target`, modern practice is to ensure that `target` doesn't
exist. Thus:

    test_expect_success 'clone to target directory' '
        rm -rf target &&
        git svn clone "$svnrepo"/project/trunk target &&
        ...

is a more robust implementation. This also addresses the problem that
the `rm -rf target` at the very end of each test won't be executed if
any command earlier in the test fails (due to the short-circuiting
behavior of the &&-operator).

As noted, this type of cleanup is probably overkill for your GSoC
microproject so you need not tackle it. I mention it only for
completeness. Also, if someone does tackle such a cleanup, it should
be done as multiple patches, each making one distinct change (i.e. one
patch dropping `test !-d` and moving `rm -rf` to the start of the
test, and one which employs `test_path_foo` for the remaining `test
-blah` invocations).


  reply	other threads:[~2024-03-01  4:45 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 [this message]
2024-03-01 11:29       ` shejialuo
2024-03-01  5:09     ` Junio C Hamano
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='CAPig+cRfO8t1tdCL6MB4b9XopF3HkZ==hU83AFZ38b-2zsXDjQ@mail.gmail.com' \
    --to=sunshine@sunshineco.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=shejialuo@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.
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).