[PATCH 0/6] modernize t1500

STALE3738d

Revision v1 of 3 in this series.

14 messages, 3 authors, 2016-06-16 · open the first message on its own page

[PATCH 0/6] modernize t1500

From: Eric Sunshine <hidden>
Date: 2016-06-16 02:19:16

This series modernizes t1500; it takes an entirely different approach
than [1][2] and is intended to replace that series. Whereas [1][2]
dropped the systematic function-driven testing of git-rev-parse in favor
of dozens of nearly identical copy/paste tests, this series retains the
structure of the existing script and instead updates the tests to set up
their own needed state rather than relying upon transient and fragile
manipulation of global state.

Due to its systematic function-driven approach, the original script is
small at 87 lines, and easily understood. When [1] dropped the
systematic approach and replaced it with individual copy/paste tests,
the script ballooned to a whopping 573 lines; its v2 successor, while
smaller, still inflated it to 322 lines. Writing that amount of code
correctly (even when primarily copy/paste) is difficult; reviewing it
for correctness is tedious, mind-numbing, and error-prone, as evidenced
by [3].

This series, on the other hand, values the concision of the original and
only makes changes necessary to modernize it; the script size remains
about the same; it drops from 87 to 83 lines.

[1]: http://thread.gmane.org/gmane.comp.version-control.git/291087/focus=291088
[2]: http://thread.gmane.org/gmane.comp.version-control.git/291729/focus=291731
[3]: http://thread.gmane.org/gmane.comp.version-control.git/291729/focus=291745

Eric Sunshine (6):
  t1500: test_rev_parse: facilitate future test enhancements
  t1500: reduce dependence upon global state
  t1500: avoid changing working directory outside of tests
  t1500: avoid setting configuration options outside of tests
  t1500: avoid setting environment variables outside of tests
  t1500: be considerate to future potential tests

 t/t1500-rev-parse.sh | 116 +++++++++++++++++++++++++--------------------------
 1 file changed, 56 insertions(+), 60 deletions(-)

-- 
2.8.2.530.g51d527d

[PATCH 4/6] t1500: avoid setting configuration options outside of tests

From: Eric Sunshine <hidden>
Date: 2016-06-16 02:19:16

Ideally, each test should be responsible for setting up state it needs
rather than relying upon transient global state. Toward this end, teach
test_rev_parse() to accept a "-b <value>" option to allow callers to set
"core.bare" explicitly or undefine it, and take advantage of this new
option to avoid setting "core.bare" outside of tests.

Under the hood, "-b <value>" invokes "test_config -C <dir>" (or
"test_unconfig -C <dir>"), which means that git-config knows explicitly
where to find its configuration file. Consequently, the global
GIT_CONFIG environment variable, which was needed by the manual
git-config invocations outside of tests, is no longer needed, and is
thus dropped.

Signed-off-by: Eric Sunshine <redacted>
---
 t/t1500-rev-parse.sh | 39 ++++++++++++++++++++-------------------
 1 file changed, 20 insertions(+), 19 deletions(-)
diff --git a/t/t1500-rev-parse.sh b/t/t1500-rev-parse.sh
index f294ecc..c058aa4 100755
--- a/t/t1500-rev-parse.sh
+++ b/t/t1500-rev-parse.sh
@@ -6,15 +6,25 @@ test_description='test git rev-parse'
 # usage: [options] label is-bare is-inside-git is-inside-work prefix git-dir
 test_rev_parse () {
 	dir=
+	bare=
 	while :
 	do
 		case "$1" in
 		-C) dir="-C $2"; shift; shift ;;
+		-b) bare="$2"; shift; shift ;;
 		-*) error "test_rev_parse: unrecognized option '$1'" ;;
 		*) break ;;
 		esac
 	done
 
+	case "$bare" in
+	'') ;;
+	t*) bare="test_config $dir core.bare true" ;;
+	f*) bare="test_config $dir core.bare false" ;;
+	u*) bare="test_unconfig $dir core.bare" ;;
+	*) error "test_rev_parse: unrecognized core.bare value '$bare'"
+	esac
+
 	name=$1
 	shift
 
