t0030-stripspace.sh 13.5 KB
Newer Older
1 2 3 4 5
#!/bin/sh
#
# Copyright (c) 2007 Carlos Rica
#

6
test_description='git stripspace'
7 8 9 10 11 12 13 14 15 16 17

. ./test-lib.sh

t40='A quick brown fox jumps over the lazy do'
s40='                                        '
sss="$s40$s40$s40$s40$s40$s40$s40$s40$s40$s40" # 400
ttt="$t40$t40$t40$t40$t40$t40$t40$t40$t40$t40" # 400

test_expect_success \
    'long lines without spaces should be unchanged' '
    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 34 35 36
'

test_expect_success \
    'lines with spaces at the beginning should be unchanged' '
    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 49 50 51
'

test_expect_success \
    'lines with intermediate spaces should be unchanged' '
    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 60 61 62
'

test_expect_success \
    'consecutive blank lines should be unified' '
    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 87

    printf "$ttt\n\n$ttt\n" > expect &&
    printf "$ttt\n\t\n \n\n  \t\t\n$ttt\n" | git stripspace >actual &&
88
    test_cmp expect actual &&
89 90 91

    printf "$ttt$ttt\n\n$ttt\n" > expect &&
    printf "$ttt$ttt\n\t\n \n\n  \t\t\n$ttt\n" | git stripspace >actual &&
92
    test_cmp expect actual &&
93 94 95

    printf "$ttt$ttt$ttt\n\n$ttt\n" > expect &&
    printf "$ttt$ttt$ttt\n\t\n \n\n  \t\t\n$ttt\n" | git stripspace >actual &&
96
    test_cmp expect actual &&
97 98 99

    printf "$ttt\n\n$ttt\n" > expect &&
    printf "$ttt\n\t\n \n\n  \t\t\n$ttt\n" | git stripspace >actual &&
100
    test_cmp expect actual &&
101 102 103

    printf "$ttt\n\n$ttt$ttt\n" > expect &&
    printf "$ttt\n\t\n \n\n  \t\t\n$ttt$ttt\n" | git stripspace >actual &&
104
    test_cmp expect actual &&
105 106 107

    printf "$ttt\n\n$ttt$ttt$ttt\n" > expect &&
    printf "$ttt\n\t\n \n\n  \t\t\n$ttt$ttt$ttt\n" | git stripspace >actual &&
108
    test_cmp expect actual
109 110 111
'

test_expect_success \
112 113 114
    'only consecutive blank lines should be completely removed' '
    > expect &&

115
    printf "\n" | git stripspace >actual &&
116
    test_cmp expect actual &&
117

118
    printf "\n\n\n" | git stripspace >actual &&
119
    test_cmp expect actual &&
120

121
    printf "$sss\n$sss\n$sss\n" | git stripspace >actual &&
122
    test_cmp expect actual &&
123

124
    printf "$sss$sss\n$sss\n\n" | git stripspace >actual &&
125
    test_cmp expect actual &&
126

127
    printf "\n$sss\n$sss$sss\n" | git stripspace >actual &&
128
    test_cmp expect actual &&
129

130
    printf "$sss$sss$sss$sss\n\n\n" | git stripspace >actual &&
131
    test_cmp expect actual &&
132

133
    printf "\n$sss$sss$sss$sss\n\n" | git stripspace >actual &&
134
    test_cmp expect actual &&
135

136
    printf "\n\n$sss$sss$sss$sss\n" | git stripspace >actual &&
137
    test_cmp expect actual
138
'
139

140 141
test_expect_success \
    '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 183 184 185
'

test_expect_success \
    'consecutive blank lines at the end should be removed' '
    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 226 227
'

test_expect_success \
    'text without newline at end should end with newline' '
228 229 230 231
    test $(printf "$ttt" | git stripspace | wc -l) -gt 0 &&
    test $(printf "$ttt$ttt" | git stripspace | wc -l) -gt 0 &&
    test $(printf "$ttt$ttt$ttt" | git stripspace | wc -l) -gt 0 &&
    test $(printf "$ttt$ttt$ttt$ttt" | git stripspace | wc -l) -gt 0
232 233 234 235 236 237
'

# text plus spaces at the end:

test_expect_success \
    'text plus spaces without newline at end should end with newline' '
238 239 240 241 242 243
    test $(printf "$ttt$sss" | git stripspace | wc -l) -gt 0 &&
    test $(printf "$ttt$ttt$sss" | git stripspace | wc -l) -gt 0 &&
    test $(printf "$ttt$ttt$ttt$sss" | git stripspace | wc -l) -gt 0 &&
    test $(printf "$ttt$sss$sss" | git stripspace | wc -l) -gt 0 &&
    test $(printf "$ttt$ttt$sss$sss" | git stripspace | wc -l) -gt 0 &&
    test $(printf "$ttt$sss$sss$sss" | git stripspace | wc -l) -gt 0
244 245
'

246
test_expect_success \
247
    'text plus spaces without newline at end should not show spaces' '
248 249 250 251 252 253
    ! (printf "$ttt$sss" | git stripspace | grep "  " >/dev/null) &&
    ! (printf "$ttt$ttt$sss" | git stripspace | grep "  " >/dev/null) &&
    ! (printf "$ttt$ttt$ttt$sss" | git stripspace | grep "  " >/dev/null) &&
    ! (printf "$ttt$sss$sss" | git stripspace | grep "  " >/dev/null) &&
    ! (printf "$ttt$ttt$sss$sss" | git stripspace | grep "  " >/dev/null) &&
    ! (printf "$ttt$sss$sss$sss" | git stripspace | grep "  " >/dev/null)
254 255 256 257 258
'

