3 test_description='Test automatic use of a pager.'
6 . "$TEST_DIRECTORY"/lib-pager.sh
7 . "$TEST_DIRECTORY"/lib-terminal.sh
10 echo >&2 cleanup failed
14 test_expect_success 'setup' '
15 sane_unset GIT_PAGER GIT_PAGER_IN_USE &&
16 test_might_fail git config --unset core.pager &&
18 PAGER="cat >paginated.out" &&
24 test_expect_success TTY 'some commands use a pager' '
25 rm -f paginated.out ||
28 test_terminal git log &&
32 test_expect_failure TTY 'pager runs from subdir' '
33 echo subdir/paginated.out >expected &&
35 rm -f paginated.out subdir/paginated.out &&
41 ls paginated.out subdir/paginated.out ||
44 test_cmp expected actual
47 test_expect_success TTY 'some commands do not use a pager' '
48 rm -f paginated.out ||
51 test_terminal git rev-list HEAD &&
52 ! test -e paginated.out
55 test_expect_success 'no pager when stdout is a pipe' '
56 rm -f paginated.out ||
60 ! test -e paginated.out
63 test_expect_success 'no pager when stdout is a regular file' '
64 rm -f paginated.out ||
68 ! test -e paginated.out
71 test_expect_success TTY 'git --paginate rev-list uses a pager' '
72 rm -f paginated.out ||
75 test_terminal git --paginate rev-list HEAD &&
79 test_expect_success 'no pager even with --paginate when stdout is a pipe' '
80 rm -f file paginated.out ||
83 git --paginate log | cat &&
84 ! test -e paginated.out
87 test_expect_success TTY 'no pager with --no-pager' '
88 rm -f paginated.out ||
91 test_terminal git --no-pager log &&
92 ! test -e paginated.out
95 test_expect_success TTY 'configuration can disable pager' '
96 rm -f paginated.out &&
97 test_might_fail git config --unset pager.grep &&
98 test_terminal git grep initial &&
99 test -e paginated.out &&
101 rm -f paginated.out &&
102 git config pager.grep false &&
103 test_when_finished "git config --unset pager.grep" &&
104 test_terminal git grep initial &&
105 ! test -e paginated.out
108 test_expect_success TTY 'git config uses a pager if configured to' '
109 rm -f paginated.out &&
110 git config pager.config true &&
111 test_when_finished "git config --unset pager.config" &&
112 test_terminal git config --list &&
113 test -e paginated.out
116 test_expect_success TTY 'configuration can enable pager (from subdir)' '
117 rm -f paginated.out &&
119 git config pager.bundle true &&
120 test_when_finished "git config --unset pager.bundle" &&
122 git bundle create test.bundle --all &&
123 rm -f paginated.out subdir/paginated.out &&
126 test_terminal git bundle unbundle ../test.bundle
129 test -e paginated.out ||
130 test -e subdir/paginated.out
134 # A colored commit log will begin with an appropriate ANSI escape
135 # for the first color; the text "commit" comes later.
138 ! expr "$firstline" : "[a-zA-Z]" >/dev/null
141 test_expect_success 'tests can detect color' '
142 rm -f colorful.log colorless.log ||
145 git log --no-color >colorless.log &&
146 git log --color >colorful.log &&
147 ! colorful colorless.log &&
148 colorful colorful.log
151 test_expect_success 'no color when stdout is a regular file' '
152 rm -f colorless.log &&
153 git config color.ui auto ||
156 git log >colorless.log &&
157 ! colorful colorless.log
160 test_expect_success TTY 'color when writing to a pager' '
161 rm -f paginated.out &&
162 git config color.ui auto ||
168 test_terminal git log
170 colorful paginated.out
173 test_expect_success 'color when writing to a file intended for a pager' '
174 rm -f colorful.log &&
175 git config color.ui auto ||
180 GIT_PAGER_IN_USE=true &&
181 export TERM GIT_PAGER_IN_USE &&
182 git log >colorful.log
184 colorful colorful.log
187 # Use this helper to make it easy for the caller of your
188 # terminal-using function to specify whether it should fail.
194 # $test_expectation "$cmd - behaves well" "
201 # then your test can be used like this:
203 # your_test expect_(success|failure) [test_must_fail] 'git foo'
206 test_expectation="test_$1"
208 if test "$1" = test_must_fail
210 full_command="test_must_fail test_terminal "
213 full_command="test_terminal "
216 full_command="$full_command $1"
219 test_default_pager() {
222 $test_expectation SIMPLEPAGER,TTY "$cmd - default pager is used by default" "
223 sane_unset PAGER GIT_PAGER &&
224 test_might_fail git config --unset core.pager &&
225 rm -f default_pager_used ||
228 cat >\$less <<-\EOF &&
230 wc >default_pager_used
238 test -e default_pager_used
242 test_PAGER_overrides() {
245 $test_expectation TTY "$cmd - PAGER overrides default pager" "
246 sane_unset GIT_PAGER &&
247 test_might_fail git config --unset core.pager &&
251 PAGER='wc >PAGER_used' &&
258 test_core_pager_overrides() {
260 used_if_wanted='overrides PAGER'
264 test_local_config_ignored() {
266 used_if_wanted='is not used'
273 $test_expectation TTY "$cmd - repository-local core.pager setting $used_if_wanted" "
274 sane_unset GIT_PAGER &&
275 rm -f core.pager_used ||
280 git config core.pager 'wc >core.pager_used' &&
282 ${if_local_config}test -e core.pager_used
286 test_core_pager_subdir() {
288 used_if_wanted='overrides PAGER'
289 test_pager_subdir_helper "$@"
292 test_no_local_config_subdir() {
294 used_if_wanted='is not used'
295 test_pager_subdir_helper "$@"
298 test_pager_subdir_helper() {
301 $test_expectation TTY "$cmd - core.pager $used_if_wanted from subdirectory" "
302 sane_unset GIT_PAGER &&
303 rm -f core.pager_used &&
308 stampname=\$(pwd)/core.pager_used &&
309 export PAGER stampname &&
310 git config core.pager 'wc >\"\$stampname\"' &&
316 ${if_local_config}test -e core.pager_used
320 test_GIT_PAGER_overrides() {
323 $test_expectation TTY "$cmd - GIT_PAGER overrides core.pager" "
324 rm -f GIT_PAGER_used ||
327 git config core.pager wc &&
328 GIT_PAGER='wc >GIT_PAGER_used' &&
331 test -e GIT_PAGER_used
335 test_doesnt_paginate() {
338 $test_expectation TTY "no pager for '$cmd'" "
339 rm -f GIT_PAGER_used ||
342 GIT_PAGER='wc >GIT_PAGER_used' &&
345 ! test -e GIT_PAGER_used
349 test_pager_choices() {
350 test_default_pager expect_success "$@"
351 test_PAGER_overrides expect_success "$@"
352 test_core_pager_overrides expect_success "$@"
353 test_core_pager_subdir expect_success "$@"
354 test_GIT_PAGER_overrides expect_success "$@"
357 test_expect_success 'setup: some aliases' '
358 git config alias.aliasedlog log &&
359 git config alias.true "!true"
362 test_pager_choices 'git log'
363 test_pager_choices 'git -p log'
364 test_pager_choices 'git aliasedlog'
366 test_default_pager expect_success 'git -p aliasedlog'
367 test_PAGER_overrides expect_success 'git -p aliasedlog'
368 test_core_pager_overrides expect_success 'git -p aliasedlog'
369 test_core_pager_subdir expect_failure 'git -p aliasedlog'
370 test_GIT_PAGER_overrides expect_success 'git -p aliasedlog'
372 test_default_pager expect_success 'git -p true'
373 test_PAGER_overrides expect_success 'git -p true'
374 test_core_pager_overrides expect_success 'git -p true'
375 test_core_pager_subdir expect_failure 'git -p true'
376 test_GIT_PAGER_overrides expect_success 'git -p true'
378 test_default_pager expect_success test_must_fail 'git -p request-pull'
379 test_PAGER_overrides expect_success test_must_fail 'git -p request-pull'
380 test_core_pager_overrides expect_success test_must_fail 'git -p request-pull'
381 test_core_pager_subdir expect_failure test_must_fail 'git -p request-pull'
382 test_GIT_PAGER_overrides expect_success test_must_fail 'git -p request-pull'
384 test_default_pager expect_success test_must_fail 'git -p'
385 test_PAGER_overrides expect_success test_must_fail 'git -p'
386 test_local_config_ignored expect_failure test_must_fail 'git -p'
387 test_no_local_config_subdir expect_success test_must_fail 'git -p'
388 test_GIT_PAGER_overrides expect_success test_must_fail 'git -p'
390 test_doesnt_paginate expect_failure test_must_fail 'git -p nonsense'
392 test_pager_choices 'git shortlog'
393 test_expect_success 'setup: configure shortlog not to paginate' '
394 git config pager.shortlog false
396 test_doesnt_paginate expect_success 'git shortlog'
397 test_no_local_config_subdir expect_success 'git shortlog'
398 test_default_pager expect_success 'git -p shortlog'
399 test_core_pager_subdir expect_success 'git -p shortlog'
401 test_core_pager_subdir expect_success test_must_fail \
402 'git -p apply </dev/null'