@@ -26,6 +36,7 @@ test_rev_parse () {
 	do
 		expect="$1"
 		test_expect_success "$name: $o" '
+			$bare &&
 			echo "$expect" >expect &&
 			git $dir rev-parse --$o >actual &&
 			test_cmp expect actual
@@ -45,37 +56,27 @@ test_rev_parse -C .git/objects .git/objects/ false true false '' "$ROOT/.git"
 test_expect_success 'setup untracked sub/dir' 'mkdir -p sub/dir'
 test_rev_parse -C sub/dir subdirectory false false true sub/dir/ "$ROOT/.git"
 
-git config core.bare true
-test_rev_parse 'core.bare = true' true false false
+test_rev_parse -b t 'core.bare = true' true false false
 
-git config --unset core.bare
-test_rev_parse 'core.bare undefined' false false true
+test_rev_parse -b u 'core.bare undefined' false false true
 
 test_expect_success 'setup non-local database ../.git' 'mkdir work'
 GIT_DIR=../.git
-GIT_CONFIG="$ROOT/work/../.git/config"
-export GIT_DIR GIT_CONFIG
+export GIT_DIR
 
-git config core.bare false
-test_rev_parse -C work 'GIT_DIR=../.git, core.bare = false' false false true ''
+test_rev_parse -C work -b f 'GIT_DIR=../.git, core.bare = false' false false true ''
 
-git config core.bare true
-test_rev_parse -C work 'GIT_DIR=../.git, core.bare = true' true false false ''
+test_rev_parse -C work -b t 'GIT_DIR=../.git, core.bare = true' true false false ''
 
-git config --unset core.bare
-test_rev_parse -C work 'GIT_DIR=../.git, core.bare undefined' false false true ''
+test_rev_parse -C work -b u 'GIT_DIR=../.git, core.bare undefined' false false true ''
 
 test_expect_success 'setup non-local database ../repo.git' 'mv .git repo.git'
 GIT_DIR=../repo.git
-GIT_CONFIG="$ROOT/work/../repo.git/config"
 
-git config core.bare false
-test_rev_parse -C work 'GIT_DIR=../repo.git, core.bare = false' false false true ''
+test_rev_parse -C work -b f 'GIT_DIR=../repo.git, core.bare = false' false false true ''
 
-git config core.bare true
-test_rev_parse -C work 'GIT_DIR=../repo.git, core.bare = true' true false false ''
+test_rev_parse -C work -b t 'GIT_DIR=../repo.git, core.bare = true' true false false ''
 
-git config --unset core.bare
-test_rev_parse -C work 'GIT_DIR=../repo.git, core.bare undefined' false false true ''
+test_rev_parse -C work -b u 'GIT_DIR=../repo.git, core.bare undefined' false false true ''
 
 test_done
-- 
2.8.2.530.g51d527d

[PATCH 5/6] t1500: avoid setting environment variables outside of tests

From: Eric Sunshine <hidden>
Date: 2016-06-16 02:19:16

Ideally, each test should be responsible for setting up state it needs
rather than relying upon transient global state. Toward this end, teach
test_rev_parse() to accept a "-g <dir>" option to allow callers to
specify the value of the GIT_DIR environment variable explicitly, and
take advantage of this new option to avoid polluting the global scope
with GIT_DIR assignments.

Regarding the implementation: Typically, tests avoid polluting the
global state by wrapping transient environment variable assignments
within a subshell, however, this technique can not be used here since
test_config() and test_unconfig() need to know GIT_DIR, as well, but
neither function can be used within a subshell. Consequently, GIT_DIR is
cleared manually via sane_unset().

Signed-off-by: Eric Sunshine <redacted>
---
 t/t1500-rev-parse.sh | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
diff --git a/t/t1500-rev-parse.sh b/t/t1500-rev-parse.sh
index c058aa4..525e6d3 100755
--- a/t/t1500-rev-parse.sh
+++ b/t/t1500-rev-parse.sh
@@ -7,11 +7,13 @@ test_description='test git rev-parse'
 test_rev_parse () {
 	dir=
 	bare=
+	env=
 	while :
 	do
 		case "$1" in
 		-C) dir="-C $2"; shift; shift ;;
 		-b) bare="$2"; shift; shift ;;
