Code

6c86d7035ffa32d75bfd7e93f61299eafb2b3603
[git.git] / t / t7006-pager.sh
1 #!/bin/sh
3 test_description='Test automatic use of a pager.'
5 . ./test-lib.sh
6 . "$TEST_DIRECTORY"/lib-pager.sh
8 cleanup_fail() {
9         echo >&2 cleanup failed
10         (exit 1)
11 }
13 test_expect_success 'set up terminal for tests' '
14         rm -f stdout_is_tty ||
15         cleanup_fail &&
17         if test -t 1
18         then
19                 >stdout_is_tty
20         elif
21                 test_have_prereq PERL &&
22                 "$PERL_PATH" "$TEST_DIRECTORY"/t7006/test-terminal.perl \
23                         sh -c "test -t 1"
24         then
25                 >test_terminal_works
26         fi
27 '
29 if test -e stdout_is_tty
30 then
31         test_terminal() { "$@"; }
32         test_set_prereq TTY
33 elif test -e test_terminal_works
34 then
35         test_terminal() {
36                 "$PERL_PATH" "$TEST_DIRECTORY"/t7006/test-terminal.perl "$@"
37         }
38         test_set_prereq TTY
39 else
40         say "# no usable terminal, so skipping some tests"
41 fi
43 test_expect_success 'setup' '
44         unset GIT_PAGER GIT_PAGER_IN_USE;
45         test_might_fail git config --unset core.pager &&
47         PAGER="cat >paginated.out" &&
48         export PAGER &&
50         test_commit initial
51 '
53 test_expect_success TTY 'some commands use a pager' '
54         rm -f paginated.out ||
55         cleanup_fail &&
57         test_terminal git log &&
58         test -e paginated.out
59 '
61 test_expect_success TTY 'some commands do not use a pager' '
62         rm -f paginated.out ||
63         cleanup_fail &&
65         test_terminal git rev-list HEAD &&
66         ! test -e paginated.out
67 '
69 test_expect_success 'no pager when stdout is a pipe' '
70         rm -f paginated.out ||
71         cleanup_fail &&
73         git log | cat &&
74         ! test -e paginated.out
75 '
77 test_expect_success 'no pager when stdout is a regular file' '
78         rm -f paginated.out ||
79         cleanup_fail &&
81         git log >file &&
82         ! test -e paginated.out
83 '
85 test_expect_success TTY 'git --paginate rev-list uses a pager' '
86         rm -f paginated.out ||
87         cleanup_fail &&
89         test_terminal git --paginate rev-list HEAD &&
90         test -e paginated.out
91 '
93 test_expect_success 'no pager even with --paginate when stdout is a pipe' '
94         rm -f file paginated.out ||
95         cleanup_fail &&
97         git --paginate log | cat &&
98         ! test -e paginated.out
99 '
101 test_expect_success TTY 'no pager with --no-pager' '
102         rm -f paginated.out ||
103         cleanup_fail &&
105         test_terminal git --no-pager log &&
106         ! test -e paginated.out
109 test_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 &&
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
122 # A colored commit log will begin with an appropriate ANSI escape
123 # for the first color; the text "commit" comes later.
124 colorful() {
125         read firstline <$1
126         ! expr "$firstline" : "[a-zA-Z]" >/dev/null
129 test_expect_success 'tests can detect color' '
130         rm -f colorful.log colorless.log ||
131         cleanup_fail &&
133         git log --no-color >colorless.log &&
134         git log --color >colorful.log &&
135         ! colorful colorless.log &&
136         colorful colorful.log
139 test_expect_success 'no color when stdout is a regular file' '
140         rm -f colorless.log &&
141         git config color.ui auto ||
142         cleanup_fail &&
144         git log >colorless.log &&
145         ! colorful colorless.log
148 test_expect_success TTY 'color when writing to a pager' '
149         rm -f paginated.out &&
150         git config color.ui auto ||
151         cleanup_fail &&
153         (
154                 TERM=vt100 &&
155                 export TERM &&
156                 test_terminal git log
157         ) &&
158         colorful paginated.out
161 test_expect_success 'color when writing to a file intended for a pager' '
162         rm -f colorful.log &&
163         git config color.ui auto ||
164         cleanup_fail &&
166         (
167                 TERM=vt100 &&
168                 GIT_PAGER_IN_USE=true &&
169                 export TERM GIT_PAGER_IN_USE &&
170                 git log >colorful.log
171         ) &&
172         colorful colorful.log
175 if test_have_prereq SIMPLEPAGER && test_have_prereq TTY
176 then
177         test_set_prereq SIMPLEPAGERTTY
178 fi
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
184 #       your_test() {
185 #               parse_args "$@"
187 #               $test_expectation "$cmd - behaves well" "
188 #                       ...
189 #                       $full_command &&
190 #                       ...
191 #               "
192 #       }
194 # then your test can be used like this:
196 #       your_test expect_(success|failure) [test_must_fail] 'git foo'
198 parse_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"
212 test_default_pager() {
213         parse_args "$@"
215         $test_expectation SIMPLEPAGERTTY "$cmd - default pager is used by default" "
216                 unset PAGER GIT_PAGER;
217                 test_might_fail git config --unset core.pager &&
218                 rm -f default_pager_used ||
219                 cleanup_fail &&
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         "
235 test_PAGER_overrides() {
236         parse_args "$@"
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 &&
244                 PAGER='wc >PAGER_used' &&
245                 export PAGER &&
246                 $full_command &&
247                 test -e PAGER_used
248         "
251 test_core_pager_overrides() {
252         if_local_config=
253         used_if_wanted='overrides PAGER'
254         test_core_pager "$@"
257 test_local_config_ignored() {
258         if_local_config='! '
259         used_if_wanted='is not used'
260         test_core_pager "$@"
263 test_core_pager() {
264         parse_args "$@"
266         $test_expectation TTY "$cmd - repository-local core.pager setting $used_if_wanted" "
267                 unset GIT_PAGER;
268                 rm -f core.pager_used ||
269                 cleanup_fail &&
271                 PAGER=wc &&
272                 export PAGER &&
273                 git config core.pager 'wc >core.pager_used' &&
274                 $full_command &&
275                 ${if_local_config}test -e core.pager_used
276         "
279 test_core_pager_subdir() {
280         if_local_config=
281         used_if_wanted='overrides PAGER'
282         test_pager_subdir_helper "$@"
285 test_no_local_config_subdir() {
286         if_local_config='! '
287         used_if_wanted='is not used'
288         test_pager_subdir_helper "$@"
291 test_pager_subdir_helper() {
292         parse_args "$@"
294         $test_expectation TTY "$cmd - core.pager $used_if_wanted from subdirectory" "
295                 unset GIT_PAGER;
296                 rm -f core.pager_used &&
297                 rm -fr sub ||
298                 cleanup_fail &&
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                 ) &&
309                 ${if_local_config}test -e core.pager_used
310         "
313 test_GIT_PAGER_overrides() {
314         parse_args "$@"
316         $test_expectation TTY "$cmd - GIT_PAGER overrides core.pager" "
317                 rm -f GIT_PAGER_used ||
318                 cleanup_fail &&
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         "
328 test_doesnt_paginate() {
329         parse_args "$@"
331         $test_expectation TTY "no pager for '$cmd'" "
332                 rm -f GIT_PAGER_used ||
333                 cleanup_fail &&
335                 GIT_PAGER='wc >GIT_PAGER_used' &&
336                 export GIT_PAGER &&
337                 $full_command &&
338                 ! test -e GIT_PAGER_used
339         "
342 test_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 "$@"
350 test_expect_success 'setup: some aliases' '
351         git config alias.aliasedlog log &&
352         git config alias.true "!true"
355 test_pager_choices                       'git log'
356 test_pager_choices                       'git -p log'
357 test_pager_choices                       'git aliasedlog'
359 test_default_pager        expect_success 'git -p aliasedlog'
360 test_PAGER_overrides      expect_success 'git -p aliasedlog'
361 test_core_pager_overrides expect_success 'git -p aliasedlog'
362 test_core_pager_subdir    expect_failure 'git -p aliasedlog'
363 test_GIT_PAGER_overrides  expect_success 'git -p aliasedlog'
365 test_default_pager        expect_success 'git -p true'
366 test_PAGER_overrides      expect_success 'git -p true'
367 test_core_pager_overrides expect_success 'git -p true'
368 test_core_pager_subdir    expect_failure 'git -p true'
369 test_GIT_PAGER_overrides  expect_success 'git -p true'
371 test_default_pager        expect_success test_must_fail 'git -p request-pull'
372 test_PAGER_overrides      expect_success test_must_fail 'git -p request-pull'
373 test_core_pager_overrides expect_success test_must_fail 'git -p request-pull'
374 test_core_pager_subdir    expect_failure test_must_fail 'git -p request-pull'
375 test_GIT_PAGER_overrides  expect_success test_must_fail 'git -p request-pull'
377 test_default_pager        expect_success test_must_fail 'git -p'
378 test_PAGER_overrides      expect_success test_must_fail 'git -p'
379 test_local_config_ignored expect_failure test_must_fail 'git -p'
380 test_no_local_config_subdir expect_success test_must_fail 'git -p'
381 test_GIT_PAGER_overrides  expect_success test_must_fail 'git -p'
383 test_doesnt_paginate      expect_failure test_must_fail 'git -p nonsense'
385 test_pager_choices                       'git shortlog'
386 test_expect_success 'setup: configure shortlog not to paginate' '
387         git config pager.shortlog false
389 test_doesnt_paginate      expect_success 'git shortlog'
390 test_no_local_config_subdir expect_success 'git shortlog'
391 test_default_pager        expect_success 'git -p shortlog'
392 test_core_pager_subdir    expect_success 'git -p shortlog'
394 test_core_pager_subdir    expect_success test_must_fail \
395                                          'git -p apply </dev/null'
397 test_done