Skip to content

Commit 39fbe92

Browse files
committed
Merge branch 'es/t1500-modernize'
test updates to make it more readable and maintainable. * es/t1500-modernize: t1500: avoid setting environment variables outside of tests t1500: avoid setting configuration options outside of tests t1500: avoid changing working directory outside of tests t1500: test_rev_parse: facilitate future test enhancements t1500: be considerate to future potential tests
2 parents 6289913 + e6273f4 commit 39fbe92

File tree

1 file changed

+63
-60
lines changed

1 file changed

+63
-60
lines changed

t/t1500-rev-parse.sh

Lines changed: 63 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,85 +3,88 @@
33
test_description='test git rev-parse'
44
. ./test-lib.sh
55

6-
test_rev_parse() {
7-
name=$1
8-
shift
9-
10-
test_expect_success "$name: is-bare-repository" \
11-
"test '$1' = \"\$(git rev-parse --is-bare-repository)\""
12-
shift
13-
[ $# -eq 0 ] && return
14-
15-
test_expect_success "$name: is-inside-git-dir" \
16-
"test '$1' = \"\$(git rev-parse --is-inside-git-dir)\""
17-
shift
18-
[ $# -eq 0 ] && return
6+
# usage: [options] label is-bare is-inside-git is-inside-work prefix git-dir
7+
test_rev_parse () {
8+
d=
9+
bare=
10+
gitdir=
11+
while :
12+
do
13+
case "$1" in
14+
-C) d="$2"; shift; shift ;;
15+
-b) case "$2" in
16+
[tfu]*) bare="$2"; shift; shift ;;
17+
*) error "test_rev_parse: bogus core.bare value '$2'" ;;
18+
esac ;;
19+
-g) gitdir="$2"; shift; shift ;;
20+
-*) error "test_rev_parse: unrecognized option '$1'" ;;
21+
*) break ;;
22+
esac
23+
done
1924

20-
test_expect_success "$name: is-inside-work-tree" \
21-
"test '$1' = \"\$(git rev-parse --is-inside-work-tree)\""
22-
shift
23-
[ $# -eq 0 ] && return
24-
25-
test_expect_success "$name: prefix" \
26-
"test '$1' = \"\$(git rev-parse --show-prefix)\""
25+
name=$1
2726
shift
28-
[ $# -eq 0 ] && return
2927

30-
test_expect_success "$name: git-dir" \
31-
"test '$1' = \"\$(git rev-parse --git-dir)\""
32-
shift
33-
[ $# -eq 0 ] && return
28+
for o in --is-bare-repository \
29+
--is-inside-git-dir \
30+
--is-inside-work-tree \
31+
--show-prefix \
32+
--git-dir
33+
do
34+
test $# -eq 0 && break
35+
expect="$1"
36+
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+
44+
case "$bare" in
45+
t*) test_config ${d:+-C} ${d:+"$d"} core.bare true ;;
46+
f*) test_config ${d:+-C} ${d:+"$d"} core.bare false ;;
47+
u*) test_unconfig ${d:+-C} ${d:+"$d"} core.bare ;;
48+
esac &&
49+
50+
echo "$expect" >expect &&
51+
git ${d:+-C} ${d:+"$d"} rev-parse $o >actual &&
52+
test_cmp expect actual
53+
'
54+
shift
55+
done
3456
}
3557

36-
# label is-bare is-inside-git is-inside-work prefix git-dir
37-
3858
ROOT=$(pwd)
3959

60+
test_expect_success 'setup' '
61+
mkdir -p sub/dir work &&
62+
cp -R .git repo.git
63+
'
64+
4065
test_rev_parse toplevel false false true '' .git
4166

42-
cd .git || exit 1
43-
test_rev_parse .git/ false true false '' .
44-
cd objects || exit 1
45-
test_rev_parse .git/objects/ false true false '' "$ROOT/.git"
46-
cd ../.. || exit 1
67+
test_rev_parse -C .git .git/ false true false '' .
68+
test_rev_parse -C .git/objects .git/objects/ false true false '' "$ROOT/.git"
4769

48-
mkdir -p sub/dir || exit 1
49-
cd sub/dir || exit 1
50-
test_rev_parse subdirectory false false true sub/dir/ "$ROOT/.git"
51-
cd ../.. || exit 1
70+
test_rev_parse -C sub/dir subdirectory false false true sub/dir/ "$ROOT/.git"
5271

53-
git config core.bare true
54-
test_rev_parse 'core.bare = true' true false false
72+
test_rev_parse -b t 'core.bare = true' true false false
5573

56-
git config --unset core.bare
57-
test_rev_parse 'core.bare undefined' false false true
74+
test_rev_parse -b u 'core.bare undefined' false false true
5875

59-
mkdir work || exit 1
60-
cd work || exit 1
61-
GIT_DIR=../.git
62-
GIT_CONFIG="$(pwd)"/../.git/config
63-
export GIT_DIR GIT_CONFIG
6476

65-
git config core.bare false
66-
test_rev_parse '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 ''
6778

68-
git config core.bare true
69-
test_rev_parse '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 ''
7080

71-
git config --unset core.bare
72-
test_rev_parse '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 ''
7382

74-
mv ../.git ../repo.git || exit 1
75-
GIT_DIR=../repo.git
76-
GIT_CONFIG="$(pwd)"/../repo.git/config
7783

78-
git config core.bare false
79-
test_rev_parse '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 ''
8085

81-
git config core.bare true
82-
test_rev_parse '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 ''
8387

84-
git config --unset core.bare
85-
test_rev_parse '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 ''
8689

8790
test_done

0 commit comments

Comments
 (0)