+		-g) env="GIT_DIR=$2; export GIT_DIR"; shift; shift ;;
 		-*) error "test_rev_parse: unrecognized option '$1'" ;;
 		*) break ;;
 		esac
@@ -36,6 +38,8 @@ test_rev_parse () {
 	do
 		expect="$1"
 		test_expect_success "$name: $o" '
+			test_when_finished "sane_unset GIT_DIR" &&
+			eval $env &&
 			$bare &&
 			echo "$expect" >expect &&
 			git $dir rev-parse --$o >actual &&
@@ -61,22 +65,19 @@ test_rev_parse -b t 'core.bare = true' true false false
 test_rev_parse -b u 'core.bare undefined' false false true
 
 test_expect_success 'setup non-local database ../.git' 'mkdir work'
-GIT_DIR=../.git
-export GIT_DIR
 
-test_rev_parse -C work -b f 'GIT_DIR=../.git, core.bare = false' false false true ''
+test_rev_parse -C work -g ../.git -b f 'GIT_DIR=../.git, core.bare = false' false false true ''
 
-test_rev_parse -C work -b t 'GIT_DIR=../.git, core.bare = true' true false false ''
+test_rev_parse -C work -g ../.git -b t 'GIT_DIR=../.git, core.bare = true' true false false ''
 
-test_rev_parse -C work -b u 'GIT_DIR=../.git, core.bare undefined' false false true ''
+test_rev_parse -C work -g ../.git -b u 'GIT_DIR=../.git, core.bare undefined' false false true ''
 
 test_expect_success 'setup non-local database ../repo.git' 'mv .git repo.git'
-GIT_DIR=../repo.git
 
-test_rev_parse -C work -b f 'GIT_DIR=../repo.git, core.bare = false' false false true ''
+test_rev_parse -C work -g ../repo.git -b f 'GIT_DIR=../repo.git, core.bare = false' false false true ''
 
-test_rev_parse -C work -b t 'GIT_DIR=../repo.git, core.bare = true' true false false ''
+test_rev_parse -C work -g ../repo.git -b t 'GIT_DIR=../repo.git, core.bare = true' true false false ''
 
-test_rev_parse -C work -b u 'GIT_DIR=../repo.git, core.bare undefined' false false true ''
+test_rev_parse -C work -g ../repo.git -b u 'GIT_DIR=../repo.git, core.bare undefined' false false true ''
 
 test_done
-- 
2.8.2.530.g51d527d

[PATCH 3/6] t1500: avoid changing working directory outside of tests

From: Eric Sunshine <hidden>
Date: 2016-06-16 02:19:16

Ideally, each test should be responsible for setting up state it needs
rather than relying upon transient global state. Toward this end, teach
test_rev_parse() to accept a "-C <dir>" option to allow callers to
instruct it explicitly in which directory its tests should be run, and
take advantage of this new option to avoid changing the working
directory outside of tests.

Signed-off-by: Eric Sunshine <redacted>
---
 t/t1500-rev-parse.sh | 44 ++++++++++++++++++++++++--------------------
 1 file changed, 24 insertions(+), 20 deletions(-)
diff --git a/t/t1500-rev-parse.sh b/t/t1500-rev-parse.sh
index 3d79568..f294ecc 100755
--- a/t/t1500-rev-parse.sh
+++ b/t/t1500-rev-parse.sh
@@ -3,8 +3,18 @@
 test_description='test git rev-parse'
 . ./test-lib.sh
 
-# usage: label is-bare is-inside-git is-inside-work prefix git-dir
+# usage: [options] label is-bare is-inside-git is-inside-work prefix git-dir
 test_rev_parse () {
+	dir=
+	while :
+	do
+		case "$1" in
+		-C) dir="-C $2"; shift; shift ;;
+		-*) error "test_rev_parse: unrecognized option '$1'" ;;
+		*) break ;;
+		esac
+	done
+
 	name=$1
 	shift
 
