git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Mar 1, 2024, 04:44 UTC
Message-ID
<CAPig+cRfO8t1tdCL6MB4b9XopF3HkZ==hU83AFZ38b-2zsXDjQ@mail.gmail.com>
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/
Show 7 quoted lines
> 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`.

Show 5 quoted lines
>         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.

Show 10 quoted lines
>  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).

Previous: shejialuoNext: shejialuo
Message 12 of 26 in “microproject: Use test_path_is_* functions in test scripts”
  1. shejialuoFeb 29, 2024
  2. 1/1 [GSoC][PATCH] t3070: refactor test -e commandshejialuo, Feb 29, 2024
  3. Eric SunshineFeb 29, 2024
  4. Junio C HamanoFeb 29, 2024
  5. SoC 2024: clarify `test_path_is_*` conversion microprojectPatrick Steinhardt, Mar 4, 2024
  6. Christian CouderMar 4, 2024
  7. Junio C HamanoMar 4, 2024
  8. Patrick SteinhardtMar 4, 2024
  9. shejialuoMar 1, 2024
  10. 0/1 [GSoC][PATCH] t9117: prefer test_path_* helper functionsshejialuo, Mar 1, 2024
  11. 1/1 t9117: prefer test_path_* helper functionsshejialuo, Mar 1, 2024
  12. Eric SunshineMar 1, 2024
  13. shejialuoMar 1, 2024
  14. Junio C HamanoMar 1, 2024
  15. shejialuoMar 1, 2024
  16. 0/1 t9117: prefer test_path_* helper functionsshejialuo, Mar 1, 2024
  17. 1/1 [PATCH] t9117: prefer test_path_* helper functionsshejialuo, Mar 1, 2024
  18. Patrick SteinhardtMar 4, 2024
  19. 0/1 Change commit messageshejialuo, Mar 4, 2024
  20. 1/1 [PATCH] t9117: prefer test_path_* helper functionsshejialuo, Mar 4, 2024
  21. Patrick SteinhardtMar 4, 2024
  22. shejialuoMar 4, 2024
  23. Junio C HamanoMar 4, 2024
  24. Junio C HamanoMar 4, 2024
  25. shejialuoMar 5, 2024
  26. Junio C HamanoMar 4, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.