Raw
1 #!/bin/sh
2
3 test_description='basic sanity checks for git var'
4
5 . ./test-lib.sh
6
7 sane_unset_all_editors () {
8 sane_unset GIT_EDITOR &&
9 sane_unset VISUAL &&
10 sane_unset EDITOR
11 }
12
13 test_expect_success 'get GIT_AUTHOR_IDENT' '
14 test_tick &&
15 echo "$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE" >expect &&
16 git var GIT_AUTHOR_IDENT >actual &&
17 test_cmp expect actual
18 '
19
20 test_expect_success 'get GIT_COMMITTER_IDENT' '
21 test_tick &&
22 echo "$GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE" >expect &&
23 git var GIT_COMMITTER_IDENT >actual &&
24 test_cmp expect actual
25 '
26
27 test_expect_success !FAIL_PREREQS,!AUTOIDENT 'requested identities are strict' '
28 (
29 sane_unset GIT_COMMITTER_NAME &&
30 sane_unset GIT_COMMITTER_EMAIL &&
31 test_must_fail git var GIT_COMMITTER_IDENT
32 )
33 '
34
35 test_expect_success 'get GIT_DEFAULT_BRANCH without configuration' '
36 (
37 sane_unset GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME &&
38 git init defbranch &&
39 git -C defbranch symbolic-ref --short HEAD >expect &&
40 git var GIT_DEFAULT_BRANCH >actual &&
41 test_cmp expect actual
42 )
43 '
44
45 test_expect_success 'get GIT_DEFAULT_BRANCH with configuration' '
46 test_config init.defaultbranch foo &&
47 (
48 sane_unset GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME &&
49 echo foo >expect &&
50 git var GIT_DEFAULT_BRANCH >actual &&
51 test_cmp expect actual
52 )
53 '
54
55 test_expect_success 'get GIT_EDITOR without configuration' '
56 (
57 sane_unset_all_editors &&
58 test_expect_code 1 git var GIT_EDITOR >out &&
59 test_must_be_empty out
60 )
61 '
62
63 test_expect_success 'get GIT_EDITOR with configuration' '
64 test_config core.editor foo &&
65 (
66 sane_unset_all_editors &&
67 echo foo >expect &&
68 git var GIT_EDITOR >actual &&
69 test_cmp expect actual
70 )
71 '
72
73 test_expect_success 'get GIT_EDITOR with environment variable GIT_EDITOR' '
74 (
75 sane_unset_all_editors &&
76 echo bar >expect &&
77 GIT_EDITOR=bar git var GIT_EDITOR >actual &&
78 test_cmp expect actual
79 )
80 '
81
82 test_expect_success 'get GIT_EDITOR with environment variable EDITOR' '
83 (
84 sane_unset_all_editors &&
85 echo bar >expect &&
86 EDITOR=bar git var GIT_EDITOR >actual &&
87 test_cmp expect actual
88 )
89 '
90
91 test_expect_success 'get GIT_EDITOR with configuration and environment variable GIT_EDITOR' '
92 test_config core.editor foo &&
93 (
94 sane_unset_all_editors &&
95 echo bar >expect &&
96 GIT_EDITOR=bar git var GIT_EDITOR >actual &&
97 test_cmp expect actual
98 )
99 '
100
101 test_expect_success 'get GIT_EDITOR with configuration and environment variable EDITOR' '
102 test_config core.editor foo &&
103 (
104 sane_unset_all_editors &&
105 echo foo >expect &&
106 EDITOR=bar git var GIT_EDITOR >actual &&
107 test_cmp expect actual
108 )
109 '
110
111 test_expect_success 'get GIT_SEQUENCE_EDITOR without configuration' '
112 (
113 sane_unset GIT_SEQUENCE_EDITOR &&
114 git var GIT_EDITOR >expect &&
115 git var GIT_SEQUENCE_EDITOR >actual &&
116 test_cmp expect actual
117 )
118 '
119
120 test_expect_success 'get GIT_SEQUENCE_EDITOR with configuration' '
121 test_config sequence.editor foo &&
122 (
123 sane_unset GIT_SEQUENCE_EDITOR &&
124 echo foo >expect &&
125 git var GIT_SEQUENCE_EDITOR >actual &&
126 test_cmp expect actual
127 )
128 '
129
130 test_expect_success 'get GIT_SEQUENCE_EDITOR with environment variable' '
131 (
132 sane_unset GIT_SEQUENCE_EDITOR &&
133 echo bar >expect &&
134 GIT_SEQUENCE_EDITOR=bar git var GIT_SEQUENCE_EDITOR >actual &&
135 test_cmp expect actual
136 )
137 '
138
139 test_expect_success 'get GIT_SEQUENCE_EDITOR with configuration and environment variable' '
140 test_config sequence.editor foo &&
141 (
142 sane_unset GIT_SEQUENCE_EDITOR &&
143 echo bar >expect &&
144 GIT_SEQUENCE_EDITOR=bar git var GIT_SEQUENCE_EDITOR >actual &&
145 test_cmp expect actual
146 )
147 '
148
149 test_expect_success POSIXPERM 'GIT_SHELL_PATH points to a valid executable' '
150 shellpath=$(git var GIT_SHELL_PATH) &&
151 test_path_is_executable "$shellpath"
152 '
153
154 # We know in this environment that our shell will be one of a few fixed values
155 # that all end in "sh".
156 test_expect_success MINGW 'GIT_SHELL_PATH points to a suitable shell' '
157 shellpath=$(git var GIT_SHELL_PATH) &&
158 case "$shellpath" in
159 [A-Z]:/*/sh.exe) test -f "$shellpath";;
160 *) return 1;;
161 esac
162 '
163
164 test_expect_success 'GIT_ATTR_SYSTEM produces expected output' '
165 test_must_fail env GIT_ATTR_NOSYSTEM=1 git var GIT_ATTR_SYSTEM &&
166 (
167 sane_unset GIT_ATTR_NOSYSTEM &&
168 systempath=$(git var GIT_ATTR_SYSTEM) &&
169 test "$systempath" != ""
170 )
171 '
172
173 test_expect_success 'GIT_ATTR_GLOBAL points to the correct location' '
174 TRASHDIR="$(test-tool path-utils normalize_path_copy "$(pwd)")" &&
175 globalpath=$(XDG_CONFIG_HOME="$TRASHDIR/.config" git var GIT_ATTR_GLOBAL) &&
176 test "$globalpath" = "$TRASHDIR/.config/git/attributes" &&
177 (
178 sane_unset XDG_CONFIG_HOME &&
179 globalpath=$(HOME="$TRASHDIR" git var GIT_ATTR_GLOBAL) &&
180 test "$globalpath" = "$TRASHDIR/.config/git/attributes"
181 )
182 '
183
184 test_expect_success 'GIT_CONFIG_SYSTEM points to the correct location' '
185 TRASHDIR="$(test-tool path-utils normalize_path_copy "$(pwd)")" &&
186 test_must_fail env GIT_CONFIG_NOSYSTEM=1 git var GIT_CONFIG_SYSTEM &&
187 (
188 sane_unset GIT_CONFIG_NOSYSTEM &&
189 systempath=$(git var GIT_CONFIG_SYSTEM) &&
190 test "$systempath" != "" &&
191 systempath=$(GIT_CONFIG_SYSTEM=/dev/null git var GIT_CONFIG_SYSTEM) &&
192 if test_have_prereq MINGW
193 then
194 test "$systempath" = "nul"
195 else
196 test "$systempath" = "/dev/null"
197 fi &&
198 systempath=$(GIT_CONFIG_SYSTEM="$TRASHDIR/gitconfig" git var GIT_CONFIG_SYSTEM) &&
199 test "$systempath" = "$TRASHDIR/gitconfig"
200 )
201 '
202
203 test_expect_success 'GIT_CONFIG_GLOBAL points to the correct location' '
204 TRASHDIR="$(test-tool path-utils normalize_path_copy "$(pwd)")" &&
205 HOME="$TRASHDIR" XDG_CONFIG_HOME="$TRASHDIR/foo" git var GIT_CONFIG_GLOBAL >actual &&
206 echo "$TRASHDIR/foo/git/config" >expected &&
207 echo "$TRASHDIR/.gitconfig" >>expected &&
208 test_cmp expected actual &&
209 (
210 sane_unset XDG_CONFIG_HOME &&
211 HOME="$TRASHDIR" git var GIT_CONFIG_GLOBAL >actual &&
212 echo "$TRASHDIR/.config/git/config" >expected &&
213 echo "$TRASHDIR/.gitconfig" >>expected &&
214 test_cmp expected actual &&
215 globalpath=$(GIT_CONFIG_GLOBAL=/dev/null git var GIT_CONFIG_GLOBAL) &&
216 if test_have_prereq MINGW
217 then
218 test "$globalpath" = "nul"
219 else
220 test "$globalpath" = "/dev/null"
221 fi &&
222 globalpath=$(GIT_CONFIG_GLOBAL="$TRASHDIR/gitconfig" git var GIT_CONFIG_GLOBAL) &&
223 test "$globalpath" = "$TRASHDIR/gitconfig"
224 )
225 '
226
227 # For git var -l, we check only a representative variable;
228 # testing the whole output would make our test too brittle with
229 # respect to unrelated changes in the test suite's environment.
230 test_expect_success 'git var -l lists variables' '
231 git var -l >actual &&
232 echo "$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE" >expect &&
233 sed -n s/GIT_AUTHOR_IDENT=//p <actual >actual.author &&
234 test_cmp expect actual.author
235 '
236
237 test_expect_success 'git var -l lists config' '
238 git var -l >actual &&
239 echo false >expect &&
240 sed -n s/core\\.bare=//p <actual >actual.bare &&
241 test_cmp expect actual.bare
242 '
243
244 test_expect_success 'git var -l lists multiple global configs' '
245 TRASHDIR="$(test-tool path-utils normalize_path_copy "$(pwd)")" &&
246 HOME="$TRASHDIR" XDG_CONFIG_HOME="$TRASHDIR/foo" git var -l >actual &&
247 grep "^GIT_CONFIG_GLOBAL=" actual >filtered &&
248 echo "GIT_CONFIG_GLOBAL=$TRASHDIR/foo/git/config" >expected &&
249 echo "GIT_CONFIG_GLOBAL=$TRASHDIR/.gitconfig" >>expected &&
250 test_cmp expected filtered
251 '
252
253 test_expect_success 'git var -l does not split multiline editors' '
254 (
255 GIT_EDITOR="!f() {
256 echo Hello!
257 }; f" &&
258 export GIT_EDITOR &&
259 echo "GIT_EDITOR=$GIT_EDITOR" >expected &&
260 git var -l >var &&
261 sed -n -e "/^GIT_EDITOR/,\$p" var | head -n 3 >actual &&
262 test_cmp expected actual
263 )
264 '
265
266 test_expect_success 'listing and asking for variables are exclusive' '
267 test_must_fail git var -l GIT_COMMITTER_IDENT
268 '
269
270 test_expect_success '`git var -l` works even without HOME' '
271 (
272 XDG_CONFIG_HOME= &&
273 export XDG_CONFIG_HOME &&
274 unset HOME &&
275 git var -l
276 )
277 '
278
279 test_done