@@ -17,7 +27,7 @@ test_rev_parse () {
 		expect="$1"
 		test_expect_success "$name: $o" '
 			echo "$expect" >expect &&
-			git rev-parse --$o >actual &&
+			git $dir rev-parse --$o >actual &&
 			test_cmp expect actual
 		'
 		shift
@@ -29,16 +39,11 @@ ROOT=$(pwd)
 
 test_rev_parse toplevel false false true '' .git
 
-cd .git || exit 1
-test_rev_parse .git/ false true false '' .
-cd objects || exit 1
-test_rev_parse .git/objects/ false true false '' "$ROOT/.git"
-cd ../.. || exit 1
+test_rev_parse -C .git .git/ false true false '' .
+test_rev_parse -C .git/objects .git/objects/ false true false '' "$ROOT/.git"
 
-mkdir -p sub/dir || exit 1
-cd sub/dir || exit 1
-test_rev_parse subdirectory false false true sub/dir/ "$ROOT/.git"
-cd ../.. || exit 1
+test_expect_success 'setup untracked sub/dir' 'mkdir -p sub/dir'
+test_rev_parse -C sub/dir subdirectory false false true sub/dir/ "$ROOT/.git"
 
 git config core.bare true
 test_rev_parse 'core.bare = true' true false false
@@ -46,32 +51,31 @@ test_rev_parse 'core.bare = true' true false false
 git config --unset core.bare
 test_rev_parse 'core.bare undefined' false false true
 
-mkdir work || exit 1
-cd work || exit 1
+test_expect_success 'setup non-local database ../.git' 'mkdir work'
 GIT_DIR=../.git
 GIT_CONFIG="$ROOT/work/../.git/config"
 export GIT_DIR GIT_CONFIG
 
 git config core.bare false
-test_rev_parse 'GIT_DIR=../.git, core.bare = false' false false true ''
+test_rev_parse -C work 'GIT_DIR=../.git, core.bare = false' false false true ''
 
 git config core.bare true
-test_rev_parse 'GIT_DIR=../.git, core.bare = true' true false false ''
+test_rev_parse -C work 'GIT_DIR=../.git, core.bare = true' true false false ''
 
 git config --unset core.bare
-test_rev_parse 'GIT_DIR=../.git, core.bare undefined' false false true ''
+test_rev_parse -C work 'GIT_DIR=../.git, core.bare undefined' false false true ''
 
-mv ../.git ../repo.git || exit 1
+test_expect_success 'setup non-local database ../repo.git' 'mv .git repo.git'
 GIT_DIR=../repo.git
 GIT_CONFIG="$ROOT/work/../repo.git/config"
 
 git config core.bare false
-test_rev_parse 'GIT_DIR=../repo.git, core.bare = false' false false true ''
+test_rev_parse -C work 'GIT_DIR=../repo.git, core.bare = false' false false true ''
 
 git config core.bare true
-test_rev_parse 'GIT_DIR=../repo.git, core.bare = true' true false false ''
+test_rev_parse -C work 'GIT_DIR=../repo.git, core.bare = true' true false false ''
 
 git config --unset core.bare
-test_rev_parse 'GIT_DIR=../repo.git, core.bare undefined' false false true ''
+test_rev_parse -C work 'GIT_DIR=../repo.git, core.bare undefined' false false true ''
 
 test_done
-- 
2.8.2.530.g51d527d

[PATCH 6/6] t1500: be considerate to future potential tests

From: Eric Sunshine <hidden>
Date: 2016-06-16 02:19:16

The final batch of git-rev-parse tests work against a non-local object
database named ../repo.git rather than the typically-named ../.git. It
prepares by renaming .git/ to repo.git/ and pointing GIT_DIR at
../repo.git, but never restores the name to .git/, which can be
problematic for tests added in the future. Be more friendly by instead
making repo.git/ a copy of .git/.

Signed-off-by: Eric Sunshine <redacted>
---
 t/t1500-rev-parse.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/t/t1500-rev-parse.sh b/t/t1500-rev-parse.sh
index 525e6d3..af223ed 100755
--- a/t/t1500-rev-parse.sh
+++ b/t/t1500-rev-parse.sh
@@ -72,7 +72,7 @@ test_rev_parse -C work -g ../.git -b t 'GIT_DIR=../.git, core.bare = true' true
 
 test_rev_parse -C work -g ../.git -b u 'GIT_DIR=../.git, core.bare undefined' false false true ''
 
-test_expect_success 'setup non-local database ../repo.git' 'mv .git repo.git'
+test_expect_success 'setup non-local database ../repo.git' 'cp -R .git repo.git'
 
 test_rev_parse -C work -g ../repo.git -b f 'GIT_DIR=../repo.git, core.bare = false' false false true ''
 
-- 
2.8.2.530.g51d527d

[PATCH 1/6] t1500: test_rev_parse: facilitate future test enhancements

From: Eric Sunshine <hidden>
Date: 2016-06-16 02:19:16

Tests run by test_rev_parse() are nearly identical; each invokes
git-rev-parse with a single option and compares the result against an
expected value. Such duplication makes it onerous to extend the tests
since any change needs to be repeated in each test. Reduce the
duplication by parameterizing the test and driving it via a for-loop.

Signed-off-by: Eric Sunshine <redacted>
---
 t/t1500-rev-parse.sh | 44 +++++++++++++++++---------------------------
 1 file changed, 17 insertions(+), 27 deletions(-)
diff --git a/t/t1500-rev-parse.sh b/t/t1500-rev-parse.sh
index 48ee077..03f22fe 100755
--- a/t/t1500-rev-parse.sh
+++ b/t/t1500-rev-parse.sh
@@ -3,38 +3,28 @@
 test_description='test git rev-parse'
 . ./test-lib.sh
 
-test_rev_parse() {
+# usage: label is-bare is-inside-git is-inside-work prefix git-dir
+test_rev_parse () {
 	name=$1
 	shift
 
-	test_expect_success "$name: is-bare-repository" \
-	"test '$1' = \"\$(git rev-parse --is-bare-repository)\""
-	shift
-	[ $# -eq 0 ] && return
-
-	test_expect_success "$name: is-inside-git-dir" \
-	"test '$1' = \"\$(git rev-parse --is-inside-git-dir)\""
-	shift
-	[ $# -eq 0 ] && return
-
-	test_expect_success "$name: is-inside-work-tree" \
-	"test '$1' = \"\$(git rev-parse --is-inside-work-tree)\""
-	shift
-	[ $# -eq 0 ] && return
-
-	test_expect_success "$name: prefix" \
-	"test '$1' = \"\$(git rev-parse --show-prefix)\""
-	shift
-	[ $# -eq 0 ] && return
-
-	test_expect_success "$name: git-dir" \
-	"test '$1' = \"\$(git rev-parse --git-dir)\""
-	shift
-	[ $# -eq 0 ] && return
+	for o in is-bare-repository \
+		 is-inside-git-dir \
+		 is-inside-work-tree \
+		 show-prefix \
+		 git-dir
+	do
+		expect="$1"
+		test_expect_success "$name: $o" '
+			echo "$expect" >expect &&
+			git rev-parse --$o >actual &&
+			test_cmp expect actual
+		'
+		shift
+		test $# -eq 0 && break
+	done
 }
 
-# label is-bare is-inside-git is-inside-work prefix git-dir
-
 ROOT=$(pwd)
 
 test_rev_parse toplevel false false true '' .git
-- 
2.8.2.530.g51d527d

[PATCH 2/6] t1500: reduce dependence upon global state

From: Eric Sunshine <hidden>
Date: 2016-06-16 02:19:16

This script pollutes its global state by changing its working directory
and capturing that state via $(pwd) in the GIT_CONFIG environment
variable. This makes it difficult to modernize the script since tests
ideally should be self-contained and not rely upon such transient global
state.

With the ultimate goal of eliminating working directory changes, as a
first step, avoid capturing global state by instead taking advantage of
$ROOT which captured $(pwd) prior to any working directory changes, and
compose GIT_CONFIG from it and local knowledge of the working directory.

Subsequent patches will eliminate changes of working directory and drop
GIT_CONFIG altogether.

Signed-off-by: Eric Sunshine <redacted>
---
 t/t1500-rev-parse.sh | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/t/t1500-rev-parse.sh b/t/t1500-rev-parse.sh
index 03f22fe..3d79568 100755
--- a/t/t1500-rev-parse.sh
+++ b/t/t1500-rev-parse.sh
@@ -49,7 +49,7 @@ test_rev_parse 'core.bare undefined' false false true
 mkdir work || exit 1
 cd work || exit 1
 GIT_DIR=../.git
-GIT_CONFIG="$(pwd)"/../.git/config
+GIT_CONFIG="$ROOT/work/../.git/config"
 export GIT_DIR GIT_CONFIG
 
 git config core.bare false
@@ -63,7 +63,7 @@ test_rev_parse 'GIT_DIR=../.git, core.bare undefined' false false true ''
 
 mv ../.git ../repo.git || exit 1
 GIT_DIR=../repo.git
-GIT_CONFIG="$(pwd)"/../repo.git/config
+GIT_CONFIG="$ROOT/work/../repo.git/config"
 
 git config core.bare false
 test_rev_parse 'GIT_DIR=../repo.git, core.bare = false' false false true ''
-- 
2.8.2.530.g51d527d

Re: [PATCH 4/6] t1500: avoid setting configuration options outside of tests

From: Eric Sunshine <hidden>
Date: 2016-06-16 02:19:17

On Tue, May 10, 2016 at 1:20 AM, Eric Sunshine [off-list ref] wrote:
quoted hunk
Ideally, each test should be responsible for setting up state it needs
rather than relying upon transient global state. Toward this end, teach
test_rev_parse() to accept a "-b <value>" option to allow callers to set
"core.bare" explicitly or undefine it, and take advantage of this new
option to avoid setting "core.bare" outside of tests.
[...snip...]

Signed-off-by: Eric Sunshine <redacted>
---
diff --git a/t/t1500-rev-parse.sh b/t/t1500-rev-parse.sh
@@ -6,15 +6,25 @@ test_description='test git rev-parse'
+       case "$bare" in
+       '') ;;
+       t*) bare="test_config $dir core.bare true" ;;
+       f*) bare="test_config $dir core.bare false" ;;
+       u*) bare="test_unconfig $dir core.bare" ;;
+       *) error "test_rev_parse: unrecognized core.bare value '$bare'"
Oops, this line lost its ;; at some point while refining the code.
+       esac
+

Re: [PATCH 5/6] t1500: avoid setting environment variables outside of tests

From: Jeff King <hidden>
Date: 2016-06-16 02:19:17

On Tue, May 10, 2016 at 01:20:54AM -0400, Eric Sunshine wrote:
quoted hunk
diff --git a/t/t1500-rev-parse.sh b/t/t1500-rev-parse.sh
index c058aa4..525e6d3 100755
--- a/t/t1500-rev-parse.sh
+++ b/t/t1500-rev-parse.sh
@@ -7,11 +7,13 @@ test_description='test git rev-parse'
 test_rev_parse () {
 	dir=
 	bare=
+	env=
 	while :
 	do
 		case "$1" in
 		-C) dir="-C $2"; shift; shift ;;
 		-b) bare="$2"; shift; shift ;;
+		-g) env="GIT_DIR=$2; export GIT_DIR"; shift; shift ;;
This will expand $2 inside $env, which is later eval'd. So funny things
happen if there are spaces or metacharacters. It looks like you only use
it with short relative paths ("../repo.git", etc), which is OK, but this
would probably break badly if we ever used absolute paths.

