Code

setup_pager: set GIT_PAGER_IN_USE
[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
7 . "$TEST_DIRECTORY"/lib-terminal.sh
9 cleanup_fail() {
10         echo >&2 cleanup failed
11         (exit 1)
12 }
14 test_expect_success 'setup' '
15         sane_unset GIT_PAGER GIT_PAGER_IN_USE &&
16         test_unconfig 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_unconfig pager.grep &&
98         test_terminal git grep initial &&
99         test -e paginated.out &&
101         rm -f paginated.out &&
102         test_config pager.grep false &&
103         test_terminal git grep initial &&
104         ! test -e paginated.out
107 test_expect_success TTY 'git config uses a pager if configured to' '
108         rm -f paginated.out &&
109         test_config pager.config true &&
110         test_terminal git config --list &&
111         test -e paginated.out
114 test_expect_success TTY 'configuration can enable pager (from subdir)' '
115         rm -f paginated.out &&
116         mkdir -p subdir &&
117         test_config pager.bundle true &&
119         git bundle create test.bundle --all &&
120         rm -f paginated.out subdir/paginated.out &&
121         (
122                 cd subdir &&
123                 test_terminal git bundle unbundle ../test.bundle
124         ) &&
125         {
126                 test -e paginated.out ||
127                 test -e subdir/paginated.out
128         }
131 # A colored commit log will begin with an appropriate ANSI escape
132 # for the first color; the text "commit" comes later.
133 colorful() {
134         read firstline <$1
135         ! expr "$firstline" : "[a-zA-Z]" >/dev/null
138 test_expect_success 'tests can detect color' '
139         rm -f colorful.log colorless.log ||
140         cleanup_fail &&
142         git log --no-color >colorless.log &&
143         git log --color >colorful.log &&
144         ! colorful colorless.log &&
145         colorful colorful.log
148 test_expect_success 'no color when stdout is a regular file' '
149         rm -f colorless.log &&
150         test_config color.ui auto ||
151         cleanup_fail &&
153         git log >colorless.log &&
154         ! colorful colorless.log
157 test_expect_success TTY 'color when writing to a pager' '
158         rm -f paginated.out &&
159         test_config color.ui auto ||
160         cleanup_fail &&
162         (
163                 TERM=vt100 &&
164                 export TERM &&
165                 test_terminal git log
166         ) &&
167         colorful paginated.out
170 test_expect_success 'color when writing to a file intended for a pager' '
171         rm -f colorful.log &&
172         test_config color.ui auto ||
173         cleanup_fail &&
175         (
176                 TERM=vt100 &&
177                 GIT_PAGER_IN_USE=true &&
178                 export TERM GIT_PAGER_IN_USE &&
179                 git log >colorful.log
180         ) &&
181         colorful colorful.log
184 test_expect_success TTY 'colors are sent to pager for external commands' '
185         test_config alias.externallog "!git log" &&
186         test_config color.ui auto &&
187         (
188                 TERM=vt100 &&
189                 export TERM &&
190                 test_terminal git -p externallog
191         ) &&
192         colorful paginated.out
195 # Use this helper to make it easy for the caller of your
196 # terminal-using function to specify whether it should fail.
197 # If you write
199 #       your_test() {
200 #               parse_args "$@"
202 #               $test_expectation "$cmd - behaves well" "
203 #                       ...
204 #                       $full_command &&
205 #                       ...
206 #               "
207 #       }
209 # then your test can be used like this:
211 #       your_test expect_(success|failure) [test_must_fail] 'git foo'
213 parse_args() {
214         test_expectation="test_$1"
215         shift
216         if test "$1" = test_must_fail
217         then
218                 full_command="test_must_fail test_terminal "
219                 shift
220         else
221                 full_command="test_terminal "
222         fi
223         cmd=$1
224         full_command="$full_command $1"
227 test_default_pager() {
228         parse_args "$@"
230         $test_expectation SIMPLEPAGER,TTY "$cmd - default pager is used by default" "
231                 sane_unset PAGER GIT_PAGER &&
232                 test_unconfig core.pager &&
233                 rm -f default_pager_used ||
234                 cleanup_fail &&
236                 cat >\$less <<-\EOF &&
237                 #!/bin/sh
238                 wc >default_pager_used
239                 EOF
240                 chmod +x \$less &&
241                 (
242                         PATH=.:\$PATH &&
243                         export PATH &&
244                         $full_command
245                 ) &&
246                 test -e default_pager_used
247         "
250 test_PAGER_overrides() {
251         parse_args "$@"
253         $test_expectation TTY "$cmd - PAGER overrides default pager" "
254                 sane_unset GIT_PAGER &&
255                 test_unconfig core.pager &&
256                 rm -f PAGER_used ||
257                 cleanup_fail &&
259                 PAGER='wc >PAGER_used' &&
260                 export PAGER &&
261                 $full_command &&
262                 test -e PAGER_used
263         "
266 test_core_pager_overrides() {
267         if_local_config=
268         used_if_wanted='overrides PAGER'
269         test_core_pager "$@"
272 test_local_config_ignored() {
273         if_local_config='! '
274         used_if_wanted='is not used'
275         test_core_pager "$@"
278 test_core_pager() {
279         parse_args "$@"
281         $test_expectation TTY "$cmd - repository-local core.pager setting $used_if_wanted" "
282                 sane_unset GIT_PAGER &&
283                 rm -f core.pager_used ||
284                 cleanup_fail &&
286                 PAGER=wc &&
287                 export PAGER &&
288                 test_config core.pager 'wc >core.pager_used' &&
289                 $full_command &&
290                 ${if_local_config}test -e core.pager_used
291         "
294 test_core_pager_subdir() {
295         if_local_config=
296         used_if_wanted='overrides PAGER'
297         test_pager_subdir_helper "$@"
300 test_no_local_config_subdir() {
301         if_local_config='! '
302         used_if_wanted='is not used'
303         test_pager_subdir_helper "$@"
306 test_pager_subdir_helper() {
307         parse_args "$@"
309         $test_expectation TTY "$cmd - core.pager $used_if_wanted from subdirectory" "
310                 sane_unset GIT_PAGER &&
311                 rm -f core.pager_used &&
312                 rm -fr sub ||
313                 cleanup_fail &&
315                 PAGER=wc &&
316                 stampname=\$(pwd)/core.pager_used &&
317                 export PAGER stampname &&
318                 test_config core.pager 'wc >\"\$stampname\"' &&
319                 mkdir sub &&
320                 (
321                         cd sub &&
322                         $full_command
323                 ) &&
324                 ${if_local_config}test -e core.pager_used
325         "
328 test_GIT_PAGER_overrides() {
329         parse_args "$@"
331         $test_expectation TTY "$cmd - GIT_PAGER overrides core.pager" "
332                 rm -f GIT_PAGER_used ||
333                 cleanup_fail &&
335                 test_config core.pager wc &&
336                 GIT_PAGER='wc >GIT_PAGER_used' &&
337                 export GIT_PAGER &&
338                 $full_command &&
339                 test -e GIT_PAGER_used
340         "
343 test_doesnt_paginate() {
344         parse_args "$@"
346         $test_expectation TTY "no pager for '$cmd'" "
347                 rm -f GIT_PAGER_used ||
348                 cleanup_fail &&
350                 GIT_PAGER='wc >GIT_PAGER_used' &&
351                 export GIT_PAGER &&
352                 $full_command &&
353                 ! test -e GIT_PAGER_used
354         "
357 test_pager_choices() {
358         test_default_pager        expect_success "$@"
359         test_PAGER_overrides      expect_success "$@"
360         test_core_pager_overrides expect_success "$@"
361         test_core_pager_subdir    expect_success "$@"
362         test_GIT_PAGER_overrides  expect_success "$@"
365 test_expect_success 'setup: some aliases' '
366         git config alias.aliasedlog log &&
367         git config alias.true "!true"
370 test_pager_choices                       'git log'
371 test_pager_choices                       'git -p log'
372 test_pager_choices                       'git aliasedlog'
374 test_default_pager        expect_success 'git -p aliasedlog'
375 test_PAGER_overrides      expect_success 'git -p aliasedlog'
376 test_core_pager_overrides expect_success 'git -p aliasedlog'
377 test_core_pager_subdir    expect_failure 'git -p aliasedlog'
378 test_GIT_PAGER_overrides  expect_success 'git -p aliasedlog'
380 test_default_pager        expect_success 'git -p true'
381 test_PAGER_overrides      expect_success 'git -p true'
382 test_core_pager_overrides expect_success 'git -p true'
383 test_core_pager_subdir    expect_failure 'git -p true'
384 test_GIT_PAGER_overrides  expect_success 'git -p true'
386 test_default_pager        expect_success test_must_fail 'git -p request-pull'
387 test_PAGER_overrides      expect_success test_must_fail 'git -p request-pull'
388 test_core_pager_overrides expect_success test_must_fail 'git -p request-pull'
389 test_core_pager_subdir    expect_failure test_must_fail 'git -p request-pull'
390 test_GIT_PAGER_overrides  expect_success test_must_fail 'git -p request-pull'
392 test_default_pager        expect_success test_must_fail 'git -p'
393 test_PAGER_overrides      expect_success test_must_fail 'git -p'
394 test_local_config_ignored expect_failure test_must_fail 'git -p'
395 test_no_local_config_subdir expect_success test_must_fail 'git -p'
396 test_GIT_PAGER_overrides  expect_success test_must_fail 'git -p'
398 test_doesnt_paginate      expect_failure test_must_fail 'git -p nonsense'
400 test_pager_choices                       'git shortlog'
401 test_expect_success 'setup: configure shortlog not to paginate' '
402         git config pager.shortlog false
404 test_doesnt_paginate      expect_success 'git shortlog'
405 test_no_local_config_subdir expect_success 'git shortlog'
406 test_default_pager        expect_success 'git -p shortlog'
407 test_core_pager_subdir    expect_success 'git -p shortlog'
409 test_core_pager_subdir    expect_success test_must_fail \
410                                          'git -p apply </dev/null'
412 test_expect_success TTY 'command-specific pager' '
413         sane_unset PAGER GIT_PAGER &&
414         echo "foo:initial" >expect &&
415         >actual &&
416         test_unconfig core.pager &&
417         test_config pager.log "sed s/^/foo:/ >actual" &&
418         test_terminal git log --format=%s -1 &&
419         test_cmp expect actual
422 test_expect_success TTY 'command-specific pager overrides core.pager' '
423         sane_unset PAGER GIT_PAGER &&
424         echo "foo:initial" >expect &&
425         >actual &&
426         test_config core.pager "exit 1"
427         test_config pager.log "sed s/^/foo:/ >actual" &&
428         test_terminal git log --format=%s -1 &&
429         test_cmp expect actual
432 test_expect_success TTY 'command-specific pager overridden by environment' '
433         GIT_PAGER="sed s/^/foo:/ >actual" && export GIT_PAGER &&
434         >actual &&
435         echo "foo:initial" >expect &&
436         test_config pager.log "exit 1" &&
437         test_terminal git log --format=%s -1 &&
438         test_cmp expect actual
441 test_done