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
'LESS and LV envvars are set for pagination' '
43 PAGER="env >pager-env.out; wc" &&
48 grep ^LESS= pager-env.out &&
49 grep ^LV= pager-env.out
52 test_expect_success
!MINGW
,TTY
'LESS and LV envvars set by git-sh-setup' '
55 PAGER="env >pager-env.out; wc" &&
57 PATH="$(git --exec-path):$PATH" &&
59 test_terminal sh -c ". git-sh-setup && git_pager"
61 grep ^LESS= pager-env.out &&
62 grep ^LV= pager-env.out
65 test_expect_success TTY
'some commands do not use a pager' '
66 rm -f paginated.out &&
67 test_terminal git rev-list HEAD &&
68 ! test -e paginated.out
71 test_expect_success
'no pager when stdout is a pipe' '
72 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 &&
80 ! test -e paginated.out
83 test_expect_success TTY
'git --paginate rev-list uses a pager' '
84 rm -f paginated.out &&
85 test_terminal git --paginate rev-list HEAD &&
89 test_expect_success
'no pager even with --paginate when stdout is a pipe' '
90 rm -f file paginated.out &&
91 git --paginate log | cat &&
92 ! test -e paginated.out
95 test_expect_success TTY
'no pager with --no-pager' '
96 rm -f paginated.out &&
97 test_terminal git --no-pager log &&
98 ! test -e paginated.out
101 test_expect_success TTY
'configuration can disable pager' '
102 rm -f paginated.out &&
103 test_unconfig pager.grep &&
104 test_terminal git grep initial &&
105 test -e paginated.out &&
107 rm -f paginated.out &&
108 test_config pager.grep false &&
109 test_terminal git grep initial &&
110 ! test -e paginated.out
113 test_expect_success TTY
'git config uses a pager if configured to' '
114 rm -f paginated.out &&
115 test_config pager.config true &&
116 test_terminal git config --list &&
117 test -e paginated.out
120 test_expect_success TTY
'configuration can enable pager (from subdir)' '
121 rm -f paginated.out &&
123 test_config pager.bundle true &&
125 git bundle create test.bundle --all &&
126 rm -f paginated.out subdir/paginated.out &&
129 test_terminal git bundle unbundle ../test.bundle
132 test -e paginated.out ||
133 test -e subdir/paginated.out
137 # A colored commit log will begin with an appropriate ANSI escape
138 # for the first color; the text "commit" comes later.
141 ! expr "$firstline" : "[a-zA-Z]" >/dev
/null
144 test_expect_success
'tests can detect color' '
145 rm -f colorful.log colorless.log &&
146 git log --no-color >colorless.log &&
147 git log --color >colorful.log &&
148 ! colorful colorless.log &&
149 colorful colorful.log
152 test_expect_success
'no color when stdout is a regular file' '
153 rm -f colorless.log &&
154 test_config color.ui auto &&
155 git log >colorless.log &&
156 ! colorful colorless.log
159 test_expect_success TTY
'color when writing to a pager' '
160 rm -f paginated.out &&
161 test_config color.ui auto &&
162 test_terminal env TERM=vt100 git log &&
163 colorful paginated.out
166 test_expect_success TTY
'colors are suppressed by color.pager' '
167 rm -f paginated.out &&
168 test_config color.ui auto &&
169 test_config color.pager false &&
170 test_terminal env TERM=vt100 git log &&
171 ! colorful paginated.out
174 test_expect_success
'color when writing to a file intended for a pager' '
175 rm -f colorful.log &&
176 test_config color.ui auto &&
179 GIT_PAGER_IN_USE=true &&
180 export TERM GIT_PAGER_IN_USE &&
181 git log >colorful.log
183 colorful colorful.log
186 test_expect_success TTY
'colors are sent to pager for external commands' '
187 test_config alias.externallog "!git log" &&
188 test_config color.ui auto &&
189 test_terminal env TERM=vt100 git -p externallog &&
190 colorful paginated.out
193 # Use this helper to make it easy for the caller of your
194 # terminal-using function to specify whether it should fail.
200 # $test_expectation "$cmd - behaves well" "
207 # then your test can be used like this:
209 # your_test expect_(success|failure) [test_must_fail] 'git foo'
212 test_expectation
="test_$1"
214 if test "$1" = test_must_fail
216 full_command
="test_must_fail test_terminal "
219 full_command
="test_terminal "
222 full_command
="$full_command $1"
225 test_default_pager
() {
228 $test_expectation SIMPLEPAGER
,TTY
"$cmd - default pager is used by default" "
229 sane_unset PAGER GIT_PAGER &&
230 test_unconfig core.pager &&
231 rm -f default_pager_used &&
232 cat >\$less <<-\EOF &&
234 wc >default_pager_used
242 test -e default_pager_used
246 test_PAGER_overrides
() {
249 $test_expectation TTY
"$cmd - PAGER overrides default pager" "
250 sane_unset GIT_PAGER &&
251 test_unconfig core.pager &&
253 PAGER='wc >PAGER_used' &&
260 test_core_pager_overrides
() {
262 used_if_wanted
='overrides PAGER'
266 test_local_config_ignored
() {
268 used_if_wanted
='is not used'
275 $test_expectation TTY
"$cmd - repository-local core.pager setting $used_if_wanted" "
276 sane_unset GIT_PAGER &&
277 rm -f core.pager_used &&
280 test_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 &&
306 stampname=\$(pwd)/core.pager_used &&
307 export PAGER stampname &&
308 test_config core.pager 'wc >\"\$stampname\"' &&
314 ${if_local_config}test -e core.pager_used
318 test_GIT_PAGER_overrides
() {
321 $test_expectation TTY
"$cmd - GIT_PAGER overrides core.pager" "
322 rm -f GIT_PAGER_used &&
323 test_config core.pager wc &&
324 GIT_PAGER='wc >GIT_PAGER_used' &&
327 test -e GIT_PAGER_used
331 test_doesnt_paginate
() {
334 $test_expectation TTY
"no pager for '$cmd'" "
335 rm -f GIT_PAGER_used &&
336 GIT_PAGER='wc >GIT_PAGER_used' &&
339 ! test -e GIT_PAGER_used
343 test_pager_choices
() {
344 test_default_pager expect_success
"$@"
345 test_PAGER_overrides expect_success
"$@"
346 test_core_pager_overrides expect_success
"$@"
347 test_core_pager_subdir expect_success
"$@"
348 test_GIT_PAGER_overrides expect_success
"$@"
351 test_expect_success
'setup: some aliases' '
352 git config alias.aliasedlog log &&
353 git config alias.true "!true"
356 test_pager_choices
'git log'
357 test_pager_choices
'git -p log'
358 test_pager_choices
'git aliasedlog'
360 test_default_pager expect_success
'git -p aliasedlog'
361 test_PAGER_overrides expect_success
'git -p aliasedlog'
362 test_core_pager_overrides expect_success
'git -p aliasedlog'
363 test_core_pager_subdir expect_success
'git -p aliasedlog'
364 test_GIT_PAGER_overrides expect_success
'git -p aliasedlog'
366 test_default_pager expect_success
'git -p true'
367 test_PAGER_overrides expect_success
'git -p true'
368 test_core_pager_overrides expect_success
'git -p true'
369 test_core_pager_subdir expect_success
'git -p true'
370 test_GIT_PAGER_overrides expect_success
'git -p true'
372 test_default_pager expect_success test_must_fail
'git -p request-pull'
373 test_PAGER_overrides expect_success test_must_fail
'git -p request-pull'
374 test_core_pager_overrides expect_success test_must_fail
'git -p request-pull'
375 test_core_pager_subdir expect_success test_must_fail
'git -p request-pull'
376 test_GIT_PAGER_overrides expect_success test_must_fail
'git -p request-pull'
378 test_default_pager expect_success test_must_fail
'git -p'
379 test_PAGER_overrides expect_success test_must_fail
'git -p'
380 test_local_config_ignored expect_failure test_must_fail
'git -p'
381 test_GIT_PAGER_overrides expect_success test_must_fail
'git -p'
383 test_expect_success TTY
'core.pager in repo config works and retains cwd' '
384 sane_unset GIT_PAGER &&
385 test_config core.pager "cat >cwd-retained" &&
388 rm -f cwd-retained &&
389 test_terminal git -p rev-parse HEAD &&
390 test_path_is_file cwd-retained
394 test_doesnt_paginate expect_failure test_must_fail
'git -p nonsense'
396 test_pager_choices
'git shortlog'
397 test_expect_success
'setup: configure shortlog not to paginate' '
398 git config pager.shortlog false
400 test_doesnt_paginate expect_success
'git shortlog'
401 test_no_local_config_subdir expect_success
'git shortlog'
402 test_default_pager expect_success
'git -p shortlog'
403 test_core_pager_subdir expect_success
'git -p shortlog'
405 test_core_pager_subdir expect_success test_must_fail \
406 'git -p apply </dev/null'
408 test_expect_success TTY
'command-specific pager' '
409 sane_unset PAGER GIT_PAGER &&
410 echo "foo:initial" >expect &&
412 test_unconfig core.pager &&
413 test_config pager.log "sed s/^/foo:/ >actual" &&
414 test_terminal git log --format=%s -1 &&
415 test_cmp expect actual
418 test_expect_success TTY
'command-specific pager overrides core.pager' '
419 sane_unset PAGER GIT_PAGER &&
420 echo "foo:initial" >expect &&
422 test_config core.pager "exit 1" &&
423 test_config pager.log "sed s/^/foo:/ >actual" &&
424 test_terminal git log --format=%s -1 &&
425 test_cmp expect actual
428 test_expect_success TTY
'command-specific pager overridden by environment' '
429 GIT_PAGER="sed s/^/foo:/ >actual" && export GIT_PAGER &&
431 echo "foo:initial" >expect &&
432 test_config pager.log "exit 1" &&
433 test_terminal git log --format=%s -1 &&
434 test_cmp expect actual
437 test_expect_success
'setup external command' '
438 cat >git-external <<-\EOF &&
442 chmod +x git-external
445 test_expect_success TTY
'command-specific pager works for external commands' '
446 sane_unset PAGER GIT_PAGER &&
447 echo "foo:initial" >expect &&
449 test_config pager.external "sed s/^/foo:/ >actual" &&
450 test_terminal git --exec-path="$(pwd)" external log --format=%s -1 &&
451 test_cmp expect actual
454 test_expect_success TTY
'sub-commands of externals use their own pager' '
455 sane_unset PAGER GIT_PAGER &&
456 echo "foo:initial" >expect &&
458 test_config pager.log "sed s/^/foo:/ >actual" &&
459 test_terminal git --exec-path=. external log --format=%s -1 &&
460 test_cmp expect actual
463 test_expect_success TTY
'external command pagers override sub-commands' '
464 sane_unset PAGER GIT_PAGER &&
467 test_config pager.external false &&
468 test_config pager.log "sed s/^/log:/ >actual" &&
469 test_terminal git --exec-path=. external log --format=%s -1 &&
470 test_cmp expect actual
473 test_expect_success
'command with underscores does not complain' '
474 write_script git-under_score <<-\EOF &&
477 git --exec-path=. under_score >actual 2>&1 &&
479 test_cmp expect actual