I don't know if it's worth worrying about or not. The usual solution is
something like:

  env_git_dir=$2
  env='GIT_DIR=$env_git_dir; export GIT_DIR'
  ...
  eval "$env"
quoted hunk
@@ -36,6 +38,8 @@ test_rev_parse () {
 	do
 		expect="$1"
 		test_expect_success "$name: $o" '
+			test_when_finished "sane_unset GIT_DIR" &&
+			eval $env &&
I was surprised not to see quoting around $env here, but it probably
doesn't matter (I think it may affect how some whitespace is treated,
but the contents of $env are pretty tame).

This will set up the sane_unset regardless of whether $env does
anything. Would it make more sense to stick the test_when_finished
inside $env? You could use regular unset then, too, since you know the
variable would be set.

-Peff

Re: [PATCH 5/6] t1500: avoid setting environment variables outside of tests

From: Eric Sunshine <hidden>
Date: 2016-06-16 02:19:17

On Tue, May 10, 2016 at 2:39 PM, Jeff King [off-list ref] wrote:
On Tue, May 10, 2016 at 01:20:54AM -0400, Eric Sunshine wrote:
quoted
diff --git a/t/t1500-rev-parse.sh b/t/t1500-rev-parse.sh
@@ -7,11 +7,13 @@ test_description='test git rev-parse'
      while :
      do
              case "$1" in
              -C) dir="-C $2"; shift; shift ;;
              -b) bare="$2"; shift; shift ;;
