Skip to contentSkip to conversation

test-lib: use subshell instead of cd $new && .. && cd $old

PATCH In review Ævar Arnfjörð Bjarmason 3 participants 6 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 test-lib: use subshell instead of cd $new && .. && cd $old

Change the test_create_repo code added in v1.2.2~6 to use a subshell instead of keeping track of the old working directory and cd-ing back when it's done.

---
 t/test-lib.sh |    6 ++----
 1 files changed, 2 insertions(+), 4 deletions(-)
t/test-lib.sh +2 -4
index dc934b8..778ba8d 100644
@@ -687,14 +687,12 @@ test_when_finished () {
 test_create_repo () {
 	test "$#" = 1 ||
 	error "bug in the test script: not 1 parameter to test-create-repo"
-	owd=`pwd`
 	repo="$1"
 	mkdir -p "$repo"
-	cd "$repo" || error "Cannot setup test environment"
+	(cd "$repo" || error "Cannot setup test environment"
 	"$GIT_EXEC_PATH/git-init" "--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 ||
 	error "cannot run git init -- have you built things yet?"
-	mv .git/hooks .git/hooks-disabled
-	cd "$owd"
+	mv .git/hooks .git/hooks-disabled)
 }
 
 test_done () {
Signature · 1 line
1.7.2.2.518.gf0ba8
#1 lore raw
Quoted 5 lines · Ævar Arnfjörð Bjarmason

+++ b/t/test-lib.sh

t/test-lib.sh +0 -0
@@ -687,14 +687,12 @@ test_when_finished () {
 test_create_repo () {
 	test "$#" = 1 ||
 	error "bug in the test script: not 1 parameter to test-create-repo"

t/test-lib.sh test_when_finished () { · line 695

Quoted 10 lines
t/test-lib.sh +2 -4
-	owd=`pwd`
 	repo="$1"
 	mkdir -p "$repo"
-	cd "$repo" || error "Cannot setup test environment"
+	(cd "$repo" || error "Cannot setup test environment"
 	"$GIT_EXEC_PATH/git-init" "--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 ||
 	error "cannot run git init -- have you built things yet?"
-	mv .git/hooks .git/hooks-disabled
-	cd "$owd"
+	mv .git/hooks .git/hooks-disabled)

Style: why not use

	(
		cd "$repo" ...
		... .git/hooks-disabled
	)

?

#2 lore raw
Quoted 20 lines · Jonathan Nieder
Quoted 7 lines · Ævar Arnfjörð Bjarmason

+++ b/t/test-lib.sh

t/test-lib.sh +0 -0
@@ -687,14 +687,12 @@ test_when_finished () {
  test_create_repo () {

      test "$#" = 1 ||
      error "bug in the test script: not 1 parameter to test-create-repo"
-     owd=pwd
      repo="$1"

Quoted 8 lines

      mkdir -p "$repo"
-     cd "$repo" || error "Cannot setup test environment"
+     (cd "$repo" || error "Cannot setup test environment" "$GIT_EXEC_PATH/git-init" "--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 || error "cannot run git init -- have you built things yet?"
-     mv .git/hooks .git/hooks-disabled
-     cd "$owd"
+     mv .git/hooks .git/hooks-disabled)

Style: why not use

Quoted 5 lines

       (
               cd "$repo" ...
               ... .git/hooks-disabled
       )

I've seen both used and I don't know which is preferred.

#3 lore raw
Quoted 13 lines · Ævar Arnfjörð Bjarmason
Quoted 12 lines · Jonathan Nieder
Quoted 7 lines · Ævar Arnfjörð Bjarmason

-     cd "$repo" || error "Cannot setup test environment"
+     (cd "$repo" || error "Cannot setup test environment" "$GIT_EXEC_PATH/git-init" "--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 || error "cannot run git init -- have you built things yet?"
-     mv .git/hooks .git/hooks-disabled
-     cd "$owd"
+     mv .git/hooks .git/hooks-disabled)

Style: why not use

Quoted 7 lines
Quoted 5 lines

       (
               cd "$repo" ...
               ... .git/hooks-disabled
       )

I've seen both used and I don't know which is preferred.

Okay. I maintain that the latter is way more readable. Is there any advantage to the former?

Whichever the project chooses can be enshrined in
Documentation/CodingGuidelines, of course.

#4 lore raw
In reply to Jonathan Nieder
Quoted 20 lines · Jonathan Nieder
Quoted 5 lines · Ævar Arnfjörð Bjarmason

+++ b/t/test-lib.sh

t/test-lib.sh +0 -0
@@ -687,14 +687,12 @@ test_when_finished () {
 test_create_repo () {
 	test "$#" = 1 ||
 	error "bug in the test script: not 1 parameter to test-create-repo"

t/test-lib.sh test_when_finished () { · line 695

Quoted 10 lines
t/test-lib.sh +2 -4
-	owd=`pwd`
 	repo="$1"
 	mkdir -p "$repo"
-	cd "$repo" || error "Cannot setup test environment"
+	(cd "$repo" || error "Cannot setup test environment"
 	"$GIT_EXEC_PATH/git-init" "--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 ||
 	error "cannot run git init -- have you built things yet?"
-	mv .git/hooks .git/hooks-disabled
-	cd "$owd"
+	mv .git/hooks .git/hooks-disabled)

Style: why not use

Quoted 4 lines
	(
		cd "$repo" ...
		... .git/hooks-disabled
	)

That is a sensible suggestion, but what does "error" do?

I think you would need || exit at the end, i.e.

	(
        	... || error "..."
	) || exit

or something.

#5 lore raw
Quoted 29 lines · Junio C Hamano
Quoted 20 lines · Jonathan Nieder
Quoted 7 lines · Ævar Arnfjörð Bjarmason

+++ b/t/test-lib.sh

t/test-lib.sh +0 -0
@@ -687,14 +687,12 @@ test_when_finished () {
  test_create_repo () {

     test "$#" = 1 ||
     error "bug in the test script: not 1 parameter to test-create-repo"
-    owd=pwd
     repo="$1"

Quoted 8 lines

     mkdir -p "$repo"
-    cd "$repo" || error "Cannot setup test environment"
+    (cd "$repo" || error "Cannot setup test environment" "$GIT_EXEC_PATH/git-init" "--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 || error "cannot run git init -- have you built things yet?"
-    mv .git/hooks .git/hooks-disabled
-    cd "$owd"
+    mv .git/hooks .git/hooks-disabled)

Style: why not use

Quoted 4 lines

      (
              cd "$repo" ...
              ... .git/hooks-disabled
      )

That is a sensible suggestion, but what does "error" do?

Quoted 7 lines

I think you would need || exit at the end, i.e.

       (
               ... || error "..."
       ) || exit

or something.

Good catch, yes. error() calls exit so this patch as-is introduces a logic error.