t7006: add tests for how git tag paginates
[git/git.git] / t / t7006-pager.sh
CommitLineData
60b6e220
JN
1#!/bin/sh
2
3test_description='Test automatic use of a pager.'
4
5. ./test-lib.sh
678e484b 6. "$TEST_DIRECTORY"/lib-pager.sh
cc4e48fc 7. "$TEST_DIRECTORY"/lib-terminal.sh
60b6e220 8
60b6e220 9test_expect_success 'setup' '
00648ba0 10 sane_unset GIT_PAGER GIT_PAGER_IN_USE &&
8d68a6d5 11 test_unconfig core.pager &&
fdf1bc48
JN
12
13 PAGER="cat >paginated.out" &&
14 export PAGER &&
15
60b6e220
JN
16 test_commit initial
17'
18
60b6e220 19test_expect_success TTY 'some commands use a pager' '
0d164519 20 rm -f paginated.out &&
2d3ca216 21 test_terminal git log &&
60b6e220
JN
22 test -e paginated.out
23'
24
2cb60093
NTND
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
e54c1f2d
JN
40test_expect_success TTY 'LESS and LV envvars are set for pagination' '
41 (
42 sane_unset LESS LV &&
52c02f65 43 PAGER="env >pager-env.out; wc" &&
e54c1f2d
JN
44 export PAGER &&
45
46 test_terminal git log
47 ) &&
48 grep ^LESS= pager-env.out &&
49 grep ^LV= pager-env.out
50'
51
995bc22d
EW
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
60b6e220 65test_expect_success TTY 'some commands do not use a pager' '
0d164519 66 rm -f paginated.out &&
2d3ca216 67 test_terminal git rev-list HEAD &&
60b6e220
JN
68 ! test -e paginated.out
69'
70
60b6e220 71test_expect_success 'no pager when stdout is a pipe' '
0d164519 72 rm -f paginated.out &&
60b6e220
JN
73 git log | cat &&
74 ! test -e paginated.out
75'
76
60b6e220 77test_expect_success 'no pager when stdout is a regular file' '
0d164519 78 rm -f paginated.out &&
fdf1bc48 79 git log >file &&
60b6e220
JN
80 ! test -e paginated.out
81'
82
60b6e220 83test_expect_success TTY 'git --paginate rev-list uses a pager' '
0d164519 84 rm -f paginated.out &&
2d3ca216 85 test_terminal git --paginate rev-list HEAD &&
60b6e220
JN
86 test -e paginated.out
87'
88
60b6e220 89test_expect_success 'no pager even with --paginate when stdout is a pipe' '
0d164519 90 rm -f file paginated.out &&
60b6e220
JN
91 git --paginate log | cat &&
92 ! test -e paginated.out
93'
94
60b6e220 95test_expect_success TTY 'no pager with --no-pager' '
0d164519 96 rm -f paginated.out &&
2d3ca216 97 test_terminal git --no-pager log &&
60b6e220
JN
98 ! test -e paginated.out
99'
100
ff38d1a9
NTND
101test_expect_success TTY 'configuration can disable pager' '
102 rm -f paginated.out &&
8d68a6d5 103 test_unconfig pager.grep &&
ff38d1a9
NTND
104 test_terminal git grep initial &&
105 test -e paginated.out &&
106
107 rm -f paginated.out &&
8d68a6d5 108 test_config pager.grep false &&
ff38d1a9
NTND
109 test_terminal git grep initial &&
110 ! test -e paginated.out
111'
112
41bf3bc2 113test_expect_success TTY 'git config uses a pager if configured to' '
3ba7e6e2 114 rm -f paginated.out &&
8d68a6d5 115 test_config pager.config true &&
3ba7e6e2
NTND
116 test_terminal git config --list &&
117 test -e paginated.out
118'
119
41bf3bc2 120test_expect_success TTY 'configuration can enable pager (from subdir)' '
2cb60093
NTND
121 rm -f paginated.out &&
122 mkdir -p subdir &&
8d68a6d5 123 test_config pager.bundle true &&
2cb60093
NTND
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
b3ee740c
137test_expect_success TTY 'git tag -l defaults to not 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 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 not 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 tag &&
166 test -e paginated.out
167'
168
169test_expect_success TTY 'git tag --contains defaults to not 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 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_failure 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
60b6e220
JN
204# A colored commit log will begin with an appropriate ANSI escape
205# for the first color; the text "commit" comes later.
206colorful() {
fdf1bc48 207 read firstline <$1
e0ae1e6f 208 ! expr "$firstline" : "[a-zA-Z]" >/dev/null
60b6e220
JN
209}
210
60b6e220 211test_expect_success 'tests can detect color' '
0d164519 212 rm -f colorful.log colorless.log &&
fdf1bc48
JN
213 git log --no-color >colorless.log &&
214 git log --color >colorful.log &&
60b6e220
JN
215 ! colorful colorless.log &&
216 colorful colorful.log
217'
218
60b6e220 219test_expect_success 'no color when stdout is a regular file' '
fdf1bc48 220 rm -f colorless.log &&
0d164519 221 test_config color.ui auto &&
fdf1bc48 222 git log >colorless.log &&
60b6e220
JN
223 ! colorful colorless.log
224'
225
60b6e220 226test_expect_success TTY 'color when writing to a pager' '
fdf1bc48 227 rm -f paginated.out &&
0d164519 228 test_config color.ui auto &&
512477b1 229 test_terminal env TERM=vt100 git log &&
60b6e220
JN
230 colorful paginated.out
231'
232
daa0c3d9
JK
233test_expect_success TTY 'colors are suppressed by color.pager' '
234 rm -f paginated.out &&
235 test_config color.ui auto &&
236 test_config color.pager false &&
512477b1 237 test_terminal env TERM=vt100 git log &&
daa0c3d9
JK
238 ! colorful paginated.out
239'
240
60b6e220 241test_expect_success 'color when writing to a file intended for a pager' '
fdf1bc48 242 rm -f colorful.log &&
0d164519 243 test_config color.ui auto &&
fdf1bc48
JN
244 (
245 TERM=vt100 &&
246 GIT_PAGER_IN_USE=true &&
247 export TERM GIT_PAGER_IN_USE &&
248 git log >colorful.log
249 ) &&
60b6e220
JN
250 colorful colorful.log
251'
252
2e6c012e
JK
253test_expect_success TTY 'colors are sent to pager for external commands' '
254 test_config alias.externallog "!git log" &&
255 test_config color.ui auto &&
512477b1 256 test_terminal env TERM=vt100 git -p externallog &&
2e6c012e
JK
257 colorful paginated.out
258'
259
3c7406d4
JN
260# Use this helper to make it easy for the caller of your
261# terminal-using function to specify whether it should fail.
262# If you write
263#
264# your_test() {
265# parse_args "$@"
266#
267# $test_expectation "$cmd - behaves well" "
268# ...
269# $full_command &&
270# ...
271# "
272# }
273#
274# then your test can be used like this:
275#
276# your_test expect_(success|failure) [test_must_fail] 'git foo'
277#
278parse_args() {
279 test_expectation="test_$1"
280 shift
281 if test "$1" = test_must_fail
282 then
283 full_command="test_must_fail test_terminal "
284 shift
285 else
286 full_command="test_terminal "
287 fi
288 cmd=$1
289 full_command="$full_command $1"
290}
fdf1bc48 291
8f81449e
JN
292test_default_pager() {
293 parse_args "$@"
294
996621eb 295 $test_expectation SIMPLEPAGER,TTY "$cmd - default pager is used by default" "
00648ba0 296 sane_unset PAGER GIT_PAGER &&
8d68a6d5 297 test_unconfig core.pager &&
0d164519 298 rm -f default_pager_used &&
8f81449e
JN
299 cat >\$less <<-\EOF &&
300 #!/bin/sh
301 wc >default_pager_used
302 EOF
303 chmod +x \$less &&
304 (
305 PATH=.:\$PATH &&
306 export PATH &&
307 $full_command
308 ) &&
309 test -e default_pager_used
310 "
311}
60b6e220 312
8f81449e
JN
313test_PAGER_overrides() {
314 parse_args "$@"
fdf1bc48 315
8f81449e 316 $test_expectation TTY "$cmd - PAGER overrides default pager" "
00648ba0 317 sane_unset GIT_PAGER &&
8d68a6d5 318 test_unconfig core.pager &&
0d164519 319 rm -f PAGER_used &&
8f81449e
JN
320 PAGER='wc >PAGER_used' &&
321 export PAGER &&
322 $full_command &&
323 test -e PAGER_used
324 "
325}
fdf1bc48 326
8f81449e 327test_core_pager_overrides() {
73e25e7c
NTND
328 if_local_config=
329 used_if_wanted='overrides PAGER'
330 test_core_pager "$@"
331}
60b6e220 332
73e25e7c
NTND
333test_local_config_ignored() {
334 if_local_config='! '
335 used_if_wanted='is not used'
336 test_core_pager "$@"
337}
fdf1bc48 338
73e25e7c 339test_core_pager() {
8f81449e
JN
340 parse_args "$@"
341
73e25e7c 342 $test_expectation TTY "$cmd - repository-local core.pager setting $used_if_wanted" "
00648ba0 343 sane_unset GIT_PAGER &&
0d164519 344 rm -f core.pager_used &&
8f81449e
JN
345 PAGER=wc &&
346 export PAGER &&
8d68a6d5 347 test_config core.pager 'wc >core.pager_used' &&
8f81449e 348 $full_command &&
73e25e7c 349 ${if_local_config}test -e core.pager_used
8f81449e
JN
350 "
351}
352
bce2c9ae 353test_core_pager_subdir() {
73e25e7c
NTND
354 if_local_config=
355 used_if_wanted='overrides PAGER'
356 test_pager_subdir_helper "$@"
357}
358
359test_no_local_config_subdir() {
360 if_local_config='! '
361 used_if_wanted='is not used'
362 test_pager_subdir_helper "$@"
363}
364
365test_pager_subdir_helper() {
bce2c9ae
JN
366 parse_args "$@"
367
73e25e7c 368 $test_expectation TTY "$cmd - core.pager $used_if_wanted from subdirectory" "
00648ba0 369 sane_unset GIT_PAGER &&
bce2c9ae 370 rm -f core.pager_used &&
0d164519 371 rm -fr sub &&
bce2c9ae
JN
372 PAGER=wc &&
373 stampname=\$(pwd)/core.pager_used &&
374 export PAGER stampname &&
8d68a6d5 375 test_config core.pager 'wc >\"\$stampname\"' &&
bce2c9ae
JN
376 mkdir sub &&
377 (
378 cd sub &&
379 $full_command
380 ) &&
73e25e7c 381 ${if_local_config}test -e core.pager_used
bce2c9ae
JN
382 "
383}
384
8f81449e
JN
385test_GIT_PAGER_overrides() {
386 parse_args "$@"
387
388 $test_expectation TTY "$cmd - GIT_PAGER overrides core.pager" "
0d164519 389 rm -f GIT_PAGER_used &&
8d68a6d5 390 test_config core.pager wc &&
8f81449e
JN
391 GIT_PAGER='wc >GIT_PAGER_used' &&
392 export GIT_PAGER &&
393 $full_command &&
394 test -e GIT_PAGER_used
395 "
396}
fdf1bc48 397
73e25e7c
NTND
398test_doesnt_paginate() {
399 parse_args "$@"
400
401 $test_expectation TTY "no pager for '$cmd'" "
0d164519 402 rm -f GIT_PAGER_used &&
73e25e7c
NTND
403 GIT_PAGER='wc >GIT_PAGER_used' &&
404 export GIT_PAGER &&
405 $full_command &&
406 ! test -e GIT_PAGER_used
407 "
408}
409
030149a4
JN
410test_pager_choices() {
411 test_default_pager expect_success "$@"
412 test_PAGER_overrides expect_success "$@"
413 test_core_pager_overrides expect_success "$@"
414 test_core_pager_subdir expect_success "$@"
415 test_GIT_PAGER_overrides expect_success "$@"
416}
417
418test_expect_success 'setup: some aliases' '
419 git config alias.aliasedlog log &&
420 git config alias.true "!true"
60b6e220
JN
421'
422
030149a4
JN
423test_pager_choices 'git log'
424test_pager_choices 'git -p log'
425test_pager_choices 'git aliasedlog'
426
427test_default_pager expect_success 'git -p aliasedlog'
428test_PAGER_overrides expect_success 'git -p aliasedlog'
429test_core_pager_overrides expect_success 'git -p aliasedlog'
1a27409a 430test_core_pager_subdir expect_success 'git -p aliasedlog'
030149a4
JN
431test_GIT_PAGER_overrides expect_success 'git -p aliasedlog'
432
433test_default_pager expect_success 'git -p true'
434test_PAGER_overrides expect_success 'git -p true'
435test_core_pager_overrides expect_success 'git -p true'
1a27409a 436test_core_pager_subdir expect_success 'git -p true'
030149a4
JN
437test_GIT_PAGER_overrides expect_success 'git -p true'
438
439test_default_pager expect_success test_must_fail 'git -p request-pull'
440test_PAGER_overrides expect_success test_must_fail 'git -p request-pull'
441test_core_pager_overrides expect_success test_must_fail 'git -p request-pull'
1a27409a 442test_core_pager_subdir expect_success test_must_fail 'git -p request-pull'
030149a4 443test_GIT_PAGER_overrides expect_success test_must_fail 'git -p request-pull'
8f81449e
JN
444
445test_default_pager expect_success test_must_fail 'git -p'
446test_PAGER_overrides expect_success test_must_fail 'git -p'
73e25e7c 447test_local_config_ignored expect_failure test_must_fail 'git -p'
8f81449e
JN
448test_GIT_PAGER_overrides expect_success test_must_fail 'git -p'
449
1a27409a 450test_expect_success TTY 'core.pager in repo config works and retains cwd' '
551d535d
JS
451 sane_unset GIT_PAGER &&
452 test_config core.pager "cat >cwd-retained" &&
453 (
454 cd sub &&
455 rm -f cwd-retained &&
456 test_terminal git -p rev-parse HEAD &&
457 test_path_is_file cwd-retained
458 )
459'
460
a9bcf658 461test_expect_success TTY 'core.pager is found via alias in subdirectory' '
3f9c5dfb
JS
462 sane_unset GIT_PAGER &&
463 test_config core.pager "cat >via-alias" &&
464 (
465 cd sub &&
466 rm -f via-alias &&
467 test_terminal git -c alias.r="-p rev-parse" r HEAD &&
468 test_path_is_file via-alias
469 )
470'
471
030149a4 472test_doesnt_paginate expect_failure test_must_fail 'git -p nonsense'
60b6e220 473
773b69bf
NTND
474test_pager_choices 'git shortlog'
475test_expect_success 'setup: configure shortlog not to paginate' '
476 git config pager.shortlog false
477'
478test_doesnt_paginate expect_success 'git shortlog'
479test_no_local_config_subdir expect_success 'git shortlog'
480test_default_pager expect_success 'git -p shortlog'
481test_core_pager_subdir expect_success 'git -p shortlog'
482
d1ea8962
NTND
483test_core_pager_subdir expect_success test_must_fail \
484 'git -p apply </dev/null'
485
9bad7233 486test_expect_success TTY 'command-specific pager' '
212ad944 487 sane_unset PAGER GIT_PAGER &&
9bad7233
JK
488 echo "foo:initial" >expect &&
489 >actual &&
8d68a6d5
JK
490 test_unconfig core.pager &&
491 test_config pager.log "sed s/^/foo:/ >actual" &&
9bad7233
JK
492 test_terminal git log --format=%s -1 &&
493 test_cmp expect actual
494'
495
496test_expect_success TTY 'command-specific pager overrides core.pager' '
212ad944 497 sane_unset PAGER GIT_PAGER &&
9bad7233
JK
498 echo "foo:initial" >expect &&
499 >actual &&
99094a7a 500 test_config core.pager "exit 1" &&
8d68a6d5 501 test_config pager.log "sed s/^/foo:/ >actual" &&
9bad7233
JK
502 test_terminal git log --format=%s -1 &&
503 test_cmp expect actual
504'
505
506test_expect_success TTY 'command-specific pager overridden by environment' '
507 GIT_PAGER="sed s/^/foo:/ >actual" && export GIT_PAGER &&
508 >actual &&
509 echo "foo:initial" >expect &&
8d68a6d5 510 test_config pager.log "exit 1" &&
9bad7233
JK
511 test_terminal git log --format=%s -1 &&
512 test_cmp expect actual
513'
514
92058e4d
JK
515test_expect_success 'setup external command' '
516 cat >git-external <<-\EOF &&
517 #!/bin/sh
518 git "$@"
519 EOF
520 chmod +x git-external
521'
522
523test_expect_success TTY 'command-specific pager works for external commands' '
524 sane_unset PAGER GIT_PAGER &&
525 echo "foo:initial" >expect &&
526 >actual &&
527 test_config pager.external "sed s/^/foo:/ >actual" &&
90ae5d27 528 test_terminal git --exec-path="$(pwd)" external log --format=%s -1 &&
92058e4d
JK
529 test_cmp expect actual
530'
531
532test_expect_success TTY 'sub-commands of externals use their own pager' '
533 sane_unset PAGER GIT_PAGER &&
534 echo "foo:initial" >expect &&
535 >actual &&
536 test_config pager.log "sed s/^/foo:/ >actual" &&
537 test_terminal git --exec-path=. external log --format=%s -1 &&
538 test_cmp expect actual
539'
540
541test_expect_success TTY 'external command pagers override sub-commands' '
542 sane_unset PAGER GIT_PAGER &&
543 >expect &&
544 >actual &&
545 test_config pager.external false &&
546 test_config pager.log "sed s/^/log:/ >actual" &&
547 test_terminal git --exec-path=. external log --format=%s -1 &&
548 test_cmp expect actual
549'
550
9e9de18f
JK
551test_expect_success 'command with underscores does not complain' '
552 write_script git-under_score <<-\EOF &&
553 echo ok
554 EOF
555 git --exec-path=. under_score >actual 2>&1 &&
556 echo ok >expect &&
557 test_cmp expect actual
558'
559
60b6e220 560test_done