t7006 (pager): introduce helper for parameterized tests
[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
fdf1bc48
JN
7cleanup_fail() {
8 echo >&2 cleanup failed
9 (exit 1)
10}
11
2d3ca216 12test_expect_success 'set up terminal for tests' '
fdf1bc48
JN
13 rm -f stdout_is_tty ||
14 cleanup_fail &&
15
60b6e220
JN
16 if test -t 1
17 then
fdf1bc48 18 >stdout_is_tty
2d3ca216
JN
19 elif
20 test_have_prereq PERL &&
21 "$PERL_PATH" "$TEST_DIRECTORY"/t7006/test-terminal.perl \
22 sh -c "test -t 1"
23 then
fdf1bc48 24 >test_terminal_works
60b6e220
JN
25 fi
26'
27
28if test -e stdout_is_tty
29then
2d3ca216
JN
30 test_terminal() { "$@"; }
31 test_set_prereq TTY
32elif test -e test_terminal_works
33then
34 test_terminal() {
35 "$PERL_PATH" "$TEST_DIRECTORY"/t7006/test-terminal.perl "$@"
36 }
60b6e220
JN
37 test_set_prereq TTY
38else
2d3ca216 39 say no usable terminal, so skipping some tests
60b6e220
JN
40fi
41
60b6e220 42test_expect_success 'setup' '
c289c315 43 unset GIT_PAGER GIT_PAGER_IN_USE;
fdf1bc48
JN
44 test_might_fail git config --unset core.pager &&
45
46 PAGER="cat >paginated.out" &&
47 export PAGER &&
48
60b6e220
JN
49 test_commit initial
50'
51
60b6e220 52test_expect_success TTY 'some commands use a pager' '
fdf1bc48
JN
53 rm -f paginated.out ||
54 cleanup_fail &&
55
2d3ca216 56 test_terminal git log &&
60b6e220
JN
57 test -e paginated.out
58'
59
60b6e220 60test_expect_success TTY 'some commands do not use a pager' '
fdf1bc48
JN
61 rm -f paginated.out ||
62 cleanup_fail &&
63
2d3ca216 64 test_terminal git rev-list HEAD &&
60b6e220
JN
65 ! test -e paginated.out
66'
67
60b6e220 68test_expect_success 'no pager when stdout is a pipe' '
fdf1bc48
JN
69 rm -f paginated.out ||
70 cleanup_fail &&
71
60b6e220
JN
72 git log | cat &&
73 ! test -e paginated.out
74'
75
60b6e220 76test_expect_success 'no pager when stdout is a regular file' '
fdf1bc48
JN
77 rm -f paginated.out ||
78 cleanup_fail &&
79
80 git log >file &&
60b6e220
JN
81 ! test -e paginated.out
82'
83
60b6e220 84test_expect_success TTY 'git --paginate rev-list uses a pager' '
fdf1bc48
JN
85 rm -f paginated.out ||
86 cleanup_fail &&
87
2d3ca216 88 test_terminal git --paginate rev-list HEAD &&
60b6e220
JN
89 test -e paginated.out
90'
91
60b6e220 92test_expect_success 'no pager even with --paginate when stdout is a pipe' '
fdf1bc48
JN
93 rm -f file paginated.out ||
94 cleanup_fail &&
95
60b6e220
JN
96 git --paginate log | cat &&
97 ! test -e paginated.out
98'
99
60b6e220 100test_expect_success TTY 'no pager with --no-pager' '
fdf1bc48
JN
101 rm -f paginated.out ||
102 cleanup_fail &&
103
2d3ca216 104 test_terminal git --no-pager log &&
60b6e220
JN
105 ! test -e paginated.out
106'
107
108# A colored commit log will begin with an appropriate ANSI escape
109# for the first color; the text "commit" comes later.
110colorful() {
fdf1bc48 111 read firstline <$1
e0ae1e6f 112 ! expr "$firstline" : "[a-zA-Z]" >/dev/null
60b6e220
JN
113}
114
60b6e220 115test_expect_success 'tests can detect color' '
fdf1bc48
JN
116 rm -f colorful.log colorless.log ||
117 cleanup_fail &&
118
119 git log --no-color >colorless.log &&
120 git log --color >colorful.log &&
60b6e220
JN
121 ! colorful colorless.log &&
122 colorful colorful.log
123'
124
60b6e220 125test_expect_success 'no color when stdout is a regular file' '
fdf1bc48
JN
126 rm -f colorless.log &&
127 git config color.ui auto ||
128 cleanup_fail &&
129
130 git log >colorless.log &&
60b6e220
JN
131 ! colorful colorless.log
132'
133
60b6e220 134test_expect_success TTY 'color when writing to a pager' '
fdf1bc48
JN
135 rm -f paginated.out &&
136 git config color.ui auto ||
137 cleanup_fail &&
138
139 (
140 TERM=vt100 &&
141 export TERM &&
142 test_terminal git log
143 ) &&
60b6e220
JN
144 colorful paginated.out
145'
146
60b6e220 147test_expect_success 'color when writing to a file intended for a pager' '
fdf1bc48
JN
148 rm -f colorful.log &&
149 git config color.ui auto ||
150 cleanup_fail &&
151
152 (
153 TERM=vt100 &&
154 GIT_PAGER_IN_USE=true &&
155 export TERM GIT_PAGER_IN_USE &&
156 git log >colorful.log
157 ) &&
60b6e220
JN
158 colorful colorful.log
159'
160
60b6e220 161test_expect_success 'determine default pager' '
c289c315 162 unset PAGER GIT_PAGER;
fdf1bc48
JN
163 test_might_fail git config --unset core.pager ||
164 cleanup_fail &&
165
60b6e220
JN
166 less=$(git var GIT_PAGER) &&
167 test -n "$less"
168'
169
e0ae1e6f 170if expr "$less" : '[a-z][a-z]*$' >/dev/null && test_have_prereq TTY
60b6e220
JN
171then
172 test_set_prereq SIMPLEPAGER
173fi
174
3c7406d4
JN
175# Use this helper to make it easy for the caller of your
176# terminal-using function to specify whether it should fail.
177# If you write
178#
179# your_test() {
180# parse_args "$@"
181#
182# $test_expectation "$cmd - behaves well" "
183# ...
184# $full_command &&
185# ...
186# "
187# }
188#
189# then your test can be used like this:
190#
191# your_test expect_(success|failure) [test_must_fail] 'git foo'
192#
193parse_args() {
194 test_expectation="test_$1"
195 shift
196 if test "$1" = test_must_fail
197 then
198 full_command="test_must_fail test_terminal "
199 shift
200 else
201 full_command="test_terminal "
202 fi
203 cmd=$1
204 full_command="$full_command $1"
205}
206
207parse_args expect_success 'git log'
208$test_expectation SIMPLEPAGER "$cmd - default pager is used by default" "
c289c315 209 unset PAGER GIT_PAGER;
fdf1bc48
JN
210 test_might_fail git config --unset core.pager &&
211 rm -f default_pager_used ||
212 cleanup_fail &&
213
3c7406d4 214 cat >\$less <<-\EOF &&
fdf1bc48
JN
215 #!/bin/sh
216 wc >default_pager_used
60b6e220 217 EOF
3c7406d4 218 chmod +x \$less &&
fdf1bc48 219 (
3c7406d4 220 PATH=.:\$PATH &&
fdf1bc48 221 export PATH &&
3c7406d4 222 $full_command
fdf1bc48 223 ) &&
60b6e220 224 test -e default_pager_used
3c7406d4 225"
60b6e220 226
3c7406d4
JN
227parse_args expect_success 'git log'
228$test_expectation TTY "$cmd - PAGER overrides default pager" "
c289c315 229 unset GIT_PAGER;
fdf1bc48
JN
230 test_might_fail git config --unset core.pager &&
231 rm -f PAGER_used ||
232 cleanup_fail &&
233
3c7406d4 234 PAGER='wc >PAGER_used' &&
60b6e220 235 export PAGER &&
3c7406d4 236 $full_command &&
60b6e220 237 test -e PAGER_used
3c7406d4 238"
60b6e220 239
3c7406d4
JN
240parse_args expect_success 'git log'
241$test_expectation TTY "$cmd - core.pager overrides PAGER" "
c289c315 242 unset GIT_PAGER;
fdf1bc48
JN
243 rm -f core.pager_used ||
244 cleanup_fail &&
245
8c33b4cf 246 PAGER=wc &&
60b6e220 247 export PAGER &&
3c7406d4
JN
248 git config core.pager 'wc >core.pager_used' &&
249 $full_command &&
60b6e220 250 test -e core.pager_used
3c7406d4 251"
60b6e220 252
3c7406d4
JN
253parse_args expect_success 'git log'
254$test_expectation TTY "$cmd - GIT_PAGER overrides core.pager" "
fdf1bc48
JN
255 rm -f GIT_PAGER_used ||
256 cleanup_fail &&
257
8c33b4cf 258 git config core.pager wc &&
3c7406d4 259 GIT_PAGER='wc >GIT_PAGER_used' &&
60b6e220 260 export GIT_PAGER &&
3c7406d4 261 $full_command &&
60b6e220 262 test -e GIT_PAGER_used
3c7406d4 263"
60b6e220
JN
264
265test_done