Add tests for "git diff --check" with core.whitespace options
[git/git.git] / t / t4015-diff-whitespace.sh
CommitLineData
2344d47f
JS
1#!/bin/sh
2#
3# Copyright (c) 2006 Johannes E. Schindelin
4#
5
6test_description='Test special whitespace in diff engine.
7
8'
9. ./test-lib.sh
10. ../diff-lib.sh
11
12# Ray Lehtiniemi's example
13
14cat << EOF > x
15do {
16 nothing;
17} while (0);
18EOF
19
5be60078 20git update-index --add x
2344d47f
JS
21
22cat << EOF > x
23do
24{
25 nothing;
26}
27while (0);
28EOF
29
30cat << EOF > expect
31diff --git a/x b/x
32index adf3937..6edc172 100644
33--- a/x
34+++ b/x
35@@ -1,3 +1,5 @@
36-do {
37+do
38+{
39 nothing;
40-} while (0);
41+}
42+while (0);
43EOF
44
5be60078 45git diff > out
5bd74506 46test_expect_success "Ray's example without options" 'git diff expect out'
2344d47f 47
5be60078 48git diff -w > out
5bd74506 49test_expect_success "Ray's example with -w" 'git diff expect out'
2344d47f 50
5be60078 51git diff -b > out
5bd74506 52test_expect_success "Ray's example with -b" 'git diff expect out'
2344d47f 53
4035b46e 54tr 'Q' '\015' << EOF > x
2344d47f
JS
55whitespace at beginning
56whitespace change
57whitespace in the middle
58whitespace at end
59unchanged line
4035b46e 60CR at endQ
2344d47f
JS
61EOF
62
5be60078 63git update-index x
2344d47f
JS
64
65cat << EOF > x
66 whitespace at beginning
67whitespace change
68white space in the middle
69whitespace at end
70unchanged line
71CR at end
72EOF
73
4035b46e 74tr 'Q' '\015' << EOF > expect
2344d47f
JS
75diff --git a/x b/x
76index d99af23..8b32fb5 100644
77--- a/x
78+++ b/x
79@@ -1,6 +1,6 @@
80-whitespace at beginning
81-whitespace change
82-whitespace in the middle
83-whitespace at end
84+ whitespace at beginning
85+whitespace change
86+white space in the middle
87+whitespace at end
88 unchanged line
4035b46e 89-CR at endQ
2344d47f
JS
90+CR at end
91EOF
5be60078 92git diff > out
5bd74506 93test_expect_success 'another test, without options' 'git diff expect out'
2344d47f
JS
94
95cat << EOF > expect
96diff --git a/x b/x
97index d99af23..8b32fb5 100644
98EOF
5be60078 99git diff -w > out
5bd74506 100test_expect_success 'another test, with -w' 'git diff expect out'
2344d47f 101
4035b46e 102tr 'Q' '\015' << EOF > expect
2344d47f
JS
103diff --git a/x b/x
104index d99af23..8b32fb5 100644
105--- a/x
106+++ b/x
107@@ -1,6 +1,6 @@
108-whitespace at beginning
109+ whitespace at beginning
110 whitespace change
111-whitespace in the middle
2344d47f 112+white space in the middle
c7c24889 113 whitespace at end
2344d47f 114 unchanged line
c7c24889 115 CR at endQ
2344d47f 116EOF
5be60078 117git diff -b > out
5bd74506 118test_expect_success 'another test, with -b' 'git diff expect out'
2344d47f 119
86f8c236
WC
120test_expect_success 'check mixed spaces and tabs in indent' '
121
122 # This is indented with SP HT SP.
123 echo " foo();" > x &&
c1795bb0 124 git diff --check | grep "Space in indent is followed by a tab"
86f8c236
WC
125
126'
127
62c64895
WC
128test_expect_success 'check with no whitespace errors' '
129
130 git commit -m "snapshot" &&
131 echo "foo();" > x &&
132 git diff --check
133
134'
135
f8175466 136test_expect_success 'check with trailing whitespace' '
62c64895
WC
137
138 echo "foo(); " > x &&
f8175466 139 ! git diff --check
62c64895
WC
140
141'
142
f8175466 143test_expect_success 'check with space before tab in indent' '
62c64895
WC
144
145 # indent has space followed by hard tab
146 echo " foo();" > x &&
f8175466 147 ! git diff --check
62c64895
WC
148
149'
150
da31b358 151test_expect_success '--check and --exit-code are not exclusive' '
62c64895
WC
152
153 git checkout x &&
154 git diff --check --exit-code
155
156'
157
da31b358 158test_expect_success '--check and --quiet are not exclusive' '
62c64895
WC
159
160 git diff --check --quiet
161
162'
163
164test_expect_success 'check staged with no whitespace errors' '
165
166 echo "foo();" > x &&
167 git add x &&
168 git diff --cached --check
169
170'
171
f8175466 172test_expect_success 'check staged with trailing whitespace' '
62c64895
WC
173
174 echo "foo(); " > x &&
175 git add x &&
f8175466 176 ! git diff --cached --check
62c64895
WC
177
178'
179
f8175466 180test_expect_success 'check staged with space before tab in indent' '
62c64895
WC
181
182 # indent has space followed by hard tab
183 echo " foo();" > x &&
184 git add x &&
f8175466 185 ! git diff --cached --check
62c64895
WC
186
187'
188
189test_expect_success 'check with no whitespace errors (diff-index)' '
190
191 echo "foo();" > x &&
192 git add x &&
193 git diff-index --check HEAD
194
195'
196
f8175466 197test_expect_success 'check with trailing whitespace (diff-index)' '
62c64895
WC
198
199 echo "foo(); " > x &&
200 git add x &&
f8175466 201 ! git diff-index --check HEAD
62c64895
WC
202
203'
204
f8175466 205test_expect_success 'check with space before tab in indent (diff-index)' '
62c64895
WC
206
207 # indent has space followed by hard tab
208 echo " foo();" > x &&
209 git add x &&
f8175466 210 ! git diff-index --check HEAD
62c64895
WC
211
212'
213
214test_expect_success 'check staged with no whitespace errors (diff-index)' '
215
216 echo "foo();" > x &&
217 git add x &&
218 git diff-index --cached --check HEAD
219
220'
221
f8175466 222test_expect_success 'check staged with trailing whitespace (diff-index)' '
62c64895
WC
223
224 echo "foo(); " > x &&
225 git add x &&
f8175466 226 ! git diff-index --cached --check HEAD
62c64895
WC
227
228'
229
f8175466 230test_expect_success 'check staged with space before tab in indent (diff-index)' '
62c64895
WC
231
232 # indent has space followed by hard tab
233 echo " foo();" > x &&
234 git add x &&
f8175466 235 ! git diff-index --cached --check HEAD
62c64895
WC
236
237'
238
239test_expect_success 'check with no whitespace errors (diff-tree)' '
240
241 echo "foo();" > x &&
242 git commit -m "new commit" x &&
243 git diff-tree --check HEAD^ HEAD
244
245'
246
f8175466 247test_expect_success 'check with trailing whitespace (diff-tree)' '
62c64895
WC
248
249 echo "foo(); " > x &&
250 git commit -m "another commit" x &&
f8175466 251 ! git diff-tree --check HEAD^ HEAD
62c64895
WC
252
253'
254
f8175466 255test_expect_success 'check with space before tab in indent (diff-tree)' '
62c64895
WC
256
257 # indent has space followed by hard tab
258 echo " foo();" > x &&
259 git commit -m "yet another" x &&
f8175466
JH
260 ! git diff-tree --check HEAD^ HEAD
261
262'
263
264test_expect_success 'check trailing whitespace (trailing-space: off)' '
265
266 git config core.whitespace "-trailing-space" &&
267 echo "foo (); " > x &&
268 git diff --check
269
270'
271
272test_expect_success 'check trailing whitespace (trailing-space: on)' '
273
274 git config core.whitespace "trailing-space" &&
275 echo "foo (); " > x &&
276 ! git diff --check
277
278'
279
280test_expect_success 'check space before tab in indent (space-before-tab: off)' '
281
282 # indent contains space followed by HT
283 git config core.whitespace "-space-before-tab" &&
284 echo " foo ();" > x &&
285 git diff --check
286
287'
288
289test_expect_success 'check space before tab in indent (space-before-tab: on)' '
290
291 # indent contains space followed by HT
292 git config core.whitespace "space-before-tab" &&
293 echo " foo (); " > x &&
294 ! git diff --check
295
296'
297
298test_expect_success 'check spaces as indentation (indent-with-non-tab: off)' '
299
300 git config core.whitespace "-indent-with-non-tab"
301 echo " foo ();" > x &&
302 git diff --check
303
304'
305
306test_expect_success 'check spaces as indentation (indent-with-non-tab: on)' '
307
308 git config core.whitespace "indent-with-non-tab" &&
309 echo " foo ();" > x &&
310 ! git diff --check
62c64895
WC
311
312'
313
2344d47f 314test_done