test_expect_success \
    'text plus spaces without newline should show the correct lines' '
    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
'

283
test_expect_success \
284
    'text plus spaces at end should not show spaces' '
285 286 287 288 289 290
    ! (echo "$ttt$sss" | git stripspace | grep "  " >/dev/null) &&
    ! (echo "$ttt$ttt$sss" | git stripspace | grep "  " >/dev/null) &&
    ! (echo "$ttt$ttt$ttt$sss" | git stripspace | grep "  " >/dev/null) &&
    ! (echo "$ttt$sss$sss" | git stripspace | grep "  " >/dev/null) &&
    ! (echo "$ttt$ttt$sss$sss" | git stripspace | grep "  " >/dev/null) &&
    ! (echo "$ttt$sss$sss$sss" | git stripspace | grep "  " >/dev/null)
291 292 293 294 295
'

test_expect_success \
    'text plus spaces at end should be cleaned and newline must remain' '
    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 321 322 323 324 325
'

# spaces only:

test_expect_success \
    'spaces with newline at end should be replaced with empty string' '
    printf "" >expect &&

326
    echo | git stripspace >actual &&
327
    test_cmp expect actual &&
328

329
    echo "$sss" | git stripspace >actual &&
330
    test_cmp expect actual &&
331

332
    echo "$sss$sss" | git stripspace >actual &&
333
    test_cmp expect actual &&
334

335
    echo "$sss$sss$sss" | git stripspace >actual &&
336
    test_cmp expect actual &&
337

338
    echo "$sss$sss$sss$sss" | git stripspace >actual &&
339
    test_cmp expect actual
340 341
'

342
test_expect_success \
343
    'spaces without newline at end should not show spaces' '
344 345 346 347 348
    ! (printf "" | git stripspace | grep " " >/dev/null) &&
    ! (printf "$sss" | git stripspace | grep " " >/dev/null) &&
    ! (printf "$sss$sss" | git stripspace | grep " " >/dev/null) &&
    ! (printf "$sss$sss$sss" | git stripspace | grep " " >/dev/null) &&
    ! (printf "$sss$sss$sss$sss" | git stripspace | grep " " >/dev/null)
349 350 351 352 353 354
'

test_expect_success \
    'spaces without newline at end should be replaced with empty string' '
    printf "" >expect &&

355
    printf "" | git stripspace >actual &&
356
    test_cmp expect actual &&
357

358
    printf "$sss$sss" | git stripspace >actual &&
359
    test_cmp expect actual &&
360

361
    printf "$sss$sss$sss" | git stripspace >actual &&
362
    test_cmp expect actual &&
363

364
    printf "$sss$sss$sss$sss" | git stripspace >actual &&
365
    test_cmp expect actual
366 367
'

368 369 370 371
test_expect_success \
    'consecutive text lines should be unchanged' '
    printf "$ttt$ttt\n$ttt\n" >expect &&
    printf "$ttt$ttt\n$ttt\n" | git stripspace >actual &&
372
    test_cmp expect actual &&
373 374 375

    printf "$ttt\n$ttt$ttt\n$ttt\n" >expect &&
    printf "$ttt\n$ttt$ttt\n$ttt\n" | git stripspace >actual &&
376
    test_cmp expect actual &&
377 378 379

    printf "$ttt\n$ttt\n$ttt\n$ttt$ttt\n" >expect &&
    printf "$ttt\n$ttt\n$ttt\n$ttt$ttt\n" | git stripspace >actual &&
380
    test_cmp expect actual &&
381 382 383

    printf "$ttt\n$ttt\n\n$ttt$ttt\n$ttt\n" >expect &&
    printf "$ttt\n$ttt\n\n$ttt$ttt\n$ttt\n" | git stripspace >actual &&
384
    test_cmp expect actual &&
385 386 387

    printf "$ttt$ttt\n\n$ttt\n$ttt$ttt\n" >expect &&
    printf "$ttt$ttt\n\n$ttt\n$ttt$ttt\n" | git stripspace >actual &&
388
    test_cmp expect actual &&
389 390 391

    printf "$ttt\n$ttt$ttt\n\n$ttt\n" >expect &&
    printf "$ttt\n$ttt$ttt\n\n$ttt\n" | git stripspace >actual &&
392
    test_cmp expect actual
393 394
'

395 396 397 398 399
test_expect_success 'strip comments, too' '
	test ! -z "$(echo "# comment" | git stripspace)" &&
	test -z "$(echo "# comment" | git stripspace -s)"
'

Junio C Hamano's avatar
Junio C Hamano committed
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
test_expect_success 'strip comments with changed comment char' '
	test ! -z "$(echo "; comment" | git -c core.commentchar=";" stripspace)" &&
	test -z "$(echo "; comment" | git -c core.commentchar=";" stripspace -s)"
'

test_expect_success '-c with single line' '
	printf "# foo\n" >expect &&
	printf "foo" | git stripspace -c >actual &&
	test_cmp expect actual
'

test_expect_success '-c with single line followed by empty line' '
	printf "# foo\n#\n" >expect &&
	printf "foo\n\n" | git stripspace -c >actual &&
	test_cmp expect actual
'

test_expect_success '-c with newline only' '
	printf "#\n" >expect &&
	printf "\n" | git stripspace -c >actual &&
	test_cmp expect actual
'

test_expect_success '--comment-lines with single line' '
	printf "# foo\n" >expect &&
	printf "foo" | git stripspace -c >actual &&
	test_cmp expect actual
'

test_expect_success '-c with changed comment char' '
	printf "; foo\n" >expect &&
	printf "foo" | git -c core.commentchar=";" stripspace -c >actual &&
	test_cmp expect actual
'

435
test_done