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