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

Re: [PATCH] test_cmp: diagnose incorrect arguments more precisely

From
Junio C Hamano <gitster@pobox.com>
Date
Oct 3, 2020, 17:22 UTC
Message-ID
<xmqqpn5z8bsq.fsf@gitster.c.googlers.com>
In-Reply-To
<20201003070412.33015-1-sunshine@sunshineco.com>
Eric Sunshine <sunshine@sunshineco.com> writes:
Show 24 quoted lines
> d572f52a64 (test_cmp: diagnose incorrect arguments, 2020-08-09) taught
> test_cmp() and test_cmp_bin() to diagnose a missing input source. Even
> though the arguments to test_cmp() must name regular files (or standard
> input), it only diagnoses whether a source is missing, which makes the
> check a bit loose. Teach the check to be more precise by diagnosing, not
> only a missing source, but also if the source is not a regular file.
>
> Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
> ---
>
>   On Wed, Sep 16, 2020 at 5:14 PM Junio C Hamano <gitster@pobox.com> wrote:
>   > Eric Sunshine <sunshine@sunshineco.com> writes:
>   > > [...] I ask because test_cmp() was updated not long ago to
>   > > provide better diagnostics when one of the files is missing.
>   > > [1]: d572f52a64 (test_cmp: diagnose incorrect arguments, 2020-08-09)
>   >
>   > Yes, you did this with the commit,
>   >         test "x$1" = x- || test -e "$1" || BUG "test_cmp '$1' missing"
>   >         test "x$2" = x- || test -e "$2" || BUG "test_cmp '$2' missing"
>   > and I do not immediately see why "test -e" shouldn't be "test -f".
>   > It should ideally be "stdin is OK, otherwise it must be a readable
>   > regular file".
>
>   Perhaps the present patch suitably address your concern?

Certainly s/-e/-f/ is better to reject directories, but if we are doing more checks, it would probably be more maintainable to have a helper and catch unreadable files.

Note that
	mkdir one two &&
	test_cmp one two

would likely succeed, so I am not sure how much this matters in practice, even though we would catch

	mkdir one >two &&
	test_cmp one two
as a mistake.
 t/test-lib-functions.sh | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)
diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh
index 8d59b90348..ddaa14275a 100644
--- a/t/test-lib-functions.sh
+++ b/t/test-lib-functions.sh
@@ -938,6 +938,20 @@ test_expect_code () {
 	return 1
 } 7>&2 2>&4
 
+# helper for test_cmp and test_cmp_bin to diagnose programmer errors
+# usage: test_cmp_missing "$funcname" "$filename"
+test_cmp_missing_check () {
+	if test "x$2" = x-
+	then
+		:; # standard input is OK
+	elif test -f "$2" && test -r "$2"
+	then
+		:; # readable file is OK
+	else
+		BUG "$1 '$2' not a readable file"
+	fi
+}
+
 # test_cmp is a helper function to compare actual and expected output.
 # You can use it like:
 #
@@ -955,9 +969,9 @@ test_cmp() {
 	test $# -eq 2 || BUG "test_cmp requires two arguments"
 	if ! eval "$GIT_TEST_CMP" '"$@"'
 	then
-		test "x$1" = x- || test -e "$1" || BUG "test_cmp '$1' missing"
-		test "x$2" = x- || test -e "$2" || BUG "test_cmp '$2' missing"
-		return 1
+		test_cmp_missing_check test_cmp "$1"
+		test_cmp_missing_check test_cmp "$2"
+		return 1;
 	fi
 }
 
@@ -990,8 +1004,8 @@ test_cmp_bin() {
 	test $# -eq 2 || BUG "test_cmp_bin requires two arguments"
 	if ! cmp "$@"
 	then
-		test "x$1" = x- || test -e "$1" || BUG "test_cmp_bin '$1' missing"
-		test "x$2" = x- || test -e "$2" || BUG "test_cmp_bin '$2' missing"
+		test_cmp_missing_check test_cmp_bin "$1"
+		test_cmp_missing_check test_cmp_bin "$2"
 		return 1
 	fi
 }
