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

[PATCH v3 06/10] t7001: avoid using `cd` outside of subshells

From
Shubham Verma <shubhunic@gmail.com>
Date
Feb 11, 2021, 19:47 UTC
Message-ID
<20210211194704.28913-7-shubhunic@gmail.com>
In-Reply-To
<20210211194704.28913-1-shubhunic@gmail.com>

Avoid using `cd` outside of subshells since, if the test fails, there is no guarantee that the current working directory is the expected one, which may cause subsequent tests to run in the wrong directory.

While at it, make some other tests more concise by replacing simple subshells with `git -C`.

Signed-off-by: Shubham Verma <shubhunic@gmail.com>
---
 t/t7001-mv.sh | 43 +++++++++++--------------------------------
 1 file changed, 11 insertions(+), 32 deletions(-)
diff --git a/t/t7001-mv.sh b/t/t7001-mv.sh
index 2e49e9af81..6412c4f882 100755
--- a/t/t7001-mv.sh
+++ b/t/t7001-mv.sh
@@ -11,12 +11,12 @@ test_expect_success 'prepare reference tree' '
 '
 
 test_expect_success 'moving the file out of subdirectory' '
-	cd path0 && git mv COPYING ../path1/COPYING
+	git -C path0 mv COPYING ../path1/COPYING
 '
 
 # in path0 currently
 test_expect_success 'commiting the change' '
-	cd .. && git commit -m move-out -a
+	git commit -m move-out -a
 '
 
 test_expect_success 'checking the commit' '
@@ -25,12 +25,12 @@ test_expect_success 'checking the commit' '
 '
 
 test_expect_success 'moving the file back into subdirectory' '
-	cd path0 && git mv ../path1/COPYING COPYING
+	git -C path0 mv ../path1/COPYING COPYING
 '
 
 # in path0 currently
 test_expect_success 'commiting the change' '
-	cd .. && git commit -m move-in -a
+	git commit -m move-in -a
 '
 
 test_expect_success 'checking the commit' '
@@ -328,10 +328,7 @@ test_expect_success 'git mv moves a submodule with a .git directory and no .gitm
 	git mv sub mod/sub &&
 	! test -e sub &&
 	[ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
-	(
-		cd mod/sub &&
-		git status
-	) &&
+	git -C mod/sub status &&
 	git update-index --refresh &&
 	git diff-files --quiet
 '
@@ -351,10 +348,7 @@ test_expect_success 'git mv moves a submodule with a .git directory and .gitmodu
 	git mv sub mod/sub &&
 	! test -e sub &&
 	[ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
-	(
-		cd mod/sub &&
-		git status
-	) &&
+	git -C mod/sub status &&
 	echo mod/sub >expected &&
 	git config -f .gitmodules submodule.sub.path >actual &&
 	test_cmp expected actual &&
@@ -368,16 +362,10 @@ test_expect_success 'git mv moves a submodule with gitfile' '
 	git submodule update &&
 	entry="$(git ls-files --stage sub | cut -f 1)" &&
 	mkdir mod &&
-	(
-		cd mod &&
-		git mv ../sub/ .
-	) &&
+	git -C mod mv ../sub/ . &&
 	! test -e sub &&
 	[ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
-	(
-		cd mod/sub &&
-		git status
-	) &&
+	git -C mod/sub status &&
 	echo mod/sub >expected &&
 	git config -f .gitmodules submodule.sub.path >actual &&
 	test_cmp expected actual &&
@@ -396,10 +384,7 @@ test_expect_success 'mv does not complain when no .gitmodules file is found' '
 	test_must_be_empty actual.err &&
 	! test -e sub &&
 	[ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
-	(
-		cd mod/sub &&
-		git status
-	) &&
+	git -C mod/sub status &&
 	git update-index --refresh &&
 	git diff-files --quiet
 '
@@ -420,10 +405,7 @@ test_expect_success 'mv will error out on a modified .gitmodules file unless sta
 	test_must_be_empty actual.err &&
 	! test -e sub &&
 	[ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
-	(
-		cd mod/sub &&
-		git status
-	) &&
+	git -C mod/sub status &&
 	git update-index --refresh &&
 	git diff-files --quiet
 '
@@ -441,10 +423,7 @@ test_expect_success 'mv issues a warning when section is not found in .gitmodule
 	test_i18ncmp expect.err actual.err &&
 	! test -e sub &&
 	[ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
-	(
-		cd mod/sub &&
-		git status
-	) &&
+	git -C mod/sub status &&
 	git update-index --refresh &&
 	git diff-files --quiet
 '
-- 
2.25.1
Previous: Shubham VermaNext: Shubham Verma
Message 8 of 14 in “Modernizing the t7001 test script”
  1. 00/10 Modernizing the t7001 test scriptShubham Verma, Feb 11, 2021
  2. 01/10 t7001: modernize test formattingShubham Verma, Feb 11, 2021
  3. 03/10 t7001: remove unnecessary blank linesShubham Verma, Feb 11, 2021
  4. 02/10 t7001: indent with TABs instead of spacesShubham Verma, Feb 11, 2021
  5. 05/10 t7001: remove whitespace after redirect operatorsShubham Verma, Feb 11, 2021
  6. 07/10 t7001: use '>' rather than 'touch'Shubham Verma, Feb 11, 2021
  7. 04/10 t7001: modernize subshell formattingShubham Verma, Feb 11, 2021
  8. 06/10 t7001: avoid using `cd` outside of subshellsShubham Verma, Feb 11, 2021
  9. 10/10 t7001: use `test` rather than `[`Shubham Verma, Feb 11, 2021
  10. 08/10 t7001: put each command on a separate lineShubham Verma, Feb 11, 2021
  11. 09/10 t7001: use here-docs instead of echoShubham Verma, Feb 11, 2021
  12. Junio C HamanoFeb 11, 2021
  13. Shubham VermaFeb 23, 2021
  14. Junio C HamanoFeb 23, 2021

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.