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

[PATCH] t1020: Get rid of 'cd "$HERE"' at the start of each test

From
Jens Lehmann <jens.lehmann@web.de>
Date
Sep 7, 2010, 10:29 UTC
Message-ID
<4C861400.2030901@web.de>
In-Reply-To
<7vhbi2tqd0.fsf@alter.siamese.dyndns.org>

To achieve that, all cd commands which weren't inside a subshell had to be put into a new one.

Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de>
---
Am 07.09.2010 01:16, schrieb Junio C Hamano:
> If we were to do this, shouldn't we be able to lose 'cd "$HERE"' at the
> beginning of each test?  The test after this one does "cd dir" without
> even coming back up and relies on the next one to go back itself,

You are right, patch included. I had to introduce some new subshells for that and used the new formatting proposed by Jonathan (but I didn't touch the one introduced by my earlier patch which is reformatted by Jonathan's "[PATCH 1/7] tests: subshell indentation stylefix", so both patches won't conflict.

> suggesting that grepping for 'cd ..' may not be sufficient, depending on
> what we are trying to fix.

Yeah, I knew this would only show a part of the problems. But you have to start somewhere, no? :-) (My first intention was to systematically search for missing &&, as those really can be nasty, but my script-fu was not up to that task)

Show 7 quoted lines
> If we were to insist that no matter how an individual test fail, the test
> that follows it must start in a known location (namely, $TRASH), then we
> might want to use something like the attached patch.  On the other hand,
> we might want to be lenient to a test suite whose one test moves around
> and the test that follows such a test knows that, and also anticipates
> that the previous one _can_ fail and tries recover from that failure (e.g.
> use of 'cd "$HERE"' in the test that follows the above one.

I think we should force that each test finishes somewhere inside $TRASH, but not necessarily at the top level.

 t/t1020-subdirectory.sh |   91 ++++++++++++++++++++++++++---------------------
 1 files changed, 50 insertions(+), 41 deletions(-)
diff --git a/t/t1020-subdirectory.sh b/t/t1020-subdirectory.sh
index c36157a..0eb4488 100755
--- a/t/t1020-subdirectory.sh
+++ b/t/t1020-subdirectory.sh
@@ -16,12 +16,10 @@ test_expect_success setup '
 	cp one original.one &&
 	cp dir/two original.two
 '
-HERE=`pwd`
 LF='
 '

 test_expect_success 'update-index and ls-files' '
-	cd "$HERE" &&
 	git update-index --add one &&
 	case "`git ls-files`" in
 	one) echo pass one ;;
@@ -41,20 +39,20 @@ test_expect_success 'update-index and ls-files' '
 '

 test_expect_success 'cat-file' '
-	cd "$HERE" &&
 	two=`git ls-files -s dir/two` &&
 	two=`expr "$two" : "[0-7]* \\([0-9a-f]*\\)"` &&
 	echo "$two" &&
 	git cat-file -p "$two" >actual &&
 	cmp dir/two actual &&
-	cd dir &&
-	git cat-file -p "$two" >actual &&
-	cmp two actual
+	(
+		cd dir &&
+		git cat-file -p "$two" >actual &&
+		cmp two actual
+	)
 '
 rm -f actual dir/actual

 test_expect_success 'diff-files' '
-	cd "$HERE" &&
 	echo a >>one &&
 	echo d >>dir/two &&
 	case "`git diff-files --name-only`" in
@@ -62,77 +60,88 @@ test_expect_success 'diff-files' '
 	*) echo bad top; exit 1 ;;
 	esac &&
 	# diff should not omit leading paths
-	cd dir &&
-	case "`git diff-files --name-only`" in
-	dir/two"$LF"one) echo pass subdir ;;
-	*) echo bad subdir; exit 1 ;;
-	esac &&
-	case "`git diff-files --name-only .`" in
-	dir/two) echo pass subdir limited ;;
-	*) echo bad subdir limited; exit 1 ;;
-	esac
+	(
+		cd dir &&
+		case "`git diff-files --name-only`" in
+		dir/two"$LF"one) echo pass subdir ;;
+		*) echo bad subdir; exit 1 ;;
+		esac &&
+		case "`git diff-files --name-only .`" in
+		dir/two) echo pass subdir limited ;;
+		*) echo bad subdir limited; exit 1 ;;
+		esac
+	)
 '

 test_expect_success 'write-tree' '
-	cd "$HERE" &&
 	top=`git write-tree` &&
 	echo $top &&
-	cd dir &&
-	sub=`git write-tree` &&
-	echo $sub &&
-	test "z$top" = "z$sub"
+	(
+		cd dir &&
+		sub=`git write-tree` &&
+		echo $sub &&
+		test "z$top" = "z$sub"
+	)
 '

 test_expect_success 'checkout-index' '
