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
'some commands do not use a pager' '
41 rm -f paginated.out &&
42 test_terminal git rev-list HEAD &&
43 ! test -e paginated.out
46 test_expect_success
'no pager when stdout is a pipe' '
47 rm -f paginated.out &&
49 ! test -e paginated.out
52 test_expect_success
'no pager when stdout is a regular file' '
53 rm -f paginated.out &&
55 ! test -e paginated.out
58 test_expect_success TTY
'git --paginate rev-list uses a pager' '
59 rm -f paginated.out &&
60 test_terminal git --paginate rev-list HEAD &&
64 test_expect_success
'no pager even with --paginate when stdout is a pipe' '
65 rm -f file paginated.out &&
66 git --paginate log | cat &&
67 ! test -e paginated.out
70 test_expect_success TTY
'no pager with --no-pager' '
71 rm -f paginated.out &&
72 test_terminal git --no-pager log &&
73 ! test -e paginated.out
76 test_expect_success TTY
'configuration can disable pager' '
77 rm -f paginated.out &&
78 test_unconfig pager.grep &&
79 test_terminal git grep initial &&
80 test -e paginated.out &&
82 rm -f paginated.out &&
83 test_config pager.grep false &&
84 test_terminal git grep initial &&
85 ! test -e paginated.out
88 test_expect_success TTY
'git config uses a pager if configured to' '
89 rm -f paginated.out &&
90 test_config pager.config true &&
91 test_terminal git config --list &&
95 test_expect_success TTY
'configuration can enable pager (from subdir)' '
96 rm -f paginated.out &&
98 test_config pager.bundle true &&
100 git bundle create test.bundle --all &&
101 rm -f paginated.out subdir/paginated.out &&
104 test_terminal git bundle unbundle ../test.bundle
107 test -e paginated.out ||
108 test -e subdir/paginated.out
112 # A colored commit log will begin with an appropriate ANSI escape
113 # for the first color; the text "commit" comes later.
116 ! expr "$firstline" : "[a-zA-Z]" >/dev
/null
119 test_expect_success
'tests can detect color' '
120 rm -f colorful.log colorless.log &&
121 git log --no-color >colorless.log &&
122 git log --color >colorful.log &&
123 ! colorful colorless.log &&
124 colorful colorful.log
127 test_expect_success
'no color when stdout is a regular file' '
128 rm -f colorless.log &&
129 test_config color.ui auto &&
130 git log >colorless.log &&
131 ! colorful colorless.log
134 test_expect_success TTY
'color when writing to a pager' '
135 rm -f paginated.out &&
136 test_config color.ui auto &&
140 test_terminal git log
142 colorful paginated.out
145 test_expect_success TTY
'colors are suppressed by color.pager' '
146 rm -f paginated.out &&
147 test_config color.ui auto &&
148 test_config color.pager false &&
152 test_terminal git log
154 ! colorful paginated.out
157 test_expect_success
'color when writing to a file intended for a pager' '
158 rm -f colorful.log &&
159 test_config color.ui auto &&
162 GIT_PAGER_IN_USE=true &&
163 export TERM GIT_PAGER_IN_USE &&
164 git log >colorful.log
166 colorful colorful.log
169 test_expect_success TTY
'colors are sent to pager for external commands' '
170 test_config alias.externallog "!git log" &&
171 test_config color.ui auto &&
175 test_terminal git -p externallog
177 colorful paginated.out
180 # Use this helper to make it easy for the caller of your
181 # terminal-using function to specify whether it should fail.
187 # $test_expectation "$cmd - behaves well" "
194 # then your test can be used like this:
196 # your_test expect_(success|failure) [test_must_fail] 'git foo'
199 test_expectation
="test_$1"
201 if test "$1" = test_must_fail
203 full_command
="test_must_fail test_terminal "
206 full_command
="test_terminal "
209 full_command
="$full_command $1"
212 test_default_pager
() {
215 $test_expectation SIMPLEPAGER
,TTY
"$cmd - default pager is used by default" "
216 sane_unset PAGER GIT_PAGER &&
217 test_unconfig core.pager &&
218 rm -f default_pager_used &&
219 cat >\$less <<-\EOF &&
221 wc >default_pager_used
229 test -e default_pager_used
233 test_PAGER_overrides
() {
236 $test_expectation TTY
"$cmd - PAGER overrides default pager" "
237 sane_unset GIT_PAGER &&
238 test_unconfig core.pager &&
240 PAGER='wc >PAGER_used' &&
247 test_core_pager_overrides
() {
249 used_if_wanted
='overrides PAGER'
253 test_local_config_ignored
() {
255 used_if_wanted
='is not used'
262 $test_expectation TTY
"$cmd - repository-local core.pager setting $used_if_wanted" "
263 sane_unset GIT_PAGER &&
264 rm -f core.pager_used &&
267 test_config core.pager 'wc >core.pager_used' &&
269 ${if_local_config}test -e core.pager_used
273 test_core_pager_subdir
() {
275 used_if_wanted
='overrides PAGER'
276 test_pager_subdir_helper
"$@"
279 test_no_local_config_subdir
() {
281 used_if_wanted
='is not used'
282 test_pager_subdir_helper
"$@"
285 test_pager_subdir_helper
() {
288 $test_expectation TTY
"$cmd - core.pager $used_if_wanted from subdirectory" "
289 sane_unset GIT_PAGER &&
290 rm -f core.pager_used &&
293 stampname=\$(pwd)/core.pager_used &&
294 export PAGER stampname &&
295 test_config core.pager 'wc >\"\$stampname\"' &&
301 ${if_local_config}test -e core.pager_used
305 test_GIT_PAGER_overrides
() {
308 $test_expectation TTY
"$cmd - GIT_PAGER overrides core.pager" "
309 rm -f GIT_PAGER_used &&
310 test_config core.pager wc &&
311 GIT_PAGER='wc >GIT_PAGER_used' &&
314 test -e GIT_PAGER_used
318 test_doesnt_paginate
() {
321 $test_expectation TTY
"no pager for '$cmd'" "
322 rm -f GIT_PAGER_used &&
323 GIT_PAGER='wc >GIT_PAGER_used' &&
326 ! test -e GIT_PAGER_used
330 test_pager_choices
() {
331 test_default_pager expect_success
"$@"
332 test_PAGER_overrides expect_success
"$@"
333 test_core_pager_overrides expect_success
"$@"
334 test_core_pager_subdir expect_success
"$@"
335 test_GIT_PAGER_overrides expect_success
"$@"
338 test_expect_success
'setup: some aliases' '
339 git config alias.aliasedlog log &&
340 git config alias.true "!true"
343 test_pager_choices
'git log'
344 test_pager_choices
'git -p log'
345 test_pager_choices
'git aliasedlog'
347 test_default_pager expect_success
'git -p aliasedlog'
348 test_PAGER_overrides expect_success
'git -p aliasedlog'
349 test_core_pager_overrides expect_success
'git -p aliasedlog'
350 test_core_pager_subdir expect_failure
'git -p aliasedlog'
351 test_GIT_PAGER_overrides expect_success
'git -p aliasedlog'
353 test_default_pager expect_success
'git -p true'
354 test_PAGER_overrides expect_success
'git -p true'
355 test_core_pager_overrides expect_success
'git -p true'
356 test_core_pager_subdir expect_failure
'git -p true'
357 test_GIT_PAGER_overrides expect_success
'git -p true'
359 test_default_pager expect_success test_must_fail
'git -p request-pull'
360 test_PAGER_overrides expect_success test_must_fail
'git -p request-pull'
361 test_core_pager_overrides expect_success test_must_fail
'git -p request-pull'
362 test_core_pager_subdir expect_failure test_must_fail
'git -p request-pull'
363 test_GIT_PAGER_overrides expect_success test_must_fail
'git -p request-pull'
365 test_default_pager expect_success test_must_fail
'git -p'
366 test_PAGER_overrides expect_success test_must_fail
'git -p'
367 test_local_config_ignored expect_failure test_must_fail
'git -p'
368 test_no_local_config_subdir expect_success test_must_fail
'git -p'
369 test_GIT_PAGER_overrides expect_success test_must_fail
'git -p'
371 test_doesnt_paginate expect_failure test_must_fail
'git -p nonsense'
373 test_pager_choices
'git shortlog'
374 test_expect_success
'setup: configure shortlog not to paginate' '
375 git config pager.shortlog false
377 test_doesnt_paginate expect_success
'git shortlog'
378 test_no_local_config_subdir expect_success
'git shortlog'
379 test_default_pager expect_success
'git -p shortlog'
380 test_core_pager_subdir expect_success
'git -p shortlog'
382 test_core_pager_subdir expect_success test_must_fail \
383 'git -p apply </dev/null'
385 test_expect_success TTY
'command-specific pager' '
386 sane_unset PAGER GIT_PAGER &&
387 echo "foo:initial" >expect &&
389 test_unconfig core.pager &&
390 test_config pager.log "sed s/^/foo:/ >actual" &&
391 test_terminal git log --format=%s -1 &&
392 test_cmp expect actual
395 test_expect_success TTY
'command-specific pager overrides core.pager' '
396 sane_unset PAGER GIT_PAGER &&
397 echo "foo:initial" >expect &&
399 test_config core.pager "exit 1"
400 test_config pager.log "sed s/^/foo:/ >actual" &&
401 test_terminal git log --format=%s -1 &&
402 test_cmp expect actual
405 test_expect_success TTY
'command-specific pager overridden by environment' '
406 GIT_PAGER="sed s/^/foo:/ >actual" && export GIT_PAGER &&
408 echo "foo:initial" >expect &&
409 test_config pager.log "exit 1" &&
410 test_terminal git log --format=%s -1 &&
411 test_cmp expect actual
414 test_expect_success
'setup external command' '
415 cat >git-external <<-\EOF &&
419 chmod +x git-external
422 test_expect_success TTY
'command-specific pager works for external commands' '
423 sane_unset PAGER GIT_PAGER &&
424 echo "foo:initial" >expect &&
426 test_config pager.external "sed s/^/foo:/ >actual" &&
427 test_terminal git --exec-path="`pwd`" external log --format=%s -1 &&
428 test_cmp expect actual
431 test_expect_success TTY
'sub-commands of externals use their own pager' '
432 sane_unset PAGER GIT_PAGER &&
433 echo "foo:initial" >expect &&
435 test_config pager.log "sed s/^/foo:/ >actual" &&
436 test_terminal git --exec-path=. external log --format=%s -1 &&
437 test_cmp expect actual
440 test_expect_success TTY
'external command pagers override sub-commands' '
441 sane_unset PAGER GIT_PAGER &&
444 test_config pager.external false &&
445 test_config pager.log "sed s/^/log:/ >actual" &&
446 test_terminal git --exec-path=. external log --format=%s -1 &&
447 test_cmp expect actual