+             -g) env="GIT_DIR=$2; export GIT_DIR"; shift; shift ;;
This will expand $2 inside $env, which is later eval'd. So funny things
happen if there are spaces or metacharacters. It looks like you only use
it with short relative paths ("../repo.git", etc), which is OK, but this
would probably break badly if we ever used absolute paths.

I don't know if it's worth worrying about or not. The usual solution is
something like:

  env_git_dir=$2
  env='GIT_DIR=$env_git_dir; export GIT_DIR'
  ...
  eval "$env"
Makes sense; I wasn't quite happy with having $2 interpolated
unquoted. Like you, though, I don't know if it's worth worrying
about...
quoted
@@ -36,6 +38,8 @@ test_rev_parse () {
      do
              expect="$1"
              test_expect_success "$name: $o" '
+                     test_when_finished "sane_unset GIT_DIR" &&
+                     eval $env &&
I was surprised not to see quoting around $env here, but it probably
doesn't matter (I think it may affect how some whitespace is treated,
but the contents of $env are pretty tame).
I flip-flopped on this one several times, quoting, and not quoting.
Documentation for 'eval' says:

    The args are read and concatenated together into a single
    command.

so, I ultimately left it unquoted, but don't feel strongly about it.
This will set up the sane_unset regardless of whether $env does
anything. Would it make more sense to stick the test_when_finished
inside $env? You could use regular unset then, too, since you know the
variable would be set.
I didn't worry about it too much because the end result is effectively
the same and, with all the 'case' arms being short one-liners, I think
the code is a bit easier to read as-is; bundling 'test_when_finished'
into the 'env' assignment line would probably require wrapping the
line. I do like the improved encapsulation of your suggestion but
don't otherwise feel strongly about it.

