Subject: [PATCH 2/3] t7005: sanitize test environment for subsequent tests

From: "D. Ben Knoble" <[email-masked]>
Date: Sun, 10 Aug 2025 12:03:18 -0400

Some of the editor tests manipulate the environment or config in ways
that affect future tests, but those modifications are visible to future
tests and create a footgun for them.

Use test_config, subshells, and test helpers to automatically undo
environment and config modifications once finished.

Best-viewed-with: --ignore-all-space
Signed-off-by: D. Ben Knoble <[email-masked]>
---
 t/t7005-editor.sh | 132 +++++++++++++++++++++++++---------------------
 1 file changed, 71 insertions(+), 61 deletions(-)

diff --git a/t/t7005-editor.sh b/t/t7005-editor.sh
index 2f59fc0549..eae76cc2f2 100755
--- a/t/t7005-editor.sh
+++ b/t/t7005-editor.sh
@@ -38,73 +38,84 @@
 	test_cmp expect actual
 '
 
-TERM=dumb
-export TERM
 test_expect_success 'dumb should error out when falling back on vi' '
-	if git commit --amend
-	then
-		echo "Oops?"
-		false
-	else
-		: happy
-	fi
+	(
+		TERM=dumb &&
+		export TERM &&
+		if git commit --amend
+		then
+			echo "Oops?"
+			false
+		else
+			: happy
+		fi
+	)
 '
 
 test_expect_success 'dumb should prefer EDITOR to VISUAL' '
-	EDITOR=./e-EDITOR.sh &&
-	VISUAL=./e-VISUAL.sh &&
-	export EDITOR VISUAL &&
-	git commit --amend &&
-	echo "Edited by EDITOR" >expect &&
-	git show -s --format=%s >actual &&
+	(
+		TERM=dumb &&
+		export TERM &&
+		EDITOR=./e-EDITOR.sh &&
+		VISUAL=./e-VISUAL.sh &&
+		export EDITOR VISUAL &&
+		git commit --amend &&
+		echo "Edited by EDITOR" >expect &&
+		git show -s --format=%s >actual
+	) &&
 	test_cmp expect actual
 '
 
-TERM=vt100
-export TERM
-for i in $vi EDITOR VISUAL core_editor GIT_EDITOR
-do
-	echo "Edited by $i" >expect
-	unset EDITOR VISUAL GIT_EDITOR
-	git config --unset-all core.editor
-	case "$i" in
-	core_editor)
-		git config core.editor ./e-core_editor.sh
-		;;
-	[A-Z]*)
-		eval "$i=./e-$i.sh"
-		export $i
-		;;
-	esac
-	test_expect_success "Using $i" '
-		git --exec-path=. commit --amend &&
-		git show -s --pretty=oneline >show &&
-		<show sed -e "s/^[0-9a-f]* //" >actual &&
-		test_cmp expect actual
-	'
-done
+test_expect_success 'Using individual editors' '
+	test_when_finished "test_unconfig --unset-all core.editor" &&
+	(
+		TERM=vt100 &&
+		export TERM &&
+		for i in $vi EDITOR VISUAL core_editor GIT_EDITOR
+		do
+			sane_unset EDITOR VISUAL GIT_EDITOR &&
+			test_might_fail git config --unset-all core.editor &&
+			echo "Edited by $i" >expect &&
+			case "$i" in
+			core_editor)
+				git config core.editor ./e-core_editor.sh
+				;;
+			[A-Z]*)
+				eval "$i=./e-$i.sh" &&
+				export $i
+				;;
+			esac &&
+			git --exec-path=. commit --amend &&
+			git show -s --pretty=oneline >show &&
+			<show sed -e "s/^[0-9a-f]* //" >actual &&
+			test_cmp expect actual
+		done
+	)
+'
 
-unset EDITOR VISUAL GIT_EDITOR
-git config --unset-all core.editor
-for i in $vi EDITOR VISUAL core_editor GIT_EDITOR
-do
-	echo "Edited by $i" >expect
-	case "$i" in
-	core_editor)
-		git config core.editor ./e-core_editor.sh
-		;;
-	[A-Z]*)
-		eval "$i=./e-$i.sh"
-		export $i
-		;;
-	esac
-	test_expect_success "Using $i (override)" '
-		git --exec-path=. commit --amend &&
-		git show -s --pretty=oneline >show &&
-		<show sed -e "s/^[0-9a-f]* //" >actual &&
-		test_cmp expect actual
-	'
-done
+test_expect_success 'Using editors with overrides' '
+	(
+		TERM=vt100 &&
+		export TERM &&
+		for i in $vi EDITOR VISUAL core_editor GIT_EDITOR
+		do
+			echo "Edited by $i" >expect &&
+			case "$i" in
+			core_editor)
+				git config core.editor ./e-core_editor.sh
+				;;
+			[A-Z]*)
+				eval "$i=./e-$i.sh" &&
+				export $i
+				;;
+			esac &&
+			git --exec-path=. commit --amend &&
+			git show -s --pretty=oneline >show &&
+			<show sed -e "s/^[0-9a-f]* //" >actual &&
+			test_cmp expect actual
+		done
+	)
+'
 
 test_expect_success 'editor with a space' '
 	echo "echo space >\"\$1\"" >"e space.sh" &&
@@ -115,9 +126,8 @@
 	test_cmp expect actual
 '
 
-unset GIT_EDITOR
 test_expect_success 'core.editor with a space' '
-	git config core.editor \"./e\ space.sh\" &&
+	test_config core.editor \"./e\ space.sh\" &&
 	git commit --amend &&
 	echo space >expect &&
 	git show -s --pretty=tformat:%s >actual &&
-- 
2.48.1