Skip to content

Commit e6273f4

Browse files
sunshinecogitster
authored andcommitted
t1500: avoid setting environment variables outside of tests
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. Take advantage of this new option to avoid polluting the global scope with GIT_DIR assignments. Implementation note: Typically, tests avoid polluting the global state by wrapping transient environment variable assignments within a subshell, however, this technique doesn't work 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 instead cleared manually via test_when_finished(). Signed-off-by: Eric Sunshine <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1dea0dc commit e6273f4

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

t/t1500-rev-parse.sh

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ test_description='test git rev-parse'
77
test_rev_parse () {
88
d=
99
bare=
10+
gitdir=
1011
while :
1112
do
1213
case "$1" in
@@ -15,6 +16,7 @@ test_rev_parse () {
1516
[tfu]*) bare="$2"; shift; shift ;;
1617
*) error "test_rev_parse: bogus core.bare value '$2'" ;;
1718
esac ;;
19+
-g) gitdir="$2"; shift; shift ;;
1820
-*) error "test_rev_parse: unrecognized option '$1'" ;;
1921
*) break ;;
2022
esac
@@ -32,6 +34,13 @@ test_rev_parse () {
3234
test $# -eq 0 && break
3335
expect="$1"
3436
test_expect_success "$name: $o" '
37+
if test -n "$gitdir"
38+
then
39+
test_when_finished "unset GIT_DIR" &&
40+
GIT_DIR="$gitdir" &&
41+
export GIT_DIR
42+
fi &&
43+
3544
case "$bare" in
3645
t*) test_config ${d:+-C} ${d:+"$d"} core.bare true ;;
3746
f*) test_config ${d:+-C} ${d:+"$d"} core.bare false ;;
@@ -64,21 +73,18 @@ test_rev_parse -b t 'core.bare = true' true false false
6473

6574
test_rev_parse -b u 'core.bare undefined' false false true
6675

67-
GIT_DIR=../.git
68-
export GIT_DIR
6976

70-
test_rev_parse -C work -b f 'GIT_DIR=../.git, core.bare = false' false false true ''
77+
test_rev_parse -C work -g ../.git -b f 'GIT_DIR=../.git, core.bare = false' false false true ''
7178

72-
test_rev_parse -C work -b t 'GIT_DIR=../.git, core.bare = true' true false false ''
79+
test_rev_parse -C work -g ../.git -b t 'GIT_DIR=../.git, core.bare = true' true false false ''
7380

74-
test_rev_parse -C work -b u 'GIT_DIR=../.git, core.bare undefined' false false true ''
81+
test_rev_parse -C work -g ../.git -b u 'GIT_DIR=../.git, core.bare undefined' false false true ''
7582

76-
GIT_DIR=../repo.git
7783

78-
test_rev_parse -C work -b f 'GIT_DIR=../repo.git, core.bare = false' false false true ''
84+
test_rev_parse -C work -g ../repo.git -b f 'GIT_DIR=../repo.git, core.bare = false' false false true ''
7985

80-
test_rev_parse -C work -b t 'GIT_DIR=../repo.git, core.bare = true' true false false ''
86+
test_rev_parse -C work -g ../repo.git -b t 'GIT_DIR=../repo.git, core.bare = true' true false false ''
8187

82-
test_rev_parse -C work -b u 'GIT_DIR=../repo.git, core.bare undefined' false false true ''
88+
test_rev_parse -C work -g ../repo.git -b u 'GIT_DIR=../repo.git, core.bare undefined' false false true ''
8389

8490
test_done

0 commit comments

Comments
 (0)