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