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

[PATCH 1/4] t5510: make confusing config cleanup more explicit

From
Jeff King <peff@peff.net>
Date
Aug 19, 2025, 19:24 UTC
Message-ID
<20250819192455.GA1059295@coredump.intra.peff.net>
In-Reply-To
<20250819192004.GA1058857@coredump.intra.peff.net>

Several tests set a config variable in a sub-repo we chdir into via a subshell, like this:

  (
	cd "$D" &&
	cd two &&
	git config foo.bar baz
  )

But they also clean up the variable with a when_finished directive outside of the subshell, like this:

  test_when_finished "git config unset foo.bar"

At first glance, this shouldn't work! The cleanup clause cannot be run from the subshell (since environment changes there are lost by the time the test snippet finishes). But since the cleanup command runs outside the subshell, our working directory will not have been switched into "two".

But it does work. Why?

The answer is that an earlier test does a "cd two" that moves the whole test's working directory out of $TRASH_DIRECTORY and into "two". So the subshell is a bit of a red herring; we are already in the right directory! That's why we need the "cd $D" at the top of the shell, to put us back to a known spot.

Let's make this cleanup code more explicitly specify where we expect the config command to run. That makes the script more robust against running a subset of the tests, and ultimately will make it easier to refactor the script to avoid these top-level chdirs.

Signed-off-by: Jeff King <peff@peff.net>
---
 t/t5510-fetch.sh | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
diff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh
index ebc696546b..64fea9f4a5 100755
--- a/t/t5510-fetch.sh
+++ b/t/t5510-fetch.sh
@@ -119,7 +119,7 @@ test_expect_success "fetch test remote HEAD change" '
 	test "z$head" = "z$branch"'
 
 test_expect_success "fetch test followRemoteHEAD never" '
-	test_when_finished "git config unset remote.origin.followRemoteHEAD" &&
+	test_when_finished "git -C \"$D/two\" config unset remote.origin.followRemoteHEAD" &&
 	(
 		cd "$D" &&
 		cd two &&
@@ -134,7 +134,7 @@ test_expect_success "fetch test followRemoteHEAD never" '
 '
 
 test_expect_success "fetch test followRemoteHEAD warn no change" '
-	test_when_finished "git config unset remote.origin.followRemoteHEAD" &&
+	test_when_finished "git -C \"$D/two\" config unset remote.origin.followRemoteHEAD" &&
 	(
 		cd "$D" &&
 		cd two &&
@@ -154,7 +154,7 @@ test_expect_success "fetch test followRemoteHEAD warn no change" '
 '
 
 test_expect_success "fetch test followRemoteHEAD warn create" '
-	test_when_finished "git config unset remote.origin.followRemoteHEAD" &&
+	test_when_finished "git -C \"$D/two\" config unset remote.origin.followRemoteHEAD" &&
 	(
 		cd "$D" &&
 		cd two &&
@@ -170,7 +170,7 @@ test_expect_success "fetch test followRemoteHEAD warn create" '
 '
 
 test_expect_success "fetch test followRemoteHEAD warn detached" '
-	test_when_finished "git config unset remote.origin.followRemoteHEAD" &&
+	test_when_finished "git -C \"$D/two\" config unset remote.origin.followRemoteHEAD" &&
 	(
 		cd "$D" &&
 		cd two &&
@@ -187,7 +187,7 @@ test_expect_success "fetch test followRemoteHEAD warn detached" '
 '
 
 test_expect_success "fetch test followRemoteHEAD warn quiet" '
-	test_when_finished "git config unset remote.origin.followRemoteHEAD" &&
+	test_when_finished "git -C \"$D/two\" config unset remote.origin.followRemoteHEAD" &&
 	(
 		cd "$D" &&
 		cd two &&
@@ -205,7 +205,7 @@ test_expect_success "fetch test followRemoteHEAD warn quiet" '
 '
 
 test_expect_success "fetch test followRemoteHEAD warn-if-not-branch branch is same" '
-	test_when_finished "git config unset remote.origin.followRemoteHEAD" &&
+	test_when_finished "git -C \"$D/two\" config unset remote.origin.followRemoteHEAD" &&
 	(
 		cd "$D" &&
 		cd two &&
@@ -223,7 +223,7 @@ test_expect_success "fetch test followRemoteHEAD warn-if-not-branch branch is sa
 '
 
 test_expect_success "fetch test followRemoteHEAD warn-if-not-branch branch is different" '
-	test_when_finished "git config unset remote.origin.followRemoteHEAD" &&
+	test_when_finished "git -C \"$D/two\" config unset remote.origin.followRemoteHEAD" &&
 	(
 		cd "$D" &&
 		cd two &&
@@ -243,7 +243,7 @@ test_expect_success "fetch test followRemoteHEAD warn-if-not-branch branch is di
 '
 
 test_expect_success "fetch test followRemoteHEAD always" '
-	test_when_finished "git config unset remote.origin.followRemoteHEAD" &&
+	test_when_finished "git -C \"$D/two\" config unset remote.origin.followRemoteHEAD" &&
 	(
 		cd "$D" &&
 		cd two &&
@@ -260,7 +260,7 @@ test_expect_success "fetch test followRemoteHEAD always" '
 '
 
 test_expect_success 'followRemoteHEAD does not kick in with refspecs' '
-	test_when_finished "git config unset remote.origin.followRemoteHEAD" &&
+	test_when_finished "git -C \"$D/two\" config unset remote.origin.followRemoteHEAD" &&
 	(
 		cd "$D" &&
 		cd two &&
-- 
2.51.0.326.gecbb38d78e
Previous: Jeff KingNext: Eric Sunshine
Message 3 of 22 in “dangling symrefs and fetchRemoteHEAD=create”
  1. 0/4 dangling symrefs and fetchRemoteHEAD=createJeff King, Aug 19, 2025
  2. Jeff KingAug 19, 2025
  3. 1/4 t5510: make confusing config cleanup more explicitJeff King, Aug 19, 2025
  4. Eric SunshineAug 19, 2025
  5. Eric SunshineAug 19, 2025
  6. Jeff KingAug 19, 2025
  7. 2/4 t5510: stop changing top-level working directoryJeff King, Aug 19, 2025
  8. 3/4 t5510: prefer "git -C" to subshell for followRemoteHEAD testsJeff King, Aug 19, 2025
  9. SZEDER GáborAug 24, 2025
  10. Junio C HamanoAug 25, 2025
  11. Jeff KingAug 26, 2025
  12. Junio C HamanoAug 26, 2025
  13. 4/4 refs: do not clobber dangling symrefsJeff King, Aug 19, 2025
  14. Patrick SteinhardtAug 20, 2025
  15. Jeff KingAug 20, 2025
  16. Toon ClaesSep 22, 2025
  17. Junio C HamanoSep 22, 2025
  18. Jeff KingSep 22, 2025
  19. Junio C HamanoSep 22, 2025
  20. Jeff KingSep 22, 2025
  21. Toon ClaesSep 23, 2025
  22. Jeff KingSep 23, 2025

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.