1 #!/bin/sh
3 test_description='Test automatic use of a pager.'
5 . ./test-lib.sh
6 . "$TEST_DIRECTORY"/lib-pager.sh
7 . "$TEST_DIRECTORY"/lib-terminal.sh
9 cleanup_fail() {
10 echo >&2 cleanup failed
11 (exit 1)
12 }
14 test_expect_success 'setup' '
15 unset GIT_PAGER GIT_PAGER_IN_USE;
16 test_might_fail git config --unset core.pager &&
18 PAGER="cat >paginated.out" &&
19 export PAGER &&
21 test_commit initial
22 '
24 test_expect_success TTY 'some commands use a pager' '
25 rm -f paginated.out ||
26 cleanup_fail &&
28 test_terminal git log &&
29 test -e paginated.out
30 '
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 '
47 test_expect_success TTY 'some commands do not use a pager' '
48 rm -f paginated.out ||
49 cleanup_fail &&
51 test_terminal git rev-list HEAD &&
52 ! test -e paginated.out
53 '
55 test_expect_success 'no pager when stdout is a pipe' '
56 rm -f paginated.out ||
57 cleanup_fail &&
59 git log | cat &&
60 ! test -e paginated.out
61 '
63 test_expect_success 'no pager when stdout is a regular file' '
64 rm -f paginated.out ||
65 cleanup_fail &&
67 git log >file &&
68 ! test -e paginated.out
69 '
71 test_expect_success TTY 'git --paginate rev-list uses a pager' '
72 rm -f paginated.out ||
73 cleanup_fail &&
75 test_terminal git --paginate rev-list HEAD &&
76 test -e paginated.out
77 '
79 test_expect_success 'no pager even with --paginate when stdout is a pipe' '
80 rm -f file paginated.out ||
81 cleanup_fail &&
83 git --paginate log | cat &&
84 ! test -e paginated.out
85 '
87 test_expect_success TTY 'no pager with --no-pager' '
88 rm -f paginated.out ||
89 cleanup_fail &&
91 test_terminal git --no-pager log &&
92 ! test -e paginated.out
93 '
95 test_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 &&
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 '
108 test_expect_success TTY 'git config uses a pager if configured to' '
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 '
116 test_expect_success TTY 'configuration can enable pager (from subdir)' '
117 rm -f paginated.out &&
118 mkdir -p subdir &&
119 git config pager.bundle true &&
120 test_when_finished "git config --unset pager.bundle" &&
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 '
134 # A colored commit log will begin with an appropriate ANSI escape
135 # for the first color; the text "commit" comes later.
136 colorful() {
137 read firstline <$1
138 ! expr "$firstline" : "[a-zA-Z]" >/dev/null
139 }
141 test_expect_success 'tests can detect color' '
142 rm -f colorful.log colorless.log ||
143 cleanup_fail &&
145 git log --no-color >colorless.log &&
146 git log --color >colorful.log &&
147 ! colorful colorless.log &&
148 colorful colorful.log
149 '
151 test_expect_success 'no color when stdout is a regular file' '
152 rm -f colorless.log &&
153 git config color.ui auto ||
154 cleanup_fail &&
156 git log >colorless.log &&
157 ! colorful colorless.log
158 '
160 test_expect_success TTY 'color when writing to a pager' '
161 rm -f paginated.out &&
162 git config color.ui auto ||
163 cleanup_fail &&
165 (
166 TERM=vt100 &&
167 export TERM &&
168 test_terminal git log
169 ) &&
170 colorful paginated.out
171 '
173 test_expect_success 'color when writing to a file intended for a pager' '
174 rm -f colorful.log &&
175 git config color.ui auto ||
176 cleanup_fail &&
178 (
179 TERM=vt100 &&
180 GIT_PAGER_IN_USE=true &&
181 export TERM GIT_PAGER_IN_USE &&
182 git log >colorful.log
183 ) &&
184 colorful colorful.log
185 '
187 if test_have_prereq SIMPLEPAGER && test_have_prereq TTY
188 then
189 test_set_prereq SIMPLEPAGERTTY
190 fi
192 # Use this helper to make it easy for the caller of your
193 # terminal-using function to specify whether it should fail.
194 # If you write
195 #
196 # your_test() {
197 # parse_args "$@"
198 #
199 # $test_expectation "$cmd - behaves well" "
200 # ...
201 # $full_command &&
202 # ...
203 # "
204 # }
205 #
206 # then your test can be used like this:
207 #
208 # your_test expect_(success|failure) [test_must_fail] 'git foo'
209 #
210 parse_args() {
211 test_expectation="test_$1"
212 shift
213 if test "$1" = test_must_fail
214 then
215 full_command="test_must_fail test_terminal "
216 shift
217 else
218 full_command="test_terminal "
219 fi
220 cmd=$1
221 full_command="$full_command $1"
222 }
224 test_default_pager() {
225 parse_args "$@"
227 $test_expectation SIMPLEPAGERTTY "$cmd - default pager is used by default" "
228 unset PAGER GIT_PAGER;
229 test_might_fail git config --unset core.pager &&
230 rm -f default_pager_used ||
231 cleanup_fail &&
233 cat >\$less <<-\EOF &&
234 #!/bin/sh
235 wc >default_pager_used
236 EOF
237 chmod +x \$less &&
238 (
239 PATH=.:\$PATH &&
240 export PATH &&
241 $full_command
242 ) &&
243 test -e default_pager_used
244 "
245 }
247 test_PAGER_overrides() {
248 parse_args "$@"
250 $test_expectation TTY "$cmd - PAGER overrides default pager" "
251 unset GIT_PAGER;
252 test_might_fail git config --unset core.pager &&
253 rm -f PAGER_used ||
254 cleanup_fail &&
256 PAGER='wc >PAGER_used' &&
257 export PAGER &&
258 $full_command &&
259 test -e PAGER_used
260 "
261 }
263 test_core_pager_overrides() {
264 if_local_config=
265 used_if_wanted='overrides PAGER'
266 test_core_pager "$@"
267 }
269 test_local_config_ignored() {
270 if_local_config='! '
271 used_if_wanted='is not used'
272 test_core_pager "$@"
273 }
275 test_core_pager() {
276 parse_args "$@"
278 $test_expectation TTY "$cmd - repository-local core.pager setting $used_if_wanted" "
279 unset GIT_PAGER;
280 rm -f core.pager_used ||
281 cleanup_fail &&
283 PAGER=wc &&
284 export PAGER &&
285 git config core.pager 'wc >core.pager_used' &&
286 $full_command &&
287 ${if_local_config}test -e core.pager_used
288 "
289 }
291 test_core_pager_subdir() {
292 if_local_config=
293 used_if_wanted='overrides PAGER'
294 test_pager_subdir_helper "$@"
295 }
297 test_no_local_config_subdir() {
298 if_local_config='! '
299 used_if_wanted='is not used'
300 test_pager_subdir_helper "$@"
301 }
303 test_pager_subdir_helper() {
304 parse_args "$@"
306 $test_expectation TTY "$cmd - core.pager $used_if_wanted from subdirectory" "
307 unset GIT_PAGER;
308 rm -f core.pager_used &&
309 rm -fr sub ||
310 cleanup_fail &&
312 PAGER=wc &&
313 stampname=\$(pwd)/core.pager_used &&
314 export PAGER stampname &&
315 git config core.pager 'wc >\"\$stampname\"' &&
316 mkdir sub &&
317 (
318 cd sub &&
319 $full_command
320 ) &&
321 ${if_local_config}test -e core.pager_used
322 "
323 }
325 test_GIT_PAGER_overrides() {
326 parse_args "$@"
328 $test_expectation TTY "$cmd - GIT_PAGER overrides core.pager" "
329 rm -f GIT_PAGER_used ||
330 cleanup_fail &&
332 git config core.pager wc &&
333 GIT_PAGER='wc >GIT_PAGER_used' &&
334 export GIT_PAGER &&
335 $full_command &&
336 test -e GIT_PAGER_used
337 "
338 }
340 test_doesnt_paginate() {
341 parse_args "$@"
343 $test_expectation TTY "no pager for '$cmd'" "
344 rm -f GIT_PAGER_used ||
345 cleanup_fail &&
347 GIT_PAGER='wc >GIT_PAGER_used' &&
348 export GIT_PAGER &&
349 $full_command &&
350 ! test -e GIT_PAGER_used
351 "
352 }
354 test_pager_choices() {
355 test_default_pager expect_success "$@"
356 test_PAGER_overrides expect_success "$@"
357 test_core_pager_overrides expect_success "$@"
358 test_core_pager_subdir expect_success "$@"
359 test_GIT_PAGER_overrides expect_success "$@"
360 }
362 test_expect_success 'setup: some aliases' '
363 git config alias.aliasedlog log &&
364 git config alias.true "!true"
365 '
367 test_pager_choices 'git log'
368 test_pager_choices 'git -p log'
369 test_pager_choices 'git aliasedlog'
371 test_default_pager expect_success 'git -p aliasedlog'
372 test_PAGER_overrides expect_success 'git -p aliasedlog'
373 test_core_pager_overrides expect_success 'git -p aliasedlog'
374 test_core_pager_subdir expect_failure 'git -p aliasedlog'
375 test_GIT_PAGER_overrides expect_success 'git -p aliasedlog'
377 test_default_pager expect_success 'git -p true'
378 test_PAGER_overrides expect_success 'git -p true'
379 test_core_pager_overrides expect_success 'git -p true'
380 test_core_pager_subdir expect_failure 'git -p true'
381 test_GIT_PAGER_overrides expect_success 'git -p true'
383 test_default_pager expect_success test_must_fail 'git -p request-pull'
384 test_PAGER_overrides expect_success test_must_fail 'git -p request-pull'
385 test_core_pager_overrides expect_success test_must_fail 'git -p request-pull'
386 test_core_pager_subdir expect_failure test_must_fail 'git -p request-pull'
387 test_GIT_PAGER_overrides expect_success test_must_fail 'git -p request-pull'
389 test_default_pager expect_success test_must_fail 'git -p'
390 test_PAGER_overrides expect_success test_must_fail 'git -p'
391 test_local_config_ignored expect_failure test_must_fail 'git -p'
392 test_no_local_config_subdir expect_success test_must_fail 'git -p'
393 test_GIT_PAGER_overrides expect_success test_must_fail 'git -p'
395 test_doesnt_paginate expect_failure test_must_fail 'git -p nonsense'
397 test_pager_choices 'git shortlog'
398 test_expect_success 'setup: configure shortlog not to paginate' '
399 git config pager.shortlog false
400 '
401 test_doesnt_paginate expect_success 'git shortlog'
402 test_no_local_config_subdir expect_success 'git shortlog'
403 test_default_pager expect_success 'git -p shortlog'
404 test_core_pager_subdir expect_success 'git -p shortlog'
406 test_core_pager_subdir expect_success test_must_fail \
407 'git -p apply </dev/null'
409 test_done