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