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 <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric Sunshine committed May 18, 2016 at 16:15 UTC 1e043cff7815786b3d1a4c07bac63b3d8e1e30ef
1 file changed +23 -19
t/t1500-rev-parse.sh
+23 -19
@@ -3,8 +3,18 @@
3 test_description='test git rev-parse'
4 . ./test-lib.sh
5
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
7 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 +
18 name=$1
19 shift
20
@@ -18,7 +28,7 @@ test_rev_parse () {
28 expect="$1"
29 test_expect_success "$name: $o" '
30 echo "$expect" >expect &&
21 - git rev-parse $o >actual &&
31 + git ${d:+-C} ${d:+"$d"} rev-parse $o >actual &&
32 test_cmp expect actual
33 '
34 shift
@@ -34,15 +44,10 @@ test_expect_success 'setup' '
44
45 test_rev_parse toplevel false false true '' .git
46
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"
49
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"
51
52 git config core.bare true
53 test_rev_parse 'core.bare = true' true false false
@@ -50,30 +55,29 @@ test_rev_parse 'core.bare = true' true false false
55 git config --unset core.bare
56 test_rev_parse 'core.bare undefined' false false true
57
53 -cd work || exit 1
58 GIT_DIR=../.git
55 -GIT_CONFIG="$(pwd)"/../.git/config
59 +GIT_CONFIG="$(pwd)/work/../.git/config"
60 export GIT_DIR GIT_CONFIG
61
62 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 ''
64
65 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 ''
67
68 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 ''
70
71 GIT_DIR=../repo.git
68 -GIT_CONFIG="$(pwd)"/../repo.git/config
72 +GIT_CONFIG="$(pwd)/work/../repo.git/config"
73
74 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 ''
76
77 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 ''
79
80 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 ''
82
83 test_done