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
'git config uses a pager if configured to' '
138 rm -f paginated.out &&
139 git config pager.config true &&
140 test_when_finished "git config --unset pager.config" &&
141 test_terminal git config --list &&
142 test -e paginated.out
145 test_expect_success
'configuration can enable pager (from subdir)' '
146 rm -f paginated.out &&
148 git config pager.bundle true &&
149 test_when_finished "git config --unset pager.bundle" &&
151 git bundle create test.bundle --all &&
152 rm -f paginated.out subdir/paginated.out &&
155 test_terminal git bundle unbundle ../test.bundle
158 test -e paginated.out ||
159 test -e subdir/paginated.out
163 # A colored commit log will begin with an appropriate ANSI escape
164 # for the first color; the text "commit" comes later.
167 ! expr "$firstline" : "[a-zA-Z]" >/dev
/null
170 test_expect_success
'tests can detect color' '
171 rm -f colorful.log colorless.log ||
174 git log --no-color >colorless.log &&
175 git log --color >colorful.log &&
176 ! colorful colorless.log &&
177 colorful colorful.log
180 test_expect_success
'no color when stdout is a regular file' '
181 rm -f colorless.log &&
182 git config color.ui auto ||
185 git log >colorless.log &&
186 ! colorful colorless.log
189 test_expect_success TTY
'color when writing to a pager' '
190 rm -f paginated.out &&
191 git config color.ui auto ||
197 test_terminal git log
199 colorful paginated.out
202 test_expect_success
'color when writing to a file intended for a pager' '
203 rm -f colorful.log &&
204 git config color.ui auto ||
209 GIT_PAGER_IN_USE=true &&
210 export TERM GIT_PAGER_IN_USE &&
211 git log >colorful.log
213 colorful colorful.log
216 if test_have_prereq SIMPLEPAGER
&& test_have_prereq TTY
218 test_set_prereq SIMPLEPAGERTTY
221 # Use this helper to make it easy for the caller of your
222 # terminal-using function to specify whether it should fail.
228 # $test_expectation "$cmd - behaves well" "
235 # then your test can be used like this:
237 # your_test expect_(success|failure) [test_must_fail] 'git foo'
240 test_expectation
="test_$1"
242 if test "$1" = test_must_fail
244 full_command
="test_must_fail test_terminal "
247 full_command
="test_terminal "
250 full_command
="$full_command $1"
253 test_default_pager
() {
256 $test_expectation SIMPLEPAGERTTY
"$cmd - default pager is used by default" "
257 unset PAGER GIT_PAGER;
258 test_might_fail git config --unset core.pager &&
259 rm -f default_pager_used ||
262 cat >\$less <<-\EOF &&
264 wc >default_pager_used
272 test -e default_pager_used
276 test_PAGER_overrides
() {
279 $test_expectation TTY
"$cmd - PAGER overrides default pager" "
281 test_might_fail git config --unset core.pager &&
285 PAGER='wc >PAGER_used' &&
292 test_core_pager_overrides
() {
294 used_if_wanted
='overrides PAGER'
298 test_local_config_ignored
() {
300 used_if_wanted
='is not used'
307 $test_expectation TTY
"$cmd - repository-local core.pager setting $used_if_wanted" "
309 rm -f core.pager_used ||
314 git config core.pager 'wc >core.pager_used' &&
316 ${if_local_config}test -e core.pager_used
320 test_core_pager_subdir
() {
322 used_if_wanted
='overrides PAGER'
323 test_pager_subdir_helper
"$@"
326 test_no_local_config_subdir
() {
328 used_if_wanted
='is not used'
329 test_pager_subdir_helper
"$@"
332 test_pager_subdir_helper
() {
335 $test_expectation TTY
"$cmd - core.pager $used_if_wanted from subdirectory" "
337 rm -f core.pager_used &&
342 stampname=\$(pwd)/core.pager_used &&
343 export PAGER stampname &&
344 git config core.pager 'wc >\"\$stampname\"' &&
350 ${if_local_config}test -e core.pager_used
354 test_GIT_PAGER_overrides
() {
357 $test_expectation TTY
"$cmd - GIT_PAGER overrides core.pager" "
358 rm -f GIT_PAGER_used ||
361 git config core.pager wc &&
362 GIT_PAGER='wc >GIT_PAGER_used' &&
365 test -e GIT_PAGER_used
369 test_doesnt_paginate
() {
372 $test_expectation TTY
"no pager for '$cmd'" "
373 rm -f GIT_PAGER_used ||
376 GIT_PAGER='wc >GIT_PAGER_used' &&
379 ! test -e GIT_PAGER_used
383 test_pager_choices
() {
384 test_default_pager expect_success
"$@"
385 test_PAGER_overrides expect_success
"$@"
386 test_core_pager_overrides expect_success
"$@"
387 test_core_pager_subdir expect_success
"$@"
388 test_GIT_PAGER_overrides expect_success
"$@"
391 test_expect_success
'setup: some aliases' '
392 git config alias.aliasedlog log &&
393 git config alias.true "!true"
396 test_pager_choices
'git log'
397 test_pager_choices
'git -p log'
398 test_pager_choices
'git aliasedlog'
400 test_default_pager expect_success
'git -p aliasedlog'
401 test_PAGER_overrides expect_success
'git -p aliasedlog'
402 test_core_pager_overrides expect_success
'git -p aliasedlog'
403 test_core_pager_subdir expect_failure
'git -p aliasedlog'
404 test_GIT_PAGER_overrides expect_success
'git -p aliasedlog'
406 test_default_pager expect_success
'git -p true'
407 test_PAGER_overrides expect_success
'git -p true'
408 test_core_pager_overrides expect_success
'git -p true'
409 test_core_pager_subdir expect_failure
'git -p true'
410 test_GIT_PAGER_overrides expect_success
'git -p true'
412 test_default_pager expect_success test_must_fail
'git -p request-pull'
413 test_PAGER_overrides expect_success test_must_fail
'git -p request-pull'
414 test_core_pager_overrides expect_success test_must_fail
'git -p request-pull'
415 test_core_pager_subdir expect_failure test_must_fail
'git -p request-pull'
416 test_GIT_PAGER_overrides expect_success test_must_fail
'git -p request-pull'
418 test_default_pager expect_success test_must_fail
'git -p'
419 test_PAGER_overrides expect_success test_must_fail
'git -p'
420 test_local_config_ignored expect_failure test_must_fail
'git -p'
421 test_no_local_config_subdir expect_success test_must_fail
'git -p'
422 test_GIT_PAGER_overrides expect_success test_must_fail
'git -p'
424 test_doesnt_paginate expect_failure test_must_fail
'git -p nonsense'
426 test_pager_choices
'git shortlog'
427 test_expect_success
'setup: configure shortlog not to paginate' '
428 git config pager.shortlog false
430 test_doesnt_paginate expect_success
'git shortlog'
431 test_no_local_config_subdir expect_success
'git shortlog'
432 test_default_pager expect_success
'git -p shortlog'
433 test_core_pager_subdir expect_success
'git -p shortlog'
435 test_core_pager_subdir expect_success test_must_fail \
436 'git -p apply </dev/null'