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

Re: [PATCH v3 5/8] tests: use "test_cmp" instead of "test" in sub-shells

From
Junio C Hamano <gitster@pobox.com>
Date
Dec 5, 2022, 00:39 UTC
Message-ID
<xmqq7cz6u1in.fsf@gitster.g>
In-Reply-To
<patch-v3-5.8-58ac6fe5604-20221202T114733Z-avarab@gmail.com>
Ævar Arnfjörð Bjarmason  <avarab@gmail.com> writes:
> Convert a few cases where we were using "test" inside a sub-shell, and
> were losing the exit code of "git".
That makes it sound like
	(
		cd there &&
		a=$(git something expected to be silent) &&
		test -z "$a"
	) &&
	...

is bad, and it can be improved somehow by using "test_cmp" instead of "test", but I do not think that is what you meant (in fact, the command substitution used above is safe and we catch failing git).

After looking at a few samples from the patch s/sub-shell/command substitution/ might be what you meant, i.e.

	test -z "$(git something) &&
	...
is bad and we want 
	git something >out &&
	! test -s out

to keep the exit code from "git". IOW, fixing the lossage of exit code has little to do with the use of test vs test_cmp.

Perhaps retitle to
    Subject: [PATCH] tests: avoid "test op $(git foo)" lose exit status of git
    Rewrite tests that ran "git" inside command substitution and
    lost exit status of "git" so that we notice failing "git".
