Re: [PATCH 1/1] t9117: prefer test_path_* helper functions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Feb 29, 2024 at 10:46 PM shejialuo <shejialuo@xxxxxxxxx> 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@xxxxxxxxxxxxxx/

> Signed-off-by: shejialuo <shejialuo@xxxxxxxxx>
> ---
> 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).





[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux