]> git.ipfire.org Git - thirdparty/git.git/blame - t/t0030-stripspace.sh
Sync with Git 2.45.1
[thirdparty/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 7
956d2e46 8TEST_PASSES_SANITIZE_LEAK=true
30d038e2
CR
9. ./test-lib.sh
10
11t40='A quick brown fox jumps over the lazy do'
12s40=' '
13sss="$s40$s40$s40$s40$s40$s40$s40$s40$s40$s40" # 400
14ttt="$t40$t40$t40$t40$t40$t40$t40$t40$t40$t40" # 400
15
eed36fce
SM
16printf_git_stripspace () {
17 printf "$1" | git stripspace
18}
19
27990663
JC
20test_expect_success 'long lines without spaces should be unchanged' '
21 echo "$ttt" >expect &&
22 git stripspace <expect >actual &&
23 test_cmp expect actual &&
24
25 echo "$ttt$ttt" >expect &&
26 git stripspace <expect >actual &&
27 test_cmp expect actual &&
28
29 echo "$ttt$ttt$ttt" >expect &&
30 git stripspace <expect >actual &&
31 test_cmp expect actual &&
32
33 echo "$ttt$ttt$ttt$ttt" >expect &&
34 git stripspace <expect >actual &&
35 test_cmp expect actual
30d038e2
CR
36'
37
27990663
JC
38test_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 &&
30d038e2 42
27990663
JC
43 echo "$sss$sss$ttt" >expect &&
44 git stripspace <expect >actual &&
45 test_cmp expect actual &&
30d038e2 46
27990663
JC
47 echo "$sss$sss$sss$ttt" >expect &&
48 git stripspace <expect >actual &&
49 test_cmp expect actual
30d038e2
CR
50'
51
27990663
JC
52test_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 &&
30d038e2 56
27990663
JC
57 echo "$ttt$sss$sss$ttt" >expect &&
58 git stripspace <expect >actual &&
59 test_cmp expect actual
30d038e2
CR
60'
61
27990663
JC
62test_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 &&
30d038e2 66
27990663
JC
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 &&
30d038e2 70
27990663
JC
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 &&
30d038e2 74
27990663
JC
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 &&
30d038e2 78
27990663
JC
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 &&
30d038e2 82
27990663
JC
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 &&
b61a8a67 86
27990663
JC
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 &&
b61a8a67 90
27990663
JC
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 &&
b61a8a67 94
27990663
JC
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 &&
b61a8a67 98
27990663
JC
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 &&
b61a8a67 102
27990663
JC
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 &&
b61a8a67 106
27990663
JC
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
30d038e2
CR
110'
111
27990663
JC
112test_expect_success 'only consecutive blank lines should be completely removed' '
113 printf "\n" | git stripspace >actual &&
114 test_must_be_empty actual &&
defd5314 115
27990663
JC
116 printf "\n\n\n" | git stripspace >actual &&
117 test_must_be_empty actual &&
30d038e2 118
27990663
JC
119 printf "$sss\n$sss\n$sss\n" | git stripspace >actual &&
120 test_must_be_empty actual &&
30d038e2 121
27990663
JC
122 printf "$sss$sss\n$sss\n\n" | git stripspace >actual &&
123 test_must_be_empty actual &&
30d038e2 124
27990663
JC
125 printf "\n$sss\n$sss$sss\n" | git stripspace >actual &&
126 test_must_be_empty actual &&
30d038e2 127
27990663
JC
128 printf "$sss$sss$sss$sss\n\n\n" | git stripspace >actual &&
129 test_must_be_empty actual &&
30d038e2 130
27990663
JC
131 printf "\n$sss$sss$sss$sss\n\n" | git stripspace >actual &&
132 test_must_be_empty actual &&
30d038e2 133
27990663
JC
134 printf "\n\n$sss$sss$sss$sss\n" | git stripspace >actual &&
135 test_must_be_empty actual
defd5314 136'
30d038e2 137
27990663
JC
138test_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 &&
30d038e2 142
27990663
JC
143 printf "$ttt\n" > expect &&
144 printf "\n\n\n$ttt\n" | git stripspace >actual &&
145 test_cmp expect actual &&
30d038e2 146
27990663
JC
147 printf "$ttt$ttt\n" > expect &&
148 printf "\n\n\n$ttt$ttt\n" | git stripspace >actual &&
149 test_cmp expect actual &&
30d038e2 150
27990663
JC
151 printf "$ttt$ttt$ttt\n" > expect &&
152 printf "\n\n\n$ttt$ttt$ttt\n" | git stripspace >actual &&
153 test_cmp expect actual &&
30d038e2 154
27990663
JC
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 &&
30d038e2 158
27990663 159 printf "$ttt\n" > expect &&
defd5314 160
27990663
JC
161 printf "$sss\n$sss\n$sss\n$ttt\n" | git stripspace >actual &&
162 test_cmp expect actual &&
30d038e2 163
27990663
JC
164 printf "\n$sss\n$sss$sss\n$ttt\n" | git stripspace >actual &&
165 test_cmp expect actual &&
30d038e2 166
27990663
JC
167 printf "$sss$sss\n$sss\n\n$ttt\n" | git stripspace >actual &&
168 test_cmp expect actual &&
30d038e2 169
27990663
JC
170 printf "$sss$sss$sss\n\n\n$ttt\n" | git stripspace >actual &&
171 test_cmp expect actual &&
30d038e2 172
27990663
JC
173 printf "\n$sss$sss$sss\n\n$ttt\n" | git stripspace >actual &&
174 test_cmp expect actual &&
30d038e2 175
27990663
JC
176 printf "\n\n$sss$sss$sss\n$ttt\n" | git stripspace >actual &&
177 test_cmp expect actual
30d038e2
CR
178'
179
27990663
JC
180test_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 &&
30d038e2 184
27990663
JC
185 printf "$ttt\n" > expect &&
186 printf "$ttt\n\n\n\n" | git stripspace >actual &&
187 test_cmp expect actual &&
30d038e2 188
27990663
JC
189 printf "$ttt$ttt\n" > expect &&
190 printf "$ttt$ttt\n\n\n\n" | git stripspace >actual &&
191 test_cmp expect actual &&
30d038e2 192
27990663
JC
193 printf "$ttt$ttt$ttt\n" > expect &&
194 printf "$ttt$ttt$ttt\n\n\n\n" | git stripspace >actual &&
195 test_cmp expect actual &&
30d038e2 196
27990663
JC
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 &&
30d038e2 200
27990663 201 printf "$ttt\n" > expect &&
defd5314 202
27990663
JC
203 printf "$ttt\n$sss\n$sss\n$sss\n" | git stripspace >actual &&
204 test_cmp expect actual &&
30d038e2 205
27990663
JC
206 printf "$ttt\n\n$sss\n$sss$sss\n" | git stripspace >actual &&
207 test_cmp expect actual &&
30d038e2 208
27990663
JC
209 printf "$ttt\n$sss$sss\n$sss\n\n" | git stripspace >actual &&
210 test_cmp expect actual &&
30d038e2 211
27990663
JC
212 printf "$ttt\n$sss$sss$sss\n\n\n" | git stripspace >actual &&
213 test_cmp expect actual &&
30d038e2 214
27990663
JC
215 printf "$ttt\n\n$sss$sss$sss\n\n" | git stripspace >actual &&
216 test_cmp expect actual &&
30d038e2 217
27990663
JC
218 printf "$ttt\n\n\n$sss$sss$sss\n" | git stripspace >actual &&
219 test_cmp expect actual
30d038e2
CR
220'
221
27990663
JC
222test_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"
30d038e2
CR
227'
228
229# text plus spaces at the end:
230
27990663
JC
231test_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"
30d038e2
CR
238'
239
27990663
JC
240test_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
30d038e2
CR
253'
254
27990663
JC
255test_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 &&
30d038e2 259
27990663
JC
260 printf "$ttt\n" >expect &&
261 printf "$ttt$sss$sss" | git stripspace >actual &&
262 test_cmp expect actual &&
30d038e2 263
27990663
JC
264 printf "$ttt\n" >expect &&
265 printf "$ttt$sss$sss$sss" | git stripspace >actual &&
266 test_cmp expect actual &&
30d038e2 267
27990663
JC
268 printf "$ttt$ttt\n" >expect &&
269 printf "$ttt$ttt$sss" | git stripspace >actual &&
270 test_cmp expect actual &&
30d038e2 271
27990663
JC
272 printf "$ttt$ttt\n" >expect &&
273 printf "$ttt$ttt$sss$sss" | git stripspace >actual &&
274 test_cmp expect actual &&
30d038e2 275
27990663
JC
276 printf "$ttt$ttt$ttt\n" >expect &&
277 printf "$ttt$ttt$ttt$sss" | git stripspace >actual &&
278 test_cmp expect actual
30d038e2
CR
279'
280
27990663
JC
281test_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
30d038e2
CR
294'
295
27990663
JC
296test_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 &&
30d038e2 300
27990663
JC
301 echo "$ttt" >expect &&
302 echo "$ttt$sss$sss" | git stripspace >actual &&
303 test_cmp expect actual &&
30d038e2 304
27990663
JC
305 echo "$ttt" >expect &&
306 echo "$ttt$sss$sss$sss" | git stripspace >actual &&
307 test_cmp expect actual &&
30d038e2 308
27990663
JC
309 echo "$ttt$ttt" >expect &&
310 echo "$ttt$ttt$sss" | git stripspace >actual &&
311 test_cmp expect actual &&
30d038e2 312
27990663
JC
313 echo "$ttt$ttt" >expect &&
314 echo "$ttt$ttt$sss$sss" | git stripspace >actual &&
315 test_cmp expect actual &&
30d038e2 316
27990663
JC
317 echo "$ttt$ttt$ttt" >expect &&
318 echo "$ttt$ttt$ttt$sss" | git stripspace >actual &&
319 test_cmp expect actual
30d038e2
CR
320'
321
322# spaces only:
323
27990663
JC
324test_expect_success 'spaces with newline at end should be replaced with empty string' '
325 echo | git stripspace >actual &&
326 test_must_be_empty actual &&
30d038e2 327
27990663
JC
328 echo "$sss" | git stripspace >actual &&
329 test_must_be_empty actual &&
30d038e2 330
27990663
JC
331 echo "$sss$sss" | git stripspace >actual &&
332 test_must_be_empty actual &&
30d038e2 333
27990663
JC
334 echo "$sss$sss$sss" | git stripspace >actual &&
335 test_must_be_empty actual &&
30d038e2 336
27990663
JC
337 echo "$sss$sss$sss$sss" | git stripspace >actual &&
338 test_must_be_empty actual
30d038e2
CR
339'
340
27990663
JC
341test_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
30d038e2
CR
352'
353
27990663
JC
354test_expect_success 'spaces without newline at end should be replaced with empty string' '
355 printf "" | git stripspace >actual &&
356 test_must_be_empty actual &&
30d038e2 357
27990663
JC
358 printf "$sss$sss" | git stripspace >actual &&
359 test_must_be_empty actual &&
30d038e2 360
27990663
JC
361 printf "$sss$sss$sss" | git stripspace >actual &&
362 test_must_be_empty actual &&
30d038e2 363
27990663
JC
364 printf "$sss$sss$sss$sss" | git stripspace >actual &&
365 test_must_be_empty actual
30d038e2
CR
366'
367
27990663
JC
368test_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 &&
b61a8a67 372
27990663
JC
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 &&
b61a8a67 376
27990663
JC
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 &&
b61a8a67 380
27990663
JC
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 &&
b61a8a67 384
27990663
JC
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 &&
b61a8a67 388
27990663
JC
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
b61a8a67
CR
392'
393
f653aee5
JS
394test_expect_success 'strip comments, too' '
395 test ! -z "$(echo "# comment" | git stripspace)" &&
396 test -z "$(echo "# comment" | git stripspace -s)"
397'
398
eff80a9f
JH
399test_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)"
402'
403
9ccf3e9b
JK
404test_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)"
407'
408
727565ef
JK
409test_expect_success 'newline as commentchar is forbidden' '
410 test_must_fail git -c core.commentChar="$LF" stripspace -s 2>err &&
9ccf3e9b 411 grep "core.commentchar cannot contain newline" err
8b311478
JK
412'
413
414test_expect_success 'empty commentchar is forbidden' '
415 test_must_fail git -c core.commentchar= stripspace -s 2>err &&
9ccf3e9b 416 grep "core.commentchar must have at least one character" err
727565ef
JK
417'
418
eff80a9f
JH
419test_expect_success '-c with single line' '
420 printf "# foo\n" >expect &&
421 printf "foo" | git stripspace -c >actual &&
422 test_cmp expect actual
423'
424
425test_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
429'
430
431test_expect_success '-c with newline only' '
432 printf "#\n" >expect &&
433 printf "\n" | git stripspace -c >actual &&
434 test_cmp expect actual
435'
436
437test_expect_success '--comment-lines with single line' '
438 printf "# foo\n" >expect &&
439 printf "foo" | git stripspace -c >actual &&
440 test_cmp expect actual
441'
442
443test_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
447'
448
92068ae8 449test_expect_success '-c with comment char defined in .git/config' '
66458388
JS
450 test_config core.commentchar = &&
451 printf "= foo\n" >expect &&
957da758
JN
452 rm -fr sub &&
453 mkdir sub &&
454 printf "foo" | git -C sub stripspace -c >actual &&
455 test_cmp expect actual
456'
457
458test_expect_success '-c outside git repository' '
459 printf "# foo\n" >expect &&
460 printf "foo" | nongit git stripspace -c >actual &&
66458388
JS
461 test_cmp expect actual
462'
463
d55aeb76
JH
464test_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
468'
469
30d038e2 470test_done