Nevertheless, I can re-roll with these changes if you feel more
strongly than I about it.

Re: [PATCH 5/6] t1500: avoid setting environment variables outside of tests

From: Eric Sunshine <hidden>
Date: 2016-06-16 02:19:17

On Tue, May 10, 2016 at 3:12 PM, Eric Sunshine [off-list ref] wrote:
On Tue, May 10, 2016 at 2:39 PM, Jeff King [off-list ref] wrote:
quoted
On Tue, May 10, 2016 at 01:20:54AM -0400, Eric Sunshine wrote:
quoted
      while :
      do
              case "$1" in
              -C) dir="-C $2"; shift; shift ;;
              -b) bare="$2"; shift; shift ;;
+             -g) env="GIT_DIR=$2; export GIT_DIR"; shift; shift ;;
@@ -36,6 +38,8 @@ test_rev_parse () {
      do
              expect="$1"
              test_expect_success "$name: $o" '
+                     test_when_finished "sane_unset GIT_DIR" &&
+                     eval $env &&
This will set up the sane_unset regardless of whether $env does
anything. Would it make more sense to stick the test_when_finished
inside $env? You could use regular unset then, too, since you know the
variable would be set.
I didn't worry about it too much because the end result is effectively
the same and, with all the 'case' arms being short one-liners, I think
the code is a bit easier to read as-is; bundling 'test_when_finished'
into the 'env' assignment line would probably require wrapping the
line. I do like the improved encapsulation of your suggestion but
don't otherwise feel strongly about it.
Actually, I think we can have improved encapsulation and maintain
readability like this:

    case "$1" in
    ...
    -g) env="$2"; shift;  shift ;;
    ...
    esac

    ...
    test_expect_success "..." '
        if test -n "$env"
        do
            test_when_finished "unset GIT_DIR"
            GIT_DIR="$env"
            export GIT_DIR
        fi
        ...
    '

