3 test_description='Test automatic use of a pager.'
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" &&
19 test_expect_success TTY 'some commands use a pager' '
20 rm -f paginated.out &&
21 test_terminal git log &&
25 test_expect_failure TTY 'pager runs from subdir' '
26 echo subdir/paginated.out >expected &&
28 rm -f paginated.out subdir/paginated.out &&
34 ls paginated.out subdir/paginated.out ||
37 test_cmp expected actual
40 test_expect_success TTY 'LESS and LV envvars are set for pagination' '
43 PAGER="env >pager-env.out; wc" &&
48 grep ^LESS= pager-env.out &&
49 grep ^LV= pager-env.out
52 test_expect_success TTY 'some commands do not use a pager' '
53 rm -f paginated.out &&
54 test_terminal git rev-list HEAD &&
55 ! test -e paginated.out
58 test_expect_success 'no pager when stdout is a pipe' '
59 rm -f paginated.out &&
61 ! test -e paginated.out
64 test_expect_success 'no pager when stdout is a regular file' '
65 rm -f paginated.out &&
67 ! test -e paginated.out
70 test_expect_success TTY 'git --paginate rev-list uses a pager' '
71 rm -f paginated.out &&
72 test_terminal git --paginate rev-list HEAD &&
76 test_expect_success 'no pager even with --paginate when stdout is a pipe' '
77 rm -f file paginated.out &&
78 git --paginate log | cat &&
79 ! test -e paginated.out
82 test_expect_success TTY 'no pager with --no-pager' '
83 rm -f paginated.out &&
84 test_terminal git --no-pager log &&
85 ! test -e paginated.out
88 test_expect_success TTY 'configuration can disable pager' '
89 rm -f paginated.out &&
90 test_unconfig pager.grep &&
91 test_terminal git grep initial &&
92 test -e paginated.out &&
94 rm -f paginated.out &&
95 test_config pager.grep false &&
96 test_terminal git grep initial &&
97 ! test -e paginated.out
100 test_expect_success TTY 'git config uses a pager if configured to' '
101 rm -f paginated.out &&
102 test_config pager.config true &&
103 test_terminal git config --list &&
104 test -e paginated.out
107 test_expect_success TTY 'configuration can enable pager (from subdir)' '
108 rm -f paginated.out &&
110 test_config pager.bundle true &&
112 git bundle create test.bundle --all &&
113 rm -f paginated.out subdir/paginated.out &&
116 test_terminal git bundle unbundle ../test.bundle
119 test -e paginated.out ||
120 test -e subdir/paginated.out
124 # A colored commit log will begin with an appropriate ANSI escape
125 # for the first color; the text "commit" comes later.
128 ! expr "$firstline" : "[a-zA-Z]" >/dev/null
131 test_expect_success 'tests can detect color' '
132 rm -f colorful.log colorless.log &&
133 git log --no-color >colorless.log &&
134 git log --color >colorful.log &&
135 ! colorful colorless.log &&
136 colorful colorful.log
139 test_expect_success 'no color when stdout is a regular file' '
140 rm -f colorless.log &&
141 test_config color.ui auto &&
142 git log >colorless.log &&
143 ! colorful colorless.log
146 test_expect_success TTY 'color when writing to a pager' '
147 rm -f paginated.out &&
148 test_config color.ui auto &&
152 test_terminal git log
154 colorful paginated.out
157 test_expect_success TTY 'colors are suppressed by color.pager' '
158 rm -f paginated.out &&
159 test_config color.ui auto &&
160 test_config color.pager false &&
164 test_terminal git log
166 ! colorful paginated.out
169 test_expect_success 'color when writing to a file intended for a pager' '
170 rm -f colorful.log &&
171 test_config color.ui auto &&
174 GIT_PAGER_IN_USE=true &&
175 export TERM GIT_PAGER_IN_USE &&
176 git log >colorful.log
178 colorful colorful.log
181 test_expect_success TTY 'colors are sent to pager for external commands' '
182 test_config alias.externallog "!git log" &&
183 test_config color.ui auto &&
187 test_terminal git -p externallog
189 colorful paginated.out
192 # Use this helper to make it easy for the caller of your
193 # terminal-using function to specify whether it should fail.
199 # $test_expectation "$cmd - behaves well" "
206 # then your test can be used like this:
208 # your_test expect_(success|failure) [test_must_fail] 'git foo'
211 test_expectation="test_$1"
213 if test "$1" = test_must_fail
215 full_command="test_must_fail test_terminal "
218 full_command="test_terminal "
221 full_command="$full_command $1"
224 test_default_pager() {
227 $test_expectation SIMPLEPAGER,TTY "$cmd - default pager is used by default" "
228 sane_unset PAGER GIT_PAGER &&
229 test_unconfig core.pager &&
230 rm -f default_pager_used &&
231 cat >\$less <<-\EOF &&
233 wc >default_pager_used
241 test -e default_pager_used
245 test_PAGER_overrides() {
248 $test_expectation TTY "$cmd - PAGER overrides default pager" "
249 sane_unset GIT_PAGER &&
250 test_unconfig core.pager &&
252 PAGER='wc >PAGER_used' &&
259 test_core_pager_overrides() {
261 used_if_wanted='overrides PAGER'
265 test_local_config_ignored() {
267 used_if_wanted='is not used'
274 $test_expectation TTY "$cmd - repository-local core.pager setting $used_if_wanted" "
275 sane_unset GIT_PAGER &&
276 rm -f core.pager_used &&
279 test_config core.pager 'wc >core.pager_used' &&
281 ${if_local_config}test -e core.pager_used
285 test_core_pager_subdir() {
287 used_if_wanted='overrides PAGER'
288 test_pager_subdir_helper "$@"
291 test_no_local_config_subdir() {
293 used_if_wanted='is not used'
294 test_pager_subdir_helper "$@"
297 test_pager_subdir_helper() {
300 $test_expectation TTY "$cmd - core.pager $used_if_wanted from subdirectory" "
301 sane_unset GIT_PAGER &&
302 rm -f core.pager_used &&
305 stampname=\$(pwd)/core.pager_used &&
306 export PAGER stampname &&
307 test_config core.pager 'wc >\"\$stampname\"' &&
313 ${if_local_config}test -e core.pager_used
317 test_GIT_PAGER_overrides() {
320 $test_expectation TTY "$cmd - GIT_PAGER overrides core.pager" "
321 rm -f GIT_PAGER_used &&
322 test_config core.pager wc &&
323 GIT_PAGER='wc >GIT_PAGER_used' &&
326 test -e GIT_PAGER_used
330 test_doesnt_paginate() {
333 $test_expectation TTY "no pager for '$cmd'" "
334 rm -f GIT_PAGER_used &&
335 GIT_PAGER='wc >GIT_PAGER_used' &&
338 ! test -e GIT_PAGER_used
342 test_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 "$@"
350 test_expect_success 'setup: some aliases' '
351 git config alias.aliasedlog log &&
352 git config alias.true "!true"
355 test_pager_choices 'git log'
356 test_pager_choices 'git -p log'
357 test_pager_choices 'git aliasedlog'
359 test_default_pager expect_success 'git -p aliasedlog'
360 test_PAGER_overrides expect_success 'git -p aliasedlog'
361 test_core_pager_overrides expect_success 'git -p aliasedlog'
362 test_core_pager_subdir expect_failure 'git -p aliasedlog'
363 test_GIT_PAGER_overrides expect_success 'git -p aliasedlog'
365 test_default_pager expect_success 'git -p true'
366 test_PAGER_overrides expect_success 'git -p true'
367 test_core_pager_overrides expect_success 'git -p true'
368 test_core_pager_subdir expect_failure 'git -p true'
369 test_GIT_PAGER_overrides expect_success 'git -p true'
371 test_default_pager expect_success test_must_fail 'git -p request-pull'
372 test_PAGER_overrides expect_success test_must_fail 'git -p request-pull'
373 test_core_pager_overrides expect_success test_must_fail 'git -p request-pull'
374 test_core_pager_subdir expect_failure test_must_fail 'git -p request-pull'
375 test_GIT_PAGER_overrides expect_success test_must_fail 'git -p request-pull'
377 test_default_pager expect_success test_must_fail 'git -p'
378 test_PAGER_overrides expect_success test_must_fail 'git -p'
379 test_local_config_ignored expect_failure test_must_fail 'git -p'
380 test_no_local_config_subdir expect_success test_must_fail 'git -p'
381 test_GIT_PAGER_overrides expect_success test_must_fail 'git -p'
383 test_doesnt_paginate expect_failure test_must_fail 'git -p nonsense'
385 test_pager_choices 'git shortlog'
386 test_expect_success 'setup: configure shortlog not to paginate' '
387 git config pager.shortlog false
389 test_doesnt_paginate expect_success 'git shortlog'
390 test_no_local_config_subdir expect_success 'git shortlog'
391 test_default_pager expect_success 'git -p shortlog'
392 test_core_pager_subdir expect_success 'git -p shortlog'
394 test_core_pager_subdir expect_success test_must_fail \
395 'git -p apply </dev/null'
397 test_expect_success TTY 'command-specific pager' '
398 sane_unset PAGER GIT_PAGER &&
399 echo "foo:initial" >expect &&
401 test_unconfig core.pager &&
402 test_config pager.log "sed s/^/foo:/ >actual" &&
403 test_terminal git log --format=%s -1 &&
404 test_cmp expect actual
407 test_expect_success TTY 'command-specific pager overrides core.pager' '
408 sane_unset PAGER GIT_PAGER &&
409 echo "foo:initial" >expect &&
411 test_config core.pager "exit 1"
412 test_config pager.log "sed s/^/foo:/ >actual" &&
413 test_terminal git log --format=%s -1 &&
414 test_cmp expect actual
417 test_expect_success TTY 'command-specific pager overridden by environment' '
418 GIT_PAGER="sed s/^/foo:/ >actual" && export GIT_PAGER &&
420 echo "foo:initial" >expect &&
421 test_config pager.log "exit 1" &&
422 test_terminal git log --format=%s -1 &&
423 test_cmp expect actual
426 test_expect_success 'setup external command' '
427 cat >git-external <<-\EOF &&
431 chmod +x git-external
434 test_expect_success TTY 'command-specific pager works for external commands' '
435 sane_unset PAGER GIT_PAGER &&
436 echo "foo:initial" >expect &&
438 test_config pager.external "sed s/^/foo:/ >actual" &&
439 test_terminal git --exec-path="`pwd`" external log --format=%s -1 &&
440 test_cmp expect actual
443 test_expect_success TTY 'sub-commands of externals use their own pager' '
444 sane_unset PAGER GIT_PAGER &&
445 echo "foo:initial" >expect &&
447 test_config pager.log "sed s/^/foo:/ >actual" &&
448 test_terminal git --exec-path=. external log --format=%s -1 &&
449 test_cmp expect actual
452 test_expect_success TTY 'external command pagers override sub-commands' '
453 sane_unset PAGER GIT_PAGER &&
456 test_config pager.external false &&
457 test_config pager.log "sed s/^/log:/ >actual" &&
458 test_terminal git --exec-path=. external log --format=%s -1 &&
459 test_cmp expect actual