or something like that.
> In the case of "t3200-branch.sh" some adjacent code outside of a
> sub-shell that was losing the exit code is also being converted, as
> it's within the same hunk.
Again s/sub-shell/command substitution?, I think.
Show 158 quoted lines
> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
> ---
>  t/lib-httpd.sh              |  5 +++--
>  t/lib-submodule-update.sh   | 22 +++++++++-------------
>  t/t0060-path-utils.sh       |  4 +++-
>  t/t3200-branch.sh           | 13 +++++++------
>  t/t5605-clone-local.sh      | 15 ++++++++++-----
>  t/t7402-submodule-rebase.sh | 14 +++++++++++---
>  6 files changed, 43 insertions(+), 30 deletions(-)
>
> diff --git a/t/lib-httpd.sh b/t/lib-httpd.sh
> index 608949ea80b..31e7fa3010c 100644
> --- a/t/lib-httpd.sh
> +++ b/t/lib-httpd.sh
> @@ -217,8 +217,9 @@ test_http_push_nonff () {
>  		git commit -a -m path2 --amend &&
>  
>  		test_must_fail git push -v origin >output 2>&1 &&
> -		(cd "$REMOTE_REPO" &&
> -		 test $HEAD = $(git rev-parse --verify HEAD))
> +		echo "$HEAD" >expect &&
> +		git -C "$REMOTE_REPO" rev-parse --verify HEAD >actual &&
> +		test_cmp expect actual
>  	'
>  
>  	test_expect_success 'non-fast-forward push show ref status' '
> diff --git a/t/lib-submodule-update.sh b/t/lib-submodule-update.sh
> index 2d31fcfda1f..d7c2b670b4a 100644
> --- a/t/lib-submodule-update.sh
> +++ b/t/lib-submodule-update.sh
> @@ -168,20 +168,16 @@ replace_gitfile_with_git_dir () {
>  # Note that this only supports submodules at the root level of the
>  # superproject, with the default name, i.e. same as its path.
>  test_git_directory_is_unchanged () {
> -	(
> -		cd ".git/modules/$1" &&
> -		# does core.worktree point at the right place?
> -		test "$(git config core.worktree)" = "../../../$1" &&
> -		# remove it temporarily before comparing, as
> -		# "$1/.git/config" lacks it...
> -		git config --unset core.worktree
> -	) &&
> +	# does core.worktree point at the right place?
> +	echo "../../../$1" >expect &&
> +	git -C ".git/modules/$1" config core.worktree >actual &&
> +	test_cmp expect actual &&
> +	# remove it temporarily before comparing, as
> +	# "$1/.git/config" lacks it...
> +	git -C ".git/modules/$1" config --unset core.worktree &&
>  	diff -r ".git/modules/$1" "$1/.git" &&
> -	(
> -		# ... and then restore.
> -		cd ".git/modules/$1" &&
> -		git config core.worktree "../../../$1"
> -	)
> +	# ... and then restore.
> +	git -C ".git/modules/$1" config core.worktree "../../../$1"
>  }
>  
>  test_git_directory_exists () {
> diff --git a/t/t0060-path-utils.sh b/t/t0060-path-utils.sh
> index 68e29c904a6..53ec717cbca 100755
> --- a/t/t0060-path-utils.sh
> +++ b/t/t0060-path-utils.sh
> @@ -255,7 +255,9 @@ test_expect_success 'prefix_path rejects absolute path to dir with same beginnin
>  test_expect_success SYMLINKS 'prefix_path works with absolute path to a symlink to work tree having  same beginning as work tree' '
>  	git init repo &&
>  	ln -s repo repolink &&
> -	test "a" = "$(cd repo && test-tool path-utils prefix_path prefix "$(pwd)/../repolink/a")"
> +	echo "a" >expect &&
> +	test-tool -C repo path-utils prefix_path prefix "$(cd repo && pwd)/../repolink/a" >actual &&
> +	test_cmp expect actual
>  '
>  
>  relative_path /foo/a/b/c/	/foo/a/b/	c/
> diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
> index 5a169b68d6a..f5fbb84262b 100755
> --- a/t/t3200-branch.sh
> +++ b/t/t3200-branch.sh
> @@ -242,12 +242,13 @@ test_expect_success 'git branch -M baz bam should succeed when baz is checked ou
>  test_expect_success 'git branch -M baz bam should succeed within a worktree in which baz is checked out' '
>  	git checkout -b baz &&
>  	git worktree add -f bazdir baz &&
> -	(
> -		cd bazdir &&
> -		git branch -M baz bam &&
> -		test $(git rev-parse --abbrev-ref HEAD) = bam
> -	) &&
> -	test $(git rev-parse --abbrev-ref HEAD) = bam &&
> +	git -C "$bazdir" branch -M baz bam &&
> +	echo "bam" >expect &&
> +	git -C "$bazdir" rev-parse --abbrev-ref HEAD >actual &&
> +	test_cmp expect actual &&
> +	echo "bam" >expect &&
> +	git rev-parse --abbrev-ref HEAD >actual &&
> +	test_cmp expect actual &&
>  	rm -r bazdir &&
>  	git worktree prune
>  '
> diff --git a/t/t5605-clone-local.sh b/t/t5605-clone-local.sh
> index 38b850c10ef..61a2342bc2c 100755
> --- a/t/t5605-clone-local.sh
> +++ b/t/t5605-clone-local.sh
> @@ -15,8 +15,12 @@ test_expect_success 'preparing origin repository' '
>  	: >file && git add . && git commit -m1 &&
>  	git clone --bare . a.git &&
>  	git clone --bare . x &&
> -	test "$(cd a.git && git config --bool core.bare)" = true &&
> -	test "$(cd x && git config --bool core.bare)" = true &&
> +	echo true >expect &&
> +	git -C a.git config --bool core.bare >actual &&
> +	test_cmp expect actual &&
> +	echo true >expect &&
> +	git -C x config --bool core.bare >actual &&
> +	test_cmp expect actual &&
>  	git bundle create b1.bundle --all &&
>  	git bundle create b2.bundle main &&
>  	mkdir dir &&
> @@ -28,9 +32,10 @@ test_expect_success 'preparing origin repository' '
>  
>  test_expect_success 'local clone without .git suffix' '
>  	git clone -l -s a b &&
> -	(cd b &&
> -	test "$(git config --bool core.bare)" = false &&
> -	git fetch)
> +	echo false >expect &&
> +	git -C b config --bool core.bare >actual &&
> +	test_cmp expect actual &&
> +	git -C b fetch
>  '
>  
>  test_expect_success 'local clone with .git suffix' '
> diff --git a/t/t7402-submodule-rebase.sh b/t/t7402-submodule-rebase.sh
> index ebeca12a711..1927a862839 100755
> --- a/t/t7402-submodule-rebase.sh
> +++ b/t/t7402-submodule-rebase.sh
> @@ -82,11 +82,19 @@ test_expect_success 'stash with a dirty submodule' '
>  	CURRENT=$(cd submodule && git rev-parse HEAD) &&
>  	git stash &&
>  	test new != $(cat file) &&
> -	test submodule = $(git diff --name-only) &&
> -	test $CURRENT = $(cd submodule && git rev-parse HEAD) &&
> +	echo submodule >expect &&
> +	git diff --name-only >actual &&
> +	test_cmp expect actual &&
> +
> +	echo "$CURRENT" >expect &&
> +	git -C submodule rev-parse HEAD >actual &&
> +	test_cmp expect actual &&
> +
>  	git stash apply &&
>  	test new = $(cat file) &&
> -	test $CURRENT = $(cd submodule && git rev-parse HEAD)
> +	echo "$CURRENT" >expect &&
> +	git -C submodule rev-parse HEAD >actual &&
> +	test_cmp expect actual
>  
>  '
Previous: Ævar Arnfjörð BjarmasonNext: Ævar Arnfjörð Bjarmason
Message 48 of 83 in “tests: fix ignored & hidden exit codes”
  1. 0/6 tests: fix ignored & hidden exit codesÆvar Arnfjörð Bjarmason, Jul 21, 2022
  2. 1/6 diff tests: fix ignored exit codes in t4023Ævar Arnfjörð Bjarmason, Jul 21, 2022
  3. 2/6 t/lib-patch-mode.sh: fix ignored "git" exit codesÆvar Arnfjörð Bjarmason, Jul 21, 2022
  4. 3/6 auto-crlf tests: check "git checkout" exit codeÆvar Arnfjörð Bjarmason, Jul 21, 2022
  5. 4/6 test-lib-functions: add and use test_cmp_cmdÆvar Arnfjörð Bjarmason, Jul 21, 2022
  6. 5/6 merge tests: don't ignore "rev-parse" exit code in helperÆvar Arnfjörð Bjarmason, Jul 21, 2022
  7. 6/6 log tests: don't use "exit 1" outside a sub-shellÆvar Arnfjörð Bjarmason, Jul 21, 2022
  8. 0/8 tests: fix ignored & hidden exit codesÆvar Arnfjörð Bjarmason, Dec 2, 2022
  9. 1/8 log tests: don't use "exit 1" outside a sub-shellÆvar Arnfjörð Bjarmason, Dec 2, 2022
  10. Eric SunshineDec 2, 2022
  11. Junio C HamanoDec 2, 2022
  12. Ævar Arnfjörð BjarmasonDec 2, 2022
  13. Eric SunshineDec 2, 2022
  14. Ævar Arnfjörð BjarmasonDec 2, 2022
  15. Eric SunshineDec 7, 2022
  16. Junio C HamanoDec 2, 2022
  17. 2/8 auto-crlf tests: check "git checkout" exit codeÆvar Arnfjörð Bjarmason, Dec 2, 2022
  18. René ScharfeDec 2, 2022
  19. Eric SunshineDec 2, 2022
  20. Torsten BögershausenDec 2, 2022
  21. Eric SunshineDec 2, 2022
  22. 3/8 diff tests: fix ignored exit codes in t4023Ævar Arnfjörð Bjarmason, Dec 2, 2022
  23. Junio C HamanoDec 2, 2022
  24. 5/8 t/lib-patch-mode.sh: fix ignored "git" exit codesÆvar Arnfjörð Bjarmason, Dec 2, 2022
  25. René ScharfeDec 2, 2022
  26. 4/8 test-lib-functions: add and use test_cmp_cmdÆvar Arnfjörð Bjarmason, Dec 2, 2022
  27. René ScharfeDec 2, 2022
  28. Eric SunshineDec 2, 2022
  29. Eric SunshineDec 2, 2022
  30. Eric SunshineDec 2, 2022
  31. Junio C HamanoDec 2, 2022
  32. 6/8 merge tests: don't ignore "rev-parse" exit code in helperÆvar Arnfjörð Bjarmason, Dec 2, 2022
  33. René ScharfeDec 2, 2022
  34. 7/8 tests: use "test_cmp_cmd" instead of "test" in sub-shellsÆvar Arnfjörð Bjarmason, Dec 2, 2022
  35. 8/8 tests: use "test_cmp_cmd" in misc testsÆvar Arnfjörð Bjarmason, Dec 2, 2022
  36. Junio C HamanoDec 2, 2022
  37. 0/8 tests: fix ignored & hidden exit codesÆvar Arnfjörð Bjarmason, Dec 2, 2022
  38. 1/8 merge tests: don't ignore "rev-parse" exit code in helperÆvar Arnfjörð Bjarmason, Dec 2, 2022
  39. Junio C HamanoDec 5, 2022
  40. 2/8 auto-crlf tests: don't lose exit code in loops and outside testsÆvar Arnfjörð Bjarmason, Dec 2, 2022
  41. René ScharfeDec 2, 2022
  42. 3/8 diff tests: fix ignored exit codes in t4023Ævar Arnfjörð Bjarmason, Dec 2, 2022
  43. Junio C HamanoDec 5, 2022
  44. 4/8 t/lib-patch-mode.sh: fix ignored exit codesÆvar Arnfjörð Bjarmason, Dec 2, 2022
  45. René ScharfeDec 2, 2022
  46. Eric SunshineDec 4, 2022
  47. 5/8 tests: use "test_cmp" instead of "test" in sub-shellsÆvar Arnfjörð Bjarmason, Dec 2, 2022
  48. Junio C HamanoDec 5, 2022
  49. 7/8 tests: don't lose "git" exit codes in "! ( git ... | grep )"Ævar Arnfjörð Bjarmason, Dec 2, 2022
  50. René ScharfeDec 2, 2022
  51. 6/8 tests: don't lose 'test <str> = $(cmd ...)"' exit codeÆvar Arnfjörð Bjarmason, Dec 2, 2022
  52. 8/8 tests: don't lose mist "git" exit codesÆvar Arnfjörð Bjarmason, Dec 2, 2022
  53. Eric SunshineDec 4, 2022
  54. Junio C HamanoDec 5, 2022
  55. 0/6 tests: fix ignored & hidden exit codesÆvar Arnfjörð Bjarmason, Dec 19, 2022
  56. 1/6 auto-crlf tests: don't lose exit code in loops and outside testsÆvar Arnfjörð Bjarmason, Dec 19, 2022
  57. René ScharfeDec 19, 2022
  58. 2/6 t/lib-patch-mode.sh: fix ignored exit codesÆvar Arnfjörð Bjarmason, Dec 19, 2022
  59. Junio C HamanoDec 20, 2022
  60. Phillip WoodDec 27, 2022
  61. Ævar Arnfjörð BjarmasonDec 27, 2022
  62. 3/6 tests: don't lose exit status with "(cd ...; test <op> $(git ...))"Ævar Arnfjörð Bjarmason, Dec 19, 2022
  63. Junio C HamanoDec 20, 2022
  64. 4/6 tests: don't lose exit status with "test <op> $(git ...)"Ævar Arnfjörð Bjarmason, Dec 19, 2022
  65. Junio C HamanoDec 26, 2022
  66. 5/6 tests: don't lose "git" exit codes in "! ( git ... | grep )"Ævar Arnfjörð Bjarmason, Dec 19, 2022
  67. Junio C HamanoDec 26, 2022
  68. Phillip WoodDec 27, 2022
  69. Phillip WoodDec 27, 2022
  70. Junio C HamanoDec 27, 2022
  71. 6/6 tests: don't lose misc "git" exit codesÆvar Arnfjörð Bjarmason, Dec 19, 2022
  72. Phillip WoodDec 27, 2022
  73. Ævar Arnfjörð BjarmasonDec 27, 2022
  74. Junio C HamanoDec 27, 2022
  75. Junio C HamanoDec 20, 2022
  76. 0/6 tests: fix ignored & hidden exit codesÆvar Arnfjörð Bjarmason, Feb 6, 2023
  77. 1/6 auto-crlf tests: don't lose exit code in loops and outside testsÆvar Arnfjörð Bjarmason, Feb 6, 2023
  78. 2/6 t/lib-patch-mode.sh: fix ignored exit codesÆvar Arnfjörð Bjarmason, Feb 6, 2023
  79. 3/6 tests: don't lose exit status with "(cd ...; test <op> $(git ...))"Ævar Arnfjörð Bjarmason, Feb 6, 2023
  80. 5/6 tests: don't lose "git" exit codes in "! ( git ... | grep )"Ævar Arnfjörð Bjarmason, Feb 6, 2023
  81. 4/6 tests: don't lose exit status with "test <op> $(git ...)"Ævar Arnfjörð Bjarmason, Feb 6, 2023
  82. 6/6 tests: don't lose misc "git" exit codesÆvar Arnfjörð Bjarmason, Feb 6, 2023
  83. Junio C HamanoFeb 6, 2023

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.