3 test_description='Test automatic use of a pager.'
6 . "$TEST_DIRECTORY"/lib-pager.sh
9 echo >&2 cleanup failed
13 test_expect_success 'set up terminal for tests' '
14 rm -f stdout_is_tty ||
21 test_have_prereq PERL &&
22 "$PERL_PATH" "$TEST_DIRECTORY"/t7006/test-terminal.perl \
29 if test -e stdout_is_tty
31 test_terminal() { "$@"; }
33 elif test -e test_terminal_works
36 "$PERL_PATH" "$TEST_DIRECTORY"/t7006/test-terminal.perl "$@"
40 say "# no usable terminal, so skipping some tests"
43 test_expect_success 'setup' '
44 unset GIT_PAGER GIT_PAGER_IN_USE;
45 test_might_fail git config --unset core.pager &&
47 PAGER="cat >paginated.out" &&
53 test_expect_success TTY 'some commands use a pager' '
54 rm -f paginated.out ||
57 test_terminal git log &&
61 test_expect_failure TTY 'pager runs from subdir' '
62 echo subdir/paginated.out >expected &&
64 rm -f paginated.out subdir/paginated.out &&
70 ls paginated.out subdir/paginated.out ||
73 test_cmp expected actual
76 test_expect_success TTY 'some commands do not use a pager' '
77 rm -f paginated.out ||
80 test_terminal git rev-list HEAD &&
81 ! test -e paginated.out
84 test_expect_success 'no pager when stdout is a pipe' '
85 rm -f paginated.out ||
89 ! test -e paginated.out
92 test_expect_success 'no pager when stdout is a regular file' '
93 rm -f paginated.out ||
97 ! test -e paginated.out
100 test_expect_success TTY 'git --paginate rev-list uses a pager' '
101 rm -f paginated.out ||
104 test_terminal git --paginate rev-list HEAD &&
105 test -e paginated.out
108 test_expect_success 'no pager even with --paginate when stdout is a pipe' '
109 rm -f file paginated.out ||
112 git --paginate log | cat &&
113 ! test -e paginated.out
116 test_expect_success TTY 'no pager with --no-pager' '
117 rm -f paginated.out ||
120 test_terminal git --no-pager log &&
121 ! test -e paginated.out
124 test_expect_success TTY 'configuration can disable pager' '
125 rm -f paginated.out &&
126 test_might_fail git config --unset pager.grep &&
127 test_terminal git grep initial &&
128 test -e paginated.out &&
130 rm -f paginated.out &&
131 git config pager.grep false &&
132 test_when_finished "git config --unset pager.grep" &&
133 test_terminal git grep initial &&
134 ! test -e paginated.out
137 test_expect_success 'configuration can enable pager (from subdir)' '
138 rm -f paginated.out &&
140 git config pager.bundle true &&
141 test_when_finished "git config --unset pager.bundle" &&
143 git bundle create test.bundle --all &&
144 rm -f paginated.out subdir/paginated.out &&
147 test_terminal git bundle unbundle ../test.bundle
150 test -e paginated.out ||
151 test -e subdir/paginated.out
155 # A colored commit log will begin with an appropriate ANSI escape
156 # for the first color; the text "commit" comes later.
159 ! expr "$firstline" : "[a-zA-Z]" >/dev/null
162 test_expect_success 'tests can detect color' '
163 rm -f colorful.log colorless.log ||
166 git log --no-color >colorless.log &&
167 git log --color >colorful.log &&
168 ! colorful colorless.log &&
169 colorful colorful.log
172 test_expect_success 'no color when stdout is a regular file' '
173 rm -f colorless.log &&
174 git config color.ui auto ||
177 git log >colorless.log &&
178 ! colorful colorless.log
181 test_expect_success TTY 'color when writing to a pager' '
182 rm -f paginated.out &&
183 git config color.ui auto ||
189 test_terminal git log
191 colorful paginated.out
194 test_expect_success 'color when writing to a file intended for a pager' '
195 rm -f colorful.log &&
196 git config color.ui auto ||
201 GIT_PAGER_IN_USE=true &&
202 export TERM GIT_PAGER_IN_USE &&
203 git log >colorful.log
205 colorful colorful.log
208 if test_have_prereq SIMPLEPAGER && test_have_prereq TTY
210 test_set_prereq SIMPLEPAGERTTY
213 # Use this helper to make it easy for the caller of your
214 # terminal-using function to specify whether it should fail.
220 # $test_expectation "$cmd - behaves well" "
227 # then your test can be used like this:
229 # your_test expect_(success|failure) [test_must_fail] 'git foo'
232 test_expectation="test_$1"
234 if test "$1" = test_must_fail
236 full_command="test_must_fail test_terminal "
239 full_command="test_terminal "
242 full_command="$full_command $1"
245 test_default_pager() {
248 $test_expectation SIMPLEPAGERTTY "$cmd - default pager is used by default" "
249 unset PAGER GIT_PAGER;
250 test_might_fail git config --unset core.pager &&
251 rm -f default_pager_used ||
254 cat >\$less <<-\EOF &&
256 wc >default_pager_used
264 test -e default_pager_used
268 test_PAGER_overrides() {
271 $test_expectation TTY "$cmd - PAGER overrides default pager" "
273 test_might_fail git config --unset core.pager &&
277 PAGER='wc >PAGER_used' &&
284 test_core_pager_overrides() {
286 used_if_wanted='overrides PAGER'
290 test_local_config_ignored() {
292 used_if_wanted='is not used'
299 $test_expectation TTY "$cmd - repository-local core.pager setting $used_if_wanted" "
301 rm -f core.pager_used ||
306 git config core.pager 'wc >core.pager_used' &&
308 ${if_local_config}test -e core.pager_used
312 test_core_pager_subdir() {
314 used_if_wanted='overrides PAGER'
315 test_pager_subdir_helper "$@"
318 test_no_local_config_subdir() {
320 used_if_wanted='is not used'
321 test_pager_subdir_helper "$@"
324 test_pager_subdir_helper() {
327 $test_expectation TTY "$cmd - core.pager $used_if_wanted from subdirectory" "
329 rm -f core.pager_used &&
334 stampname=\$(pwd)/core.pager_used &&
335 export PAGER stampname &&
336 git config core.pager 'wc >\"\$stampname\"' &&
342 ${if_local_config}test -e core.pager_used
346 test_GIT_PAGER_overrides() {
349 $test_expectation TTY "$cmd - GIT_PAGER overrides core.pager" "
350 rm -f GIT_PAGER_used ||
353 git config core.pager wc &&
354 GIT_PAGER='wc >GIT_PAGER_used' &&
357 test -e GIT_PAGER_used
361 test_doesnt_paginate() {
364 $test_expectation TTY "no pager for '$cmd'" "
365 rm -f GIT_PAGER_used ||
368 GIT_PAGER='wc >GIT_PAGER_used' &&
371 ! test -e GIT_PAGER_used
375 test_pager_choices() {
376 test_default_pager expect_success "$@"
377 test_PAGER_overrides expect_success "$@"
378 test_core_pager_overrides expect_success "$@"
379 test_core_pager_subdir expect_success "$@"
380 test_GIT_PAGER_overrides expect_success "$@"
383 test_expect_success 'setup: some aliases' '
384 git config alias.aliasedlog log &&
385 git config alias.true "!true"
388 test_pager_choices 'git log'
389 test_pager_choices 'git -p log'
390 test_pager_choices 'git aliasedlog'
392 test_default_pager expect_success 'git -p aliasedlog'
393 test_PAGER_overrides expect_success 'git -p aliasedlog'
394 test_core_pager_overrides expect_success 'git -p aliasedlog'
395 test_core_pager_subdir expect_failure 'git -p aliasedlog'
396 test_GIT_PAGER_overrides expect_success 'git -p aliasedlog'
398 test_default_pager expect_success 'git -p true'
399 test_PAGER_overrides expect_success 'git -p true'
400 test_core_pager_overrides expect_success 'git -p true'
401 test_core_pager_subdir expect_failure 'git -p true'
402 test_GIT_PAGER_overrides expect_success 'git -p true'
404 test_default_pager expect_success test_must_fail 'git -p request-pull'
405 test_PAGER_overrides expect_success test_must_fail 'git -p request-pull'
406 test_core_pager_overrides expect_success test_must_fail 'git -p request-pull'
407 test_core_pager_subdir expect_failure test_must_fail 'git -p request-pull'
408 test_GIT_PAGER_overrides expect_success test_must_fail 'git -p request-pull'
410 test_default_pager expect_success test_must_fail 'git -p'
411 test_PAGER_overrides expect_success test_must_fail 'git -p'
412 test_local_config_ignored expect_failure test_must_fail 'git -p'
413 test_no_local_config_subdir expect_success test_must_fail 'git -p'
414 test_GIT_PAGER_overrides expect_success test_must_fail 'git -p'
416 test_doesnt_paginate expect_failure test_must_fail 'git -p nonsense'
418 test_pager_choices 'git shortlog'
419 test_expect_success 'setup: configure shortlog not to paginate' '
420 git config pager.shortlog false
422 test_doesnt_paginate expect_success 'git shortlog'
423 test_no_local_config_subdir expect_success 'git shortlog'
424 test_default_pager expect_success 'git -p shortlog'
425 test_core_pager_subdir expect_success 'git -p shortlog'
427 test_core_pager_subdir expect_success test_must_fail \
428 'git -p apply </dev/null'