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

Re: [GSoC] [PATCH] t1011: replace test -f with test_path_is_file

From
Junio C Hamano <gitster@pobox.com>
Date
Apr 11, 2022, 19:09 UTC
Message-ID
<xmqq1qy3igif.fsf@gitster.g>
In-Reply-To
<20220409114458.23435-1-siddharthasthana31@gmail.com>
Siddharth Asthana <siddharthasthana31@gmail.com> writes:
> Use test_path_is_file() instead of 'test -f' for better debugging
> information.
> ---
missing Sign-off.
Show 6 quoted lines
>  	test_cmp expected.swt result &&
> -	! test -f init.t &&
> -	! test -f sub/added
> +	! test_path_is_file init.t &&
> +	! test_path_is_file sub/added
>  '
Given the definition of the helper function, i.e.
        test_path_is_file () {
                test "$#" -ne 1 && BUG "1 param"
                if ! test -f "$1"
                then
                        echo "File $1 doesn't exist"
                        false
                fi
        }

the new test will _complain_ "init.t doesn't exist" when we have successfully run the test, while it will be _silent_ when init.t that _should_ not exist is there.

Which is the complete opposite of the spirit of why we want to use the helper when we expect the path "$1" to exist, i.e. loudly fail when our expectation is _not_ met.

$ git grep '! test_path_is' t/

shows that we already have such a misuse of test_path_is_dir in one place, but luckily we do not have any for test_path_is_file or other similar helpers. test_path_is_hidden is sort-of OK as that is not about verbosity.

In these two test, we do not expect init.t or sub/added to _exist_ at all. It's not like we are happy if we see init.d exist as a directory (which is not a file). test_path_is_missing is probably the right helper to use.

It is not very plausible that we'd want to assert that existence of a path as a file the only bad condition (i.e. we are happy if the path did not exist or it is a directory, symlink, or a socket), so I think the simple

	Never use '! test_path_is_file'; test_path_is_missing may be
	what you are looking for.
is a good enough rule.

If not, we could allow the caller to write such a convoluted "only existence of a path as a file is unacceptable and everything else is good" assertion as

    test_path_is_file ! init.d
with something like
        test_path_is_file () {
		expecting_file=true
		if test "$1" = "!"
		then
			expecting_file=false
			shift
		fi
                test "$#" -ne 1 && BUG "1 param"
                if test -f "$1"
                then
                	$expecting_file || echo "File $1 exists"
                        $expecting_file
		else
			$expecting_file && echo "File $1 doesn't exist"
                        ! $expecting_file
                fi
        }
but I do not think we want to go that way.
Previous: Siddharth AsthanaNext: Siddharth Asthana
Message 2 of 8 in “t1011: replace test -f with test_path_is_file”
  1. Siddharth AsthanaApr 9, 2022
  2. Junio C HamanoApr 11, 2022
  3. Siddharth AsthanaApr 12, 2022
  4. [GSoC] [PATCH v2] t1011: replace test -f with test_path_is_fileSiddharth Asthana, Apr 12, 2022
  5. Christian CouderApr 14, 2022
  6. Junio C HamanoApr 14, 2022
  7. Siddharth AsthanaApr 16, 2022
  8. t1011: replace test -f with test_path_is* helpersSiddharth Asthana, Apr 16, 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.