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

[PATCH v3 0/3] replace test [-f|-d] with more verbose functions

From
COGONI Guillaume <cogoni.guillaume@gmail.com>
Date
Feb 22, 2022, 21:54 UTC
Message-ID
<20220222215430.605254-1-cogoni.guillaume@gmail.com>
In-Reply-To
<xmqqbkz4105s.fsf@gitster.g>

Make the code more readable in t/t3903-stash.sh and give more friendly error message by replacing test [-f|-d] by the right test_path_is_* functions. Add new functions like test_path_is_* to cover more specifics cases like symbolic link or file that we explicitly refuse to be symbolic link.

> On 18/11/2022, Junio ​​C Hamano wrote:
> The "ls -l" parsing the original does is to check how "stash apply"
> recovers the stashed state, where "file" wants to be a symbolic link
> and it wants to be pointing at "file2".
> It seems we have test_readlink() available these days, so with a
> separate clean-up patch, you may want to make the final version
> to read something like this, perhaps?
> I am not sure what test_readlink which is a one-liner Perl script
> does when it is fed a non symbolic link, so I do not know if the
> "path is truly a file and not a symlink"

- case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac + test_path_is_symlink file && + test "$(test_readlink file)" = file2

Firstly, we check if file is a symbolic link then if it is a symbolic link on file2. We check if it is a symbolic link because test_readlink() raise an error if we give it something that is not a symbolic link and this error is less readable.

> Why not
> 	if test -h "$1"
> instead???  I think "is truly a dir not a symlink" has the same
> "Huh?" puzzle.
We fixed it.
COGONI Guillaume (3):
  t/t3903-stash.sh: replace test [-d|-f] with test_path_is_*
  tests: allow testing if a path is truly a file or a directory
  tests: make the code more readable
 t/t3903-stash.sh        | 21 ++++++++++++---------
 t/test-lib-functions.sh | 29 +++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+), 9 deletions(-)
Difference between V2 and V3.
diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
index 0ec19a4499..d5ecee4fcc 100755
--- a/t/t3903-stash.sh
+++ b/t/t3903-stash.sh
@@ -392,7 +392,9 @@ test_expect_success SYMLINKS 'stash file to symlink' '
        git stash save "file to symlink" &&
        test_path_is_file_not_symlink file &&
        test bar = "$(cat file)" &&
-       git stash apply
+       git stash apply &&
+       test_path_is_symlink file &&
+       test "$(test_readlink file)" = file2
 '
 
 test_expect_success SYMLINKS 'stash file to symlink (stage rm)' '
@@ -402,7 +404,9 @@ test_expect_success SYMLINKS 'stash file to symlink (stage rm)' '
        git stash save "file to symlink (stage rm)" &&
        test_path_is_file_not_symlink file &&
        test bar = "$(cat file)" &&
-       git stash apply
+       git stash apply &&
+       test_path_is_symlink file &&
+       test "$(test_readlink file)" = file2
 '
 
 test_expect_success SYMLINKS 'stash file to symlink (full stage)' '
@@ -413,7 +417,9 @@ test_expect_success SYMLINKS 'stash file to symlink (full stage)' '
        git stash save "file to symlink (full stage)" &&
        test_path_is_file_not_symlink file &&
        test bar = "$(cat file)" &&
-       git stash apply
+       git stash apply &&
+       test_path_is_symlink file &&
+       test "$(test_readlink file)" = file2
 '
 
 # This test creates a commit with a symlink used for the following tests
diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh
index 61fc5f37e3..0f439c99d6 100644
--- a/t/test-lib-functions.sh
+++ b/t/test-lib-functions.sh
@@ -859,9 +859,9 @@ test_path_is_file () {
 test_path_is_file_not_symlink () {
        test "$#" -ne 1 && BUG "1 param"
        test_path_is_file "$1" &&
-       if ! test ! -h "$1"
+       if test -h "$1"
        then
-               echo "$1 is a symbolic link"
+               echo "$1 shouldn't be a symbolic link"
                false
        fi
 }
@@ -878,9 +878,9 @@ test_path_is_dir () {
 test_path_is_dir_not_symlink () {
        test "$#" -ne 1 && BUG "1 param"
        test_path_is_dir "$1" &&
-       if ! test ! -h "$1"
+       if test -h "$1"
        then
-               echo "$1 is a symbolic link"
+               echo "$1 shouldn't be a symbolic link"
                false
        fi
 }
@@ -894,6 +894,15 @@ test_path_exists () {
        fi
 }
 
+test_path_is_symlink () {
+       test "$#" -ne 1 && BUG "1 param"
+       if ! test -h "$1"
+       then
+               echo "Symbolic link $1 doesn't exist"
+               false
+       fi
+}
+
-- 
2.25.1
Previous: Junio C HamanoNext: COGONI Guillaume
Message 10 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.