t7006: use test_config helpers
[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
3c7406d4
JN
184# Use this helper to make it easy for the caller of your
185# terminal-using function to specify whether it should fail.
186# If you write
187#
188# your_test() {
189# parse_args "$@"
190#
191# $test_expectation "$cmd - behaves well" "
192# ...
193# $full_command &&
194# ...
195# "
196# }
197#
198# then your test can be used like this:
199#
200# your_test expect_(success|failure) [test_must_fail] 'git foo'
201#
202parse_args() {
203 test_expectation="test_$1"
204 shift
205 if test "$1" = test_must_fail
206 then
207 full_command="test_must_fail test_terminal "
208 shift
209 else
210 full_command="test_terminal "
211 fi
212 cmd=$1
213 full_command="$full_command $1"
214}
fdf1bc48 215
8f81449e
JN
216test_default_pager() {
217 parse_args "$@"
218
996621eb 219 $test_expectation SIMPLEPAGER,TTY "$cmd - default pager is used by default" "
00648ba0 220 sane_unset PAGER GIT_PAGER &&
8d68a6d5 221 test_unconfig core.pager &&
8f81449e
JN
222 rm -f default_pager_used ||
223 cleanup_fail &&
224
225 cat >\$less <<-\EOF &&
226 #!/bin/sh
227 wc >default_pager_used
228 EOF
229 chmod +x \$less &&
230 (
231 PATH=.:\$PATH &&
232 export PATH &&
233 $full_command
234 ) &&
235 test -e default_pager_used
236 "
237}
60b6e220 238
8f81449e
JN
239test_PAGER_overrides() {
240 parse_args "$@"
fdf1bc48 241
8f81449e 242 $test_expectation TTY "$cmd - PAGER overrides default pager" "
00648ba0 243 sane_unset GIT_PAGER &&
8d68a6d5 244 test_unconfig core.pager &&
8f81449e
JN
245 rm -f PAGER_used ||
246 cleanup_fail &&
60b6e220 247
8f81449e
JN
248 PAGER='wc >PAGER_used' &&
249 export PAGER &&
250 $full_command &&
251 test -e PAGER_used
252 "
253}
fdf1bc48 254
8f81449e 255test_core_pager_overrides() {
73e25e7c
NTND
256 if_local_config=
257 used_if_wanted='overrides PAGER'
258 test_core_pager "$@"
259}
60b6e220 260
73e25e7c
NTND
261test_local_config_ignored() {
262 if_local_config='! '
263 used_if_wanted='is not used'
264 test_core_pager "$@"
265}
fdf1bc48 266
73e25e7c 267test_core_pager() {
8f81449e
JN
268 parse_args "$@"
269
73e25e7c 270 $test_expectation TTY "$cmd - repository-local core.pager setting $used_if_wanted" "
00648ba0 271 sane_unset GIT_PAGER &&
8f81449e
JN
272 rm -f core.pager_used ||
273 cleanup_fail &&
274
275 PAGER=wc &&
276 export PAGER &&
8d68a6d5 277 test_config core.pager 'wc >core.pager_used' &&
8f81449e 278 $full_command &&
73e25e7c 279 ${if_local_config}test -e core.pager_used
8f81449e
JN
280 "
281}
282
bce2c9ae 283test_core_pager_subdir() {
73e25e7c
NTND
284 if_local_config=
285 used_if_wanted='overrides PAGER'
286 test_pager_subdir_helper "$@"
287}
288
289test_no_local_config_subdir() {
290 if_local_config='! '
291 used_if_wanted='is not used'
292 test_pager_subdir_helper "$@"
293}
294
295test_pager_subdir_helper() {
bce2c9ae
JN
296 parse_args "$@"
297
73e25e7c 298 $test_expectation TTY "$cmd - core.pager $used_if_wanted from subdirectory" "
00648ba0 299 sane_unset GIT_PAGER &&
bce2c9ae
JN
300 rm -f core.pager_used &&
301 rm -fr sub ||
302 cleanup_fail &&
303
304 PAGER=wc &&
305 stampname=\$(pwd)/core.pager_used &&
306 export PAGER stampname &&
8d68a6d5 307 test_config core.pager 'wc >\"\$stampname\"' &&
bce2c9ae
JN
308 mkdir sub &&
309 (
310 cd sub &&
311 $full_command
312 ) &&
73e25e7c 313 ${if_local_config}test -e core.pager_used
bce2c9ae
JN
314 "
315}
316
8f81449e
JN
317test_GIT_PAGER_overrides() {
318 parse_args "$@"
319
320 $test_expectation TTY "$cmd - GIT_PAGER overrides core.pager" "
321 rm -f GIT_PAGER_used ||
322 cleanup_fail &&
323
8d68a6d5 324 test_config core.pager wc &&
8f81449e
JN
325 GIT_PAGER='wc >GIT_PAGER_used' &&
326 export GIT_PAGER &&
327 $full_command &&
328 test -e GIT_PAGER_used
329 "
330}
fdf1bc48 331
73e25e7c
NTND
332test_doesnt_paginate() {
333 parse_args "$@"
334
335 $test_expectation TTY "no pager for '$cmd'" "
336 rm -f GIT_PAGER_used ||
337 cleanup_fail &&
338
339 GIT_PAGER='wc >GIT_PAGER_used' &&
340 export GIT_PAGER &&
341 $full_command &&
342 ! test -e GIT_PAGER_used
343 "
344}
345
030149a4
JN
346test_pager_choices() {
347 test_default_pager expect_success "$@"
348 test_PAGER_overrides expect_success "$@"
349 test_core_pager_overrides expect_success "$@"
350 test_core_pager_subdir expect_success "$@"
351 test_GIT_PAGER_overrides expect_success "$@"
352}
353
354test_expect_success 'setup: some aliases' '
355 git config alias.aliasedlog log &&
356 git config alias.true "!true"
60b6e220
JN
357'
358
030149a4
JN
359test_pager_choices 'git log'
360test_pager_choices 'git -p log'
361test_pager_choices 'git aliasedlog'
362
363test_default_pager expect_success 'git -p aliasedlog'
364test_PAGER_overrides expect_success 'git -p aliasedlog'
365test_core_pager_overrides expect_success 'git -p aliasedlog'
366test_core_pager_subdir expect_failure 'git -p aliasedlog'
367test_GIT_PAGER_overrides expect_success 'git -p aliasedlog'
368
369test_default_pager expect_success 'git -p true'
370test_PAGER_overrides expect_success 'git -p true'
371test_core_pager_overrides expect_success 'git -p true'
372test_core_pager_subdir expect_failure 'git -p true'
373test_GIT_PAGER_overrides expect_success 'git -p true'
374
375test_default_pager expect_success test_must_fail 'git -p request-pull'
376test_PAGER_overrides expect_success test_must_fail 'git -p request-pull'
377test_core_pager_overrides expect_success test_must_fail 'git -p request-pull'
378test_core_pager_subdir expect_failure test_must_fail 'git -p request-pull'
379test_GIT_PAGER_overrides expect_success test_must_fail 'git -p request-pull'
8f81449e
JN
380
381test_default_pager expect_success test_must_fail 'git -p'
382test_PAGER_overrides expect_success test_must_fail 'git -p'
73e25e7c
NTND
383test_local_config_ignored expect_failure test_must_fail 'git -p'
384test_no_local_config_subdir expect_success test_must_fail 'git -p'
8f81449e
JN
385test_GIT_PAGER_overrides expect_success test_must_fail 'git -p'
386
030149a4 387test_doesnt_paginate expect_failure test_must_fail 'git -p nonsense'
60b6e220 388
773b69bf
NTND
389test_pager_choices 'git shortlog'
390test_expect_success 'setup: configure shortlog not to paginate' '
391 git config pager.shortlog false
392'
393test_doesnt_paginate expect_success 'git shortlog'
394test_no_local_config_subdir expect_success 'git shortlog'
395test_default_pager expect_success 'git -p shortlog'
396test_core_pager_subdir expect_success 'git -p shortlog'
397
d1ea8962
NTND
398test_core_pager_subdir expect_success test_must_fail \
399 'git -p apply </dev/null'
400
9bad7233 401test_expect_success TTY 'command-specific pager' '
212ad944 402 sane_unset PAGER GIT_PAGER &&
9bad7233
JK
403 echo "foo:initial" >expect &&
404 >actual &&
8d68a6d5
JK
405 test_unconfig core.pager &&
406 test_config pager.log "sed s/^/foo:/ >actual" &&
9bad7233
JK
407 test_terminal git log --format=%s -1 &&
408 test_cmp expect actual
409'
410
411test_expect_success TTY 'command-specific pager overrides core.pager' '
212ad944 412 sane_unset PAGER GIT_PAGER &&
9bad7233
JK
413 echo "foo:initial" >expect &&
414 >actual &&
8d68a6d5
JK
415 test_config core.pager "exit 1"
416 test_config pager.log "sed s/^/foo:/ >actual" &&
9bad7233
JK
417 test_terminal git log --format=%s -1 &&
418 test_cmp expect actual
419'
420
421test_expect_success TTY 'command-specific pager overridden by environment' '
422 GIT_PAGER="sed s/^/foo:/ >actual" && export GIT_PAGER &&
423 >actual &&
424 echo "foo:initial" >expect &&
8d68a6d5 425 test_config pager.log "exit 1" &&
9bad7233
JK
426 test_terminal git log --format=%s -1 &&
427 test_cmp expect actual
428'
429
60b6e220 430test_done