Previous: Eric SunshineNext: Ævar Arnfjörð Bjarmason
Message 17 of 54 in “remote-mediawiki: various fixes to make tests pass”
  1. 00/15 remote-mediawiki: various fixes to make tests passÆvar Arnfjörð Bjarmason, Sep 16, 2020
  2. 13/15 remote-mediawiki tests: use CLI installerÆvar Arnfjörð Bjarmason, Sep 16, 2020
  3. 15/15 remote-mediawiki tests: annotate failing testsÆvar Arnfjörð Bjarmason, Sep 16, 2020
  4. 14/15 remote-mediawiki: fix duplicate revisions being importedÆvar Arnfjörð Bjarmason, Sep 16, 2020
  5. 06/15 remote-mediawiki tests: use test_cmp in testsÆvar Arnfjörð Bjarmason, Sep 16, 2020
  6. Jeff KingSep 16, 2020
  7. 12/15 remote-mediawiki tests: use inline PerlIO for readabilityÆvar Arnfjörð Bjarmason, Sep 16, 2020
  8. 09/15 remote-mediawiki tests: use "$dir/" instead of "$dir."Ævar Arnfjörð Bjarmason, Sep 16, 2020
  9. Jeff KingSep 16, 2020
  10. Junio C HamanoSep 16, 2020
  11. 07/15 remote-mediawiki tests: guard test_cmp with test_path_is_fileÆvar Arnfjörð Bjarmason, Sep 16, 2020
  12. Jeff KingSep 16, 2020
  13. Đoàn Trần Công DanhSep 16, 2020
  14. Eric SunshineSep 16, 2020
  15. Junio C HamanoSep 16, 2020
  16. test_cmp: diagnose incorrect arguments more preciselyEric Sunshine, Oct 3, 2020
  17. Junio C HamanoOct 3, 2020
  18. Ævar Arnfjörð BjarmasonSep 21, 2020
  19. Ævar Arnfjörð BjarmasonSep 21, 2020
  20. 11/15 remote-mediawiki tests: replace deprecated Perl constructÆvar Arnfjörð Bjarmason, Sep 16, 2020
  21. Jeff KingSep 16, 2020
  22. 08/15 remote-mediawiki tests: change `[]` to `test`Ævar Arnfjörð Bjarmason, Sep 16, 2020
  23. 10/15 remote-mediawiki tests: use a more idiomatic dispatch tableÆvar Arnfjörð Bjarmason, Sep 16, 2020
  24. 03/15 remote-mediawiki doc: bump recommended PHP version to 7.3Ævar Arnfjörð Bjarmason, Sep 16, 2020
  25. Đoàn Trần Công DanhSep 16, 2020
  26. Junio C HamanoSep 16, 2020
  27. 02/15 remote-mediawiki doc: link to MediaWiki's current versionÆvar Arnfjörð Bjarmason, Sep 16, 2020
  28. 01/15 remote-mediawiki doc: correct link to GitHub projectÆvar Arnfjörð Bjarmason, Sep 16, 2020
  29. Jeff KingSep 16, 2020
  30. Junio C HamanoSep 17, 2020
  31. 04/15 remote-mediawiki tests: use the login/password variablesÆvar Arnfjörð Bjarmason, Sep 16, 2020
  32. Johannes SchindelinSep 16, 2020
  33. Ævar Arnfjörð BjarmasonSep 21, 2020
  34. 05/15 remote-mediawiki tests: use a 10 character passwordÆvar Arnfjörð Bjarmason, Sep 16, 2020
  35. 00/18 remote-mediawiki: fix RCE issue, and the testsÆvar Arnfjörð Bjarmason, Sep 21, 2020
  36. Jeff KingSep 25, 2020
  37. 03/18 remote-mediawiki doc: don't hardcode Debian PHP versionsÆvar Arnfjörð Bjarmason, Sep 21, 2020
  38. 01/18 remote-mediawiki doc: correct link to GitHub projectÆvar Arnfjörð Bjarmason, Sep 21, 2020
  39. 02/18 remote-mediawiki doc: link to MediaWiki's current versionÆvar Arnfjörð Bjarmason, Sep 21, 2020
  40. 07/18 remote-mediawiki tests: change `[]` to `test`Ævar Arnfjörð Bjarmason, Sep 21, 2020
  41. 09/18 remote-mediawiki tests: use a more idiomatic dispatch tableÆvar Arnfjörð Bjarmason, Sep 21, 2020
  42. 11/18 remote-mediawiki tests: use inline PerlIO for readabilityÆvar Arnfjörð Bjarmason, Sep 21, 2020
  43. 06/18 remote-mediawiki tests: use test_cmp in testsÆvar Arnfjörð Bjarmason, Sep 21, 2020
  44. 08/18 remote-mediawiki tests: use "$dir/" instead of "$dir."Ævar Arnfjörð Bjarmason, Sep 21, 2020
  45. 04/18 remote-mediawiki tests: use the login/password variablesÆvar Arnfjörð Bjarmason, Sep 21, 2020
  46. 05/18 remote-mediawiki tests: use a 10 character passwordÆvar Arnfjörð Bjarmason, Sep 21, 2020
  47. 10/18 remote-mediawiki tests: replace deprecated Perl constructÆvar Arnfjörð Bjarmason, Sep 21, 2020
  48. 17/18 remote-mediawiki: annotate unquoted uses of run_git()Ævar Arnfjörð Bjarmason, Sep 21, 2020
  49. 12/18 remote-mediawiki tests: use CLI installerÆvar Arnfjörð Bjarmason, Sep 21, 2020
  50. 18/18 remote-mediawiki: use "sh" to eliminate unquoted commandsÆvar Arnfjörð Bjarmason, Sep 21, 2020
  51. 16/18 remote-mediawiki: convert to quoted run_git() invocationÆvar Arnfjörð Bjarmason, Sep 21, 2020
  52. 15/18 remote-mediawiki: provide a list form of run_git()Ævar Arnfjörð Bjarmason, Sep 21, 2020
  53. 13/18 remote-mediawiki: fix duplicate revisions being importedÆvar Arnfjörð Bjarmason, Sep 21, 2020
  54. 14/18 remote-mediawiki tests: annotate failing testsÆvar Arnfjörð Bjarmason, Sep 21, 2020

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.