bb9533581ebaf57eb2f4904dcd41d13b85e78270
1#!/bin/sh
2
3test_description='Test automatic use of a pager.'
4
5. ./test-lib.sh
6. "$TEST_DIRECTORY"/lib-pager.sh
7
8cleanup_fail() {
9 echo >&2 cleanup failed
10 (exit 1)
11}
12
13test_expect_success 'set up terminal for tests' '
14 rm -f stdout_is_tty ||
15 cleanup_fail &&
16
17 if test -t 1
18 then
19 >stdout_is_tty
20 elif
21 test_have_prereq PERL &&
22 "$PERL_PATH" "$TEST_DIRECTORY"/t7006/test-terminal.perl \
23 sh -c "test -t 1"
24 then
25 >test_terminal_works
26 fi
27'
28
29if test -e stdout_is_tty
30then
31 test_terminal() { "$@"; }
32 test_set_prereq TTY
33elif test -e test_terminal_works
34then
35 test_terminal() {
36 "$PERL_PATH" "$TEST_DIRECTORY"/t7006/test-terminal.perl "$@"
37 }
38 test_set_prereq TTY
39else
40 say "# no usable terminal, so skipping some tests"
41fi
42
43test_expect_success 'setup' '
44 unset GIT_PAGER GIT_PAGER_IN_USE;
45 test_might_fail git config --unset core.pager &&
46
47 PAGER="cat >paginated.out" &&
48 export PAGER &&
49
50 test_commit initial
51'
52
53test_expect_success TTY 'some commands use a pager' '
54 rm -f paginated.out ||
55 cleanup_fail &&
56
57 test_terminal git log &&
58 test -e paginated.out
59'
60
61test_expect_success TTY 'some commands do not use a pager' '
62 rm -f paginated.out ||
63 cleanup_fail &&
64
65 test_terminal git rev-list HEAD &&
66 ! test -e paginated.out
67'
68
69test_expect_success 'no pager when stdout is a pipe' '
70 rm -f paginated.out ||
71 cleanup_fail &&
72
73 git log | cat &&
74 ! test -e paginated.out
75'
76
77test_expect_success 'no pager when stdout is a regular file' '
78 rm -f paginated.out ||
79 cleanup_fail &&
80
81 git log >file &&
82 ! test -e paginated.out
83'
84
85test_expect_success TTY 'git --paginate rev-list uses a pager' '
86 rm -f paginated.out ||
87 cleanup_fail &&
88
89 test_terminal git --paginate rev-list HEAD &&
90 test -e paginated.out
91'
92
93test_expect_success 'no pager even with --paginate when stdout is a pipe' '
94 rm -f file paginated.out ||
95 cleanup_fail &&
96
97 git --paginate log | cat &&
98 ! test -e paginated.out
99'
100
101test_expect_success TTY 'no pager with --no-pager' '
102 rm -f paginated.out ||
103 cleanup_fail &&
104
105 test_terminal git --no-pager log &&
106 ! test -e paginated.out
107'
108
109test_expect_success TTY 'configuration can disable pager' '
110 rm -f paginated.out &&
111 test_might_fail git config --unset pager.grep &&
112 test_terminal git grep initial &&
113 test -e paginated.out &&
114
115 rm -f paginated.out &&
116 git config pager.grep false &&
117 test_when_finished "git config --unset pager.grep" &&
118 test_terminal git grep initial &&
119 ! test -e paginated.out
120'
121
122# A colored commit log will begin with an appropriate ANSI escape
123# for the first color; the text "commit" comes later.
124colorful() {
125 read firstline <$1
126 ! expr "$firstline" : "[a-zA-Z]" >/dev/null
127}
128
129test_expect_success 'tests can detect color' '
130 rm -f colorful.log colorless.log ||
131 cleanup_fail &&
132
133 git log --no-color >colorless.log &&
134 git log --color >colorful.log &&
135 ! colorful colorless.log &&
136 colorful colorful.log
137'
138
139test_expect_success 'no color when stdout is a regular file' '
140 rm -f colorless.log &&
141 git config color.ui auto ||
142 cleanup_fail &&
143
144 git log >colorless.log &&
145 ! colorful colorless.log
146'
147
148test_expect_success TTY 'color when writing to a pager' '
149 rm -f paginated.out &&
150 git config color.ui auto ||
151 cleanup_fail &&
152
153 (
154 TERM=vt100 &&
155 export TERM &&
156 test_terminal git log
157 ) &&
158 colorful paginated.out
159'
160
161test_expect_success 'color when writing to a file intended for a pager' '
162 rm -f colorful.log &&
163 git config color.ui auto ||
164 cleanup_fail &&
165
166 (
167 TERM=vt100 &&
168 GIT_PAGER_IN_USE=true &&
169 export TERM GIT_PAGER_IN_USE &&
170 git log >colorful.log
171 ) &&
172 colorful colorful.log
173'
174
175if test_have_prereq SIMPLEPAGER && test_have_prereq TTY
176then
177 test_set_prereq SIMPLEPAGERTTY
178fi
179
180# Use this helper to make it easy for the caller of your
181# terminal-using function to specify whether it should fail.
182# If you write
183#
184# your_test() {
185# parse_args "$@"
186#
187# $test_expectation "$cmd - behaves well" "
188# ...
189# $full_command &&
190# ...
191# "
192# }
193#
194# then your test can be used like this:
195#
196# your_test expect_(success|failure) [test_must_fail] 'git foo'
197#
198parse_args() {
199 test_expectation="test_$1"
200 shift
201 if test "$1" = test_must_fail
202 then
203 full_command="test_must_fail test_terminal "
204 shift
205 else
206 full_command="test_terminal "
207 fi
208 cmd=$1
209 full_command="$full_command $1"
210}
211
212test_default_pager() {
213 parse_args "$@"
214
215 $test_expectation SIMPLEPAGERTTY "$cmd - default pager is used by default" "
216 unset PAGER GIT_PAGER;
217 test_might_fail git config --unset core.pager &&
218 rm -f default_pager_used ||
219 cleanup_fail &&
220
221 cat >\$less <<-\EOF &&
222 #!/bin/sh
223 wc >default_pager_used
224 EOF
225 chmod +x \$less &&
226 (
227 PATH=.:\$PATH &&
228 export PATH &&
229 $full_command
230 ) &&
231 test -e default_pager_used
232 "
233}
234
235test_PAGER_overrides() {
236 parse_args "$@"
237
238 $test_expectation TTY "$cmd - PAGER overrides default pager" "
239 unset GIT_PAGER;
240 test_might_fail git config --unset core.pager &&
241 rm -f PAGER_used ||
242 cleanup_fail &&
243
244 PAGER='wc >PAGER_used' &&
245 export PAGER &&
246 $full_command &&
247 test -e PAGER_used
248 "
249}
250
251test_core_pager_overrides() {
252 if_local_config=
253 used_if_wanted='overrides PAGER'
254 test_core_pager "$@"
255}
256
257test_local_config_ignored() {
258 if_local_config='! '
259 used_if_wanted='is not used'
260 test_core_pager "$@"
261}
262
263test_core_pager() {
264 parse_args "$@"
265
266 $test_expectation TTY "$cmd - repository-local core.pager setting $used_if_wanted" "
267 unset GIT_PAGER;
268 rm -f core.pager_used ||
269 cleanup_fail &&
270
271 PAGER=wc &&
272 export PAGER &&
273 git config core.pager 'wc >core.pager_used' &&
274 $full_command &&
275 ${if_local_config}test -e core.pager_used
276 "
277}
278
279test_core_pager_subdir() {
280 if_local_config=
281 used_if_wanted='overrides PAGER'
282 test_pager_subdir_helper "$@"
283}
284
285test_no_local_config_subdir() {
286 if_local_config='! '
287 used_if_wanted='is not used'
288 test_pager_subdir_helper "$@"
289}
290
291test_pager_subdir_helper() {
292 parse_args "$@"
293
294 $test_expectation TTY "$cmd - core.pager $used_if_wanted from subdirectory" "
295 unset GIT_PAGER;
296 rm -f core.pager_used &&
297 rm -fr sub ||
298 cleanup_fail &&
299
300 PAGER=wc &&
301 stampname=\$(pwd)/core.pager_used &&
302 export PAGER stampname &&
303 git config core.pager 'wc >\"\$stampname\"' &&
304 mkdir sub &&
305 (
306 cd sub &&
307 $full_command
308 ) &&
309 ${if_local_config}test -e core.pager_used
310 "
311}
312
313test_GIT_PAGER_overrides() {
314 parse_args "$@"
315
316 $test_expectation TTY "$cmd - GIT_PAGER overrides core.pager" "
317 rm -f GIT_PAGER_used ||
318 cleanup_fail &&
319
320 git config core.pager wc &&
321 GIT_PAGER='wc >GIT_PAGER_used' &&
322 export GIT_PAGER &&
323 $full_command &&
324 test -e GIT_PAGER_used
325 "
326}
327
328test_doesnt_paginate() {
329 parse_args "$@"
330
331 $test_expectation TTY "no pager for '$cmd'" "
332 rm -f GIT_PAGER_used ||
333 cleanup_fail &&
334
335 GIT_PAGER='wc >GIT_PAGER_used' &&
336 export GIT_PAGER &&
337 $full_command &&
338 ! test -e GIT_PAGER_used
339 "
340}
341
342test_pager_choices() {
343 test_default_pager expect_success "$@"
344 test_PAGER_overrides expect_success "$@"
345 test_core_pager_overrides expect_success "$@"
346 test_core_pager_subdir expect_success "$@"
347 test_GIT_PAGER_overrides expect_success "$@"
348}
349
350test_expect_success 'setup: some aliases' '
351 git config alias.aliasedlog log &&
352 git config alias.true "!true"
353'
354
355test_pager_choices 'git log'
356test_pager_choices 'git -p log'
357test_pager_choices 'git aliasedlog'
358
359test_default_pager expect_success 'git -p aliasedlog'
360test_PAGER_overrides expect_success 'git -p aliasedlog'
361test_core_pager_overrides expect_success 'git -p aliasedlog'
362test_core_pager_subdir expect_failure 'git -p aliasedlog'
363test_GIT_PAGER_overrides expect_success 'git -p aliasedlog'
364
365test_default_pager expect_success 'git -p true'
366test_PAGER_overrides expect_success 'git -p true'
367test_core_pager_overrides expect_success 'git -p true'
368test_core_pager_subdir expect_failure 'git -p true'
369test_GIT_PAGER_overrides expect_success 'git -p true'
370
371test_default_pager expect_success test_must_fail 'git -p request-pull'
372test_PAGER_overrides expect_success test_must_fail 'git -p request-pull'
373test_core_pager_overrides expect_success test_must_fail 'git -p request-pull'
374test_core_pager_subdir expect_failure test_must_fail 'git -p request-pull'
375test_GIT_PAGER_overrides expect_success test_must_fail 'git -p request-pull'
376
377test_default_pager expect_success test_must_fail 'git -p'
378test_PAGER_overrides expect_success test_must_fail 'git -p'
379test_local_config_ignored expect_failure test_must_fail 'git -p'
380test_no_local_config_subdir expect_success test_must_fail 'git -p'
381test_GIT_PAGER_overrides expect_success test_must_fail 'git -p'
382
383test_doesnt_paginate expect_failure test_must_fail 'git -p nonsense'
384
385test_pager_choices 'git shortlog'
386test_expect_success 'setup: configure shortlog not to paginate' '
387 git config pager.shortlog false
388'
389test_doesnt_paginate expect_success 'git shortlog'
390test_no_local_config_subdir expect_success 'git shortlog'
391test_default_pager expect_success 'git -p shortlog'
392test_core_pager_subdir expect_success 'git -p shortlog'
393
394test_done