Git fork
at reftables-rust 469 lines 14 kB view raw
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 15printf_git_stripspace () { 16 printf "$1" | git stripspace 17} 18 19test_expect_success 'long lines without spaces should be unchanged' ' 20 echo "$ttt" >expect && 21 git stripspace <expect >actual && 22 test_cmp expect actual && 23 24 echo "$ttt$ttt" >expect && 25 git stripspace <expect >actual && 26 test_cmp expect actual && 27 28 echo "$ttt$ttt$ttt" >expect && 29 git stripspace <expect >actual && 30 test_cmp expect actual && 31 32 echo "$ttt$ttt$ttt$ttt" >expect && 33 git stripspace <expect >actual && 34 test_cmp expect actual 35' 36 37test_expect_success 'lines with spaces at the beginning should be unchanged' ' 38 echo "$sss$ttt" >expect && 39 git stripspace <expect >actual && 40 test_cmp expect actual && 41 42 echo "$sss$sss$ttt" >expect && 43 git stripspace <expect >actual && 44 test_cmp expect actual && 45 46 echo "$sss$sss$sss$ttt" >expect && 47 git stripspace <expect >actual && 48 test_cmp expect actual 49' 50 51test_expect_success '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 '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 'only consecutive blank lines should be completely removed' ' 112 printf "\n" | git stripspace >actual && 113 test_must_be_empty actual && 114 115 printf "\n\n\n" | git stripspace >actual && 116 test_must_be_empty actual && 117 118 printf "$sss\n$sss\n$sss\n" | git stripspace >actual && 119 test_must_be_empty actual && 120 121 printf "$sss$sss\n$sss\n\n" | git stripspace >actual && 122 test_must_be_empty actual && 123 124 printf "\n$sss\n$sss$sss\n" | git stripspace >actual && 125 test_must_be_empty actual && 126 127 printf "$sss$sss$sss$sss\n\n\n" | git stripspace >actual && 128 test_must_be_empty actual && 129 130 printf "\n$sss$sss$sss$sss\n\n" | git stripspace >actual && 131 test_must_be_empty actual && 132 133 printf "\n\n$sss$sss$sss$sss\n" | git stripspace >actual && 134 test_must_be_empty actual 135' 136 137test_expect_success 'consecutive blank lines at the beginning should be removed' ' 138 printf "$ttt\n" > expect && 139 printf "\n$ttt\n" | git stripspace >actual && 140 test_cmp expect actual && 141 142 printf "$ttt\n" > expect && 143 printf "\n\n\n$ttt\n" | git stripspace >actual && 144 test_cmp expect actual && 145 146 printf "$ttt$ttt\n" > expect && 147 printf "\n\n\n$ttt$ttt\n" | git stripspace >actual && 148 test_cmp expect actual && 149 150 printf "$ttt$ttt$ttt\n" > expect && 151 printf "\n\n\n$ttt$ttt$ttt\n" | git stripspace >actual && 152 test_cmp expect actual && 153 154 printf "$ttt$ttt$ttt$ttt\n" > expect && 155 printf "\n\n\n$ttt$ttt$ttt$ttt\n" | git stripspace >actual && 156 test_cmp expect actual && 157 158 printf "$ttt\n" > expect && 159 160 printf "$sss\n$sss\n$sss\n$ttt\n" | git stripspace >actual && 161 test_cmp expect actual && 162 163 printf "\n$sss\n$sss$sss\n$ttt\n" | git stripspace >actual && 164 test_cmp expect actual && 165 166 printf "$sss$sss\n$sss\n\n$ttt\n" | git stripspace >actual && 167 test_cmp expect actual && 168 169 printf "$sss$sss$sss\n\n\n$ttt\n" | git stripspace >actual && 170 test_cmp expect actual && 171 172 printf "\n$sss$sss$sss\n\n$ttt\n" | git stripspace >actual && 173 test_cmp expect actual && 174 175 printf "\n\n$sss$sss$sss\n$ttt\n" | git stripspace >actual && 176 test_cmp expect actual 177' 178 179test_expect_success 'consecutive blank lines at the end should be removed' ' 180 printf "$ttt\n" > expect && 181 printf "$ttt\n\n" | git stripspace >actual && 182 test_cmp expect actual && 183 184 printf "$ttt\n" > expect && 185 printf "$ttt\n\n\n\n" | git stripspace >actual && 186 test_cmp expect actual && 187 188 printf "$ttt$ttt\n" > expect && 189 printf "$ttt$ttt\n\n\n\n" | git stripspace >actual && 190 test_cmp expect actual && 191 192 printf "$ttt$ttt$ttt\n" > expect && 193 printf "$ttt$ttt$ttt\n\n\n\n" | git stripspace >actual && 194 test_cmp expect actual && 195 196 printf "$ttt$ttt$ttt$ttt\n" > expect && 197 printf "$ttt$ttt$ttt$ttt\n\n\n\n" | git stripspace >actual && 198 test_cmp expect actual && 199 200 printf "$ttt\n" > expect && 201 202 printf "$ttt\n$sss\n$sss\n$sss\n" | git stripspace >actual && 203 test_cmp expect actual && 204 205 printf "$ttt\n\n$sss\n$sss$sss\n" | git stripspace >actual && 206 test_cmp expect actual && 207 208 printf "$ttt\n$sss$sss\n$sss\n\n" | git stripspace >actual && 209 test_cmp expect actual && 210 211 printf "$ttt\n$sss$sss$sss\n\n\n" | git stripspace >actual && 212 test_cmp expect actual && 213 214 printf "$ttt\n\n$sss$sss$sss\n\n" | git stripspace >actual && 215 test_cmp expect actual && 216 217 printf "$ttt\n\n\n$sss$sss$sss\n" | git stripspace >actual && 218 test_cmp expect actual 219' 220 221test_expect_success 'text without newline at end should end with newline' ' 222 test_stdout_line_count -gt 0 printf_git_stripspace "$ttt" && 223 test_stdout_line_count -gt 0 printf_git_stripspace "$ttt$ttt" && 224 test_stdout_line_count -gt 0 printf_git_stripspace "$ttt$ttt$ttt" && 225 test_stdout_line_count -gt 0 printf_git_stripspace "$ttt$ttt$ttt$ttt" 226' 227 228# text plus spaces at the end: 229 230test_expect_success 'text plus spaces without newline at end should end with newline' ' 231 test_stdout_line_count -gt 0 printf_git_stripspace "$ttt$sss" && 232 test_stdout_line_count -gt 0 printf_git_stripspace "$ttt$ttt$sss" && 233 test_stdout_line_count -gt 0 printf_git_stripspace "$ttt$ttt$ttt$sss" && 234 test_stdout_line_count -gt 0 printf_git_stripspace "$ttt$sss$sss" && 235 test_stdout_line_count -gt 0 printf_git_stripspace "$ttt$ttt$sss$sss" && 236 test_stdout_line_count -gt 0 printf_git_stripspace "$ttt$sss$sss$sss" 237' 238 239test_expect_success 'text plus spaces without newline at end should not show spaces' ' 240 printf "$ttt$sss" | git stripspace >tmp && 241 ! grep " " tmp >/dev/null && 242 printf "$ttt$ttt$sss" | git stripspace >tmp && 243 ! grep " " tmp >/dev/null && 244 printf "$ttt$ttt$ttt$sss" | git stripspace >tmp && 245 ! grep " " tmp >/dev/null && 246 printf "$ttt$sss$sss" | git stripspace >tmp && 247 ! grep " " tmp >/dev/null && 248 printf "$ttt$ttt$sss$sss" | git stripspace >tmp && 249 ! grep " " tmp >/dev/null && 250 printf "$ttt$sss$sss$sss" | git stripspace >tmp && 251 ! grep " " tmp >/dev/null 252' 253 254test_expect_success 'text plus spaces without newline should show the correct lines' ' 255 printf "$ttt\n" >expect && 256 printf "$ttt$sss" | git stripspace >actual && 257 test_cmp expect actual && 258 259 printf "$ttt\n" >expect && 260 printf "$ttt$sss$sss" | git stripspace >actual && 261 test_cmp expect actual && 262 263 printf "$ttt\n" >expect && 264 printf "$ttt$sss$sss$sss" | git stripspace >actual && 265 test_cmp expect actual && 266 267 printf "$ttt$ttt\n" >expect && 268 printf "$ttt$ttt$sss" | git stripspace >actual && 269 test_cmp expect actual && 270 271 printf "$ttt$ttt\n" >expect && 272 printf "$ttt$ttt$sss$sss" | git stripspace >actual && 273 test_cmp expect actual && 274 275 printf "$ttt$ttt$ttt\n" >expect && 276 printf "$ttt$ttt$ttt$sss" | git stripspace >actual && 277 test_cmp expect actual 278' 279 280test_expect_success 'text plus spaces at end should not show spaces' ' 281 echo "$ttt$sss" | git stripspace >tmp && 282 ! grep " " tmp >/dev/null && 283 echo "$ttt$ttt$sss" | git stripspace >tmp && 284 ! grep " " tmp >/dev/null && 285 echo "$ttt$ttt$ttt$sss" | git stripspace >tmp && 286 ! grep " " tmp >/dev/null && 287 echo "$ttt$sss$sss" | git stripspace >tmp && 288 ! grep " " tmp >/dev/null && 289 echo "$ttt$ttt$sss$sss" | git stripspace >tmp && 290 ! grep " " tmp >/dev/null && 291 echo "$ttt$sss$sss$sss" | git stripspace >tmp && 292 ! grep " " tmp >/dev/null 293' 294 295test_expect_success 'text plus spaces at end should be cleaned and newline must remain' ' 296 echo "$ttt" >expect && 297 echo "$ttt$sss" | git stripspace >actual && 298 test_cmp expect actual && 299 300 echo "$ttt" >expect && 301 echo "$ttt$sss$sss" | git stripspace >actual && 302 test_cmp expect actual && 303 304 echo "$ttt" >expect && 305 echo "$ttt$sss$sss$sss" | git stripspace >actual && 306 test_cmp expect actual && 307 308 echo "$ttt$ttt" >expect && 309 echo "$ttt$ttt$sss" | git stripspace >actual && 310 test_cmp expect actual && 311 312 echo "$ttt$ttt" >expect && 313 echo "$ttt$ttt$sss$sss" | git stripspace >actual && 314 test_cmp expect actual && 315 316 echo "$ttt$ttt$ttt" >expect && 317 echo "$ttt$ttt$ttt$sss" | git stripspace >actual && 318 test_cmp expect actual 319' 320 321# spaces only: 322 323test_expect_success '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 'spaces without newline at end should not show spaces' ' 341 printf "" | git stripspace >tmp && 342 ! grep " " tmp >/dev/null && 343 printf "$sss" | git stripspace >tmp && 344 ! grep " " tmp >/dev/null && 345 printf "$sss$sss" | git stripspace >tmp && 346 ! grep " " tmp >/dev/null && 347 printf "$sss$sss$sss" | git stripspace >tmp && 348 ! grep " " tmp >/dev/null && 349 printf "$sss$sss$sss$sss" | git stripspace >tmp && 350 ! grep " " tmp >/dev/null 351' 352 353test_expect_success 'spaces without newline at end should be replaced with empty string' ' 354 printf "" | git stripspace >actual && 355 test_must_be_empty actual && 356 357 printf "$sss$sss" | git stripspace >actual && 358 test_must_be_empty actual && 359 360 printf "$sss$sss$sss" | git stripspace >actual && 361 test_must_be_empty actual && 362 363 printf "$sss$sss$sss$sss" | git stripspace >actual && 364 test_must_be_empty actual 365' 366 367test_expect_success 'consecutive text lines should be unchanged' ' 368 printf "$ttt$ttt\n$ttt\n" >expect && 369 printf "$ttt$ttt\n$ttt\n" | git stripspace >actual && 370 test_cmp expect actual && 371 372 printf "$ttt\n$ttt$ttt\n$ttt\n" >expect && 373 printf "$ttt\n$ttt$ttt\n$ttt\n" | git stripspace >actual && 374 test_cmp expect actual && 375 376 printf "$ttt\n$ttt\n$ttt\n$ttt$ttt\n" >expect && 377 printf "$ttt\n$ttt\n$ttt\n$ttt$ttt\n" | git stripspace >actual && 378 test_cmp expect actual && 379 380 printf "$ttt\n$ttt\n\n$ttt$ttt\n$ttt\n" >expect && 381 printf "$ttt\n$ttt\n\n$ttt$ttt\n$ttt\n" | git stripspace >actual && 382 test_cmp expect actual && 383 384 printf "$ttt$ttt\n\n$ttt\n$ttt$ttt\n" >expect && 385 printf "$ttt$ttt\n\n$ttt\n$ttt$ttt\n" | git stripspace >actual && 386 test_cmp expect actual && 387 388 printf "$ttt\n$ttt$ttt\n\n$ttt\n" >expect && 389 printf "$ttt\n$ttt$ttt\n\n$ttt\n" | git stripspace >actual && 390 test_cmp expect actual 391' 392 393test_expect_success 'strip comments, too' ' 394 test ! -z "$(echo "# comment" | git stripspace)" && 395 test -z "$(echo "# comment" | git stripspace -s)" 396' 397 398test_expect_success 'strip comments with changed comment char' ' 399 test ! -z "$(echo "; comment" | git -c core.commentchar=";" stripspace)" && 400 test -z "$(echo "; comment" | git -c core.commentchar=";" stripspace -s)" 401' 402 403test_expect_success 'strip comments with changed comment string' ' 404 test ! -z "$(echo "// comment" | git -c core.commentchar=// stripspace)" && 405 test -z "$(echo "// comment" | git -c core.commentchar="//" stripspace -s)" 406' 407 408test_expect_success 'newline as commentchar is forbidden' ' 409 test_must_fail git -c core.commentChar="$LF" stripspace -s 2>err && 410 grep "core.commentchar cannot contain newline" err 411' 412 413test_expect_success 'empty commentchar is forbidden' ' 414 test_must_fail git -c core.commentchar= stripspace -s 2>err && 415 grep "core.commentchar must have at least one character" err 416' 417 418test_expect_success '-c 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 single line followed by empty line' ' 425 printf "# foo\n#\n" >expect && 426 printf "foo\n\n" | git stripspace -c >actual && 427 test_cmp expect actual 428' 429 430test_expect_success '-c with newline only' ' 431 printf "#\n" >expect && 432 printf "\n" | git stripspace -c >actual && 433 test_cmp expect actual 434' 435 436test_expect_success '--comment-lines with single line' ' 437 printf "# foo\n" >expect && 438 printf "foo" | git stripspace -c >actual && 439 test_cmp expect actual 440' 441 442test_expect_success '-c with changed comment char' ' 443 printf "; foo\n" >expect && 444 printf "foo" | git -c core.commentchar=";" stripspace -c >actual && 445 test_cmp expect actual 446' 447 448test_expect_success '-c with comment char defined in .git/config' ' 449 test_config core.commentchar = && 450 printf "= foo\n" >expect && 451 rm -fr sub && 452 mkdir sub && 453 printf "foo" | git -C sub stripspace -c >actual && 454 test_cmp expect actual 455' 456 457test_expect_success '-c outside git repository' ' 458 printf "# foo\n" >expect && 459 printf "foo" | nongit git stripspace -c >actual && 460 test_cmp expect actual 461' 462 463test_expect_success 'avoid SP-HT sequence in commented line' ' 464 printf "#\tone\n#\n# two\n" >expect && 465 printf "\tone\n\ntwo\n" | git stripspace -c >actual && 466 test_cmp expect actual 467' 468 469test_done