Allow custom "comment char"
[git/git.git] / t / t0030-stripspace.sh
CommitLineData
30d038e2
CR
1#!/bin/sh
2#
3# Copyright (c) 2007 Carlos Rica
4#
5
5be60078 6test_description='git stripspace'
30d038e2
CR
7
8. ./test-lib.sh
9
10t40='A quick brown fox jumps over the lazy do'
11s40=' '
12sss="$s40$s40$s40$s40$s40$s40$s40$s40$s40$s40" # 400
13ttt="$t40$t40$t40$t40$t40$t40$t40$t40$t40$t40" # 400
14
15test_expect_success \
16 'long lines without spaces should be unchanged' '
17 echo "$ttt" >expect &&
5be60078 18 git stripspace <expect >actual &&
3af82863 19 test_cmp expect actual &&
30d038e2
CR
20
21 echo "$ttt$ttt" >expect &&
5be60078 22 git stripspace <expect >actual &&
3af82863 23 test_cmp expect actual &&
30d038e2
CR
24
25 echo "$ttt$ttt$ttt" >expect &&
5be60078 26 git stripspace <expect >actual &&
3af82863 27 test_cmp expect actual &&
30d038e2
CR
28
29 echo "$ttt$ttt$ttt$ttt" >expect &&
5be60078 30 git stripspace <expect >actual &&
3af82863 31 test_cmp expect actual
30d038e2
CR
32'
33
34test_expect_success \
35 'lines with spaces at the beginning should be unchanged' '
36 echo "$sss$ttt" >expect &&
5be60078 37 git stripspace <expect >actual &&
3af82863 38 test_cmp expect actual &&
30d038e2
CR
39
40 echo "$sss$sss$ttt" >expect &&
5be60078 41 git stripspace <expect >actual &&
3af82863 42 test_cmp expect actual &&
30d038e2
CR
43
44 echo "$sss$sss$sss$ttt" >expect &&
5be60078 45 git stripspace <expect >actual &&
3af82863 46 test_cmp expect actual
30d038e2
CR
47'
48
49test_expect_success \
50 'lines with intermediate spaces should be unchanged' '
51 echo "$ttt$sss$ttt" >expect &&
5be60078 52 git stripspace <expect >actual &&
3af82863 53 test_cmp expect actual &&
30d038e2
CR
54
55 echo "$ttt$sss$sss$ttt" >expect &&
5be60078 56 git stripspace <expect >actual &&
3af82863 57 test_cmp expect actual
30d038e2
CR
58'
59
60test_expect_success \
61 'consecutive blank lines should be unified' '
62 printf "$ttt\n\n$ttt\n" > expect &&
5be60078 63 printf "$ttt\n\n\n\n\n$ttt\n" | git stripspace >actual &&
3af82863 64 test_cmp expect actual &&
30d038e2
CR
65
66 printf "$ttt$ttt\n\n$ttt\n" > expect &&
5be60078 67 printf "$ttt$ttt\n\n\n\n\n$ttt\n" | git stripspace >actual &&
3af82863 68 test_cmp expect actual &&
30d038e2
CR
69
70 printf "$ttt$ttt$ttt\n\n$ttt\n" > expect &&
5be60078 71 printf "$ttt$ttt$ttt\n\n\n\n\n$ttt\n" | git stripspace >actual &&
3af82863 72 test_cmp expect actual &&
30d038e2
CR
73
74 printf "$ttt\n\n$ttt\n" > expect &&
5be60078 75 printf "$ttt\n\n\n\n\n$ttt\n" | git stripspace >actual &&
3af82863 76 test_cmp expect actual &&
30d038e2
CR
77
78 printf "$ttt\n\n$ttt$ttt\n" > expect &&
5be60078 79 printf "$ttt\n\n\n\n\n$ttt$ttt\n" | git stripspace >actual &&
3af82863 80 test_cmp expect actual &&
30d038e2
CR
81
82 printf "$ttt\n\n$ttt$ttt$ttt\n" > expect &&
5be60078 83 printf "$ttt\n\n\n\n\n$ttt$ttt$ttt\n" | git stripspace >actual &&
3af82863 84 test_cmp expect actual &&
b61a8a67
CR
85
86 printf "$ttt\n\n$ttt\n" > expect &&
87 printf "$ttt\n\t\n \n\n \t\t\n$ttt\n" | git stripspace >actual &&
3af82863 88 test_cmp expect actual &&
b61a8a67
CR
89
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 &&
3af82863 92 test_cmp expect actual &&
b61a8a67
CR
93
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 &&
3af82863 96 test_cmp expect actual &&
b61a8a67
CR
97
98 printf "$ttt\n\n$ttt\n" > expect &&
99 printf "$ttt\n\t\n \n\n \t\t\n$ttt\n" | git stripspace >actual &&
3af82863 100 test_cmp expect actual &&
b61a8a67
CR
101
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 &&
3af82863 104 test_cmp expect actual &&
b61a8a67
CR
105
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 &&
3af82863 108 test_cmp expect actual
30d038e2
CR
109'
110
111test_expect_success \
defd5314
CR
112 'only consecutive blank lines should be completely removed' '
113 > expect &&
114
5be60078 115 printf "\n" | git stripspace >actual &&
3af82863 116 test_cmp expect actual &&
30d038e2 117
5be60078 118 printf "\n\n\n" | git stripspace >actual &&
3af82863 119 test_cmp expect actual &&
30d038e2 120
5be60078 121 printf "$sss\n$sss\n$sss\n" | git stripspace >actual &&
3af82863 122 test_cmp expect actual &&
30d038e2 123
5be60078 124 printf "$sss$sss\n$sss\n\n" | git stripspace >actual &&
3af82863 125 test_cmp expect actual &&
30d038e2 126
5be60078 127 printf "\n$sss\n$sss$sss\n" | git stripspace >actual &&
3af82863 128 test_cmp expect actual &&
30d038e2 129
5be60078 130 printf "$sss$sss$sss$sss\n\n\n" | git stripspace >actual &&
3af82863 131 test_cmp expect actual &&
30d038e2 132
5be60078 133 printf "\n$sss$sss$sss$sss\n\n" | git stripspace >actual &&
3af82863 134 test_cmp expect actual &&
30d038e2 135
5be60078 136 printf "\n\n$sss$sss$sss$sss\n" | git stripspace >actual &&
3af82863 137 test_cmp expect actual
defd5314 138'
30d038e2 139
defd5314
CR
140test_expect_success \
141 'consecutive blank lines at the beginning should be removed' '
30d038e2 142 printf "$ttt\n" > expect &&
5be60078 143 printf "\n$ttt\n" | git stripspace >actual &&
3af82863 144 test_cmp expect actual &&
30d038e2
CR
145
146 printf "$ttt\n" > expect &&
5be60078 147 printf "\n\n\n$ttt\n" | git stripspace >actual &&
3af82863 148 test_cmp expect actual &&
30d038e2
CR
149
150 printf "$ttt$ttt\n" > expect &&
5be60078 151 printf "\n\n\n$ttt$ttt\n" | git stripspace >actual &&
3af82863 152 test_cmp expect actual &&
30d038e2
CR
153
154 printf "$ttt$ttt$ttt\n" > expect &&
5be60078 155 printf "\n\n\n$ttt$ttt$ttt\n" | git stripspace >actual &&
3af82863 156 test_cmp expect actual &&
30d038e2
CR
157
158 printf "$ttt$ttt$ttt$ttt\n" > expect &&
5be60078 159 printf "\n\n\n$ttt$ttt$ttt$ttt\n" | git stripspace >actual &&
3af82863 160 test_cmp expect actual &&
30d038e2
CR
161
162 printf "$ttt\n" > expect &&
defd5314 163
5be60078 164 printf "$sss\n$sss\n$sss\n$ttt\n" | git stripspace >actual &&
3af82863 165 test_cmp expect actual &&
30d038e2 166
5be60078 167 printf "\n$sss\n$sss$sss\n$ttt\n" | git stripspace >actual &&
3af82863 168 test_cmp expect actual &&
30d038e2 169
5be60078 170 printf "$sss$sss\n$sss\n\n$ttt\n" | git stripspace >actual &&
3af82863 171 test_cmp expect actual &&
30d038e2 172
5be60078 173 printf "$sss$sss$sss\n\n\n$ttt\n" | git stripspace >actual &&
3af82863 174 test_cmp expect actual &&
30d038e2 175
5be60078 176 printf "\n$sss$sss$sss\n\n$ttt\n" | git stripspace >actual &&
3af82863 177 test_cmp expect actual &&
30d038e2 178
5be60078 179 printf "\n\n$sss$sss$sss\n$ttt\n" | git stripspace >actual &&
3af82863 180 test_cmp expect actual
30d038e2
CR
181'
182
183test_expect_success \
184 'consecutive blank lines at the end should be removed' '
185 printf "$ttt\n" > expect &&
5be60078 186 printf "$ttt\n\n" | git stripspace >actual &&
3af82863 187 test_cmp expect actual &&
30d038e2
CR
188
189 printf "$ttt\n" > expect &&
5be60078 190 printf "$ttt\n\n\n\n" | git stripspace >actual &&
3af82863 191 test_cmp expect actual &&
30d038e2
CR
192
193 printf "$ttt$ttt\n" > expect &&
5be60078 194 printf "$ttt$ttt\n\n\n\n" | git stripspace >actual &&
3af82863 195 test_cmp expect actual &&
30d038e2
CR
196
197 printf "$ttt$ttt$ttt\n" > expect &&
5be60078 198 printf "$ttt$ttt$ttt\n\n\n\n" | git stripspace >actual &&
3af82863 199 test_cmp expect actual &&
30d038e2
CR
200
201 printf "$ttt$ttt$ttt$ttt\n" > expect &&
5be60078 202 printf "$ttt$ttt$ttt$ttt\n\n\n\n" | git stripspace >actual &&
3af82863 203 test_cmp expect actual &&
30d038e2
CR
204
205 printf "$ttt\n" > expect &&
defd5314 206
5be60078 207 printf "$ttt\n$sss\n$sss\n$sss\n" | git stripspace >actual &&
3af82863 208 test_cmp expect actual &&
30d038e2 209
5be60078 210 printf "$ttt\n\n$sss\n$sss$sss\n" | git stripspace >actual &&
3af82863 211 test_cmp expect actual &&
30d038e2 212
5be60078 213 printf "$ttt\n$sss$sss\n$sss\n\n" | git stripspace >actual &&
3af82863 214 test_cmp expect actual &&
30d038e2 215
5be60078 216 printf "$ttt\n$sss$sss$sss\n\n\n" | git stripspace >actual &&
3af82863 217 test_cmp expect actual &&
30d038e2 218
5be60078 219 printf "$ttt\n\n$sss$sss$sss\n\n" | git stripspace >actual &&
3af82863 220 test_cmp expect actual &&
30d038e2 221
5be60078 222 printf "$ttt\n\n\n$sss$sss$sss\n" | git stripspace >actual &&
3af82863 223 test_cmp expect actual
30d038e2
CR
224'
225
226test_expect_success \
227 'text without newline at end should end with newline' '
5be60078
JH
228 test `printf "$ttt" | git stripspace | wc -l` -gt 0 &&
229 test `printf "$ttt$ttt" | git stripspace | wc -l` -gt 0 &&
230 test `printf "$ttt$ttt$ttt" | git stripspace | wc -l` -gt 0 &&
231 test `printf "$ttt$ttt$ttt$ttt" | git stripspace | wc -l` -gt 0
30d038e2
CR
232'
233
234# text plus spaces at the end:
235
236test_expect_success \
237 'text plus spaces without newline at end should end with newline' '
5be60078
JH
238 test `printf "$ttt$sss" | git stripspace | wc -l` -gt 0 &&
239 test `printf "$ttt$ttt$sss" | git stripspace | wc -l` -gt 0 &&
defd5314 240 test `printf "$ttt$ttt$ttt$sss" | git stripspace | wc -l` -gt 0 &&
5be60078
JH
241 test `printf "$ttt$sss$sss" | git stripspace | wc -l` -gt 0 &&
242 test `printf "$ttt$ttt$sss$sss" | git stripspace | wc -l` -gt 0 &&
243 test `printf "$ttt$sss$sss$sss" | git stripspace | wc -l` -gt 0
30d038e2
CR
244'
245
41ac414e 246test_expect_success \
30d038e2 247 'text plus spaces without newline at end should not show spaces' '
aadbe44f
JK
248 ! (printf "$ttt$sss" | git stripspace | grep " " >/dev/null) &&
249 ! (printf "$ttt$ttt$sss" | git stripspace | grep " " >/dev/null) &&
250 ! (printf "$ttt$ttt$ttt$sss" | git stripspace | grep " " >/dev/null) &&
251 ! (printf "$ttt$sss$sss" | git stripspace | grep " " >/dev/null) &&
252 ! (printf "$ttt$ttt$sss$sss" | git stripspace | grep " " >/dev/null) &&
253 ! (printf "$ttt$sss$sss$sss" | git stripspace | grep " " >/dev/null)
30d038e2
CR
254'
255
256test_expect_success \
257 'text plus spaces without newline should show the correct lines' '
258 printf "$ttt\n" >expect &&
5be60078 259 printf "$ttt$sss" | git stripspace >actual &&
3af82863 260 test_cmp expect actual &&
30d038e2
CR
261
262 printf "$ttt\n" >expect &&
5be60078 263 printf "$ttt$sss$sss" | git stripspace >actual &&
3af82863 264 test_cmp expect actual &&
30d038e2
CR
265
266 printf "$ttt\n" >expect &&
5be60078 267 printf "$ttt$sss$sss$sss" | git stripspace >actual &&
3af82863 268 test_cmp expect actual &&
30d038e2
CR
269
270 printf "$ttt$ttt\n" >expect &&
5be60078 271 printf "$ttt$ttt$sss" | git stripspace >actual &&
3af82863 272 test_cmp expect actual &&
30d038e2
CR
273
274 printf "$ttt$ttt\n" >expect &&
5be60078 275 printf "$ttt$ttt$sss$sss" | git stripspace >actual &&
3af82863 276 test_cmp expect actual &&
30d038e2
CR
277
278 printf "$ttt$ttt$ttt\n" >expect &&
5be60078 279 printf "$ttt$ttt$ttt$sss" | git stripspace >actual &&
3af82863 280 test_cmp expect actual
30d038e2
CR
281'
282
41ac414e 283test_expect_success \
30d038e2 284 'text plus spaces at end should not show spaces' '
aadbe44f
JK
285 ! (echo "$ttt$sss" | git stripspace | grep " " >/dev/null) &&
286 ! (echo "$ttt$ttt$sss" | git stripspace | grep " " >/dev/null) &&
287 ! (echo "$ttt$ttt$ttt$sss" | git stripspace | grep " " >/dev/null) &&
288 ! (echo "$ttt$sss$sss" | git stripspace | grep " " >/dev/null) &&
289 ! (echo "$ttt$ttt$sss$sss" | git stripspace | grep " " >/dev/null) &&
290 ! (echo "$ttt$sss$sss$sss" | git stripspace | grep " " >/dev/null)
30d038e2
CR
291'
292
293test_expect_success \
294 'text plus spaces at end should be cleaned and newline must remain' '
295 echo "$ttt" >expect &&
5be60078 296 echo "$ttt$sss" | git stripspace >actual &&
3af82863 297 test_cmp expect actual &&
30d038e2
CR
298
299 echo "$ttt" >expect &&
5be60078 300 echo "$ttt$sss$sss" | git stripspace >actual &&
3af82863 301 test_cmp expect actual &&
30d038e2
CR
302
303 echo "$ttt" >expect &&
5be60078 304 echo "$ttt$sss$sss$sss" | git stripspace >actual &&
3af82863 305 test_cmp expect actual &&
30d038e2
CR
306
307 echo "$ttt$ttt" >expect &&
5be60078 308 echo "$ttt$ttt$sss" | git stripspace >actual &&
3af82863 309 test_cmp expect actual &&
30d038e2
CR
310
311 echo "$ttt$ttt" >expect &&
5be60078 312 echo "$ttt$ttt$sss$sss" | git stripspace >actual &&
3af82863 313 test_cmp expect actual &&
30d038e2
CR
314
315 echo "$ttt$ttt$ttt" >expect &&
5be60078 316 echo "$ttt$ttt$ttt$sss" | git stripspace >actual &&
3af82863 317 test_cmp expect actual
30d038e2
CR
318'
319
320# spaces only:
321
322test_expect_success \
323 'spaces with newline at end should be replaced with empty string' '
324 printf "" >expect &&
325
5be60078 326 echo | git stripspace >actual &&
3af82863 327 test_cmp expect actual &&
30d038e2 328
5be60078 329 echo "$sss" | git stripspace >actual &&
3af82863 330 test_cmp expect actual &&
30d038e2 331
5be60078 332 echo "$sss$sss" | git stripspace >actual &&
3af82863 333 test_cmp expect actual &&
30d038e2 334
5be60078 335 echo "$sss$sss$sss" | git stripspace >actual &&
3af82863 336 test_cmp expect actual &&
30d038e2 337
5be60078 338 echo "$sss$sss$sss$sss" | git stripspace >actual &&
3af82863 339 test_cmp expect actual
30d038e2
CR
340'
341
41ac414e 342test_expect_success \
30d038e2 343 'spaces without newline at end should not show spaces' '
aadbe44f
JK
344 ! (printf "" | git stripspace | grep " " >/dev/null) &&
345 ! (printf "$sss" | git stripspace | grep " " >/dev/null) &&
346 ! (printf "$sss$sss" | git stripspace | grep " " >/dev/null) &&
347 ! (printf "$sss$sss$sss" | git stripspace | grep " " >/dev/null) &&
348 ! (printf "$sss$sss$sss$sss" | git stripspace | grep " " >/dev/null)
30d038e2
CR
349'
350
351test_expect_success \
352 'spaces without newline at end should be replaced with empty string' '
353 printf "" >expect &&
354
5be60078 355 printf "" | git stripspace >actual &&
3af82863 356 test_cmp expect actual &&
30d038e2 357
5be60078 358 printf "$sss$sss" | git stripspace >actual &&
3af82863 359 test_cmp expect actual &&
30d038e2 360
5be60078 361 printf "$sss$sss$sss" | git stripspace >actual &&
3af82863 362 test_cmp expect actual &&
30d038e2 363
5be60078 364 printf "$sss$sss$sss$sss" | git stripspace >actual &&
3af82863 365 test_cmp expect actual
30d038e2
CR
366'
367
b61a8a67
CR
368test_expect_success \
369 'consecutive text lines should be unchanged' '
370 printf "$ttt$ttt\n$ttt\n" >expect &&
371 printf "$ttt$ttt\n$ttt\n" | git stripspace >actual &&
3af82863 372 test_cmp expect actual &&
b61a8a67
CR
373
374 printf "$ttt\n$ttt$ttt\n$ttt\n" >expect &&
375 printf "$ttt\n$ttt$ttt\n$ttt\n" | git stripspace >actual &&
3af82863 376 test_cmp expect actual &&
b61a8a67
CR
377
378 printf "$ttt\n$ttt\n$ttt\n$ttt$ttt\n" >expect &&
379 printf "$ttt\n$ttt\n$ttt\n$ttt$ttt\n" | git stripspace >actual &&
3af82863 380 test_cmp expect actual &&
b61a8a67
CR
381
382 printf "$ttt\n$ttt\n\n$ttt$ttt\n$ttt\n" >expect &&
383 printf "$ttt\n$ttt\n\n$ttt$ttt\n$ttt\n" | git stripspace >actual &&
3af82863 384 test_cmp expect actual &&
b61a8a67
CR
385
386 printf "$ttt$ttt\n\n$ttt\n$ttt$ttt\n" >expect &&
387 printf "$ttt$ttt\n\n$ttt\n$ttt$ttt\n" | git stripspace >actual &&
3af82863 388 test_cmp expect actual &&
b61a8a67
CR
389
390 printf "$ttt\n$ttt$ttt\n\n$ttt\n" >expect &&
391 printf "$ttt\n$ttt$ttt\n\n$ttt\n" | git stripspace >actual &&
3af82863 392 test_cmp expect actual
b61a8a67
CR
393'
394
f653aee5
JS
395test_expect_success 'strip comments, too' '
396 test ! -z "$(echo "# comment" | git stripspace)" &&
397 test -z "$(echo "# comment" | git stripspace -s)"
398'
399
eff80a9f
JH
400test_expect_success 'strip comments with changed comment char' '
401 test ! -z "$(echo "; comment" | git -c core.commentchar=";" stripspace)" &&
402 test -z "$(echo "; comment" | git -c core.commentchar=";" stripspace -s)"
403'
404
405test_expect_success '-c with single line' '
406 printf "# foo\n" >expect &&
407 printf "foo" | git stripspace -c >actual &&
408 test_cmp expect actual
409'
410
411test_expect_success '-c with single line followed by empty line' '
412 printf "# foo\n#\n" >expect &&
413 printf "foo\n\n" | git stripspace -c >actual &&
414 test_cmp expect actual
415'
416
417test_expect_success '-c with newline only' '
418 printf "#\n" >expect &&
419 printf "\n" | git stripspace -c >actual &&
420 test_cmp expect actual
421'
422
423test_expect_success '--comment-lines with single line' '
424 printf "# foo\n" >expect &&
425 printf "foo" | git stripspace -c >actual &&
426 test_cmp expect actual
427'
428
429test_expect_success '-c with changed comment char' '
430 printf "; foo\n" >expect &&
431 printf "foo" | git -c core.commentchar=";" stripspace -c >actual &&
432 test_cmp expect actual
433'
434
30d038e2 435test_done