diff mbox series

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

Message ID 20240301034606.69673-2-shejialuo@gmail.com (mailing list archive)
State Superseded
Headers show
Series t9117: prefer test_path_* helper functions | expand

Commit Message

shejialuo March 1, 2024, 3:46 a.m. UTC
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.

Signed-off-by: shejialuo <shejialuo@gmail.com>
---
 t/t9117-git-svn-init-clone.sh | 40 +++++++++++++++++------------------
 1 file changed, 20 insertions(+), 20 deletions(-)

Comments

Eric Sunshine March 1, 2024, 4:44 a.m. UTC | #1
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).
Junio C Hamano March 1, 2024, 5:09 a.m. UTC | #2
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.
shejialuo March 1, 2024, 11:29 a.m. UTC | #3
Thanks for your comment.

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

I have run the script with `-v`, I have got the following result:

  Directory trunk doesn't exist

I come to realisize the fault with your dedicated comments. An assertion
is an assertion.

And I am impressed by the following idea:

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

The command `rm -rf target` ensures an exit status of 0 regardless of
whether the `target` exists. Thus the code will elegant make sure the
initial condition holds. I think I could add a patch to clean the code.
shejialuo March 1, 2024, 11:36 a.m. UTC | #4
Thanks for your wonderful comments. I have known that semantics is
important not only the functionality. I will send a new patch at now.
diff mbox series

Patch

diff --git a/t/t9117-git-svn-init-clone.sh b/t/t9117-git-svn-init-clone.sh
index 62de819a44..2f964f66aa 100755
--- 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 &&
 	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 &&
 	rm -rf trunk
 	'
 
 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
 	'
 
 test_expect_success 'clone with --stdlayout' '
-	test ! -d project &&
+	! test_path_is_dir project &&
 	git svn clone -s "$svnrepo"/project &&
-	test -d project/.git/svn &&
-	test -e project/foo &&
+	test_path_is_dir project/.git/svn &&
+	test_path_exists project/foo &&
 	rm -rf project
 	'
 
 test_expect_success 'clone to target directory with --stdlayout' '
-	test ! -d target &&
+	! test_path_is_dir target &&
 	git svn clone -s "$svnrepo"/project 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
 	'
 
 test_expect_success 'init without -s/-T/-b/-t does not warn' '
-	test ! -d trunk &&
+	! test_path_is_dir trunk &&
 	git svn init "$svnrepo"/project/trunk trunk 2>warning &&
 	! grep -q prefix warning &&
 	rm -rf trunk &&
@@ -55,7 +55,7 @@  test_expect_success 'init without -s/-T/-b/-t does not warn' '
 	'
 
 test_expect_success 'clone without -s/-T/-b/-t does not warn' '
-	test ! -d trunk &&
+	! test_path_is_dir trunk &&
 	git svn clone "$svnrepo"/project/trunk 2>warning &&
 	! grep -q prefix warning &&
 	rm -rf trunk &&
@@ -69,7 +69,7 @@  project/trunk:refs/remotes/${prefix}trunk
 project/branches/*:refs/remotes/${prefix}*
 project/tags/*:refs/remotes/${prefix}tags/*
 EOF
-	test ! -f actual &&
+	! test_path_is_file actual &&
 	git --git-dir=project/.git config svn-remote.svn.fetch >>actual &&
 	git --git-dir=project/.git config svn-remote.svn.branches >>actual &&
 	git --git-dir=project/.git config svn-remote.svn.tags >>actual &&
@@ -78,7 +78,7 @@  EOF
 }
 
 test_expect_success 'init with -s/-T/-b/-t assumes --prefix=origin/' '
-	test ! -d project &&
+	! test_path_is_dir project &&
 	git svn init -s "$svnrepo"/project project 2>warning &&
 	! grep -q prefix warning &&
 	test_svn_configured_prefix "origin/" &&
@@ -87,7 +87,7 @@  test_expect_success 'init with -s/-T/-b/-t assumes --prefix=origin/' '
 	'
 
 test_expect_success 'clone with -s/-T/-b/-t assumes --prefix=origin/' '
-	test ! -d project &&
+	! test_path_is_dir project &&
 	git svn clone -s "$svnrepo"/project 2>warning &&
 	! grep -q prefix warning &&
 	test_svn_configured_prefix "origin/" &&
@@ -96,7 +96,7 @@  test_expect_success 'clone with -s/-T/-b/-t assumes --prefix=origin/' '
 	'
 
 test_expect_success 'init with -s/-T/-b/-t and --prefix "" still works' '
-	test ! -d project &&
+	! test_path_is_dir project &&
 	git svn init -s "$svnrepo"/project project --prefix "" 2>warning &&
 	! grep -q prefix warning &&
 	test_svn_configured_prefix "" &&
@@ -105,7 +105,7 @@  test_expect_success 'init with -s/-T/-b/-t and --prefix "" still works' '
 	'
 
 test_expect_success 'clone with -s/-T/-b/-t and --prefix "" still works' '
-	test ! -d project &&
+	! test_path_is_dir project &&
 	git svn clone -s "$svnrepo"/project --prefix "" 2>warning &&
 	! grep -q prefix warning &&
 	test_svn_configured_prefix "" &&
@@ -114,7 +114,7 @@  test_expect_success 'clone with -s/-T/-b/-t and --prefix "" still works' '
 	'
 
 test_expect_success 'init with -T as a full url works' '
-	test ! -d project &&
+	! test_path_is_dir project &&
 	git svn init -T "$svnrepo"/project/trunk project &&
 	rm -rf project
 	'