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_success TTY
'some commands do not use a pager' '
62 rm -f paginated.out ||
65 test_terminal git rev-list HEAD &&
66 ! test -e paginated.out
69 test_expect_success
'no pager when stdout is a pipe' '
70 rm -f paginated.out ||
74 ! test -e paginated.out
77 test_expect_success
'no pager when stdout is a regular file' '
78 rm -f paginated.out ||
82 ! test -e paginated.out
85 test_expect_success TTY
'git --paginate rev-list uses a pager' '
86 rm -f paginated.out ||
89 test_terminal git --paginate rev-list HEAD &&
93 test_expect_success
'no pager even with --paginate when stdout is a pipe' '
94 rm -f file paginated.out ||
97 git --paginate log | cat &&
98 ! test -e paginated.out
101 test_expect_success TTY
'no pager with --no-pager' '
102 rm -f paginated.out ||
105 test_terminal git --no-pager log &&
106 ! test -e paginated.out
109 # A colored commit log will begin with an appropriate ANSI escape
110 # for the first color; the text "commit" comes later.
113 ! expr "$firstline" : "[a-zA-Z]" >/dev
/null
116 test_expect_success
'tests can detect color' '
117 rm -f colorful.log colorless.log ||
120 git log --no-color >colorless.log &&
121 git log --color >colorful.log &&
122 ! colorful colorless.log &&
123 colorful colorful.log
126 test_expect_success
'no color when stdout is a regular file' '
127 rm -f colorless.log &&
128 git config color.ui auto ||
131 git log >colorless.log &&
132 ! colorful colorless.log
135 test_expect_success TTY
'color when writing to a pager' '
136 rm -f paginated.out &&
137 git config color.ui auto ||
143 test_terminal git log
145 colorful paginated.out
148 test_expect_success
'color when writing to a file intended for a pager' '
149 rm -f colorful.log &&
150 git config color.ui auto ||
155 GIT_PAGER_IN_USE=true &&
156 export TERM GIT_PAGER_IN_USE &&
157 git log >colorful.log
159 colorful colorful.log
162 if test_have_prereq SIMPLEPAGER
&& test_have_prereq TTY
164 test_set_prereq SIMPLEPAGERTTY
167 # Use this helper to make it easy for the caller of your
168 # terminal-using function to specify whether it should fail.
174 # $test_expectation "$cmd - behaves well" "
181 # then your test can be used like this:
183 # your_test expect_(success|failure) [test_must_fail] 'git foo'
186 test_expectation
="test_$1"
188 if test "$1" = test_must_fail
190 full_command
="test_must_fail test_terminal "
193 full_command
="test_terminal "
196 full_command
="$full_command $1"
199 test_default_pager
() {
202 $test_expectation SIMPLEPAGERTTY
"$cmd - default pager is used by default" "
203 unset PAGER GIT_PAGER;
204 test_might_fail git config --unset core.pager &&
205 rm -f default_pager_used ||
208 cat >\$less <<-\EOF &&
210 wc >default_pager_used
218 test -e default_pager_used
222 test_PAGER_overrides
() {
225 $test_expectation TTY
"$cmd - PAGER overrides default pager" "
227 test_might_fail git config --unset core.pager &&
231 PAGER='wc >PAGER_used' &&
238 test_core_pager_overrides
() {
240 used_if_wanted
='overrides PAGER'
244 test_local_config_ignored
() {
246 used_if_wanted
='is not used'
253 $test_expectation TTY
"$cmd - repository-local core.pager setting $used_if_wanted" "
255 rm -f core.pager_used ||
260 git config core.pager 'wc >core.pager_used' &&
262 ${if_local_config}test -e core.pager_used
266 test_core_pager_subdir
() {
268 used_if_wanted
='overrides PAGER'
269 test_pager_subdir_helper
"$@"
272 test_no_local_config_subdir
() {
274 used_if_wanted
='is not used'
275 test_pager_subdir_helper
"$@"
278 test_pager_subdir_helper
() {
281 $test_expectation TTY
"$cmd - core.pager $used_if_wanted from subdirectory" "
283 rm -f core.pager_used &&
288 stampname=\$(pwd)/core.pager_used &&
289 export PAGER stampname &&
290 git config core.pager 'wc >\"\$stampname\"' &&
296 ${if_local_config}test -e core.pager_used
300 test_GIT_PAGER_overrides
() {
303 $test_expectation TTY
"$cmd - GIT_PAGER overrides core.pager" "
304 rm -f GIT_PAGER_used ||
307 git config core.pager wc &&
308 GIT_PAGER='wc >GIT_PAGER_used' &&
311 test -e GIT_PAGER_used
315 test_doesnt_paginate
() {
318 $test_expectation TTY
"no pager for '$cmd'" "
319 rm -f GIT_PAGER_used ||
322 GIT_PAGER='wc >GIT_PAGER_used' &&
325 ! test -e GIT_PAGER_used
329 test_pager_choices
() {
330 test_default_pager expect_success
"$@"
331 test_PAGER_overrides expect_success
"$@"
332 test_core_pager_overrides expect_success
"$@"
333 test_core_pager_subdir expect_success
"$@"
334 test_GIT_PAGER_overrides expect_success
"$@"
337 test_expect_success
'setup: some aliases' '
338 git config alias.aliasedlog log &&
339 git config alias.true "!true"
342 test_pager_choices
'git log'
343 test_pager_choices
'git -p log'
344 test_pager_choices
'git aliasedlog'
346 test_default_pager expect_success
'git -p aliasedlog'
347 test_PAGER_overrides expect_success
'git -p aliasedlog'
348 test_core_pager_overrides expect_success
'git -p aliasedlog'
349 test_core_pager_subdir expect_failure
'git -p aliasedlog'
350 test_GIT_PAGER_overrides expect_success
'git -p aliasedlog'
352 test_default_pager expect_success
'git -p true'
353 test_PAGER_overrides expect_success
'git -p true'
354 test_core_pager_overrides expect_success
'git -p true'
355 test_core_pager_subdir expect_failure
'git -p true'
356 test_GIT_PAGER_overrides expect_success
'git -p true'
358 test_default_pager expect_success test_must_fail
'git -p request-pull'
359 test_PAGER_overrides expect_success test_must_fail
'git -p request-pull'
360 test_core_pager_overrides expect_success test_must_fail
'git -p request-pull'
361 test_core_pager_subdir expect_failure test_must_fail
'git -p request-pull'
362 test_GIT_PAGER_overrides expect_success test_must_fail
'git -p request-pull'
364 test_default_pager expect_success test_must_fail
'git -p'
365 test_PAGER_overrides expect_success test_must_fail
'git -p'
366 test_local_config_ignored expect_failure test_must_fail
'git -p'
367 test_no_local_config_subdir expect_success test_must_fail
'git -p'
368 test_GIT_PAGER_overrides expect_success test_must_fail
'git -p'
370 test_doesnt_paginate expect_failure test_must_fail
'git -p nonsense'