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