Skip to content

Commit 1e043cf

Browse files
sunshinecogitster
authored andcommitted
t1500: avoid changing working directory 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 "-C <dir>" option to allow callers to instruct it explicitly in which directory its tests should be run. Take advantage of this new option to avoid changing the working directory outside of tests. Implementation note: test_rev_parse() passes "-C <dir>" along to git-rev-parse with <dir> properly quoted. The natural and POSIX way to do so is via ${dir:+-C "$dir"}, however, with some older broken shells, this expression evaluates incorrectly to a single argument ("-C <dir>") rather than the expected two (-C and "<dir>"). Work around this problem with the slightly ungainly expression: ${dir:+-C} ${dir:+"$dir"} Signed-off-by: Eric Sunshine <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 12f7526 commit 1e043cf

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

t/t1500-rev-parse.sh

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,18 @@
33
test_description='test git rev-parse'
44
. ./test-lib.sh
55

6-
# usage: label is-bare is-inside-git is-inside-work prefix git-dir
6+
# usage: [options] label is-bare is-inside-git is-inside-work prefix git-dir
77
test_rev_parse () {
8+
d=
9+
while :
10+
do
11+
case "$1" in
12+
-C) d="$2"; shift; shift ;;
13+
-*) error "test_rev_parse: unrecognized option '$1'" ;;
14+
*) break ;;
15+
esac
16+
done
17+
818
name=$1
919
shift
1020

@@ -18,7 +28,7 @@ test_rev_parse () {
1828
expect="$1"
1929
test_expect_success "$name: $o" '
2030
echo "$expect" >expect &&
21-
git rev-parse $o >actual &&
31+
git ${d:+-C} ${d:+"$d"} rev-parse $o >actual &&
2232
test_cmp expect actual
2333
'
2434
shift
@@ -34,46 +44,40 @@ test_expect_success 'setup' '
3444

3545
test_rev_parse toplevel false false true '' .git
3646

37-
cd .git || exit 1
38-
test_rev_parse .git/ false true false '' .
39-
cd objects || exit 1
40-
test_rev_parse .git/objects/ false true false '' "$ROOT/.git"
41-
cd ../.. || exit 1
47+
test_rev_parse -C .git .git/ false true false '' .
48+
test_rev_parse -C .git/objects .git/objects/ false true false '' "$ROOT/.git"
4249

43-
cd sub/dir || exit 1
44-
test_rev_parse subdirectory false false true sub/dir/ "$ROOT/.git"
45-
cd ../.. || exit 1
50+
test_rev_parse -C sub/dir subdirectory false false true sub/dir/ "$ROOT/.git"
4651

4752
git config core.bare true
4853
test_rev_parse 'core.bare = true' true false false
4954

5055
git config --unset core.bare
5156
test_rev_parse 'core.bare undefined' false false true
5257

53-
cd work || exit 1
5458
GIT_DIR=../.git
55-
GIT_CONFIG="$(pwd)"/../.git/config
59+
GIT_CONFIG="$(pwd)/work/../.git/config"
5660
export GIT_DIR GIT_CONFIG
5761

5862
git config core.bare false
59-
test_rev_parse 'GIT_DIR=../.git, core.bare = false' false false true ''
63+
test_rev_parse -C work 'GIT_DIR=../.git, core.bare = false' false false true ''
6064

6165
git config core.bare true
62-
test_rev_parse 'GIT_DIR=../.git, core.bare = true' true false false ''
66+
test_rev_parse -C work 'GIT_DIR=../.git, core.bare = true' true false false ''
6367

6468
git config --unset core.bare
65-
test_rev_parse 'GIT_DIR=../.git, core.bare undefined' false false true ''
69+
test_rev_parse -C work 'GIT_DIR=../.git, core.bare undefined' false false true ''
6670

6771
GIT_DIR=../repo.git
68-
GIT_CONFIG="$(pwd)"/../repo.git/config
72+
GIT_CONFIG="$(pwd)/work/../repo.git/config"
6973

7074
git config core.bare false
71-
test_rev_parse 'GIT_DIR=../repo.git, core.bare = false' false false true ''
75+
test_rev_parse -C work 'GIT_DIR=../repo.git, core.bare = false' false false true ''
7276

7377
git config core.bare true
74-
test_rev_parse 'GIT_DIR=../repo.git, core.bare = true' true false false ''
78+
test_rev_parse -C work 'GIT_DIR=../repo.git, core.bare = true' true false false ''
7579

7680
git config --unset core.bare
77-
test_rev_parse 'GIT_DIR=../repo.git, core.bare undefined' false false true ''
81+
test_rev_parse -C work 'GIT_DIR=../repo.git, core.bare undefined' false false true ''
7882

7983
test_done

0 commit comments

Comments
 (0)