]> git.ipfire.org Git - thirdparty/git.git/blob - t/t7006-pager.sh
Merge branch 'maint'
[thirdparty/git.git] / t / t7006-pager.sh
1 #!/bin/sh
2
3 test_description='Test automatic use of a pager.'
4
5 . ./test-lib.sh
6 . "$TEST_DIRECTORY"/lib-pager.sh
7
8 cleanup_fail() {
9 echo >&2 cleanup failed
10 (exit 1)
11 }
12
13 test_expect_success 'set up terminal for tests' '
14 rm -f stdout_is_tty ||
15 cleanup_fail &&
16
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 '
28
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
42
43 test_expect_success 'setup' '
44 unset GIT_PAGER GIT_PAGER_IN_USE;
45 test_might_fail git config --unset core.pager &&
46
47 PAGER="cat >paginated.out" &&
48 export PAGER &&
49
50 test_commit initial
51 '
52
53 test_expect_success TTY 'some commands use a pager' '
54 rm -f paginated.out ||
55 cleanup_fail &&
56
57 test_terminal git log &&
58 test -e paginated.out
59 '
60
61 test_expect_success TTY 'some commands do not use a pager' '
62 rm -f paginated.out ||
63 cleanup_fail &&
64
65 test_terminal git rev-list HEAD &&
66 ! test -e paginated.out
67 '
68
69 test_expect_success 'no pager when stdout is a pipe' '
70 rm -f paginated.out ||
71 cleanup_fail &&
72
73 git log | cat &&
74 ! test -e paginated.out
75 '
76
77 test_expect_success 'no pager when stdout is a regular file' '
78 rm -f paginated.out ||
79 cleanup_fail &&
80
81 git log >file &&
82 ! test -e paginated.out
83 '
84
85 test_expect_success TTY 'git --paginate rev-list uses a pager' '
86 rm -f paginated.out ||
87 cleanup_fail &&
88
89 test_terminal git --paginate rev-list HEAD &&
90 test -e paginated.out
91 '
92
93 test_expect_success 'no pager even with --paginate when stdout is a pipe' '
94 rm -f file paginated.out ||
95 cleanup_fail &&
96
97 git --paginate log | cat &&
98 ! test -e paginated.out
99 '
100
101 test_expect_success TTY 'no pager with --no-pager' '
102 rm -f paginated.out ||
103 cleanup_fail &&
104
105 test_terminal git --no-pager log &&
106 ! test -e paginated.out
107 '
108
109 # A colored commit log will begin with an appropriate ANSI escape
110 # for the first color; the text "commit" comes later.
111 colorful() {
112 read firstline <$1
113 ! expr "$firstline" : "[a-zA-Z]" >/dev/null
114 }
115
116 test_expect_success 'tests can detect color' '
117 rm -f colorful.log colorless.log ||
118 cleanup_fail &&
119
120 git log --no-color >colorless.log &&
121 git log --color >colorful.log &&
122 ! colorful colorless.log &&
123 colorful colorful.log
124 '
125
126 test_expect_success 'no color when stdout is a regular file' '
127 rm -f colorless.log &&
128 git config color.ui auto ||
129 cleanup_fail &&
130
131 git log >colorless.log &&
132 ! colorful colorless.log
133 '
134
135 test_expect_success TTY 'color when writing to a pager' '
136 rm -f paginated.out &&
137 git config color.ui auto ||
138 cleanup_fail &&
139
140 (
141 TERM=vt100 &&
142 export TERM &&
143 test_terminal git log
144 ) &&
145 colorful paginated.out
146 '
147
148 test_expect_success 'color when writing to a file intended for a pager' '
149 rm -f colorful.log &&
150 git config color.ui auto ||
151 cleanup_fail &&
152
153 (
154 TERM=vt100 &&
155 GIT_PAGER_IN_USE=true &&
156 export TERM GIT_PAGER_IN_USE &&
157 git log >colorful.log
158 ) &&
159 colorful colorful.log
160 '
161
162 if test_have_prereq SIMPLEPAGER && test_have_prereq TTY
163 then
164 test_set_prereq SIMPLEPAGERTTY
165 fi
166
167 test_expect_success SIMPLEPAGERTTY 'default pager is used by default' '
168 unset PAGER GIT_PAGER;
169 test_might_fail git config --unset core.pager &&
170 rm -f default_pager_used ||
171 cleanup_fail &&
172
173 cat >$less <<-\EOF &&
174 #!/bin/sh
175 wc >default_pager_used
176 EOF
177 chmod +x $less &&
178 (
179 PATH=.:$PATH &&
180 export PATH &&
181 test_terminal git log
182 ) &&
183 test -e default_pager_used
184 '
185
186 test_expect_success TTY 'PAGER overrides default pager' '
187 unset GIT_PAGER;
188 test_might_fail git config --unset core.pager &&
189 rm -f PAGER_used ||
190 cleanup_fail &&
191
192 PAGER="wc >PAGER_used" &&
193 export PAGER &&
194 test_terminal git log &&
195 test -e PAGER_used
196 '
197
198 test_expect_success TTY 'core.pager overrides PAGER' '
199 unset GIT_PAGER;
200 rm -f core.pager_used ||
201 cleanup_fail &&
202
203 PAGER=wc &&
204 export PAGER &&
205 git config core.pager "wc >core.pager_used" &&
206 test_terminal git log &&
207 test -e core.pager_used
208 '
209
210 test_expect_success TTY 'GIT_PAGER overrides core.pager' '
211 rm -f GIT_PAGER_used ||
212 cleanup_fail &&
213
214 git config core.pager wc &&
215 GIT_PAGER="wc >GIT_PAGER_used" &&
216 export GIT_PAGER &&
217 test_terminal git log &&
218 test -e GIT_PAGER_used
219 '
220
221 test_done