Git 2.12
[git/git.git] / t / t7006-pager.sh
CommitLineData
60b6e220
JN
1#!/bin/sh
2
3test_description='Test automatic use of a pager.'
4
5. ./test-lib.sh
678e484b 6. "$TEST_DIRECTORY"/lib-pager.sh
cc4e48fc 7. "$TEST_DIRECTORY"/lib-terminal.sh
60b6e220 8
60b6e220 9test_expect_success 'setup' '
00648ba0 10 sane_unset GIT_PAGER GIT_PAGER_IN_USE &&
8d68a6d5 11 test_unconfig core.pager &&
fdf1bc48
JN
12
13 PAGER="cat >paginated.out" &&
14 export PAGER &&
15
60b6e220
JN
16 test_commit initial
17'
18
60b6e220 19test_expect_success TTY 'some commands use a pager' '
0d164519 20 rm -f paginated.out &&
2d3ca216 21 test_terminal git log &&
60b6e220
JN
22 test -e paginated.out
23'
24
2cb60093
NTND
25test_expect_failure TTY 'pager runs from subdir' '
26 echo subdir/paginated.out >expected &&
27 mkdir -p subdir &&
28 rm -f paginated.out subdir/paginated.out &&
29 (
30 cd subdir &&
31 test_terminal git log
32 ) &&
33 {
34 ls paginated.out subdir/paginated.out ||
35 :
36 } >actual &&
37 test_cmp expected actual
38'
39
e54c1f2d
JN
40test_expect_success TTY 'LESS and LV envvars are set for pagination' '
41 (
42 sane_unset LESS LV &&
52c02f65 43 PAGER="env >pager-env.out; wc" &&
e54c1f2d
JN
44 export PAGER &&
45
46 test_terminal git log
47 ) &&
48 grep ^LESS= pager-env.out &&
49 grep ^LV= pager-env.out
50'
51
995bc22d
EW
52test_expect_success !MINGW,TTY 'LESS and LV envvars set by git-sh-setup' '
53 (
54 sane_unset LESS LV &&
55 PAGER="env >pager-env.out; wc" &&
56 export PAGER &&
57 PATH="$(git --exec-path):$PATH" &&
58 export PATH &&
59 test_terminal sh -c ". git-sh-setup && git_pager"
60 ) &&
61 grep ^LESS= pager-env.out &&
62 grep ^LV= pager-env.out
63'
64
60b6e220 65test_expect_success TTY 'some commands do not use a pager' '
0d164519 66 rm -f paginated.out &&
2d3ca216 67 test_terminal git rev-list HEAD &&
60b6e220
JN
68 ! test -e paginated.out
69'
70
60b6e220 71test_expect_success 'no pager when stdout is a pipe' '
0d164519 72 rm -f paginated.out &&
60b6e220
JN
73 git log | cat &&
74 ! test -e paginated.out
75'
76
60b6e220 77test_expect_success 'no pager when stdout is a regular file' '
0d164519 78 rm -f paginated.out &&
fdf1bc48 79 git log >file &&
60b6e220
JN
80 ! test -e paginated.out
81'
82
60b6e220 83test_expect_success TTY 'git --paginate rev-list uses a pager' '
0d164519 84 rm -f paginated.out &&
2d3ca216 85 test_terminal git --paginate rev-list HEAD &&
60b6e220
JN
86 test -e paginated.out
87'
88
60b6e220 89test_expect_success 'no pager even with --paginate when stdout is a pipe' '
0d164519 90 rm -f file paginated.out &&
60b6e220
JN
91 git --paginate log | cat &&
92 ! test -e paginated.out
93'
94
60b6e220 95test_expect_success TTY 'no pager with --no-pager' '
0d164519 96 rm -f paginated.out &&
2d3ca216 97 test_terminal git --no-pager log &&
60b6e220
JN
98 ! test -e paginated.out
99'
100
ff38d1a9
NTND
101test_expect_success TTY 'configuration can disable pager' '
102 rm -f paginated.out &&
8d68a6d5 103 test_unconfig pager.grep &&
ff38d1a9
NTND
104 test_terminal git grep initial &&
105 test -e paginated.out &&
106
107 rm -f paginated.out &&
8d68a6d5 108 test_config pager.grep false &&
ff38d1a9
NTND
109 test_terminal git grep initial &&
110 ! test -e paginated.out
111'
112
41bf3bc2 113test_expect_success TTY 'git config uses a pager if configured to' '
3ba7e6e2 114 rm -f paginated.out &&
8d68a6d5 115 test_config pager.config true &&
3ba7e6e2
NTND
116 test_terminal git config --list &&
117 test -e paginated.out
118'
119
41bf3bc2 120test_expect_success TTY 'configuration can enable pager (from subdir)' '
2cb60093
NTND
121 rm -f paginated.out &&
122 mkdir -p subdir &&
8d68a6d5 123 test_config pager.bundle true &&
2cb60093
NTND
124
125 git bundle create test.bundle --all &&
126 rm -f paginated.out subdir/paginated.out &&
127 (
128 cd subdir &&
129 test_terminal git bundle unbundle ../test.bundle
130 ) &&
131 {
132 test -e paginated.out ||
133 test -e subdir/paginated.out
134 }
135'
136
60b6e220
JN
137# A colored commit log will begin with an appropriate ANSI escape
138# for the first color; the text "commit" comes later.
139colorful() {
fdf1bc48 140 read firstline <$1
e0ae1e6f 141 ! expr "$firstline" : "[a-zA-Z]" >/dev/null
60b6e220
JN
142}
143
60b6e220 144test_expect_success 'tests can detect color' '
0d164519 145 rm -f colorful.log colorless.log &&
fdf1bc48
JN
146 git log --no-color >colorless.log &&
147 git log --color >colorful.log &&
60b6e220
JN
148 ! colorful colorless.log &&
149 colorful colorful.log
150'
151
60b6e220 152test_expect_success 'no color when stdout is a regular file' '
fdf1bc48 153 rm -f colorless.log &&
0d164519 154 test_config color.ui auto &&
fdf1bc48 155 git log >colorless.log &&
60b6e220
JN
156 ! colorful colorless.log
157'
158
60b6e220 159test_expect_success TTY 'color when writing to a pager' '
fdf1bc48 160 rm -f paginated.out &&
0d164519 161 test_config color.ui auto &&
512477b1 162 test_terminal env TERM=vt100 git log &&
60b6e220
JN
163 colorful paginated.out
164'
165
daa0c3d9
JK
166test_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 &&
512477b1 170 test_terminal env TERM=vt100 git log &&
daa0c3d9
JK
171 ! colorful paginated.out
172'
173
60b6e220 174test_expect_success 'color when writing to a file intended for a pager' '
fdf1bc48 175 rm -f colorful.log &&
0d164519 176 test_config color.ui auto &&
fdf1bc48
JN
177 (
178 TERM=vt100 &&
179 GIT_PAGER_IN_USE=true &&
180 export TERM GIT_PAGER_IN_USE &&
181 git log >colorful.log
182 ) &&
60b6e220
JN
183 colorful colorful.log
184'
185
2e6c012e
JK
186test_expect_success TTY 'colors are sent to pager for external commands' '
187 test_config alias.externallog "!git log" &&
188 test_config color.ui auto &&
512477b1 189 test_terminal env TERM=vt100 git -p externallog &&
2e6c012e
JK
190 colorful paginated.out
191'
192
3c7406d4
JN
193# Use this helper to make it easy for the caller of your
194# terminal-using function to specify whether it should fail.
195# If you write
196#
197# your_test() {
198# parse_args "$@"
199#
200# $test_expectation "$cmd - behaves well" "
201# ...
202# $full_command &&
203# ...
204# "
205# }
206#
207# then your test can be used like this:
208#
209# your_test expect_(success|failure) [test_must_fail] 'git foo'
210#
211parse_args() {
212 test_expectation="test_$1"
213 shift
214 if test "$1" = test_must_fail
215 then
216 full_command="test_must_fail test_terminal "
217 shift
218 else
219 full_command="test_terminal "
220 fi
221 cmd=$1
222 full_command="$full_command $1"
223}
fdf1bc48 224
8f81449e
JN
225test_default_pager() {
226 parse_args "$@"
227
996621eb 228 $test_expectation SIMPLEPAGER,TTY "$cmd - default pager is used by default" "
00648ba0 229 sane_unset PAGER GIT_PAGER &&
8d68a6d5 230 test_unconfig core.pager &&
0d164519 231 rm -f default_pager_used &&
8f81449e
JN
232 cat >\$less <<-\EOF &&
233 #!/bin/sh
234 wc >default_pager_used
235 EOF
236 chmod +x \$less &&
237 (
238 PATH=.:\$PATH &&
239 export PATH &&
240 $full_command
241 ) &&
242 test -e default_pager_used
243 "
244}
60b6e220 245
8f81449e
JN
246test_PAGER_overrides() {
247 parse_args "$@"
fdf1bc48 248
8f81449e 249 $test_expectation TTY "$cmd - PAGER overrides default pager" "
00648ba0 250 sane_unset GIT_PAGER &&
8d68a6d5 251 test_unconfig core.pager &&
0d164519 252 rm -f PAGER_used &&
8f81449e
JN
253 PAGER='wc >PAGER_used' &&
254 export PAGER &&
255 $full_command &&
256 test -e PAGER_used
257 "
258}
fdf1bc48 259
8f81449e 260test_core_pager_overrides() {
73e25e7c
NTND
261 if_local_config=
262 used_if_wanted='overrides PAGER'
263 test_core_pager "$@"
264}
60b6e220 265
73e25e7c
NTND
266test_local_config_ignored() {
267 if_local_config='! '
268 used_if_wanted='is not used'
269 test_core_pager "$@"
270}
fdf1bc48 271
73e25e7c 272test_core_pager() {
8f81449e
JN
273 parse_args "$@"
274
73e25e7c 275 $test_expectation TTY "$cmd - repository-local core.pager setting $used_if_wanted" "
00648ba0 276 sane_unset GIT_PAGER &&
0d164519 277 rm -f core.pager_used &&
8f81449e
JN
278 PAGER=wc &&
279 export PAGER &&
8d68a6d5 280 test_config core.pager 'wc >core.pager_used' &&
8f81449e 281 $full_command &&
73e25e7c 282 ${if_local_config}test -e core.pager_used
8f81449e
JN
283 "
284}
285
bce2c9ae 286test_core_pager_subdir() {
73e25e7c
NTND
287 if_local_config=
288 used_if_wanted='overrides PAGER'
289 test_pager_subdir_helper "$@"
290}
291
292test_no_local_config_subdir() {
293 if_local_config='! '
294 used_if_wanted='is not used'
295 test_pager_subdir_helper "$@"
296}
297
298test_pager_subdir_helper() {
bce2c9ae
JN
299 parse_args "$@"
300
73e25e7c 301 $test_expectation TTY "$cmd - core.pager $used_if_wanted from subdirectory" "
00648ba0 302 sane_unset GIT_PAGER &&
bce2c9ae 303 rm -f core.pager_used &&
0d164519 304 rm -fr sub &&
bce2c9ae
JN
305 PAGER=wc &&
306 stampname=\$(pwd)/core.pager_used &&
307 export PAGER stampname &&
8d68a6d5 308 test_config core.pager 'wc >\"\$stampname\"' &&
bce2c9ae
JN
309 mkdir sub &&
310 (
311 cd sub &&
312 $full_command
313 ) &&
73e25e7c 314 ${if_local_config}test -e core.pager_used
bce2c9ae
JN
315 "
316}
317
8f81449e
JN
318test_GIT_PAGER_overrides() {
319 parse_args "$@"
320
321 $test_expectation TTY "$cmd - GIT_PAGER overrides core.pager" "
0d164519 322 rm -f GIT_PAGER_used &&
8d68a6d5 323 test_config core.pager wc &&
8f81449e
JN
324 GIT_PAGER='wc >GIT_PAGER_used' &&
325 export GIT_PAGER &&
326 $full_command &&
327 test -e GIT_PAGER_used
328 "
329}
fdf1bc48 330
73e25e7c
NTND
331test_doesnt_paginate() {
332 parse_args "$@"
333
334 $test_expectation TTY "no pager for '$cmd'" "
0d164519 335 rm -f GIT_PAGER_used &&
73e25e7c
NTND
336 GIT_PAGER='wc >GIT_PAGER_used' &&
337 export GIT_PAGER &&
338 $full_command &&
339 ! test -e GIT_PAGER_used
340 "
341}
342
030149a4
JN
343test_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 "$@"
349}
350
351test_expect_success 'setup: some aliases' '
352 git config alias.aliasedlog log &&
353 git config alias.true "!true"
60b6e220
JN
354'
355
030149a4
JN
356test_pager_choices 'git log'
357test_pager_choices 'git -p log'
358test_pager_choices 'git aliasedlog'
359
360test_default_pager expect_success 'git -p aliasedlog'
361test_PAGER_overrides expect_success 'git -p aliasedlog'
362test_core_pager_overrides expect_success 'git -p aliasedlog'
363test_core_pager_subdir expect_failure 'git -p aliasedlog'
364test_GIT_PAGER_overrides expect_success 'git -p aliasedlog'
365
366test_default_pager expect_success 'git -p true'
367test_PAGER_overrides expect_success 'git -p true'
368test_core_pager_overrides expect_success 'git -p true'
369test_core_pager_subdir expect_failure 'git -p true'
370test_GIT_PAGER_overrides expect_success 'git -p true'
371
372test_default_pager expect_success test_must_fail 'git -p request-pull'
373test_PAGER_overrides expect_success test_must_fail 'git -p request-pull'
374test_core_pager_overrides expect_success test_must_fail 'git -p request-pull'
375test_core_pager_subdir expect_failure test_must_fail 'git -p request-pull'
376test_GIT_PAGER_overrides expect_success test_must_fail 'git -p request-pull'
8f81449e
JN
377
378test_default_pager expect_success test_must_fail 'git -p'
379test_PAGER_overrides expect_success test_must_fail 'git -p'
73e25e7c
NTND
380test_local_config_ignored expect_failure test_must_fail 'git -p'
381test_no_local_config_subdir expect_success test_must_fail 'git -p'
8f81449e
JN
382test_GIT_PAGER_overrides expect_success test_must_fail 'git -p'
383
030149a4 384test_doesnt_paginate expect_failure test_must_fail 'git -p nonsense'
60b6e220 385
773b69bf
NTND
386test_pager_choices 'git shortlog'
387test_expect_success 'setup: configure shortlog not to paginate' '
388 git config pager.shortlog false
389'
390test_doesnt_paginate expect_success 'git shortlog'
391test_no_local_config_subdir expect_success 'git shortlog'
392test_default_pager expect_success 'git -p shortlog'
393test_core_pager_subdir expect_success 'git -p shortlog'
394
d1ea8962
NTND
395test_core_pager_subdir expect_success test_must_fail \
396 'git -p apply </dev/null'
397
9bad7233 398test_expect_success TTY 'command-specific pager' '
212ad944 399 sane_unset PAGER GIT_PAGER &&
9bad7233
JK
400 echo "foo:initial" >expect &&
401 >actual &&
8d68a6d5
JK
402 test_unconfig core.pager &&
403 test_config pager.log "sed s/^/foo:/ >actual" &&
9bad7233
JK
404 test_terminal git log --format=%s -1 &&
405 test_cmp expect actual
406'
407
408test_expect_success TTY 'command-specific pager overrides core.pager' '
212ad944 409 sane_unset PAGER GIT_PAGER &&
9bad7233
JK
410 echo "foo:initial" >expect &&
411 >actual &&
99094a7a 412 test_config core.pager "exit 1" &&
8d68a6d5 413 test_config pager.log "sed s/^/foo:/ >actual" &&
9bad7233
JK
414 test_terminal git log --format=%s -1 &&
415 test_cmp expect actual
416'
417
418test_expect_success TTY 'command-specific pager overridden by environment' '
419 GIT_PAGER="sed s/^/foo:/ >actual" && export GIT_PAGER &&
420 >actual &&
421 echo "foo:initial" >expect &&
8d68a6d5 422 test_config pager.log "exit 1" &&
9bad7233
JK
423 test_terminal git log --format=%s -1 &&
424 test_cmp expect actual
425'
426
92058e4d
JK
427test_expect_success 'setup external command' '
428 cat >git-external <<-\EOF &&
429 #!/bin/sh
430 git "$@"
431 EOF
432 chmod +x git-external
433'
434
435test_expect_success TTY 'command-specific pager works for external commands' '
436 sane_unset PAGER GIT_PAGER &&
437 echo "foo:initial" >expect &&
438 >actual &&
439 test_config pager.external "sed s/^/foo:/ >actual" &&
90ae5d27 440 test_terminal git --exec-path="$(pwd)" external log --format=%s -1 &&
92058e4d
JK
441 test_cmp expect actual
442'
443
444test_expect_success TTY 'sub-commands of externals use their own pager' '
445 sane_unset PAGER GIT_PAGER &&
446 echo "foo:initial" >expect &&
447 >actual &&
448 test_config pager.log "sed s/^/foo:/ >actual" &&
449 test_terminal git --exec-path=. external log --format=%s -1 &&
450 test_cmp expect actual
451'
452
453test_expect_success TTY 'external command pagers override sub-commands' '
454 sane_unset PAGER GIT_PAGER &&
455 >expect &&
456 >actual &&
457 test_config pager.external false &&
458 test_config pager.log "sed s/^/log:/ >actual" &&
459 test_terminal git --exec-path=. external log --format=%s -1 &&
460 test_cmp expect actual
461'
462
9e9de18f
JK
463test_expect_success 'command with underscores does not complain' '
464 write_script git-under_score <<-\EOF &&
465 echo ok
466 EOF
467 git --exec-path=. under_score >actual 2>&1 &&
468 echo ok >expect &&
469 test_cmp expect actual
470'
471
60b6e220 472test_done