-	cd "$HERE" &&
 	git checkout-index -f -u one &&
 	cmp one original.one &&
-	cd dir &&
-	git checkout-index -f -u two &&
-	cmp two ../original.two
+	(
+		cd dir &&
+		git checkout-index -f -u two &&
+		cmp two ../original.two
+	)
 '

 test_expect_success 'read-tree' '
-	cd "$HERE" &&
 	rm -f one dir/two &&
 	tree=`git write-tree` &&
 	git read-tree --reset -u "$tree" &&
 	cmp one original.one &&
 	cmp dir/two original.two &&
-	cd dir &&
-	rm -f two &&
-	git read-tree --reset -u "$tree" &&
-	cmp two ../original.two &&
-	cmp ../one ../original.one
+	(
+		cd dir &&
+		rm -f two &&
+		git read-tree --reset -u "$tree" &&
+		cmp two ../original.two &&
+		cmp ../one ../original.one
+	)
 '

 test_expect_success 'no file/rev ambiguity check inside .git' '
-	cd "$HERE" &&
 	git commit -a -m 1 &&
-	cd "$HERE"/.git &&
-	git show -s HEAD
+	(
+		cd .git &&
+		git show -s HEAD
+	)
 '

 test_expect_success 'no file/rev ambiguity check inside a bare repo' '
-	cd "$HERE" &&
 	git clone -s --bare .git foo.git &&
-	cd foo.git && GIT_DIR=. git show -s HEAD
+	(
+		cd foo.git &&
+		GIT_DIR=. git show -s HEAD
+	)
 '

 # This still does not work as it should...
 : test_expect_success 'no file/rev ambiguity check inside a bare repo' '
-	cd "$HERE" &&
 	git clone -s --bare .git foo.git &&
-	cd foo.git && git show -s HEAD
+	(
+		cd foo.git &&
+		git show -s HEAD
+	)
 '

 test_expect_success SYMLINKS 'detection should not be fooled by a symlink' '
-	cd "$HERE" &&
 	rm -fr foo.git &&
 	git clone -s .git another &&
 	ln -s another yetanother &&
-	cd yetanother/.git &&
-	git show -s HEAD
+	(
+		cd yetanother/.git &&
+		git show -s HEAD
+	)
 '

 test_done
-- 
1.7.3.rc0.182.g5433f
Previous: Jonathan Nieder
Message 27 of 27 in “Several tests: cd inside subshell instead of around”
  1. Several tests: cd inside subshell instead of aroundJens Lehmann, Sep 6, 2010
  2. Jonathan NiederSep 6, 2010
  3. Jens LehmannSep 6, 2010
  4. 0/7 Re: [PATCH] Several tests: cd inside subshell instead of aroundJonathan Nieder, Sep 7, 2010
  5. 1/7 tests: subshell indentation stylefixJonathan Nieder, Sep 7, 2010
  6. Jonathan NiederSep 7, 2010
  7. 2/7 t1450 (fsck): remove dangling objectsJonathan Nieder, Sep 7, 2010
  8. 3/7 t2105 (gitfile): add missing &&Jonathan Nieder, Sep 7, 2010
  9. Brad KingSep 7, 2010
  10. 4/7 t0004 (unwritable files): simplify error handlingJonathan Nieder, Sep 7, 2010
  11. 5/7 t1302 (core.repositoryversion): style tweaksJonathan Nieder, Sep 7, 2010
  12. Nguyen Thai Ngoc DuySep 7, 2010
  13. 6/7 t1303 (config): style tweaksJonathan Nieder, Sep 7, 2010
  14. Jeff KingSep 7, 2010
  15. Junio C HamanoSep 7, 2010
  16. Jonathan NiederSep 7, 2010
  17. guarding everything with test_expect_success (Re: [PATCH 6/7] t1303 (config): style tweaks)Jonathan Nieder, Sep 7, 2010
  18. Jeff KingSep 7, 2010
  19. Jonathan NiederSep 7, 2010
  20. 7/7 t2016 (checkout -p): use printf for multiline y/n inputJonathan Nieder, Sep 7, 2010
  21. Thomas RastSep 7, 2010
  22. Jonathan NiederSep 7, 2010
  23. Junio C HamanoSep 6, 2010
  24. Jonathan NiederSep 7, 2010
  25. Junio C HamanoSep 7, 2010
  26. Jonathan NiederSep 7, 2010
  27. t1020: Get rid of 'cd "$HERE"' at the start of each testJens Lehmann, Sep 7, 2010

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.