Re: [PATCH 5/6] t1500: avoid setting environment variables outside of tests

From: Eric Sunshine <hidden>
Date: 2016-06-16 02:19:17

On Tue, May 10, 2016 at 3:48 PM, Eric Sunshine [off-list ref] wrote:
Actually, I think we can have improved encapsulation and maintain
readability like this:

    case "$1" in
    ...
    -g) env="$2"; shift;  shift ;;
    ...
    esac

    ...
    test_expect_success "..." '
        if test -n "$env"
        do
            test_when_finished "unset GIT_DIR"
            GIT_DIR="$env"
            export GIT_DIR
        fi
        ...
    '
At this point, I'd also rename 'env' to 'gitdir' to be more meaningful.

Re: [PATCH 5/6] t1500: avoid setting environment variables outside of tests

From: Jeff King <hidden>
Date: 2016-06-16 02:19:18

On Tue, May 10, 2016 at 03:59:42PM -0400, Eric Sunshine wrote:
On Tue, May 10, 2016 at 3:48 PM, Eric Sunshine [off-list ref] wrote:
quoted
Actually, I think we can have improved encapsulation and maintain
readability like this:

    case "$1" in
    ...
    -g) env="$2"; shift;  shift ;;
    ...
    esac

    ...
    test_expect_success "..." '
        if test -n "$env"
        do
            test_when_finished "unset GIT_DIR"
            GIT_DIR="$env"
            export GIT_DIR
        fi
        ...
    '
At this point, I'd also rename 'env' to 'gitdir' to be more meaningful.
Yeah, I like this even better. As much as I enjoy eval tricks, I think
this case is more readable with the condition written out.

-Peff

Re: [PATCH 5/6] t1500: avoid setting environment variables outside of tests

From: SZEDER Gábor <hidden>
Date: 2016-06-16 02:19:18

Quoting Eric Sunshine [off-list ref]:
On Tue, May 10, 2016 at 3:12 PM, Eric Sunshine  
[off-list ref] wrote:
quoted
On Tue, May 10, 2016 at 2:39 PM, Jeff King [off-list ref] wrote:
quoted
On Tue, May 10, 2016 at 01:20:54AM -0400, Eric Sunshine wrote:
quoted
      while :
      do
              case "$1" in
              -C) dir="-C $2"; shift; shift ;;
              -b) bare="$2"; shift; shift ;;
+             -g) env="GIT_DIR=$2; export GIT_DIR"; shift; shift ;;
@@ -36,6 +38,8 @@ test_rev_parse () {
      do
              expect="$1"
              test_expect_success "$name: $o" '
+                     test_when_finished "sane_unset GIT_DIR" &&
+                     eval $env &&
This will set up the sane_unset regardless of whether $env does
anything. Would it make more sense to stick the test_when_finished
inside $env? You could use regular unset then, too, since you know the
variable would be set.
I didn't worry about it too much because the end result is effectively
the same and, with all the 'case' arms being short one-liners, I think
the code is a bit easier to read as-is; bundling 'test_when_finished'
into the 'env' assignment line would probably require wrapping the
line. I do like the improved encapsulation of your suggestion but
don't otherwise feel strongly about it.
Actually, I think we can have improved encapsulation and maintain
readability like this:

    case "$1" in
    ...
    -g) env="$2"; shift;  shift ;;
    ...
    esac

    ...
    test_expect_success "..." '
        if test -n "$env"
        do
            test_when_finished "unset GIT_DIR"
            GIT_DIR="$env"
            export GIT_DIR
        fi
        ...
    '
I wonder if is it really necessary to specify the path to the .git
directory via $GIT_DIR.  Would 'git --git-dir=/over/there' be just as
good?  If yes, then how about

   git ${gitdir:+--git-dir="$gitdir"} rev-parse ...
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help