tests: Add tests for automatic use of pager
[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
6
7rm -f stdout_is_tty
8test_expect_success 'is stdout a terminal?' '
9 if test -t 1
10 then
11 : > stdout_is_tty
12 fi
13'
14
15if test -e stdout_is_tty
16then
17 test_set_prereq TTY
18else
19 say stdout is not a terminal, so skipping some tests.
20fi
21
22unset GIT_PAGER GIT_PAGER_IN_USE
23git config --unset core.pager
24PAGER='cat > paginated.out'
25export PAGER
26
27test_expect_success 'setup' '
28 test_commit initial
29'
30
31rm -f paginated.out
32test_expect_success TTY 'some commands use a pager' '
33 git log &&
34 test -e paginated.out
35'
36
37rm -f paginated.out
38test_expect_success TTY 'some commands do not use a pager' '
39 git rev-list HEAD &&
40 ! test -e paginated.out
41'
42
43rm -f paginated.out
44test_expect_success 'no pager when stdout is a pipe' '
45 git log | cat &&
46 ! test -e paginated.out
47'
48
49rm -f paginated.out
50test_expect_success 'no pager when stdout is a regular file' '
51 git log > file &&
52 ! test -e paginated.out
53'
54
55rm -f paginated.out
56test_expect_success TTY 'git --paginate rev-list uses a pager' '
57 git --paginate rev-list HEAD &&
58 test -e paginated.out
59'
60
61rm -f file paginated.out
62test_expect_success 'no pager even with --paginate when stdout is a pipe' '
63 git --paginate log | cat &&
64 ! test -e paginated.out
65'
66
67rm -f paginated.out
68test_expect_success TTY 'no pager with --no-pager' '
69 git --no-pager log &&
70 ! test -e paginated.out
71'
72
73# A colored commit log will begin with an appropriate ANSI escape
74# for the first color; the text "commit" comes later.
75colorful() {
76 read firstline < $1
77 ! expr "$firstline" : "^[a-zA-Z]" >/dev/null
78}
79
80rm -f colorful.log colorless.log
81test_expect_success 'tests can detect color' '
82 git log --no-color > colorless.log &&
83 git log --color > colorful.log &&
84 ! colorful colorless.log &&
85 colorful colorful.log
86'
87
88rm -f colorless.log
89git config color.ui auto
90test_expect_success 'no color when stdout is a regular file' '
91 git log > colorless.log &&
92 ! colorful colorless.log
93'
94
95rm -f paginated.out
96git config color.ui auto
97test_expect_success TTY 'color when writing to a pager' '
98 TERM=vt100 git log &&
99 colorful paginated.out
100'
101
102rm -f colorful.log
103git config color.ui auto
104test_expect_success 'color when writing to a file intended for a pager' '
105 TERM=vt100 GIT_PAGER_IN_USE=true git log > colorful.log &&
106 colorful colorful.log
107'
108
109unset PAGER GIT_PAGER
110git config --unset core.pager
111test_expect_success 'determine default pager' '
112 less=$(git var GIT_PAGER) &&
113 test -n "$less"
114'
115
116if expr "$less" : '^[a-z]*$' > /dev/null && test_have_prereq TTY
117then
118 test_set_prereq SIMPLEPAGER
119fi
120
121unset PAGER GIT_PAGER
122git config --unset core.pager
123rm -f default_pager_used
124test_expect_success SIMPLEPAGER 'default pager is used by default' '
125 cat > $less <<-EOF &&
126 #!$SHELL_PATH
127 : > default_pager_used
128 EOF
129 chmod +x $less &&
130 PATH=.:$PATH git log &&
131 test -e default_pager_used
132'
133
134unset GIT_PAGER
135git config --unset core.pager
136rm -f PAGER_used
137test_expect_success TTY 'PAGER overrides default pager' '
138 PAGER=": > PAGER_used" &&
139 export PAGER &&
140 git log &&
141 test -e PAGER_used
142'
143
144unset GIT_PAGER
145rm -f core.pager_used
146test_expect_success TTY 'core.pager overrides PAGER' '
147 PAGER=: &&
148 export PAGER &&
149 git config core.pager ": > core.pager_used" &&
150 git log &&
151 test -e core.pager_used
152'
153
154rm -f GIT_PAGER_used
155test_expect_success TTY 'GIT_PAGER overrides core.pager' '
156 git config core.pager : &&
157 GIT_PAGER=": > GIT_PAGER_used" &&
158 export GIT_PAGER &&
159 git log &&
160 test -e GIT_PAGER_used
161'
162
163test_done