Subject: Re: [PATCH] t/t3903-stash.sh: replace test [-d|-f] with test_path_is_*

From: Junio C Hamano <[email-masked]>
Date: Fri, 11 Feb 2022 10:02:14 -0800

COGONI Guillaume <[email-masked]> writes:

> @@ -390,7 +390,7 @@ test_expect_success SYMLINKS 'stash file to symlink' '
>  	rm file &&
>  	ln -s file2 file &&
>  	git stash save "file to symlink" &&
> -	test -f file &&
> +	test_path_is_file file &&

This is not wrong per-se, and I know I shouldn't demand too much
from a practice patch like this, but for a real patch, I hope
contributors carefully check if the original is doing the right
thing.

What does the code want to do?

 - The starting state, HEAD, has a 'file' that is a regular file.

 - We remove and replace 'file' with a symbolic link.

 - We stash.

So the expectation here is at this point, 'file' is a regular file
and not a symbolic link.  Some anticipated errors are that "stash
save" fails to turn 'file' back to a regular file include leaving it
as a symbolic link and successfully remove the symblic link version
but somehow failing to recreate a regular file.

Is "test -f file", which was used by the original, the right way to
detect these possible errors?

Whey file2 is a regular file that exists and file is a symbolic link
points at it, i.e. if "stash save" fails to operate, "test -f file" would
still say "Yes, it is a file".

    $ >regular-file
    $ rm -f missing-file
    $ ln -s regular-file link-to-file
    $ ln -s missing-file link-to-missing
    $ test -f regular-file; echo $?
    0
    $ test -f link-to-file; echo $?
    0
    $ test -f link-to-missing; echo $?
    1
    $ test ! -h regular-file && test -f regular-file; echo $?
    0
    $ test ! -h link-to-file && test -f link-to-file; echo $?
    1

As "test_path_is_file" is merely a wrapper around "test -f", this
patch may not make it any worse, but I am skeptical if this is a
good idea, given that possible follow-on project may be one or more
of these:

 * verify that all existing users of test_path_is_file want to
   reject a symlink to file, and add 'test ! -h "$1" &&' to the
   implementation of the test helper in t/test-lib-functions.sh
   (we may want to do the same for test_path_is_dir).

 * introduce test_path_is_symlink and use it appropriately.  This
   will be a more verbose version of "test -h".

 * introduce test_path_is_file_not_symlink and use it here.

If the proposed log message leaves a note on the issue, e.g.

    There are dubious uses of "test -f" in the original that should
    be differentiating a regular file and a symbolic link to an
    existing regular file, but this mechanical conversion patch does
    not fix them.

it would be nicer.

Thanks.