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