git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
To: git@vger.kernel.org
Cc: "Junio C Hamano" <gitster@pobox.com>,
	"Emily Shaffer" <emilyshaffer@google.com>,
	"Eric Sunshine" <sunshine@sunshineco.com>,
	"Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
Subject: [PATCH 06/10] hook tests: get rid of unnecessary sub-shells
Date: Wed,  2 Mar 2022 14:22:25 +0100	[thread overview]
Message-ID: <patch-06.10-d86ee06b46e-20220302T131859Z-avarab@gmail.com> (raw)
In-Reply-To: <cover-00.10-00000000000-20220302T131859Z-avarab@gmail.com>

Refactor the repository setup code for tests that test hooks the use
of sub-shells when setting up the test repository and hooks.

A subsequent commit will change the hook setup to use a helper that
makes use of "test_when_finished", which cannot be used in
sub-shells. Let's change that setup code, and the adjacent and similar
"mk_empty" code.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 t/t5516-fetch-push.sh   | 150 ++++++++++++++++++----------------------
 t/t6500-gc.sh           |  18 ++---
 t/t9800-git-p4-basic.sh |  24 ++++---
 3 files changed, 94 insertions(+), 98 deletions(-)

diff --git a/t/t5516-fetch-push.sh b/t/t5516-fetch-push.sh
index 1a20e54adc1..e4bb7581568 100755
--- a/t/t5516-fetch-push.sh
+++ b/t/t5516-fetch-push.sh
@@ -24,12 +24,8 @@ D=$(pwd)
 mk_empty () {
 	repo_name="$1"
 	rm -fr "$repo_name" &&
-	mkdir "$repo_name" &&
-	(
-		cd "$repo_name" &&
-		git init &&
-		git config receive.denyCurrentBranch warn
-	)
+	git init "$repo_name" &&
+	git -C "$repo_name" config receive.denyCurrentBranch warn
 }
 
 mk_test () {
@@ -58,36 +54,35 @@ mk_test () {
 mk_test_with_hooks() {
 	repo_name=$1
 	mk_test "$@" &&
-	(
-		cd "$repo_name" &&
-		mkdir .git/hooks &&
-		cd .git/hooks &&
-
-		cat >pre-receive <<-'EOF' &&
-		#!/bin/sh
-		cat - >>pre-receive.actual
-		EOF
 
-		cat >update <<-'EOF' &&
-		#!/bin/sh
-		printf "%s %s %s\n" "$@" >>update.actual
-		EOF
-
-		cat >post-receive <<-'EOF' &&
-		#!/bin/sh
-		cat - >>post-receive.actual
-		EOF
-
-		cat >post-update <<-'EOF' &&
-		#!/bin/sh
-		for ref in "$@"
-		do
-			printf "%s\n" "$ref" >>post-update.actual
-		done
-		EOF
-
-		chmod +x pre-receive update post-receive post-update
-	)
+	cat >"$repo_name"/.git/hooks/pre-receive <<-'EOF' &&
+	#!/bin/sh
+	cat - >>pre-receive.actual
+	EOF
+
+	cat >"$repo_name"/.git/hooks/update <<-'EOF' &&
+	#!/bin/sh
+	printf "%s %s %s\n" "$@" >>update.actual
+	EOF
+
+	cat >"$repo_name"/.git/hooks/post-receive <<-'EOF' &&
+	#!/bin/sh
+	cat - >>post-receive.actual
+	EOF
+
+	cat >"$repo_name"/.git/hooks/post-update <<-'EOF' &&
+	#!/bin/sh
+	for ref in "$@"
+	do
+		printf "%s\n" "$ref" >>post-update.actual
+	done
+	EOF
+
+	chmod +x \
+	      "$repo_name"/.git/hooks/pre-receive \
+	      "$repo_name"/.git/hooks/update \
+	      "$repo_name"/.git/hooks/post-receive \
+	      "$repo_name"/.git/hooks/post-update
 }
 
 mk_child() {
@@ -667,7 +662,6 @@ test_expect_success 'push does not update local refs on failure' '
 
 	mk_test testrepo heads/main &&
 	mk_child testrepo child &&
-	mkdir testrepo/.git/hooks &&
 	echo "#!/no/frobnication/today" >testrepo/.git/hooks/pre-receive &&
 	chmod +x testrepo/.git/hooks/pre-receive &&
 	(
@@ -1679,24 +1673,21 @@ test_expect_success 'receive.denyCurrentBranch = updateInstead' '
 test_expect_success 'updateInstead with push-to-checkout hook' '
 	rm -fr testrepo &&
 	git init testrepo &&
-	(
-		cd testrepo &&
-		git pull .. main &&
-		git reset --hard HEAD^^ &&
-		git tag initial &&
-		git config receive.denyCurrentBranch updateInstead &&
-		write_script .git/hooks/push-to-checkout <<-\EOF
-		echo >&2 updating from $(git rev-parse HEAD)
-		echo >&2 updating to "$1"
-
-		git update-index -q --refresh &&
-		git read-tree -u -m HEAD "$1" || {
-			status=$?
-			echo >&2 read-tree failed
-			exit $status
-		}
-		EOF
-	) &&
+	git -C testrepo pull .. main &&
+	git -C testrepo reset --hard HEAD^^ &&
+	git -C testrepo tag initial &&
+	git -C testrepo config receive.denyCurrentBranch updateInstead &&
+	write_script testrepo/.git/hooks/push-to-checkout <<-\EOF &&
+	echo >&2 updating from $(git rev-parse HEAD)
+	echo >&2 updating to "$1"
+
+	git update-index -q --refresh &&
+	git read-tree -u -m HEAD "$1" || {
+		status=$?
+		echo >&2 read-tree failed
+		exit $status
+	}
+	EOF
 
 	# Try pushing into a pristine
 	git push testrepo main &&
@@ -1741,33 +1732,30 @@ test_expect_success 'updateInstead with push-to-checkout hook' '
 	# push into void
 	rm -fr void &&
 	git init void &&
-	(
-		cd void &&
-		git config receive.denyCurrentBranch updateInstead &&
-		write_script .git/hooks/push-to-checkout <<-\EOF
-		if git rev-parse --quiet --verify HEAD
-		then
-			has_head=yes
-			echo >&2 updating from $(git rev-parse HEAD)
-		else
-			has_head=no
-			echo >&2 pushing into void
-		fi
-		echo >&2 updating to "$1"
-
-		git update-index -q --refresh &&
-		case "$has_head" in
-		yes)
-			git read-tree -u -m HEAD "$1" ;;
-		no)
-			git read-tree -u -m "$1" ;;
-		esac || {
-			status=$?
-			echo >&2 read-tree failed
-			exit $status
-		}
-		EOF
-	) &&
+	git -C void config receive.denyCurrentBranch updateInstead &&
+	write_script void/.git/hooks/push-to-checkout <<-\EOF &&
+	if git rev-parse --quiet --verify HEAD
+	then
+		has_head=yes
+		echo >&2 updating from $(git rev-parse HEAD)
+	else
+		has_head=no
+		echo >&2 pushing into void
+	fi
+	echo >&2 updating to "$1"
+
+	git update-index -q --refresh &&
+	case "$has_head" in
+	yes)
+		git read-tree -u -m HEAD "$1" ;;
+	no)
+		git read-tree -u -m "$1" ;;
+	esac || {
+		status=$?
+		echo >&2 read-tree failed
+		exit $status
+	}
+	EOF
 
 	git push void main &&
 	(
diff --git a/t/t6500-gc.sh b/t/t6500-gc.sh
index c2021267f2c..a6b0db22867 100755
--- a/t/t6500-gc.sh
+++ b/t/t6500-gc.sh
@@ -101,12 +101,12 @@ test_expect_success 'pre-auto-gc hook can stop auto gc' '
 	EOF
 
 	git init pre-auto-gc-hook &&
+	write_script "pre-auto-gc-hook/.git/hooks/pre-auto-gc" <<-\EOF &&
+	echo >&2 no gc for you &&
+	exit 1
+	EOF
 	(
 		cd pre-auto-gc-hook &&
-		write_script ".git/hooks/pre-auto-gc" <<-\EOF &&
-		echo >&2 no gc for you &&
-		exit 1
-		EOF
 
 		git config gc.auto 3 &&
 		git config gc.autoDetach false &&
@@ -128,12 +128,14 @@ test_expect_success 'pre-auto-gc hook can stop auto gc' '
 	See "git help gc" for manual housekeeping.
 	EOF
 
+	write_script "pre-auto-gc-hook/.git/hooks/pre-auto-gc" <<-\EOF &&
+	echo >&2 will gc for you &&
+	exit 0
+	EOF
+
 	(
 		cd pre-auto-gc-hook &&
-		write_script ".git/hooks/pre-auto-gc" <<-\EOF &&
-		echo >&2 will gc for you &&
-		exit 0
-		EOF
+
 		git gc --auto >../out.actual 2>../err.actual
 	) &&
 
diff --git a/t/t9800-git-p4-basic.sh b/t/t9800-git-p4-basic.sh
index 806005a793a..3c1534c94d6 100755
--- a/t/t9800-git-p4-basic.sh
+++ b/t/t9800-git-p4-basic.sh
@@ -277,16 +277,22 @@ test_expect_success 'run hook p4-pre-submit before submit' '
 		git commit -m "add hello.txt" &&
 		git config git-p4.skipSubmitEdit true &&
 		git p4 submit --dry-run >out &&
-		grep "Would apply" out &&
-		mkdir -p .git/hooks &&
-		write_script .git/hooks/p4-pre-submit <<-\EOF &&
-		exit 0
-		EOF
+		grep "Would apply" out
+	) &&
+	mkdir -p "$git"/.git/hooks &&
+	write_script "$git"/.git/hooks/p4-pre-submit <<-\EOF &&
+	exit 0
+	EOF
+	(
+		cd "$git" &&
 		git p4 submit --dry-run >out &&
-		grep "Would apply" out &&
-		write_script .git/hooks/p4-pre-submit <<-\EOF &&
-		exit 1
-		EOF
+		grep "Would apply" out
+	) &&
+	write_script "$git"/.git/hooks/p4-pre-submit <<-\EOF &&
+	exit 1
+	EOF
+	(
+		cd "$git" &&
 		test_must_fail git p4 submit --dry-run >errs 2>&1 &&
 		! grep "Would apply" errs
 	)
-- 
2.35.1.1228.g56895c6ee86


  parent reply	other threads:[~2022-03-02 13:22 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-02 13:22 [PATCH 00/10] tests: add and use a "test_hook" wrapper + hook fixes Ævar Arnfjörð Bjarmason
2022-03-02 13:22 ` [PATCH 01/10] hook tests: turn exit code assertions into a loop Ævar Arnfjörð Bjarmason
2022-03-02 20:55   ` Junio C Hamano
2022-03-02 13:22 ` [PATCH 02/10] t5540: don't rely on "hook/post-update.sample" Ævar Arnfjörð Bjarmason
2022-03-02 21:06   ` Junio C Hamano
2022-03-02 13:22 ` [PATCH 03/10] tests: assume the hooks are disabled by default Ævar Arnfjörð Bjarmason
2022-03-02 21:13   ` Junio C Hamano
2022-03-02 13:22 ` [PATCH 04/10] bugreport tests: tighten up "git bugreport -s hooks" test Ævar Arnfjörð Bjarmason
2022-03-02 21:22   ` Junio C Hamano
2022-03-02 13:22 ` [PATCH 05/10] tests: indent and add hook setup to "test_expect_success" Ævar Arnfjörð Bjarmason
2022-03-02 21:27   ` Junio C Hamano
2022-03-02 13:22 ` Ævar Arnfjörð Bjarmason [this message]
2022-03-02 21:31   ` [PATCH 06/10] hook tests: get rid of unnecessary sub-shells Junio C Hamano
2022-03-02 13:22 ` [PATCH 07/10] fetch+push tests: have tests clean up their own mess Ævar Arnfjörð Bjarmason
2022-03-02 21:44   ` Junio C Hamano
2022-03-02 13:22 ` [PATCH 08/10] test-lib-functions: add and use a "test_hook" wrapper Ævar Arnfjörð Bjarmason
2022-03-02 21:59   ` Junio C Hamano
2022-03-02 13:22 ` [PATCH 09/10] tests: change "mkdir -p && write_script" to use "test_hook" Ævar Arnfjörð Bjarmason
2022-03-02 22:06   ` Junio C Hamano
2022-03-02 13:22 ` [PATCH 10/10] tests: change "cat && chmod +x" " Ævar Arnfjörð Bjarmason
2022-03-03  5:46   ` Eric Sunshine
2022-03-02 22:38 ` [PATCH 00/10] tests: add and use a "test_hook" wrapper + hook fixes Junio C Hamano
2022-03-07 12:43 ` [PATCH v2 " Ævar Arnfjörð Bjarmason
2022-03-07 12:43   ` [PATCH v2 01/10] hook tests: turn exit code assertions into a loop Ævar Arnfjörð Bjarmason
2022-03-07 12:43   ` [PATCH v2 02/10] t5540: don't rely on "hook/post-update.sample" Ævar Arnfjörð Bjarmason
2022-03-07 12:43   ` [PATCH v2 03/10] tests: assume the hooks are disabled by default Ævar Arnfjörð Bjarmason
2022-03-07 12:43   ` [PATCH v2 04/10] bugreport tests: tighten up "git bugreport -s hooks" test Ævar Arnfjörð Bjarmason
2022-03-07 12:43   ` [PATCH v2 05/10] tests: indent and add hook setup to "test_expect_success" Ævar Arnfjörð Bjarmason
2022-03-07 12:43   ` [PATCH v2 06/10] hook tests: get rid of unnecessary sub-shells Ævar Arnfjörð Bjarmason
2022-03-07 12:43   ` [PATCH v2 07/10] fetch+push tests: have tests clean up their own mess Ævar Arnfjörð Bjarmason
2022-03-07 12:43   ` [PATCH v2 08/10] test-lib-functions: add and use a "test_hook" wrapper Ævar Arnfjörð Bjarmason
2022-03-07 12:43   ` [PATCH v2 09/10] tests: change "mkdir -p && write_script" to use "test_hook" Ævar Arnfjörð Bjarmason
2022-03-07 12:43   ` [PATCH v2 10/10] tests: change "cat && chmod +x" " Ævar Arnfjörð Bjarmason
2022-03-07 21:26   ` [PATCH v2 00/10] tests: add and use a "test_hook" wrapper + hook fixes Junio C Hamano
2022-03-17 10:13   ` [PATCH v3 00/13] " Ævar Arnfjörð Bjarmason
2022-03-17 10:13     ` [PATCH v3 01/13] test-lib-functions: add and use a "test_hook" wrapper Ævar Arnfjörð Bjarmason
2022-03-17 10:13     ` [PATCH v3 02/13] hook tests: turn exit code assertions into a loop Ævar Arnfjörð Bjarmason
2022-03-17 10:13     ` [PATCH v3 03/13] http tests: don't rely on "hook/post-update.sample" Ævar Arnfjörð Bjarmason
2022-03-17 10:13     ` [PATCH v3 04/13] tests: assume the hooks are disabled by default Ævar Arnfjörð Bjarmason
2022-03-17 10:13     ` [PATCH v3 05/13] bugreport tests: tighten up "git bugreport -s hooks" test Ævar Arnfjörð Bjarmason
2022-03-17 10:13     ` [PATCH v3 06/13] fetch+push tests: use "test_hook" and "test_when_finished" pattern Ævar Arnfjörð Bjarmason
2022-03-17 10:13     ` [PATCH v3 07/13] gc + p4 tests: use "test_hook", remove sub-shells Ævar Arnfjörð Bjarmason
2022-03-17 10:13     ` [PATCH v3 08/13] tests: change "cat && chmod +x" to use "test_hook" Ævar Arnfjörð Bjarmason
2022-03-17 10:13     ` [PATCH v3 09/13] tests: change "mkdir -p && write_script" " Ævar Arnfjörð Bjarmason
2022-03-17 10:13     ` [PATCH v3 10/13] tests: use "test_hook" for misc "mkdir -p" and "chmod" cases Ævar Arnfjörð Bjarmason
2022-03-17 10:13     ` [PATCH v3 11/13] tests: extend "test_hook" for "rm" and "chmod -x", convert "$HOOK" Ævar Arnfjörð Bjarmason
2022-03-17 10:13     ` [PATCH v3 12/13] proc-receive hook tests: use "test_hook" instead of "write_script" Ævar Arnfjörð Bjarmason
2022-03-17 10:13     ` [PATCH v3 13/13] http tests: use "test_hook" for "smart" and "dumb" http tests Ævar Arnfjörð Bjarmason
2022-03-17 16:31     ` [PATCH v3 00/13] tests: add and use a "test_hook" wrapper + hook fixes Junio C Hamano
2022-03-17 21:04       ` Ævar Arnfjörð Bjarmason
2022-03-17 21:40         ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=patch-06.10-d86ee06b46e-20220302T131859Z-avarab@gmail.com \
    --to=avarab@gmail.com \
    --cc=emilyshaffer@google.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=sunshine@sunshineco.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).