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

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

From
Junio C Hamano <gitster@pobox.com>
Date
Feb 11, 2022, 18:02 UTC
Message-ID
<xmqq5yplcme1.fsf@gitster.g>
In-Reply-To
<20220211134655.1149320-1-cogoni.guillaume@gmail.com>
COGONI Guillaume <cogoni.guillaume@gmail.com> writes:
Show 6 quoted lines
> @@ -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.
Previous: COGONI GuillaumeNext: Cogoni Guillaume
Message 2 of 15 in “t/t3903-stash.sh: replace test [-d|-f] with test_path_is_*”
  1. t/t3903-stash.sh: replace test [-d|-f] with test_path_is_*COGONI Guillaume, Feb 11, 2022
  2. Junio C HamanoFeb 11, 2022
  3. Cogoni GuillaumeFeb 14, 2022
  4. Ævar Arnfjörð BjarmasonFeb 15, 2022
  5. 0/2 replace test [-f|-d] with more verbose functionsCOGONI Guillaume, Feb 18, 2022
  6. 0/2 replace test [-f|-d] with more verbose functionsCOGONI Guillaume, Feb 18, 2022
  7. 1/2 t/t3903-stash.sh: replace test [-d|-f] with test_path_is_*COGONI Guillaume, Feb 18, 2022
  8. 2/2 Add new tests functions like test_path_is_*COGONI Guillaume, Feb 18, 2022
  9. Junio C HamanoFeb 18, 2022
  10. 0/3 replace test [-f|-d] with more verbose functionsCOGONI Guillaume, Feb 22, 2022
  11. 1/3 t/t3903-stash.sh: replace test [-d|-f] with test_path_is_*COGONI Guillaume, Feb 22, 2022
  12. 2/3 tests: allow testing if a path is truly a file or a directoryCOGONI Guillaume, Feb 22, 2022
  13. 3/3 tests: make the code more readableCOGONI Guillaume, Feb 22, 2022
  14. Junio C HamanoFeb 23, 2022
  15. Cogoni GuillaumeFeb 24, 2022

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.