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