parse-options: export opterr, optbug
[git/mingw.git] / t / t7006-pager.sh
blobed7575d0fdf9eebf65d808fa810ab4cc03095025
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)
14 test_expect_success 'setup' '
15 sane_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
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
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 &&
37 cd subdir &&
38 test_terminal git log
39 ) &&
41 ls paginated.out subdir/paginated.out ||
43 } >actual &&
44 test_cmp expected actual
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
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
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
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
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
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
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
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
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 &&
125 cd subdir &&
126 test_terminal git bundle unbundle ../test.bundle
127 ) &&
129 test -e paginated.out ||
130 test -e subdir/paginated.out
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
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
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
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 &&
166 TERM=vt100 &&
167 export TERM &&
168 test_terminal git log
169 ) &&
170 colorful paginated.out
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 &&
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
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
191 # your_test() {
192 # parse_args "$@"
194 # $test_expectation "$cmd - behaves well" "
195 # ...
196 # $full_command &&
197 # ...
201 # then your test can be used like this:
203 # your_test expect_(success|failure) [test_must_fail] 'git foo'
205 parse_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 "
215 cmd=$1
216 full_command="$full_command $1"
219 test_default_pager() {
220 parse_args "$@"
222 $test_expectation SIMPLEPAGER,TTY "$cmd - default pager is used by default" "
223 sane_unset PAGER GIT_PAGER &&
224 test_might_fail git config --unset core.pager &&
225 rm -f default_pager_used ||
226 cleanup_fail &&
228 cat >\$less <<-\EOF &&
229 #!/bin/sh
230 wc >default_pager_used
232 chmod +x \$less &&
234 PATH=.:\$PATH &&
235 export PATH &&
236 $full_command
237 ) &&
238 test -e default_pager_used
242 test_PAGER_overrides() {
243 parse_args "$@"
245 $test_expectation TTY "$cmd - PAGER overrides default pager" "
246 sane_unset GIT_PAGER &&
247 test_might_fail git config --unset core.pager &&
248 rm -f PAGER_used ||
249 cleanup_fail &&
251 PAGER='wc >PAGER_used' &&
252 export PAGER &&
253 $full_command &&
254 test -e PAGER_used
258 test_core_pager_overrides() {
259 if_local_config=
260 used_if_wanted='overrides PAGER'
261 test_core_pager "$@"
264 test_local_config_ignored() {
265 if_local_config='! '
266 used_if_wanted='is not used'
267 test_core_pager "$@"
270 test_core_pager() {
271 parse_args "$@"
273 $test_expectation TTY "$cmd - repository-local core.pager setting $used_if_wanted" "
274 sane_unset GIT_PAGER &&
275 rm -f core.pager_used ||
276 cleanup_fail &&
278 PAGER=wc &&
279 export PAGER &&
280 git config core.pager 'wc >core.pager_used' &&
281 $full_command &&
282 ${if_local_config}test -e core.pager_used
286 test_core_pager_subdir() {
287 if_local_config=
288 used_if_wanted='overrides PAGER'
289 test_pager_subdir_helper "$@"
292 test_no_local_config_subdir() {
293 if_local_config='! '
294 used_if_wanted='is not used'
295 test_pager_subdir_helper "$@"
298 test_pager_subdir_helper() {
299 parse_args "$@"
301 $test_expectation TTY "$cmd - core.pager $used_if_wanted from subdirectory" "
302 sane_unset GIT_PAGER &&
303 rm -f core.pager_used &&
304 rm -fr sub ||
305 cleanup_fail &&
307 PAGER=wc &&
308 stampname=\$(pwd)/core.pager_used &&
309 export PAGER stampname &&
310 git config core.pager 'wc >\"\$stampname\"' &&
311 mkdir sub &&
313 cd sub &&
314 $full_command
315 ) &&
316 ${if_local_config}test -e core.pager_used
320 test_GIT_PAGER_overrides() {
321 parse_args "$@"
323 $test_expectation TTY "$cmd - GIT_PAGER overrides core.pager" "
324 rm -f GIT_PAGER_used ||
325 cleanup_fail &&
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
335 test_doesnt_paginate() {
336 parse_args "$@"
338 $test_expectation TTY "no pager for '$cmd'" "
339 rm -f GIT_PAGER_used ||
340 cleanup_fail &&
342 GIT_PAGER='wc >GIT_PAGER_used' &&
343 export GIT_PAGER &&
344 $full_command &&
345 ! test -e GIT_PAGER_used
349 test_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 "$@"
357 test_expect_success 'setup: some aliases' '
358 git config alias.aliasedlog log &&
359 git config alias.true "!true"
362 test_pager_choices 'git log'
363 test_pager_choices 'git -p log'
364 test_pager_choices 'git aliasedlog'
366 test_default_pager expect_success 'git -p aliasedlog'
367 test_PAGER_overrides expect_success 'git -p aliasedlog'
368 test_core_pager_overrides expect_success 'git -p aliasedlog'
369 test_core_pager_subdir expect_failure 'git -p aliasedlog'
370 test_GIT_PAGER_overrides expect_success 'git -p aliasedlog'
372 test_default_pager expect_success 'git -p true'
373 test_PAGER_overrides expect_success 'git -p true'
374 test_core_pager_overrides expect_success 'git -p true'
375 test_core_pager_subdir expect_failure 'git -p true'
376 test_GIT_PAGER_overrides expect_success 'git -p true'
378 test_default_pager expect_success test_must_fail 'git -p request-pull'
379 test_PAGER_overrides expect_success test_must_fail 'git -p request-pull'
380 test_core_pager_overrides expect_success test_must_fail 'git -p request-pull'
381 test_core_pager_subdir expect_failure test_must_fail 'git -p request-pull'
382 test_GIT_PAGER_overrides expect_success test_must_fail 'git -p request-pull'
384 test_default_pager expect_success test_must_fail 'git -p'
385 test_PAGER_overrides expect_success test_must_fail 'git -p'
386 test_local_config_ignored expect_failure test_must_fail 'git -p'
387 test_no_local_config_subdir expect_success test_must_fail 'git -p'
388 test_GIT_PAGER_overrides expect_success test_must_fail 'git -p'
390 test_doesnt_paginate expect_failure test_must_fail 'git -p nonsense'
392 test_pager_choices 'git shortlog'
393 test_expect_success 'setup: configure shortlog not to paginate' '
394 git config pager.shortlog false
396 test_doesnt_paginate expect_success 'git shortlog'
397 test_no_local_config_subdir expect_success 'git shortlog'
398 test_default_pager expect_success 'git -p shortlog'
399 test_core_pager_subdir expect_success 'git -p shortlog'
401 test_core_pager_subdir expect_success test_must_fail \
402 'git -p apply </dev/null'
404 test_expect_success TTY 'command-specific pager' '
405 unset PAGER GIT_PAGER;
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
414 test_expect_success TTY 'command-specific pager overrides core.pager' '
415 unset PAGER GIT_PAGER;
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
424 test_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
433 test_done