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