1#!/bin/sh
2#
3# Copyright (c) 2007 Carlos Rica
4#
5
6test_description='git stripspace'
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 &&
18    git stripspace <expect >actual &&
19    test_cmp expect actual &&
20
21    echo "$ttt$ttt" >expect &&
22    git stripspace <expect >actual &&
23    test_cmp expect actual &&
24
25    echo "$ttt$ttt$ttt" >expect &&
26    git stripspace <expect >actual &&
27    test_cmp expect actual &&
28
29    echo "$ttt$ttt$ttt$ttt" >expect &&
30    git stripspace <expect >actual &&
31    test_cmp expect actual
32'
33
34test_expect_success \
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 &&
39
40    echo "$sss$sss$ttt" >expect &&
41    git stripspace <expect >actual &&
42    test_cmp expect actual &&
43
44    echo "$sss$sss$sss$ttt" >expect &&
45    git stripspace <expect >actual &&
46    test_cmp expect actual
47'
48
49test_expect_success \
50    'lines with intermediate spaces should be unchanged' '
51    echo "$ttt$sss$ttt" >expect &&
52    git stripspace <expect >actual &&
53    test_cmp expect actual &&
54
55    echo "$ttt$sss$sss$ttt" >expect &&
56    git stripspace <expect >actual &&
57    test_cmp expect actual
58'
59
60test_expect_success \
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 &&
65
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 &&
69
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 &&
73
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 &&
77
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 &&
81
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 &&
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 &&
88    test_cmp expect actual &&
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 &&
92    test_cmp expect actual &&
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 &&
96    test_cmp expect actual &&
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 &&
100    test_cmp expect actual &&
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 &&
104    test_cmp expect actual &&
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 &&
108    test_cmp expect actual
109'
110
111test_expect_success \
112    'only consecutive blank lines should be completely removed' '
113
114    printf "\n" | git stripspace >actual &&
115    test_must_be_empty actual &&
116
117    printf "\n\n\n" | git stripspace >actual &&
118    test_must_be_empty actual &&
119
120    printf "$sss\n$sss\n$sss\n" | git stripspace >actual &&
121    test_must_be_empty actual &&
122
123    printf "$sss$sss\n$sss\n\n" | git stripspace >actual &&
124    test_must_be_empty actual &&
125
126    printf "\n$sss\n$sss$sss\n" | git stripspace >actual &&
127    test_must_be_empty actual &&
128
129    printf "$sss$sss$sss$sss\n\n\n" | git stripspace >actual &&
130    test_must_be_empty actual &&
131
132    printf "\n$sss$sss$sss$sss\n\n" | git stripspace >actual &&
133    test_must_be_empty actual &&
134
135    printf "\n\n$sss$sss$sss$sss\n" | git stripspace >actual &&
136    test_must_be_empty actual
137'
138
139test_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 &&
144
145    printf "$ttt\n" > expect &&
146    printf "\n\n\n$ttt\n" | git stripspace >actual &&
147    test_cmp expect actual &&
148
149    printf "$ttt$ttt\n" > expect &&
150    printf "\n\n\n$ttt$ttt\n" | git stripspace >actual &&
151    test_cmp expect actual &&
152
153    printf "$ttt$ttt$ttt\n" > expect &&
154    printf "\n\n\n$ttt$ttt$ttt\n" | git stripspace >actual &&
155    test_cmp expect actual &&
156
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 &&
160
161    printf "$ttt\n" > expect &&
162
163    printf "$sss\n$sss\n$sss\n$ttt\n" | git stripspace >actual &&
164    test_cmp expect actual &&
165
166    printf "\n$sss\n$sss$sss\n$ttt\n" | git stripspace >actual &&
167    test_cmp expect actual &&
168
169    printf "$sss$sss\n$sss\n\n$ttt\n" | git stripspace >actual &&
170    test_cmp expect actual &&
171
172    printf "$sss$sss$sss\n\n\n$ttt\n" | git stripspace >actual &&
173    test_cmp expect actual &&
174
175    printf "\n$sss$sss$sss\n\n$ttt\n" | git stripspace >actual &&
176    test_cmp expect actual &&
177
178    printf "\n\n$sss$sss$sss\n$ttt\n" | git stripspace >actual &&
179    test_cmp expect actual
180'
181
182test_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 &&
187
188    printf "$ttt\n" > expect &&
189    printf "$ttt\n\n\n\n" | git stripspace >actual &&
190    test_cmp expect actual &&
191
192    printf "$ttt$ttt\n" > expect &&
193    printf "$ttt$ttt\n\n\n\n" | git stripspace >actual &&
194    test_cmp expect actual &&
195
196    printf "$ttt$ttt$ttt\n" > expect &&
197    printf "$ttt$ttt$ttt\n\n\n\n" | git stripspace >actual &&
198    test_cmp expect actual &&
199
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 &&
203
204    printf "$ttt\n" > expect &&
205
206    printf "$ttt\n$sss\n$sss\n$sss\n" | git stripspace >actual &&
207    test_cmp expect actual &&
208
209    printf "$ttt\n\n$sss\n$sss$sss\n" | git stripspace >actual &&
210    test_cmp expect actual &&
211
212    printf "$ttt\n$sss$sss\n$sss\n\n" | git stripspace >actual &&
213    test_cmp expect actual &&
214
215    printf "$ttt\n$sss$sss$sss\n\n\n" | git stripspace >actual &&
216    test_cmp expect actual &&
217
218    printf "$ttt\n\n$sss$sss$sss\n\n" | git stripspace >actual &&
219    test_cmp expect actual &&
220
221    printf "$ttt\n\n\n$sss$sss$sss\n" | git stripspace >actual &&
222    test_cmp expect actual
223'
224
225test_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
231'
232
233# text plus spaces at the end:
234
235test_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
243'
244
245test_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)
253'
254
255test_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 &&
260
261    printf "$ttt\n" >expect &&
262    printf "$ttt$sss$sss" | git stripspace >actual &&
263    test_cmp expect actual &&
264
265    printf "$ttt\n" >expect &&
266    printf "$ttt$sss$sss$sss" | git stripspace >actual &&
267    test_cmp expect actual &&
268
269    printf "$ttt$ttt\n" >expect &&
270    printf "$ttt$ttt$sss" | git stripspace >actual &&
271    test_cmp expect actual &&
272
273    printf "$ttt$ttt\n" >expect &&
274    printf "$ttt$ttt$sss$sss" | git stripspace >actual &&
275    test_cmp expect actual &&
276
277    printf "$ttt$ttt$ttt\n" >expect &&
278    printf "$ttt$ttt$ttt$sss" | git stripspace >actual &&
279    test_cmp expect actual
280'
281
282test_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)
290'
291
292test_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 &&
297
298    echo "$ttt" >expect &&
299    echo "$ttt$sss$sss" | git stripspace >actual &&
300    test_cmp expect actual &&
301
302    echo "$ttt" >expect &&
303    echo "$ttt$sss$sss$sss" | git stripspace >actual &&
304    test_cmp expect actual &&
305
306    echo "$ttt$ttt" >expect &&
307    echo "$ttt$ttt$sss" | git stripspace >actual &&
308    test_cmp expect actual &&
309
310    echo "$ttt$ttt" >expect &&
311    echo "$ttt$ttt$sss$sss" | git stripspace >actual &&
312    test_cmp expect actual &&
313
314    echo "$ttt$ttt$ttt" >expect &&
315    echo "$ttt$ttt$ttt$sss" | git stripspace >actual &&
316    test_cmp expect actual
317'
318
319# spaces only:
320
321test_expect_success \
322    'spaces with newline at end should be replaced with empty string' '
323    echo | git stripspace >actual &&
324    test_must_be_empty actual &&
325
326    echo "$sss" | git stripspace >actual &&
327    test_must_be_empty actual &&
328
329    echo "$sss$sss" | git stripspace >actual &&
330    test_must_be_empty actual &&
331
332    echo "$sss$sss$sss" | git stripspace >actual &&
333    test_must_be_empty actual &&
334
335    echo "$sss$sss$sss$sss" | git stripspace >actual &&
336    test_must_be_empty actual
337'
338
339test_expect_success \
340    'spaces without newline at end should not show spaces' '
341    ! (printf "" | git stripspace | grep " " >/dev/null) &&
342    ! (printf "$sss" | git stripspace | grep " " >/dev/null) &&
343    ! (printf "$sss$sss" | git stripspace | grep " " >/dev/null) &&
344    ! (printf "$sss$sss$sss" | git stripspace | grep " " >/dev/null) &&
345    ! (printf "$sss$sss$sss$sss" | git stripspace | grep " " >/dev/null)
346'
347
348test_expect_success \
349    'spaces without newline at end should be replaced with empty string' '
350    printf "" | git stripspace >actual &&
351    test_must_be_empty actual &&
352
353    printf "$sss$sss" | git stripspace >actual &&
354    test_must_be_empty actual &&
355
356    printf "$sss$sss$sss" | git stripspace >actual &&
357    test_must_be_empty actual &&
358
359    printf "$sss$sss$sss$sss" | git stripspace >actual &&
360    test_must_be_empty actual
361'
362
363test_expect_success \
364    'consecutive text lines should be unchanged' '
365    printf "$ttt$ttt\n$ttt\n" >expect &&
366    printf "$ttt$ttt\n$ttt\n" | git stripspace >actual &&
367    test_cmp expect actual &&
368
369    printf "$ttt\n$ttt$ttt\n$ttt\n" >expect &&
370    printf "$ttt\n$ttt$ttt\n$ttt\n" | git stripspace >actual &&
371    test_cmp expect actual &&
372
373    printf "$ttt\n$ttt\n$ttt\n$ttt$ttt\n" >expect &&
374    printf "$ttt\n$ttt\n$ttt\n$ttt$ttt\n" | git stripspace >actual &&
375    test_cmp expect actual &&
376
377    printf "$ttt\n$ttt\n\n$ttt$ttt\n$ttt\n" >expect &&
378    printf "$ttt\n$ttt\n\n$ttt$ttt\n$ttt\n" | git stripspace >actual &&
379    test_cmp expect actual &&
380
381    printf "$ttt$ttt\n\n$ttt\n$ttt$ttt\n" >expect &&
382    printf "$ttt$ttt\n\n$ttt\n$ttt$ttt\n" | git stripspace >actual &&
383    test_cmp expect actual &&
384
385    printf "$ttt\n$ttt$ttt\n\n$ttt\n" >expect &&
386    printf "$ttt\n$ttt$ttt\n\n$ttt\n" | git stripspace >actual &&
387    test_cmp expect actual
388'
389
390test_expect_success 'strip comments, too' '
391	test ! -z "$(echo "# comment" | git stripspace)" &&
392	test -z "$(echo "# comment" | git stripspace -s)"
393'
394
395test_expect_success 'strip comments with changed comment char' '
396	test ! -z "$(echo "; comment" | git -c core.commentchar=";" stripspace)" &&
397	test -z "$(echo "; comment" | git -c core.commentchar=";" stripspace -s)"
398'
399
400test_expect_success '-c with single line' '
401	printf "# foo\n" >expect &&
402	printf "foo" | git stripspace -c >actual &&
403	test_cmp expect actual
404'
405
406test_expect_success '-c with single line followed by empty line' '
407	printf "# foo\n#\n" >expect &&
408	printf "foo\n\n" | git stripspace -c >actual &&
409	test_cmp expect actual
410'
411
412test_expect_success '-c with newline only' '
413	printf "#\n" >expect &&
414	printf "\n" | git stripspace -c >actual &&
415	test_cmp expect actual
416'
417
418test_expect_success '--comment-lines with single line' '
419	printf "# foo\n" >expect &&
420	printf "foo" | git stripspace -c >actual &&
421	test_cmp expect actual
422'
423
424test_expect_success '-c with changed comment char' '
425	printf "; foo\n" >expect &&
426	printf "foo" | git -c core.commentchar=";" stripspace -c >actual &&
427	test_cmp expect actual
428'
429
430test_expect_success '-c with comment char defined in .git/config' '
431	test_config core.commentchar = &&
432	printf "= foo\n" >expect &&
433	rm -fr sub &&
434	mkdir sub &&
435	printf "foo" | git -C sub stripspace -c >actual &&
436	test_cmp expect actual
437'
438
439test_expect_success '-c outside git repository' '
440	printf "# foo\n" >expect &&
441	printf "foo" | nongit git stripspace -c >actual &&
442	test_cmp expect actual
443'
444
445test_expect_success 'avoid SP-HT sequence in commented line' '
446	printf "#\tone\n#\n# two\n" >expect &&
447	printf "\tone\n\ntwo\n" | git stripspace -c >actual &&
448	test_cmp expect actual
449'
450
451test_done
452