3 # Copyright (c) 2007 Carlos Rica
6 test_description='git stripspace'
10 t40='A quick brown fox jumps over the lazy do'
12 sss="$s40$s40$s40$s40$s40$s40$s40$s40$s40$s40" # 400
13 ttt="$t40$t40$t40$t40$t40$t40$t40$t40$t40$t40" # 400
16 'long lines without spaces should be unchanged' '
17 echo "$ttt" >expect &&
18 git stripspace <expect >actual &&
19 test_cmp expect actual &&
21 echo "$ttt$ttt" >expect &&
22 git stripspace <expect >actual &&
23 test_cmp expect actual &&
25 echo "$ttt$ttt$ttt" >expect &&
26 git stripspace <expect >actual &&
27 test_cmp expect actual &&
29 echo "$ttt$ttt$ttt$ttt" >expect &&
30 git stripspace <expect >actual &&
31 test_cmp expect actual
35 'lines with spaces at the beginning should be unchanged' '
36 echo "$sss$ttt" >expect &&
37 git stripspace <expect >actual &&
38 test_cmp expect actual &&
40 echo "$sss$sss$ttt" >expect &&
41 git stripspace <expect >actual &&
42 test_cmp expect actual &&
44 echo "$sss$sss$sss$ttt" >expect &&
45 git stripspace <expect >actual &&
46 test_cmp expect actual
50 'lines with intermediate spaces should be unchanged' '
51 echo "$ttt$sss$ttt" >expect &&
52 git stripspace <expect >actual &&
53 test_cmp expect actual &&
55 echo "$ttt$sss$sss$ttt" >expect &&
56 git stripspace <expect >actual &&
57 test_cmp expect actual
61 'consecutive blank lines should be unified' '
62 printf "$ttt\n\n$ttt\n" > expect &&
63 printf "$ttt\n\n\n\n\n$ttt\n" | git stripspace >actual &&
64 test_cmp expect actual &&
66 printf "$ttt$ttt\n\n$ttt\n" > expect &&
67 printf "$ttt$ttt\n\n\n\n\n$ttt\n" | git stripspace >actual &&
68 test_cmp expect actual &&
70 printf "$ttt$ttt$ttt\n\n$ttt\n" > expect &&
71 printf "$ttt$ttt$ttt\n\n\n\n\n$ttt\n" | git stripspace >actual &&
72 test_cmp expect actual &&
74 printf "$ttt\n\n$ttt\n" > expect &&
75 printf "$ttt\n\n\n\n\n$ttt\n" | git stripspace >actual &&
76 test_cmp expect actual &&
78 printf "$ttt\n\n$ttt$ttt\n" > expect &&
79 printf "$ttt\n\n\n\n\n$ttt$ttt\n" | git stripspace >actual &&
80 test_cmp expect actual &&
82 printf "$ttt\n\n$ttt$ttt$ttt\n" > expect &&
83 printf "$ttt\n\n\n\n\n$ttt$ttt$ttt\n" | git stripspace >actual &&
84 test_cmp expect actual &&
86 printf "$ttt\n\n$ttt\n" > expect &&
87 printf "$ttt\n\t\n \n\n \t\t\n$ttt\n" | git stripspace >actual &&
88 test_cmp expect actual &&
90 printf "$ttt$ttt\n\n$ttt\n" > expect &&
91 printf "$ttt$ttt\n\t\n \n\n \t\t\n$ttt\n" | git stripspace >actual &&
92 test_cmp expect actual &&
94 printf "$ttt$ttt$ttt\n\n$ttt\n" > expect &&
95 printf "$ttt$ttt$ttt\n\t\n \n\n \t\t\n$ttt\n" | git stripspace >actual &&
96 test_cmp expect actual &&
98 printf "$ttt\n\n$ttt\n" > expect &&
99 printf "$ttt\n\t\n \n\n \t\t\n$ttt\n" | git stripspace >actual &&
100 test_cmp expect actual &&
102 printf "$ttt\n\n$ttt$ttt\n" > expect &&
103 printf "$ttt\n\t\n \n\n \t\t\n$ttt$ttt\n" | git stripspace >actual &&
104 test_cmp expect actual &&
106 printf "$ttt\n\n$ttt$ttt$ttt\n" > expect &&
107 printf "$ttt\n\t\n \n\n \t\t\n$ttt$ttt$ttt\n" | git stripspace >actual &&
108 test_cmp expect actual
111 test_expect_success \
112 'only consecutive blank lines should be completely removed' '
114 printf "\n" | git stripspace >actual &&
115 test_must_be_empty actual &&
117 printf "\n\n\n" | git stripspace >actual &&
118 test_must_be_empty actual &&
120 printf "$sss\n$sss\n$sss\n" | git stripspace >actual &&
121 test_must_be_empty actual &&
123 printf "$sss$sss\n$sss\n\n" | git stripspace >actual &&
124 test_must_be_empty actual &&
126 printf "\n$sss\n$sss$sss\n" | git stripspace >actual &&
127 test_must_be_empty actual &&
129 printf "$sss$sss$sss$sss\n\n\n" | git stripspace >actual &&
130 test_must_be_empty actual &&
132 printf "\n$sss$sss$sss$sss\n\n" | git stripspace >actual &&
133 test_must_be_empty actual &&
135 printf "\n\n$sss$sss$sss$sss\n" | git stripspace >actual &&
136 test_must_be_empty actual
139 test_expect_success \
140 'consecutive blank lines at the beginning should be removed' '
141 printf "$ttt\n" > expect &&
142 printf "\n$ttt\n" | git stripspace >actual &&
143 test_cmp expect actual &&
145 printf "$ttt\n" > expect &&
146 printf "\n\n\n$ttt\n" | git stripspace >actual &&
147 test_cmp expect actual &&
149 printf "$ttt$ttt\n" > expect &&
150 printf "\n\n\n$ttt$ttt\n" | git stripspace >actual &&
151 test_cmp expect actual &&
153 printf "$ttt$ttt$ttt\n" > expect &&
154 printf "\n\n\n$ttt$ttt$ttt\n" | git stripspace >actual &&
155 test_cmp expect actual &&
157 printf "$ttt$ttt$ttt$ttt\n" > expect &&
158 printf "\n\n\n$ttt$ttt$ttt$ttt\n" | git stripspace >actual &&
159 test_cmp expect actual &&
161 printf "$ttt\n" > expect &&
163 printf "$sss\n$sss\n$sss\n$ttt\n" | git stripspace >actual &&
164 test_cmp expect actual &&
166 printf "\n$sss\n$sss$sss\n$ttt\n" | git stripspace >actual &&
167 test_cmp expect actual &&
169 printf "$sss$sss\n$sss\n\n$ttt\n" | git stripspace >actual &&
170 test_cmp expect actual &&
172 printf "$sss$sss$sss\n\n\n$ttt\n" | git stripspace >actual &&
173 test_cmp expect actual &&
175 printf "\n$sss$sss$sss\n\n$ttt\n" | git stripspace >actual &&
176 test_cmp expect actual &&
178 printf "\n\n$sss$sss$sss\n$ttt\n" | git stripspace >actual &&
179 test_cmp expect actual
182 test_expect_success \
183 'consecutive blank lines at the end should be removed' '
184 printf "$ttt\n" > expect &&
185 printf "$ttt\n\n" | git stripspace >actual &&
186 test_cmp expect actual &&
188 printf "$ttt\n" > expect &&
189 printf "$ttt\n\n\n\n" | git stripspace >actual &&
190 test_cmp expect actual &&
192 printf "$ttt$ttt\n" > expect &&
193 printf "$ttt$ttt\n\n\n\n" | git stripspace >actual &&
194 test_cmp expect actual &&
196 printf "$ttt$ttt$ttt\n" > expect &&
197 printf "$ttt$ttt$ttt\n\n\n\n" | git stripspace >actual &&
198 test_cmp expect actual &&
200 printf "$ttt$ttt$ttt$ttt\n" > expect &&
201 printf "$ttt$ttt$ttt$ttt\n\n\n\n" | git stripspace >actual &&
202 test_cmp expect actual &&
204 printf "$ttt\n" > expect &&
206 printf "$ttt\n$sss\n$sss\n$sss\n" | git stripspace >actual &&
207 test_cmp expect actual &&
209 printf "$ttt\n\n$sss\n$sss$sss\n" | git stripspace >actual &&
210 test_cmp expect actual &&
212 printf "$ttt\n$sss$sss\n$sss\n\n" | git stripspace >actual &&
213 test_cmp expect actual &&
215 printf "$ttt\n$sss$sss$sss\n\n\n" | git stripspace >actual &&
216 test_cmp expect actual &&
218 printf "$ttt\n\n$sss$sss$sss\n\n" | git stripspace >actual &&
219 test_cmp expect actual &&
221 printf "$ttt\n\n\n$sss$sss$sss\n" | git stripspace >actual &&
222 test_cmp expect actual
225 test_expect_success \
226 'text without newline at end should end with newline' '
227 test $(printf "$ttt" | git stripspace | wc -l) -gt 0 &&
228 test $(printf "$ttt$ttt" | git stripspace | wc -l) -gt 0 &&
229 test $(printf "$ttt$ttt$ttt" | git stripspace | wc -l) -gt 0 &&
230 test $(printf "$ttt$ttt$ttt$ttt" | git stripspace | wc -l) -gt 0
233 # text plus spaces at the end:
235 test_expect_success \
236 'text plus spaces without newline at end should end with newline' '
237 test $(printf "$ttt$sss" | git stripspace | wc -l) -gt 0 &&
238 test $(printf "$ttt$ttt$sss" | git stripspace | wc -l) -gt 0 &&
239 test $(printf "$ttt$ttt$ttt$sss" | git stripspace | wc -l) -gt 0 &&
240 test $(printf "$ttt$sss$sss" | git stripspace | wc -l) -gt 0 &&
241 test $(printf "$ttt$ttt$sss$sss" | git stripspace | wc -l) -gt 0 &&
242 test $(printf "$ttt$sss$sss$sss" | git stripspace | wc -l) -gt 0
245 test_expect_success \
246 'text plus spaces without newline at end should not show spaces' '
247 ! (printf "$ttt$sss" | git stripspace | grep " " >/dev/null) &&
248 ! (printf "$ttt$ttt$sss" | git stripspace | grep " " >/dev/null) &&
249 ! (printf "$ttt$ttt$ttt$sss" | git stripspace | grep " " >/dev/null) &&
250 ! (printf "$ttt$sss$sss" | git stripspace | grep " " >/dev/null) &&
251 ! (printf "$ttt$ttt$sss$sss" | git stripspace | grep " " >/dev/null) &&
252 ! (printf "$ttt$sss$sss$sss" | git stripspace | grep " " >/dev/null)
255 test_expect_success \
256 'text plus spaces without newline should show the correct lines' '
257 printf "$ttt\n" >expect &&
258 printf "$ttt$sss" | git stripspace >actual &&
259 test_cmp expect actual &&
261 printf "$ttt\n" >expect &&
262 printf "$ttt$sss$sss" | git stripspace >actual &&
263 test_cmp expect actual &&
265 printf "$ttt\n" >expect &&
266 printf "$ttt$sss$sss$sss" | git stripspace >actual &&
267 test_cmp expect actual &&
269 printf "$ttt$ttt\n" >expect &&
270 printf "$ttt$ttt$sss" | git stripspace >actual &&
271 test_cmp expect actual &&
273 printf "$ttt$ttt\n" >expect &&
274 printf "$ttt$ttt$sss$sss" | git stripspace >actual &&
275 test_cmp expect actual &&
277 printf "$ttt$ttt$ttt\n" >expect &&
278 printf "$ttt$ttt$ttt$sss" | git stripspace >actual &&
279 test_cmp expect actual
282 test_expect_success \
283 'text plus spaces at end should not show spaces' '
284 ! (echo "$ttt$sss" | git stripspace | grep " " >/dev/null) &&
285 ! (echo "$ttt$ttt$sss" | git stripspace | grep " " >/dev/null) &&
286 ! (echo "$ttt$ttt$ttt$sss" | git stripspace | grep " " >/dev/null) &&
287 ! (echo "$ttt$sss$sss" | git stripspace | grep " " >/dev/null) &&
288 ! (echo "$ttt$ttt$sss$sss" | git stripspace | grep " " >/dev/null) &&
289 ! (echo "$ttt$sss$sss$sss" | git stripspace | grep " " >/dev/null)
292 test_expect_success \
293 'text plus spaces at end should be cleaned and newline must remain' '
294 echo "$ttt" >expect &&
295 echo "$ttt$sss" | git stripspace >actual &&
296 test_cmp expect actual &&
298 echo "$ttt" >expect &&
299 echo "$ttt$sss$sss" | git stripspace >actual &&
300 test_cmp expect actual &&
302 echo "$ttt" >expect &&
303 echo "$ttt$sss$sss$sss" | git stripspace >actual &&
304 test_cmp expect actual &&
306 echo "$ttt$ttt" >expect &&
307 echo "$ttt$ttt$sss" | git stripspace >actual &&
308 test_cmp expect actual &&
310 echo "$ttt$ttt" >expect &&
311 echo "$ttt$ttt$sss$sss" | git stripspace >actual &&
312 test_cmp expect actual &&
314 echo "$ttt$ttt$ttt" >expect &&
315 echo "$ttt$ttt$ttt$sss" | git stripspace >actual &&
316 test_cmp expect actual
321 test_expect_success \
322 'spaces with newline at end should be replaced with empty string' '
325 echo | git stripspace >actual &&
326 test_cmp expect actual &&
328 echo "$sss" | git stripspace >actual &&
329 test_cmp expect actual &&
331 echo "$sss$sss" | git stripspace >actual &&
332 test_cmp expect actual &&
334 echo "$sss$sss$sss" | git stripspace >actual &&
335 test_cmp expect actual &&
337 echo "$sss$sss$sss$sss" | git stripspace >actual &&
338 test_cmp expect actual
341 test_expect_success \
342 'spaces without newline at end should not show spaces' '
343 ! (printf "" | git stripspace | grep " " >/dev/null) &&
344 ! (printf "$sss" | git stripspace | grep " " >/dev/null) &&
345 ! (printf "$sss$sss" | git stripspace | grep " " >/dev/null) &&
346 ! (printf "$sss$sss$sss" | git stripspace | grep " " >/dev/null) &&
347 ! (printf "$sss$sss$sss$sss" | git stripspace | grep " " >/dev/null)
350 test_expect_success \
351 'spaces without newline at end should be replaced with empty string' '
354 printf "" | git stripspace >actual &&
355 test_cmp expect actual &&
357 printf "$sss$sss" | git stripspace >actual &&
358 test_cmp expect actual &&
360 printf "$sss$sss$sss" | git stripspace >actual &&
361 test_cmp expect actual &&
363 printf "$sss$sss$sss$sss" | git stripspace >actual &&
364 test_cmp expect actual
367 test_expect_success \
368 'consecutive text lines should be unchanged' '
369 printf "$ttt$ttt\n$ttt\n" >expect &&
370 printf "$ttt$ttt\n$ttt\n" | git stripspace >actual &&
371 test_cmp expect actual &&
373 printf "$ttt\n$ttt$ttt\n$ttt\n" >expect &&
374 printf "$ttt\n$ttt$ttt\n$ttt\n" | git stripspace >actual &&
375 test_cmp expect actual &&
377 printf "$ttt\n$ttt\n$ttt\n$ttt$ttt\n" >expect &&
378 printf "$ttt\n$ttt\n$ttt\n$ttt$ttt\n" | git stripspace >actual &&
379 test_cmp expect actual &&
381 printf "$ttt\n$ttt\n\n$ttt$ttt\n$ttt\n" >expect &&
382 printf "$ttt\n$ttt\n\n$ttt$ttt\n$ttt\n" | git stripspace >actual &&
383 test_cmp expect actual &&
385 printf "$ttt$ttt\n\n$ttt\n$ttt$ttt\n" >expect &&
386 printf "$ttt$ttt\n\n$ttt\n$ttt$ttt\n" | git stripspace >actual &&
387 test_cmp expect actual &&
389 printf "$ttt\n$ttt$ttt\n\n$ttt\n" >expect &&
390 printf "$ttt\n$ttt$ttt\n\n$ttt\n" | git stripspace >actual &&
391 test_cmp expect actual
394 test_expect_success 'strip comments, too' '
395 test ! -z "$(echo "# comment" | git stripspace)" &&
396 test -z "$(echo "# comment" | git stripspace -s)"
399 test_expect_success 'strip comments with changed comment char' '
400 test ! -z "$(echo "; comment" | git -c core.commentchar=";" stripspace)" &&
401 test -z "$(echo "; comment" | git -c core.commentchar=";" stripspace -s)"
404 test_expect_success '-c with single line' '
405 printf "# foo\n" >expect &&
406 printf "foo" | git stripspace -c >actual &&
407 test_cmp expect actual
410 test_expect_success '-c with single line followed by empty line' '
411 printf "# foo\n#\n" >expect &&
412 printf "foo\n\n" | git stripspace -c >actual &&
413 test_cmp expect actual
416 test_expect_success '-c with newline only' '
417 printf "#\n" >expect &&
418 printf "\n" | git stripspace -c >actual &&
419 test_cmp expect actual
422 test_expect_success '--comment-lines with single line' '
423 printf "# foo\n" >expect &&
424 printf "foo" | git stripspace -c >actual &&
425 test_cmp expect actual
428 test_expect_success '-c with changed comment char' '
429 printf "; foo\n" >expect &&
430 printf "foo" | git -c core.commentchar=";" stripspace -c >actual &&
431 test_cmp expect actual
434 test_expect_success '-c with comment char defined in .git/config' '
435 test_config core.commentchar = &&
436 printf "= foo\n" >expect &&
438 mkdir sub && cd sub && git stripspace -c
440 test_cmp expect actual
443 test_expect_success 'avoid SP-HT sequence in commented line' '
444 printf "#\tone\n#\n# two\n" >expect &&
445 printf "\tone\n\ntwo\n" | git stripspace -c >actual &&
446 test_cmp expect actual