1#!/bin/sh
2
3test_description='Test automatic use of a pager.'
4
5. ./test-lib.sh
6. "$TEST_DIRECTORY"/lib-pager.sh
7. "$TEST_DIRECTORY"/lib-terminal.sh
8
9test_expect_success 'setup' '
10 sane_unset GIT_PAGER GIT_PAGER_IN_USE &&
11 test_unconfig core.pager &&
12
13 PAGER="cat >paginated.out" &&
14 export PAGER &&
15
16 test_commit initial
17'
18
19test_expect_success TTY 'some commands use a pager' '
20 rm -f paginated.out &&
21 test_terminal git log &&
22 test -e paginated.out
23'
24
25test_expect_failure TTY 'pager runs from subdir' '
26 echo subdir/paginated.out >expected &&
27 mkdir -p subdir &&
28 rm -f paginated.out subdir/paginated.out &&
29 (
30 cd subdir &&
31 test_terminal git log
32 ) &&
33 {
34 ls paginated.out subdir/paginated.out ||
35 :
36 } >actual &&
37 test_cmp expected actual
38'
39
40test_expect_success TTY 'LESS and LV envvars are set for pagination' '
41 (
42 sane_unset LESS LV &&
43 PAGER="env >pager-env.out; wc" &&
44 export PAGER &&
45
46 test_terminal git log
47 ) &&
48 grep ^LESS= pager-env.out &&
49 grep ^LV= pager-env.out
50'
51
52test_expect_success !MINGW,TTY 'LESS and LV envvars set by git-sh-setup' '
53 (
54 sane_unset LESS LV &&
55 PAGER="env >pager-env.out; wc" &&
56 export PAGER &&
57 PATH="$(git --exec-path):$PATH" &&
58 export PATH &&
59 test_terminal sh -c ". git-sh-setup && git_pager"
60 ) &&
61 grep ^LESS= pager-env.out &&
62 grep ^LV= pager-env.out
63'
64
65test_expect_success TTY 'some commands do not use a pager' '
66 rm -f paginated.out &&
67 test_terminal git rev-list HEAD &&
68 ! test -e paginated.out
69'
70
71test_expect_success 'no pager when stdout is a pipe' '
72 rm -f paginated.out &&
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 git log >file &&
80 ! test -e paginated.out
81'
82
83test_expect_success TTY 'git --paginate rev-list uses a pager' '
84 rm -f paginated.out &&
85 test_terminal git --paginate rev-list HEAD &&
86 test -e paginated.out
87'
88
89test_expect_success 'no pager even with --paginate when stdout is a pipe' '
90 rm -f file paginated.out &&
91 git --paginate log | cat &&
92 ! test -e paginated.out
93'
94
95test_expect_success TTY 'no pager with --no-pager' '
96 rm -f paginated.out &&
97 test_terminal git --no-pager log &&
98 ! test -e paginated.out
99'
100
101test_expect_success TTY 'configuration can disable pager' '
102 rm -f paginated.out &&
103 test_unconfig pager.grep &&
104 test_terminal git grep initial &&
105 test -e paginated.out &&
106
107 rm -f paginated.out &&
108 test_config pager.grep false &&
109 test_terminal git grep initial &&
110 ! test -e paginated.out
111'
112
113test_expect_success TTY 'git config uses a pager if configured to' '
114 rm -f paginated.out &&
115 test_config pager.config true &&
116 test_terminal git config --list &&
117 test -e paginated.out
118'
119
120test_expect_success TTY 'configuration can enable pager (from subdir)' '
121 rm -f paginated.out &&
122 mkdir -p subdir &&
123 test_config pager.bundle true &&
124
125 git bundle create test.bundle --all &&
126 rm -f paginated.out subdir/paginated.out &&
127 (
128 cd subdir &&
129 test_terminal git bundle unbundle ../test.bundle
130 ) &&
131 {
132 test -e paginated.out ||
133 test -e subdir/paginated.out
134 }
135'
136
137test_expect_success TTY 'git tag -l defaults to paging' '
138 rm -f paginated.out &&
139 test_terminal git tag -l &&
140 test -e paginated.out
141'
142
143test_expect_success TTY 'git tag -l respects pager.tag' '
144 rm -f paginated.out &&
145 test_terminal git -c pager.tag=false tag -l &&
146 ! test -e paginated.out
147'
148
149test_expect_success TTY 'git tag -l respects --no-pager' '
150 rm -f paginated.out &&
151 test_terminal git -c pager.tag --no-pager tag -l &&
152 ! test -e paginated.out
153'
154
155test_expect_success TTY 'git tag with no args defaults to paging' '
156 # no args implies -l so this should page like -l
157 rm -f paginated.out &&
158 test_terminal git tag &&
159 test -e paginated.out
160'
161
162test_expect_success TTY 'git tag with no args respects pager.tag' '
163 # no args implies -l so this should page like -l
164 rm -f paginated.out &&
165 test_terminal git -c pager.tag=false tag &&
166 ! test -e paginated.out
167'
168
169test_expect_success TTY 'git tag --contains defaults to paging' '
170 # --contains implies -l so this should page like -l
171 rm -f paginated.out &&
172 test_terminal git tag --contains &&
173 test -e paginated.out
174'
175
176test_expect_success TTY 'git tag --contains respects pager.tag' '
177 # --contains implies -l so this should page like -l
178 rm -f paginated.out &&
179 test_terminal git -c pager.tag=false tag --contains &&
180 ! test -e paginated.out
181'
182
183test_expect_success TTY 'git tag -a defaults to not paging' '
184 test_when_finished "git tag -d newtag" &&
185 rm -f paginated.out &&
186 test_terminal git tag -am message newtag &&
187 ! test -e paginated.out
188'
189
190test_expect_success TTY 'git tag -a ignores pager.tag' '
191 test_when_finished "git tag -d newtag" &&
192 rm -f paginated.out &&
193 test_terminal git -c pager.tag tag -am message newtag &&
194 ! test -e paginated.out
195'
196
197test_expect_success TTY 'git tag -a respects --paginate' '
198 test_when_finished "git tag -d newtag" &&
199 rm -f paginated.out &&
200 test_terminal git --paginate tag -am message newtag &&
201 test -e paginated.out
202'
203
204test_expect_success TTY 'git tag as alias ignores pager.tag with -a' '
205 test_when_finished "git tag -d newtag" &&
206 rm -f paginated.out &&
207 test_terminal git -c pager.tag -c alias.t=tag t -am message newtag &&
208 ! test -e paginated.out
209'
210
211test_expect_success TTY 'git tag as alias respects pager.tag with -l' '
212 rm -f paginated.out &&
213 test_terminal git -c pager.tag=false -c alias.t=tag t -l &&
214 ! test -e paginated.out
215'
216
217test_expect_success TTY 'git branch defaults to not paging' '
218 rm -f paginated.out &&
219 test_terminal git branch &&
220 ! test -e paginated.out
221'
222
223test_expect_success TTY 'git branch respects pager.branch' '
224 rm -f paginated.out &&
225 test_terminal git -c pager.branch branch &&
226 test -e paginated.out
227'
228
229test_expect_success TTY 'git branch respects --no-pager' '
230 rm -f paginated.out &&
231 test_terminal git -c pager.branch --no-pager branch &&
232 ! test -e paginated.out
233'
234
235test_expect_success TTY 'git branch --edit-description ignores pager.branch' '
236 rm -f paginated.out editor.used &&
237 write_script editor <<-\EOF &&
238 echo "New description" >"$1"
239 touch editor.used
240 EOF
241 EDITOR=./editor test_terminal git -c pager.branch branch --edit-description &&
242 ! test -e paginated.out &&
243 test -e editor.used
244'
245
246test_expect_success TTY 'git branch --set-upstream-to ignores pager.branch' '
247 rm -f paginated.out &&
248 git branch other &&
249 test_when_finished "git branch -D other" &&
250 test_terminal git -c pager.branch branch --set-upstream-to=other &&
251 test_when_finished "git branch --unset-upstream" &&
252 ! test -e paginated.out
253'
254
255# A colored commit log will begin with an appropriate ANSI escape
256# for the first color; the text "commit" comes later.
257colorful() {
258 read firstline <$1
259 ! expr "$firstline" : "[a-zA-Z]" >/dev/null
260}
261
262test_expect_success 'tests can detect color' '
263 rm -f colorful.log colorless.log &&
264 git log --no-color >colorless.log &&
265 git log --color >colorful.log &&
266 ! colorful colorless.log &&
267 colorful colorful.log
268'
269
270test_expect_success 'no color when stdout is a regular file' '
271 rm -f colorless.log &&
272 test_config color.ui auto &&
273 git log >colorless.log &&
274 ! colorful colorless.log
275'
276
277test_expect_success TTY 'color when writing to a pager' '
278 rm -f paginated.out &&
279 test_config color.ui auto &&
280 test_terminal env TERM=vt100 git log &&
281 colorful paginated.out
282'
283
284test_expect_success TTY 'colors are suppressed by color.pager' '
285 rm -f paginated.out &&
286 test_config color.ui auto &&
287 test_config color.pager false &&
288 test_terminal env TERM=vt100 git log &&
289 ! colorful paginated.out
290'
291
292test_expect_success 'color when writing to a file intended for a pager' '
293 rm -f colorful.log &&
294 test_config color.ui auto &&
295 (
296 TERM=vt100 &&
297 GIT_PAGER_IN_USE=true &&
298 export TERM GIT_PAGER_IN_USE &&
299 git log >colorful.log
300 ) &&
301 colorful colorful.log
302'
303
304test_expect_success TTY 'colors are sent to pager for external commands' '
305 test_config alias.externallog "!git log" &&
306 test_config color.ui auto &&
307 test_terminal env TERM=vt100 git -p externallog &&
308 colorful paginated.out
309'
310
311# Use this helper to make it easy for the caller of your
312# terminal-using function to specify whether it should fail.
313# If you write
314#
315# your_test() {
316# parse_args "$@"
317#
318# $test_expectation "$cmd - behaves well" "
319# ...
320# $full_command &&
321# ...
322# "
323# }
324#
325# then your test can be used like this:
326#
327# your_test expect_(success|failure) [test_must_fail] 'git foo'
328#
329parse_args() {
330 test_expectation="test_$1"
331 shift
332 if test "$1" = test_must_fail
333 then
334 full_command="test_must_fail test_terminal "
335 shift
336 else
337 full_command="test_terminal "
338 fi
339 cmd=$1
340 full_command="$full_command $1"
341}
342
343test_default_pager() {
344 parse_args "$@"
345
346 $test_expectation SIMPLEPAGER,TTY "$cmd - default pager is used by default" "
347 sane_unset PAGER GIT_PAGER &&
348 test_unconfig core.pager &&
349 rm -f default_pager_used &&
350 cat >\$less <<-\EOF &&
351 #!/bin/sh
352 wc >default_pager_used
353 EOF
354 chmod +x \$less &&
355 (
356 PATH=.:\$PATH &&
357 export PATH &&
358 $full_command
359 ) &&
360 test -e default_pager_used
361 "
362}
363
364test_PAGER_overrides() {
365 parse_args "$@"
366
367 $test_expectation TTY "$cmd - PAGER overrides default pager" "
368 sane_unset GIT_PAGER &&
369 test_unconfig core.pager &&
370 rm -f PAGER_used &&
371 PAGER='wc >PAGER_used' &&
372 export PAGER &&
373 $full_command &&
374 test -e PAGER_used
375 "
376}
377
378test_core_pager_overrides() {
379 if_local_config=
380 used_if_wanted='overrides PAGER'
381 test_core_pager "$@"
382}
383
384test_local_config_ignored() {
385 if_local_config='! '
386 used_if_wanted='is not used'
387 test_core_pager "$@"
388}
389
390test_core_pager() {
391 parse_args "$@"
392
393 $test_expectation TTY "$cmd - repository-local core.pager setting $used_if_wanted" "
394 sane_unset GIT_PAGER &&
395 rm -f core.pager_used &&
396 PAGER=wc &&
397 export PAGER &&
398 test_config core.pager 'wc >core.pager_used' &&
399 $full_command &&
400 ${if_local_config}test -e core.pager_used
401 "
402}
403
404test_core_pager_subdir() {
405 if_local_config=
406 used_if_wanted='overrides PAGER'
407 test_pager_subdir_helper "$@"
408}
409
410test_no_local_config_subdir() {
411 if_local_config='! '
412 used_if_wanted='is not used'
413 test_pager_subdir_helper "$@"
414}
415
416test_pager_subdir_helper() {
417 parse_args "$@"
418
419 $test_expectation TTY "$cmd - core.pager $used_if_wanted from subdirectory" "
420 sane_unset GIT_PAGER &&
421 rm -f core.pager_used &&
422 rm -fr sub &&
423 PAGER=wc &&
424 stampname=\$(pwd)/core.pager_used &&
425 export PAGER stampname &&
426 test_config core.pager 'wc >\"\$stampname\"' &&
427 mkdir sub &&
428 (
429 cd sub &&
430 $full_command
431 ) &&
432 ${if_local_config}test -e core.pager_used
433 "
434}
435
436test_GIT_PAGER_overrides() {
437 parse_args "$@"
438
439 $test_expectation TTY "$cmd - GIT_PAGER overrides core.pager" "
440 rm -f GIT_PAGER_used &&
441 test_config core.pager wc &&
442 GIT_PAGER='wc >GIT_PAGER_used' &&
443 export GIT_PAGER &&
444 $full_command &&
445 test -e GIT_PAGER_used
446 "
447}
448
449test_doesnt_paginate() {
450 parse_args "$@"
451
452 $test_expectation TTY "no pager for '$cmd'" "
453 rm -f GIT_PAGER_used &&
454 GIT_PAGER='wc >GIT_PAGER_used' &&
455 export GIT_PAGER &&
456 $full_command &&
457 ! test -e GIT_PAGER_used
458 "
459}
460
461test_pager_choices() {
462 test_default_pager expect_success "$@"
463 test_PAGER_overrides expect_success "$@"
464 test_core_pager_overrides expect_success "$@"
465 test_core_pager_subdir expect_success "$@"
466 test_GIT_PAGER_overrides expect_success "$@"
467}
468
469test_expect_success 'setup: some aliases' '
470 git config alias.aliasedlog log &&
471 git config alias.true "!true"
472'
473
474test_pager_choices 'git log'
475test_pager_choices 'git -p log'
476test_pager_choices 'git aliasedlog'
477
478test_default_pager expect_success 'git -p aliasedlog'
479test_PAGER_overrides expect_success 'git -p aliasedlog'
480test_core_pager_overrides expect_success 'git -p aliasedlog'
481test_core_pager_subdir expect_success 'git -p aliasedlog'
482test_GIT_PAGER_overrides expect_success 'git -p aliasedlog'
483
484test_default_pager expect_success 'git -p true'
485test_PAGER_overrides expect_success 'git -p true'
486test_core_pager_overrides expect_success 'git -p true'
487test_core_pager_subdir expect_success 'git -p true'
488test_GIT_PAGER_overrides expect_success 'git -p true'
489
490test_default_pager expect_success test_must_fail 'git -p request-pull'
491test_PAGER_overrides expect_success test_must_fail 'git -p request-pull'
492test_core_pager_overrides expect_success test_must_fail 'git -p request-pull'
493test_core_pager_subdir expect_success test_must_fail 'git -p request-pull'
494test_GIT_PAGER_overrides expect_success test_must_fail 'git -p request-pull'
495
496test_default_pager expect_success test_must_fail 'git -p'
497test_PAGER_overrides expect_success test_must_fail 'git -p'
498test_local_config_ignored expect_failure test_must_fail 'git -p'
499test_GIT_PAGER_overrides expect_success test_must_fail 'git -p'
500
501test_expect_success TTY 'core.pager in repo config works and retains cwd' '
502 sane_unset GIT_PAGER &&
503 test_config core.pager "cat >cwd-retained" &&
504 (
505 cd sub &&
506 rm -f cwd-retained &&
507 test_terminal git -p rev-parse HEAD &&
508 test_path_is_file cwd-retained
509 )
510'
511
512test_expect_success TTY 'core.pager is found via alias in subdirectory' '
513 sane_unset GIT_PAGER &&
514 test_config core.pager "cat >via-alias" &&
515 (
516 cd sub &&
517 rm -f via-alias &&
518 test_terminal git -c alias.r="-p rev-parse" r HEAD &&
519 test_path_is_file via-alias
520 )
521'
522
523test_doesnt_paginate expect_failure test_must_fail 'git -p nonsense'
524
525test_pager_choices 'git shortlog'
526test_expect_success 'setup: configure shortlog not to paginate' '
527 git config pager.shortlog false
528'
529test_doesnt_paginate expect_success 'git shortlog'
530test_no_local_config_subdir expect_success 'git shortlog'
531test_default_pager expect_success 'git -p shortlog'
532test_core_pager_subdir expect_success 'git -p shortlog'
533
534test_core_pager_subdir expect_success test_must_fail \
535 'git -p apply </dev/null'
536
537test_expect_success TTY 'command-specific pager' '
538 sane_unset PAGER GIT_PAGER &&
539 echo "foo:initial" >expect &&
540 >actual &&
541 test_unconfig core.pager &&
542 test_config pager.log "sed s/^/foo:/ >actual" &&
543 test_terminal git log --format=%s -1 &&
544 test_cmp expect actual
545'
546
547test_expect_success TTY 'command-specific pager overrides core.pager' '
548 sane_unset PAGER GIT_PAGER &&
549 echo "foo:initial" >expect &&
550 >actual &&
551 test_config core.pager "exit 1" &&
552 test_config pager.log "sed s/^/foo:/ >actual" &&
553 test_terminal git log --format=%s -1 &&
554 test_cmp expect actual
555'
556
557test_expect_success TTY 'command-specific pager overridden by environment' '
558 GIT_PAGER="sed s/^/foo:/ >actual" && export GIT_PAGER &&
559 >actual &&
560 echo "foo:initial" >expect &&
561 test_config pager.log "exit 1" &&
562 test_terminal git log --format=%s -1 &&
563 test_cmp expect actual
564'
565
566test_expect_success 'setup external command' '
567 cat >git-external <<-\EOF &&
568 #!/bin/sh
569 git "$@"
570 EOF
571 chmod +x git-external
572'
573
574test_expect_success TTY 'command-specific pager works for external commands' '
575 sane_unset PAGER GIT_PAGER &&
576 echo "foo:initial" >expect &&
577 >actual &&
578 test_config pager.external "sed s/^/foo:/ >actual" &&
579 test_terminal git --exec-path="$(pwd)" external log --format=%s -1 &&
580 test_cmp expect actual
581'
582
583test_expect_success TTY 'sub-commands of externals use their own pager' '
584 sane_unset PAGER GIT_PAGER &&
585 echo "foo:initial" >expect &&
586 >actual &&
587 test_config pager.log "sed s/^/foo:/ >actual" &&
588 test_terminal git --exec-path=. external log --format=%s -1 &&
589 test_cmp expect actual
590'
591
592test_expect_success TTY 'external command pagers override sub-commands' '
593 sane_unset PAGER GIT_PAGER &&
594 >expect &&
595 >actual &&
596 test_config pager.external false &&
597 test_config pager.log "sed s/^/log:/ >actual" &&
598 test_terminal git --exec-path=. external log --format=%s -1 &&
599 test_cmp expect actual
600'
601
602test_expect_success 'command with underscores does not complain' '
603 write_script git-under_score <<-\EOF &&
604 echo ok
605 EOF
606 git --exec-path=. under_score >actual 2>&1 &&
607 echo ok >expect &&
608 test_cmp expect actual
609'
610
611test_done