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