t7006: add tests for how git tag paginates
[git.git] / t / t7006-pager.sh
blobb56d4cdd4184eded67dc1ef2072803e8ce48592b
1 #!/bin/sh
3 test_description='Test automatic use of a pager.'
5 . ./test-lib.sh
6 . "$TEST_DIRECTORY"/lib-pager.sh
7 . "$TEST_DIRECTORY"/lib-terminal.sh
9 test_expect_success 'setup' '
10 sane_unset GIT_PAGER GIT_PAGER_IN_USE &&
11 test_unconfig core.pager &&
13 PAGER="cat >paginated.out" &&
14 export PAGER &&
16 test_commit initial
19 test_expect_success TTY 'some commands use a pager' '
20 rm -f paginated.out &&
21 test_terminal git log &&
22 test -e paginated.out
25 test_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 &&
30 cd subdir &&
31 test_terminal git log
32 ) &&
34 ls paginated.out subdir/paginated.out ||
36 } >actual &&
37 test_cmp expected actual
40 test_expect_success TTY 'LESS and LV envvars are set for pagination' '
42 sane_unset LESS LV &&
43 PAGER="env >pager-env.out; wc" &&
44 export PAGER &&
46 test_terminal git log
47 ) &&
48 grep ^LESS= pager-env.out &&
49 grep ^LV= pager-env.out
52 test_expect_success !MINGW,TTY 'LESS and LV envvars set by git-sh-setup' '
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
65 test_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
71 test_expect_success 'no pager when stdout is a pipe' '
72 rm -f paginated.out &&
73 git log | cat &&
74 ! test -e paginated.out
77 test_expect_success 'no pager when stdout is a regular file' '
78 rm -f paginated.out &&
79 git log >file &&
80 ! test -e paginated.out
83 test_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
89 test_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
95 test_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
101 test_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 &&
107 rm -f paginated.out &&
108 test_config pager.grep false &&
109 test_terminal git grep initial &&
110 ! test -e paginated.out
113 test_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
120 test_expect_success TTY 'configuration can enable pager (from subdir)' '
121 rm -f paginated.out &&
122 mkdir -p subdir &&
123 test_config pager.bundle true &&
125 git bundle create test.bundle --all &&
126 rm -f paginated.out subdir/paginated.out &&
128 cd subdir &&
129 test_terminal git bundle unbundle ../test.bundle
130 ) &&
132 test -e paginated.out ||
133 test -e subdir/paginated.out
137 test_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
143 test_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
149 test_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
155 test_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
162 test_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
169 test_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
176 test_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
183 test_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
190 test_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
197 test_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
204 # A colored commit log will begin with an appropriate ANSI escape
205 # for the first color; the text "commit" comes later.
206 colorful() {
207 read firstline <$1
208 ! expr "$firstline" : "[a-zA-Z]" >/dev/null
211 test_expect_success 'tests can detect color' '
212 rm -f colorful.log colorless.log &&
213 git log --no-color >colorless.log &&
214 git log --color >colorful.log &&
215 ! colorful colorless.log &&
216 colorful colorful.log
219 test_expect_success 'no color when stdout is a regular file' '
220 rm -f colorless.log &&
221 test_config color.ui auto &&
222 git log >colorless.log &&
223 ! colorful colorless.log
226 test_expect_success TTY 'color when writing to a pager' '
227 rm -f paginated.out &&
228 test_config color.ui auto &&
229 test_terminal env TERM=vt100 git log &&
230 colorful paginated.out
233 test_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 &&
237 test_terminal env TERM=vt100 git log &&
238 ! colorful paginated.out
241 test_expect_success 'color when writing to a file intended for a pager' '
242 rm -f colorful.log &&
243 test_config color.ui auto &&
245 TERM=vt100 &&
246 GIT_PAGER_IN_USE=true &&
247 export TERM GIT_PAGER_IN_USE &&
248 git log >colorful.log
249 ) &&
250 colorful colorful.log
253 test_expect_success TTY 'colors are sent to pager for external commands' '
254 test_config alias.externallog "!git log" &&
255 test_config color.ui auto &&
256 test_terminal env TERM=vt100 git -p externallog &&
257 colorful paginated.out
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
264 # your_test() {
265 # parse_args "$@"
267 # $test_expectation "$cmd - behaves well" "
268 # ...
269 # $full_command &&
270 # ...
274 # then your test can be used like this:
276 # your_test expect_(success|failure) [test_must_fail] 'git foo'
278 parse_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 "
288 cmd=$1
289 full_command="$full_command $1"
292 test_default_pager() {
293 parse_args "$@"
295 $test_expectation SIMPLEPAGER,TTY "$cmd - default pager is used by default" "
296 sane_unset PAGER GIT_PAGER &&
297 test_unconfig core.pager &&
298 rm -f default_pager_used &&
299 cat >\$less <<-\EOF &&
300 #!/bin/sh
301 wc >default_pager_used
303 chmod +x \$less &&
305 PATH=.:\$PATH &&
306 export PATH &&
307 $full_command
308 ) &&
309 test -e default_pager_used
313 test_PAGER_overrides() {
314 parse_args "$@"
316 $test_expectation TTY "$cmd - PAGER overrides default pager" "
317 sane_unset GIT_PAGER &&
318 test_unconfig core.pager &&
319 rm -f PAGER_used &&
320 PAGER='wc >PAGER_used' &&
321 export PAGER &&
322 $full_command &&
323 test -e PAGER_used
327 test_core_pager_overrides() {
328 if_local_config=
329 used_if_wanted='overrides PAGER'
330 test_core_pager "$@"
333 test_local_config_ignored() {
334 if_local_config='! '
335 used_if_wanted='is not used'
336 test_core_pager "$@"
339 test_core_pager() {
340 parse_args "$@"
342 $test_expectation TTY "$cmd - repository-local core.pager setting $used_if_wanted" "
343 sane_unset GIT_PAGER &&
344 rm -f core.pager_used &&
345 PAGER=wc &&
346 export PAGER &&
347 test_config core.pager 'wc >core.pager_used' &&
348 $full_command &&
349 ${if_local_config}test -e core.pager_used
353 test_core_pager_subdir() {
354 if_local_config=
355 used_if_wanted='overrides PAGER'
356 test_pager_subdir_helper "$@"
359 test_no_local_config_subdir() {
360 if_local_config='! '
361 used_if_wanted='is not used'
362 test_pager_subdir_helper "$@"
365 test_pager_subdir_helper() {
366 parse_args "$@"
368 $test_expectation TTY "$cmd - core.pager $used_if_wanted from subdirectory" "
369 sane_unset GIT_PAGER &&
370 rm -f core.pager_used &&
371 rm -fr sub &&
372 PAGER=wc &&
373 stampname=\$(pwd)/core.pager_used &&
374 export PAGER stampname &&
375 test_config core.pager 'wc >\"\$stampname\"' &&
376 mkdir sub &&
378 cd sub &&
379 $full_command
380 ) &&
381 ${if_local_config}test -e core.pager_used
385 test_GIT_PAGER_overrides() {
386 parse_args "$@"
388 $test_expectation TTY "$cmd - GIT_PAGER overrides core.pager" "
389 rm -f GIT_PAGER_used &&
390 test_config core.pager wc &&
391 GIT_PAGER='wc >GIT_PAGER_used' &&
392 export GIT_PAGER &&
393 $full_command &&
394 test -e GIT_PAGER_used
398 test_doesnt_paginate() {
399 parse_args "$@"
401 $test_expectation TTY "no pager for '$cmd'" "
402 rm -f GIT_PAGER_used &&
403 GIT_PAGER='wc >GIT_PAGER_used' &&
404 export GIT_PAGER &&
405 $full_command &&
406 ! test -e GIT_PAGER_used
410 test_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 "$@"
418 test_expect_success 'setup: some aliases' '
419 git config alias.aliasedlog log &&
420 git config alias.true "!true"
423 test_pager_choices 'git log'
424 test_pager_choices 'git -p log'
425 test_pager_choices 'git aliasedlog'
427 test_default_pager expect_success 'git -p aliasedlog'
428 test_PAGER_overrides expect_success 'git -p aliasedlog'
429 test_core_pager_overrides expect_success 'git -p aliasedlog'
430 test_core_pager_subdir expect_success 'git -p aliasedlog'
431 test_GIT_PAGER_overrides expect_success 'git -p aliasedlog'
433 test_default_pager expect_success 'git -p true'
434 test_PAGER_overrides expect_success 'git -p true'
435 test_core_pager_overrides expect_success 'git -p true'
436 test_core_pager_subdir expect_success 'git -p true'
437 test_GIT_PAGER_overrides expect_success 'git -p true'
439 test_default_pager expect_success test_must_fail 'git -p request-pull'
440 test_PAGER_overrides expect_success test_must_fail 'git -p request-pull'
441 test_core_pager_overrides expect_success test_must_fail 'git -p request-pull'
442 test_core_pager_subdir expect_success test_must_fail 'git -p request-pull'
443 test_GIT_PAGER_overrides expect_success test_must_fail 'git -p request-pull'
445 test_default_pager expect_success test_must_fail 'git -p'
446 test_PAGER_overrides expect_success test_must_fail 'git -p'
447 test_local_config_ignored expect_failure test_must_fail 'git -p'
448 test_GIT_PAGER_overrides expect_success test_must_fail 'git -p'
450 test_expect_success TTY 'core.pager in repo config works and retains cwd' '
451 sane_unset GIT_PAGER &&
452 test_config core.pager "cat >cwd-retained" &&
454 cd sub &&
455 rm -f cwd-retained &&
456 test_terminal git -p rev-parse HEAD &&
457 test_path_is_file cwd-retained
461 test_expect_success TTY 'core.pager is found via alias in subdirectory' '
462 sane_unset GIT_PAGER &&
463 test_config core.pager "cat >via-alias" &&
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
472 test_doesnt_paginate expect_failure test_must_fail 'git -p nonsense'
474 test_pager_choices 'git shortlog'
475 test_expect_success 'setup: configure shortlog not to paginate' '
476 git config pager.shortlog false
478 test_doesnt_paginate expect_success 'git shortlog'
479 test_no_local_config_subdir expect_success 'git shortlog'
480 test_default_pager expect_success 'git -p shortlog'
481 test_core_pager_subdir expect_success 'git -p shortlog'
483 test_core_pager_subdir expect_success test_must_fail \
484 'git -p apply </dev/null'
486 test_expect_success TTY 'command-specific pager' '
487 sane_unset PAGER GIT_PAGER &&
488 echo "foo:initial" >expect &&
489 >actual &&
490 test_unconfig core.pager &&
491 test_config pager.log "sed s/^/foo:/ >actual" &&
492 test_terminal git log --format=%s -1 &&
493 test_cmp expect actual
496 test_expect_success TTY 'command-specific pager overrides core.pager' '
497 sane_unset PAGER GIT_PAGER &&
498 echo "foo:initial" >expect &&
499 >actual &&
500 test_config core.pager "exit 1" &&
501 test_config pager.log "sed s/^/foo:/ >actual" &&
502 test_terminal git log --format=%s -1 &&
503 test_cmp expect actual
506 test_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 &&
510 test_config pager.log "exit 1" &&
511 test_terminal git log --format=%s -1 &&
512 test_cmp expect actual
515 test_expect_success 'setup external command' '
516 cat >git-external <<-\EOF &&
517 #!/bin/sh
518 git "$@"
520 chmod +x git-external
523 test_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" &&
528 test_terminal git --exec-path="$(pwd)" external log --format=%s -1 &&
529 test_cmp expect actual
532 test_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
541 test_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
551 test_expect_success 'command with underscores does not complain' '
552 write_script git-under_score <<-\EOF &&
553 echo ok
555 git --exec-path=. under_score >actual 2>&1 &&
556 echo ok >expect &&
557 test_cmp expect actual
560 test_done