2ac729f40c730746d799a3c02c4f86ddb89cae57
[git/git.git] / t / t7006-pager.sh
1 #!/bin/sh
2
3 test_description='Test automatic use of a pager.'
4
5 . ./test-lib.sh
6 . "$TEST_DIRECTORY"/lib-pager.sh
7 . "$TEST_DIRECTORY"/lib-terminal.sh
8
9 cleanup_fail() {
10 echo >&2 cleanup failed
11 (exit 1)
12 }
13
14 test_expect_success 'setup' '
15 sane_unset GIT_PAGER GIT_PAGER_IN_USE &&
16 test_unconfig core.pager &&
17
18 PAGER="cat >paginated.out" &&
19 export PAGER &&
20
21 test_commit initial
22 '
23
24 test_expect_success TTY 'some commands use a pager' '
25 rm -f paginated.out ||
26 cleanup_fail &&
27
28 test_terminal git log &&
29 test -e paginated.out
30 '
31
32 test_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
47 test_expect_success TTY 'some commands do not use a pager' '
48 rm -f paginated.out ||
49 cleanup_fail &&
50
51 test_terminal git rev-list HEAD &&
52 ! test -e paginated.out
53 '
54
55 test_expect_success 'no pager when stdout is a pipe' '
56 rm -f paginated.out ||
57 cleanup_fail &&
58
59 git log | cat &&
60 ! test -e paginated.out
61 '
62
63 test_expect_success 'no pager when stdout is a regular file' '
64 rm -f paginated.out ||
65 cleanup_fail &&
66
67 git log >file &&
68 ! test -e paginated.out
69 '
70
71 test_expect_success TTY 'git --paginate rev-list uses a pager' '
72 rm -f paginated.out ||
73 cleanup_fail &&
74
75 test_terminal git --paginate rev-list HEAD &&
76 test -e paginated.out
77 '
78
79 test_expect_success 'no pager even with --paginate when stdout is a pipe' '
80 rm -f file paginated.out ||
81 cleanup_fail &&
82
83 git --paginate log | cat &&
84 ! test -e paginated.out
85 '
86
87 test_expect_success TTY 'no pager with --no-pager' '
88 rm -f paginated.out ||
89 cleanup_fail &&
90
91 test_terminal git --no-pager log &&
92 ! test -e paginated.out
93 '
94
95 test_expect_success TTY 'configuration can disable pager' '
96 rm -f paginated.out &&
97 test_unconfig pager.grep &&
98 test_terminal git grep initial &&
99 test -e paginated.out &&
100
101 rm -f paginated.out &&
102 test_config pager.grep false &&
103 test_terminal git grep initial &&
104 ! test -e paginated.out
105 '
106
107 test_expect_success TTY 'git config uses a pager if configured to' '
108 rm -f paginated.out &&
109 test_config pager.config true &&
110 test_terminal git config --list &&
111 test -e paginated.out
112 '
113
114 test_expect_success TTY 'configuration can enable pager (from subdir)' '
115 rm -f paginated.out &&
116 mkdir -p subdir &&
117 test_config pager.bundle true &&
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
131 # A colored commit log will begin with an appropriate ANSI escape
132 # for the first color; the text "commit" comes later.
133 colorful() {
134 read firstline <$1
135 ! expr "$firstline" : "[a-zA-Z]" >/dev/null
136 }
137
138 test_expect_success 'tests can detect color' '
139 rm -f colorful.log colorless.log ||
140 cleanup_fail &&
141
142 git log --no-color >colorless.log &&
143 git log --color >colorful.log &&
144 ! colorful colorless.log &&
145 colorful colorful.log
146 '
147
148 test_expect_success 'no color when stdout is a regular file' '
149 rm -f colorless.log &&
150 test_config color.ui auto ||
151 cleanup_fail &&
152
153 git log >colorless.log &&
154 ! colorful colorless.log
155 '
156
157 test_expect_success TTY 'color when writing to a pager' '
158 rm -f paginated.out &&
159 test_config color.ui auto ||
160 cleanup_fail &&
161
162 (
163 TERM=vt100 &&
164 export TERM &&
165 test_terminal git log
166 ) &&
167 colorful paginated.out
168 '
169
170 test_expect_success 'color when writing to a file intended for a pager' '
171 rm -f colorful.log &&
172 test_config color.ui auto ||
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 ) &&
181 colorful colorful.log
182 '
183
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 #
202 parse_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 }
215
216 test_default_pager() {
217 parse_args "$@"
218
219 $test_expectation SIMPLEPAGER,TTY "$cmd - default pager is used by default" "
220 sane_unset PAGER GIT_PAGER &&
221 test_unconfig core.pager &&
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 }
238
239 test_PAGER_overrides() {
240 parse_args "$@"
241
242 $test_expectation TTY "$cmd - PAGER overrides default pager" "
243 sane_unset GIT_PAGER &&
244 test_unconfig core.pager &&
245 rm -f PAGER_used ||
246 cleanup_fail &&
247
248 PAGER='wc >PAGER_used' &&
249 export PAGER &&
250 $full_command &&
251 test -e PAGER_used
252 "
253 }
254
255 test_core_pager_overrides() {
256 if_local_config=
257 used_if_wanted='overrides PAGER'
258 test_core_pager "$@"
259 }
260
261 test_local_config_ignored() {
262 if_local_config='! '
263 used_if_wanted='is not used'
264 test_core_pager "$@"
265 }
266
267 test_core_pager() {
268 parse_args "$@"
269
270 $test_expectation TTY "$cmd - repository-local core.pager setting $used_if_wanted" "
271 sane_unset GIT_PAGER &&
272 rm -f core.pager_used ||
273 cleanup_fail &&
274
275 PAGER=wc &&
276 export PAGER &&
277 test_config core.pager 'wc >core.pager_used' &&
278 $full_command &&
279 ${if_local_config}test -e core.pager_used
280 "
281 }
282
283 test_core_pager_subdir() {
284 if_local_config=
285 used_if_wanted='overrides PAGER'
286 test_pager_subdir_helper "$@"
287 }
288
289 test_no_local_config_subdir() {
290 if_local_config='! '
291 used_if_wanted='is not used'
292 test_pager_subdir_helper "$@"
293 }
294
295 test_pager_subdir_helper() {
296 parse_args "$@"
297
298 $test_expectation TTY "$cmd - core.pager $used_if_wanted from subdirectory" "
299 sane_unset GIT_PAGER &&
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 &&
307 test_config core.pager 'wc >\"\$stampname\"' &&
308 mkdir sub &&
309 (
310 cd sub &&
311 $full_command
312 ) &&
313 ${if_local_config}test -e core.pager_used
314 "
315 }
316
317 test_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
324 test_config core.pager wc &&
325 GIT_PAGER='wc >GIT_PAGER_used' &&
326 export GIT_PAGER &&
327 $full_command &&
328 test -e GIT_PAGER_used
329 "
330 }
331
332 test_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
346 test_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
354 test_expect_success 'setup: some aliases' '
355 git config alias.aliasedlog log &&
356 git config alias.true "!true"
357 '
358
359 test_pager_choices 'git log'
360 test_pager_choices 'git -p log'
361 test_pager_choices 'git aliasedlog'
362
363 test_default_pager expect_success 'git -p aliasedlog'
364 test_PAGER_overrides expect_success 'git -p aliasedlog'
365 test_core_pager_overrides expect_success 'git -p aliasedlog'
366 test_core_pager_subdir expect_failure 'git -p aliasedlog'
367 test_GIT_PAGER_overrides expect_success 'git -p aliasedlog'
368
369 test_default_pager expect_success 'git -p true'
370 test_PAGER_overrides expect_success 'git -p true'
371 test_core_pager_overrides expect_success 'git -p true'
372 test_core_pager_subdir expect_failure 'git -p true'
373 test_GIT_PAGER_overrides expect_success 'git -p true'
374
375 test_default_pager expect_success test_must_fail 'git -p request-pull'
376 test_PAGER_overrides expect_success test_must_fail 'git -p request-pull'
377 test_core_pager_overrides expect_success test_must_fail 'git -p request-pull'
378 test_core_pager_subdir expect_failure test_must_fail 'git -p request-pull'
379 test_GIT_PAGER_overrides expect_success test_must_fail 'git -p request-pull'
380
381 test_default_pager expect_success test_must_fail 'git -p'
382 test_PAGER_overrides expect_success test_must_fail 'git -p'
383 test_local_config_ignored expect_failure test_must_fail 'git -p'
384 test_no_local_config_subdir expect_success test_must_fail 'git -p'
385 test_GIT_PAGER_overrides expect_success test_must_fail 'git -p'
386
387 test_doesnt_paginate expect_failure test_must_fail 'git -p nonsense'
388
389 test_pager_choices 'git shortlog'
390 test_expect_success 'setup: configure shortlog not to paginate' '
391 git config pager.shortlog false
392 '
393 test_doesnt_paginate expect_success 'git shortlog'
394 test_no_local_config_subdir expect_success 'git shortlog'
395 test_default_pager expect_success 'git -p shortlog'
396 test_core_pager_subdir expect_success 'git -p shortlog'
397
398 test_core_pager_subdir expect_success test_must_fail \
399 'git -p apply </dev/null'
400
401 test_expect_success TTY 'command-specific pager' '
402 sane_unset PAGER GIT_PAGER &&
403 echo "foo:initial" >expect &&
404 >actual &&
405 test_unconfig core.pager &&
406 test_config pager.log "sed s/^/foo:/ >actual" &&
407 test_terminal git log --format=%s -1 &&
408 test_cmp expect actual
409 '
410
411 test_expect_success TTY 'command-specific pager overrides core.pager' '
412 sane_unset PAGER GIT_PAGER &&
413 echo "foo:initial" >expect &&
414 >actual &&
415 test_config core.pager "exit 1"
416 test_config pager.log "sed s/^/foo:/ >actual" &&
417 test_terminal git log --format=%s -1 &&
418 test_cmp expect actual
419 '
420
421 test_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 &&
425 test_config pager.log "exit 1" &&
426 test_terminal git log --format=%s -1 &&
427 test_cmp expect actual
428 '
429
430 test_done