The second batch
[git.git] / t / t0030-stripspace.sh
blobf10f42ff1e4a8716609a1e8dd86e1441e020db3d
1 #!/bin/sh
3 # Copyright (c) 2007 Carlos Rica
6 test_description='git stripspace'
8 TEST_PASSES_SANITIZE_LEAK=true
9 . ./test-lib.sh
11 t40='A quick brown fox jumps over the lazy do'
12 s40=' '
13 sss="$s40$s40$s40$s40$s40$s40$s40$s40$s40$s40" # 400
14 ttt="$t40$t40$t40$t40$t40$t40$t40$t40$t40$t40" # 400
16 printf_git_stripspace () {
17 printf "$1" | git stripspace
20 test_expect_success 'long lines without spaces should be unchanged' '
21 echo "$ttt" >expect &&
22 git stripspace <expect >actual &&
23 test_cmp expect actual &&
25 echo "$ttt$ttt" >expect &&
26 git stripspace <expect >actual &&
27 test_cmp expect actual &&
29 echo "$ttt$ttt$ttt" >expect &&
30 git stripspace <expect >actual &&
31 test_cmp expect actual &&
33 echo "$ttt$ttt$ttt$ttt" >expect &&
34 git stripspace <expect >actual &&
35 test_cmp expect actual
38 test_expect_success 'lines with spaces at the beginning should be unchanged' '
39 echo "$sss$ttt" >expect &&
40 git stripspace <expect >actual &&
41 test_cmp expect actual &&
43 echo "$sss$sss$ttt" >expect &&
44 git stripspace <expect >actual &&
45 test_cmp expect actual &&
47 echo "$sss$sss$sss$ttt" >expect &&
48 git stripspace <expect >actual &&
49 test_cmp expect actual
52 test_expect_success 'lines with intermediate spaces should be unchanged' '
53 echo "$ttt$sss$ttt" >expect &&
54 git stripspace <expect >actual &&
55 test_cmp expect actual &&
57 echo "$ttt$sss$sss$ttt" >expect &&
58 git stripspace <expect >actual &&
59 test_cmp expect actual
62 test_expect_success 'consecutive blank lines should be unified' '
63 printf "$ttt\n\n$ttt\n" > expect &&
64 printf "$ttt\n\n\n\n\n$ttt\n" | git stripspace >actual &&
65 test_cmp expect actual &&
67 printf "$ttt$ttt\n\n$ttt\n" > expect &&
68 printf "$ttt$ttt\n\n\n\n\n$ttt\n" | git stripspace >actual &&
69 test_cmp expect actual &&
71 printf "$ttt$ttt$ttt\n\n$ttt\n" > expect &&
72 printf "$ttt$ttt$ttt\n\n\n\n\n$ttt\n" | git stripspace >actual &&
73 test_cmp expect actual &&
75 printf "$ttt\n\n$ttt\n" > expect &&
76 printf "$ttt\n\n\n\n\n$ttt\n" | git stripspace >actual &&
77 test_cmp expect actual &&
79 printf "$ttt\n\n$ttt$ttt\n" > expect &&
80 printf "$ttt\n\n\n\n\n$ttt$ttt\n" | git stripspace >actual &&
81 test_cmp expect actual &&
83 printf "$ttt\n\n$ttt$ttt$ttt\n" > expect &&
84 printf "$ttt\n\n\n\n\n$ttt$ttt$ttt\n" | git stripspace >actual &&
85 test_cmp expect actual &&
87 printf "$ttt\n\n$ttt\n" > expect &&
88 printf "$ttt\n\t\n \n\n \t\t\n$ttt\n" | git stripspace >actual &&
89 test_cmp expect actual &&
91 printf "$ttt$ttt\n\n$ttt\n" > expect &&
92 printf "$ttt$ttt\n\t\n \n\n \t\t\n$ttt\n" | git stripspace >actual &&
93 test_cmp expect actual &&
95 printf "$ttt$ttt$ttt\n\n$ttt\n" > expect &&
96 printf "$ttt$ttt$ttt\n\t\n \n\n \t\t\n$ttt\n" | git stripspace >actual &&
97 test_cmp expect actual &&
99 printf "$ttt\n\n$ttt\n" > expect &&
100 printf "$ttt\n\t\n \n\n \t\t\n$ttt\n" | git stripspace >actual &&
101 test_cmp expect actual &&
103 printf "$ttt\n\n$ttt$ttt\n" > expect &&
104 printf "$ttt\n\t\n \n\n \t\t\n$ttt$ttt\n" | git stripspace >actual &&
105 test_cmp expect actual &&
107 printf "$ttt\n\n$ttt$ttt$ttt\n" > expect &&
108 printf "$ttt\n\t\n \n\n \t\t\n$ttt$ttt$ttt\n" | git stripspace >actual &&
109 test_cmp expect actual
112 test_expect_success 'only consecutive blank lines should be completely removed' '
113 printf "\n" | git stripspace >actual &&
114 test_must_be_empty actual &&
116 printf "\n\n\n" | git stripspace >actual &&
117 test_must_be_empty actual &&
119 printf "$sss\n$sss\n$sss\n" | git stripspace >actual &&
120 test_must_be_empty actual &&
122 printf "$sss$sss\n$sss\n\n" | git stripspace >actual &&
123 test_must_be_empty actual &&
125 printf "\n$sss\n$sss$sss\n" | git stripspace >actual &&
126 test_must_be_empty actual &&
128 printf "$sss$sss$sss$sss\n\n\n" | git stripspace >actual &&
129 test_must_be_empty actual &&
131 printf "\n$sss$sss$sss$sss\n\n" | git stripspace >actual &&
132 test_must_be_empty actual &&
134 printf "\n\n$sss$sss$sss$sss\n" | git stripspace >actual &&
135 test_must_be_empty actual
138 test_expect_success 'consecutive blank lines at the beginning should be removed' '
139 printf "$ttt\n" > expect &&
140 printf "\n$ttt\n" | git stripspace >actual &&
141 test_cmp expect actual &&
143 printf "$ttt\n" > expect &&
144 printf "\n\n\n$ttt\n" | git stripspace >actual &&
145 test_cmp expect actual &&
147 printf "$ttt$ttt\n" > expect &&
148 printf "\n\n\n$ttt$ttt\n" | git stripspace >actual &&
149 test_cmp expect actual &&
151 printf "$ttt$ttt$ttt\n" > expect &&
152 printf "\n\n\n$ttt$ttt$ttt\n" | git stripspace >actual &&
153 test_cmp expect actual &&
155 printf "$ttt$ttt$ttt$ttt\n" > expect &&
156 printf "\n\n\n$ttt$ttt$ttt$ttt\n" | git stripspace >actual &&
157 test_cmp expect actual &&
159 printf "$ttt\n" > expect &&
161 printf "$sss\n$sss\n$sss\n$ttt\n" | git stripspace >actual &&
162 test_cmp expect actual &&
164 printf "\n$sss\n$sss$sss\n$ttt\n" | git stripspace >actual &&
165 test_cmp expect actual &&
167 printf "$sss$sss\n$sss\n\n$ttt\n" | git stripspace >actual &&
168 test_cmp expect actual &&
170 printf "$sss$sss$sss\n\n\n$ttt\n" | git stripspace >actual &&
171 test_cmp expect actual &&
173 printf "\n$sss$sss$sss\n\n$ttt\n" | git stripspace >actual &&
174 test_cmp expect actual &&
176 printf "\n\n$sss$sss$sss\n$ttt\n" | git stripspace >actual &&
177 test_cmp expect actual
180 test_expect_success 'consecutive blank lines at the end should be removed' '
181 printf "$ttt\n" > expect &&
182 printf "$ttt\n\n" | git stripspace >actual &&
183 test_cmp expect actual &&
185 printf "$ttt\n" > expect &&
186 printf "$ttt\n\n\n\n" | git stripspace >actual &&
187 test_cmp expect actual &&
189 printf "$ttt$ttt\n" > expect &&
190 printf "$ttt$ttt\n\n\n\n" | git stripspace >actual &&
191 test_cmp expect actual &&
193 printf "$ttt$ttt$ttt\n" > expect &&
194 printf "$ttt$ttt$ttt\n\n\n\n" | git stripspace >actual &&
195 test_cmp expect actual &&
197 printf "$ttt$ttt$ttt$ttt\n" > expect &&
198 printf "$ttt$ttt$ttt$ttt\n\n\n\n" | git stripspace >actual &&
199 test_cmp expect actual &&
201 printf "$ttt\n" > expect &&
203 printf "$ttt\n$sss\n$sss\n$sss\n" | git stripspace >actual &&
204 test_cmp expect actual &&
206 printf "$ttt\n\n$sss\n$sss$sss\n" | git stripspace >actual &&
207 test_cmp expect actual &&
209 printf "$ttt\n$sss$sss\n$sss\n\n" | git stripspace >actual &&
210 test_cmp expect actual &&
212 printf "$ttt\n$sss$sss$sss\n\n\n" | git stripspace >actual &&
213 test_cmp expect actual &&
215 printf "$ttt\n\n$sss$sss$sss\n\n" | git stripspace >actual &&
216 test_cmp expect actual &&
218 printf "$ttt\n\n\n$sss$sss$sss\n" | git stripspace >actual &&
219 test_cmp expect actual
222 test_expect_success 'text without newline at end should end with newline' '
223 test_stdout_line_count -gt 0 printf_git_stripspace "$ttt" &&
224 test_stdout_line_count -gt 0 printf_git_stripspace "$ttt$ttt" &&
225 test_stdout_line_count -gt 0 printf_git_stripspace "$ttt$ttt$ttt" &&
226 test_stdout_line_count -gt 0 printf_git_stripspace "$ttt$ttt$ttt$ttt"
229 # text plus spaces at the end:
231 test_expect_success 'text plus spaces without newline at end should end with newline' '
232 test_stdout_line_count -gt 0 printf_git_stripspace "$ttt$sss" &&
233 test_stdout_line_count -gt 0 printf_git_stripspace "$ttt$ttt$sss" &&
234 test_stdout_line_count -gt 0 printf_git_stripspace "$ttt$ttt$ttt$sss" &&
235 test_stdout_line_count -gt 0 printf_git_stripspace "$ttt$sss$sss" &&
236 test_stdout_line_count -gt 0 printf_git_stripspace "$ttt$ttt$sss$sss" &&
237 test_stdout_line_count -gt 0 printf_git_stripspace "$ttt$sss$sss$sss"
240 test_expect_success 'text plus spaces without newline at end should not show spaces' '
241 printf "$ttt$sss" | git stripspace >tmp &&
242 ! grep " " tmp >/dev/null &&
243 printf "$ttt$ttt$sss" | git stripspace >tmp &&
244 ! grep " " tmp >/dev/null &&
245 printf "$ttt$ttt$ttt$sss" | git stripspace >tmp &&
246 ! grep " " tmp >/dev/null &&
247 printf "$ttt$sss$sss" | git stripspace >tmp &&
248 ! grep " " tmp >/dev/null &&
249 printf "$ttt$ttt$sss$sss" | git stripspace >tmp &&
250 ! grep " " tmp >/dev/null &&
251 printf "$ttt$sss$sss$sss" | git stripspace >tmp &&
252 ! grep " " tmp >/dev/null
255 test_expect_success 'text plus spaces without newline should show the correct lines' '
256 printf "$ttt\n" >expect &&
257 printf "$ttt$sss" | git stripspace >actual &&
258 test_cmp expect actual &&
260 printf "$ttt\n" >expect &&
261 printf "$ttt$sss$sss" | git stripspace >actual &&
262 test_cmp expect actual &&
264 printf "$ttt\n" >expect &&
265 printf "$ttt$sss$sss$sss" | git stripspace >actual &&
266 test_cmp expect actual &&
268 printf "$ttt$ttt\n" >expect &&
269 printf "$ttt$ttt$sss" | git stripspace >actual &&
270 test_cmp expect actual &&
272 printf "$ttt$ttt\n" >expect &&
273 printf "$ttt$ttt$sss$sss" | git stripspace >actual &&
274 test_cmp expect actual &&
276 printf "$ttt$ttt$ttt\n" >expect &&
277 printf "$ttt$ttt$ttt$sss" | git stripspace >actual &&
278 test_cmp expect actual
281 test_expect_success 'text plus spaces at end should not show spaces' '
282 echo "$ttt$sss" | git stripspace >tmp &&
283 ! grep " " tmp >/dev/null &&
284 echo "$ttt$ttt$sss" | git stripspace >tmp &&
285 ! grep " " tmp >/dev/null &&
286 echo "$ttt$ttt$ttt$sss" | git stripspace >tmp &&
287 ! grep " " tmp >/dev/null &&
288 echo "$ttt$sss$sss" | git stripspace >tmp &&
289 ! grep " " tmp >/dev/null &&
290 echo "$ttt$ttt$sss$sss" | git stripspace >tmp &&
291 ! grep " " tmp >/dev/null &&
292 echo "$ttt$sss$sss$sss" | git stripspace >tmp &&
293 ! grep " " tmp >/dev/null
296 test_expect_success 'text plus spaces at end should be cleaned and newline must remain' '
297 echo "$ttt" >expect &&
298 echo "$ttt$sss" | git stripspace >actual &&
299 test_cmp expect actual &&
301 echo "$ttt" >expect &&
302 echo "$ttt$sss$sss" | git stripspace >actual &&
303 test_cmp expect actual &&
305 echo "$ttt" >expect &&
306 echo "$ttt$sss$sss$sss" | git stripspace >actual &&
307 test_cmp expect actual &&
309 echo "$ttt$ttt" >expect &&
310 echo "$ttt$ttt$sss" | git stripspace >actual &&
311 test_cmp expect actual &&
313 echo "$ttt$ttt" >expect &&
314 echo "$ttt$ttt$sss$sss" | git stripspace >actual &&
315 test_cmp expect actual &&
317 echo "$ttt$ttt$ttt" >expect &&
318 echo "$ttt$ttt$ttt$sss" | git stripspace >actual &&
319 test_cmp expect actual
322 # spaces only:
324 test_expect_success 'spaces with newline at end should be replaced with empty string' '
325 echo | git stripspace >actual &&
326 test_must_be_empty actual &&
328 echo "$sss" | git stripspace >actual &&
329 test_must_be_empty actual &&
331 echo "$sss$sss" | git stripspace >actual &&
332 test_must_be_empty actual &&
334 echo "$sss$sss$sss" | git stripspace >actual &&
335 test_must_be_empty actual &&
337 echo "$sss$sss$sss$sss" | git stripspace >actual &&
338 test_must_be_empty actual
341 test_expect_success 'spaces without newline at end should not show spaces' '
342 printf "" | git stripspace >tmp &&
343 ! grep " " tmp >/dev/null &&
344 printf "$sss" | git stripspace >tmp &&
345 ! grep " " tmp >/dev/null &&
346 printf "$sss$sss" | git stripspace >tmp &&
347 ! grep " " tmp >/dev/null &&
348 printf "$sss$sss$sss" | git stripspace >tmp &&
349 ! grep " " tmp >/dev/null &&
350 printf "$sss$sss$sss$sss" | git stripspace >tmp &&
351 ! grep " " tmp >/dev/null
354 test_expect_success 'spaces without newline at end should be replaced with empty string' '
355 printf "" | git stripspace >actual &&
356 test_must_be_empty actual &&
358 printf "$sss$sss" | git stripspace >actual &&
359 test_must_be_empty actual &&
361 printf "$sss$sss$sss" | git stripspace >actual &&
362 test_must_be_empty actual &&
364 printf "$sss$sss$sss$sss" | git stripspace >actual &&
365 test_must_be_empty actual
368 test_expect_success '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 'strip comments with changed comment string' '
405 test ! -z "$(echo "// comment" | git -c core.commentchar=// stripspace)" &&
406 test -z "$(echo "// comment" | git -c core.commentchar="//" stripspace -s)"
409 test_expect_success 'newline as commentchar is forbidden' '
410 test_must_fail git -c core.commentChar="$LF" stripspace -s 2>err &&
411 grep "core.commentchar cannot contain newline" err
414 test_expect_success 'empty commentchar is forbidden' '
415 test_must_fail git -c core.commentchar= stripspace -s 2>err &&
416 grep "core.commentchar must have at least one character" err
419 test_expect_success '-c with single line' '
420 printf "# foo\n" >expect &&
421 printf "foo" | git stripspace -c >actual &&
422 test_cmp expect actual
425 test_expect_success '-c with single line followed by empty line' '
426 printf "# foo\n#\n" >expect &&
427 printf "foo\n\n" | git stripspace -c >actual &&
428 test_cmp expect actual
431 test_expect_success '-c with newline only' '
432 printf "#\n" >expect &&
433 printf "\n" | git stripspace -c >actual &&
434 test_cmp expect actual
437 test_expect_success '--comment-lines with single line' '
438 printf "# foo\n" >expect &&
439 printf "foo" | git stripspace -c >actual &&
440 test_cmp expect actual
443 test_expect_success '-c with changed comment char' '
444 printf "; foo\n" >expect &&
445 printf "foo" | git -c core.commentchar=";" stripspace -c >actual &&
446 test_cmp expect actual
449 test_expect_success '-c with comment char defined in .git/config' '
450 test_config core.commentchar = &&
451 printf "= foo\n" >expect &&
452 rm -fr sub &&
453 mkdir sub &&
454 printf "foo" | git -C sub stripspace -c >actual &&
455 test_cmp expect actual
458 test_expect_success '-c outside git repository' '
459 printf "# foo\n" >expect &&
460 printf "foo" | nongit git stripspace -c >actual &&
461 test_cmp expect actual
464 test_expect_success 'avoid SP-HT sequence in commented line' '
465 printf "#\tone\n#\n# two\n" >expect &&
466 printf "\tone\n\ntwo\n" | git stripspace -c >actual &&
467 test_cmp expect actual
470 test_done