| 1 | /* eslint quote-props: ["off"] */ |
| 2 | import fs from "node:fs"; |
| 3 | import path from "node:path"; |
| 4 | const debug = (..._args) => {}; |
| 5 | import ignore from "../../src/ignore.ts"; |
| 6 | |
| 7 | function readPatterns(file) { |
| 8 | file = path.join(import.meta.dir, file); |
| 9 | return fs.readFileSync(file).toString(); |
| 10 | } |
| 11 | |
| 12 | const IS_WINDOWS = process.platform === "win32"; |
| 13 | const SHOULD_TEST_WINDOWS = IS_WINDOWS || !!process.env.IGNORE_TEST_WIN32; |
| 14 | |
| 15 | // Not Windows, but want to mimic tests on Windows |
| 16 | if (!IS_WINDOWS && !!process.env.IGNORE_TEST_WIN32) { |
| 17 | ignore[Symbol.for("setupWindows")](); |
| 18 | } |
| 19 | |
| 20 | const caseDefinitions = [ |
| 21 | ///////////////////////////////////////////////////////////////////// |
| 22 | // [ |
| 23 | // 'Example', |
| 24 | // [ |
| 25 | // // ignore pattern |
| 26 | // 'a' |
| 27 | // ], |
| 28 | // { |
| 29 | // // 1 indicates 'a' should be ignored |
| 30 | // 'a': 1 |
| 31 | // } |
| 32 | // ], |
| 33 | ///////////////////////////////////////////////////////////////////// |
| 34 | [ |
| 35 | "#148", |
| 36 | ["/.a/"], |
| 37 | { |
| 38 | ".a": 0, |
| 39 | }, |
| 40 | ], |
| 41 | [ |
| 42 | "#77: more cases for coverage", |
| 43 | ["/*"], |
| 44 | { |
| 45 | a: 1, |
| 46 | "a/": 1, |
| 47 | "a/b/": 1, |
| 48 | }, |
| 49 | ], |
| 50 | [ |
| 51 | "#77: directory ending with / not always correctly ignored", |
| 52 | ["c/*", { pattern: "foo/bar/*" }], |
| 53 | { |
| 54 | "c/": 1, |
| 55 | c: 0, |
| 56 | "foo/bar/": 1, |
| 57 | "foo/bar": 0, |
| 58 | }, |
| 59 | false, |
| 60 | false, |
| 61 | // Only for checkIgnore |
| 62 | ["checkIgnore"], |
| 63 | ], |
| 64 | [ |
| 65 | "#108: gitignore rules with BOM", |
| 66 | [readPatterns(".gitignore-with-BOM")], |
| 67 | { |
| 68 | node_modules: 1, |
| 69 | }, |
| 70 | ], |
| 71 | [ |
| 72 | "charactor ?", |
| 73 | ["foo?bar"], |
| 74 | { |
| 75 | "foo/bar": 0, |
| 76 | fooxbar: 1, |
| 77 | fooxxbar: 0, |
| 78 | }, |
| 79 | ], |
| 80 | [ |
| 81 | "#57, normal * and normal consecutive *", |
| 82 | ["**foo", "*bar", "ba*z", "folder/other-folder/**/**js"], |
| 83 | { |
| 84 | foo: 1, |
| 85 | "a/foo": 1, |
| 86 | afoo: 1, |
| 87 | abfoo: 1, |
| 88 | abcfoo: 1, |
| 89 | bar: 1, |
| 90 | abar: 1, |
| 91 | baz: 1, |
| 92 | "ba/z": 0, |
| 93 | baaaaaaz: 1, |
| 94 | "folder/other-folder/dir/main.js": 1, |
| 95 | }, |
| 96 | ], |
| 97 | [ |
| 98 | "#76 (invalid), comments with no heading whitespace", |
| 99 | ["node_modules# comments"], |
| 100 | { |
| 101 | "node_modules/a.js": 0, |
| 102 | }, |
| 103 | ], |
| 104 | [ |
| 105 | "#59 and more cases about range notation", |
| 106 | [ |
| 107 | "src/\\[foo\\]", // 1 -> 0 |
| 108 | |
| 109 | "src/\\[bar]", |
| 110 | |
| 111 | "src/[e\\\\]", |
| 112 | "s/[f\\\\\\\\]", |
| 113 | |
| 114 | "s/[a-z0-9]", |
| 115 | |
| 116 | // The following special cases are not described in gitignore manual |
| 117 | "src/[q", |
| 118 | "src/\\[u", |
| 119 | "src/[x\\]", |
| 120 | ], |
| 121 | { |
| 122 | "src/[foo]": 1, |
| 123 | |
| 124 | "src/[bar]": 1, |
| 125 | |
| 126 | "src/e": 1, |
| 127 | "s/f": 1, |
| 128 | |
| 129 | "s/a": 1, |
| 130 | |
| 131 | "s/0": 1, |
| 132 | |
| 133 | "src/[q": 0, |
| 134 | "src/[u": 1, |
| 135 | "src/[x": 0, |
| 136 | "src/[x]": 0, |
| 137 | "src/x": 0, |
| 138 | }, |
| 139 | ], |
| 140 | [ |
| 141 | "gitignore 2.22.1 example", |
| 142 | ["doc/frotz/"], |
| 143 | { |
| 144 | "doc/frotz/": 1, |
| 145 | "a/doc/frotz/": 0, |
| 146 | }, |
| 147 | ], |
| 148 | [ |
| 149 | "#56", |
| 150 | ["/*/", "!/foo/"], |
| 151 | { |
| 152 | "foo/bar.js": 0, |
| 153 | }, |
| 154 | ], |
| 155 | [ |
| 156 | "object prototype", |
| 157 | ["*", "!hasOwnProperty", "!a"], |
| 158 | { |
| 159 | hasOwnProperty: 0, |
| 160 | "a/hasOwnProperty": 0, |
| 161 | toString: 1, |
| 162 | "a/toString": 1, |
| 163 | }, |
| 164 | ], |
| 165 | [ |
| 166 | "a and a/", |
| 167 | ["a", "a2", "b/", "b2/"], |
| 168 | { |
| 169 | a: 1, |
| 170 | "a2/": 1, |
| 171 | b: 0, |
| 172 | "b2/": 1, |
| 173 | }, |
| 174 | ], |
| 175 | [ |
| 176 | "ending question mark", |
| 177 | ["*.web?"], |
| 178 | { |
| 179 | "a.webp": 1, |
| 180 | "a.webm": 1, |
| 181 | // only match one characters |
| 182 | "a.webam": 0, |
| 183 | "a.png": 0, |
| 184 | }, |
| 185 | ], |
| 186 | [ |
| 187 | "intermediate question mark", |
| 188 | ["a?c"], |
| 189 | { |
| 190 | abc: 1, |
| 191 | acc: 1, |
| 192 | ac: 0, |
| 193 | abbc: 0, |
| 194 | }, |
| 195 | ], |
| 196 | [ |
| 197 | "multiple question marks", |
| 198 | ["a?b??"], |
| 199 | { |
| 200 | acbdd: 1, |
| 201 | acbddd: 0, |
| 202 | }, |
| 203 | ], |
| 204 | [ |
| 205 | "normal *.[oa]", |
| 206 | ["*.[oa]"], |
| 207 | { |
| 208 | "a.js": 0, |
| 209 | "a.a": 1, |
| 210 | // test ending |
| 211 | "a.aa": 0, |
| 212 | "a.o": 1, |
| 213 | "a.0": 0, |
| 214 | }, |
| 215 | ], |
| 216 | [ |
| 217 | "multiple brackets", |
| 218 | ["*.[ab][cd][ef]"], |
| 219 | { |
| 220 | "a.ace": 1, |
| 221 | "a.bdf": 1, |
| 222 | "a.bce": 1, |
| 223 | "a.abc": 0, |
| 224 | "a.aceg": 0, |
| 225 | }, |
| 226 | ], |
| 227 | [ |
| 228 | "special case: []", |
| 229 | ["*.[]"], |
| 230 | { |
| 231 | "a.[]": 0, |
| 232 | "a.[]a": 0, |
| 233 | }, |
| 234 | ], |
| 235 | [ |
| 236 | "mixed with numbers, characters and symbols: *.[0a_]", |
| 237 | ["*.[0a_]"], |
| 238 | { |
| 239 | "a.0": 1, |
| 240 | "a.1": 0, |
| 241 | "a.a": 1, |
| 242 | "a.b": 0, |
| 243 | "a._": 1, |
| 244 | "a.=": 0, |
| 245 | }, |
| 246 | ], |
| 247 | [ |
| 248 | "range: [a-z]", |
| 249 | ["*.pn[a-z]"], |
| 250 | { |
| 251 | "a.pn1": 0, |
| 252 | "a.pn2": 0, |
| 253 | "a.png": 1, |
| 254 | "a.pna": 1, |
| 255 | }, |
| 256 | ], |
| 257 | [ |
| 258 | "range: [0-9]", |
| 259 | ["*.pn[0-9]"], |
| 260 | { |
| 261 | "a.pn1": 1, |
| 262 | "a.pn2": 1, |
| 263 | "a.png": 0, |
| 264 | "a.pna": 0, |
| 265 | }, |
| 266 | ], |
| 267 | [ |
| 268 | "multiple ranges: [0-9a-z]", |
| 269 | ["*.pn[0-9a-z]"], |
| 270 | { |
| 271 | "a.pn1": 1, |
| 272 | "a.pn2": 1, |
| 273 | "a.png": 1, |
| 274 | "a.pna": 1, |
| 275 | "a.pn-": 0, |
| 276 | }, |
| 277 | ], |
| 278 | [ |
| 279 | // [0-z] represents 0-0A-Za-z |
| 280 | "special range: [0-z]", |
| 281 | ["*.[0-z]"], |
| 282 | { |
| 283 | "a.0": 1, |
| 284 | "a.9": 1, |
| 285 | "a.00": 0, |
| 286 | "a.a": 1, |
| 287 | "a.z": 1, |
| 288 | "a.zz": 0, |
| 289 | }, |
| 290 | ], |
| 291 | [ |
| 292 | // If range is out of order, then omitted |
| 293 | "special case: range out of order: [a-9]", |
| 294 | ["*.[a-9]"], |
| 295 | { |
| 296 | "a.0": 0, |
| 297 | "a.-": 0, |
| 298 | "a.9": 0, |
| 299 | }, |
| 300 | ], |
| 301 | [ |
| 302 | // Just treat it as normal character set |
| 303 | "special case: range-like character set", |
| 304 | ["*.[a-]"], |
| 305 | { |
| 306 | "a.a": 1, |
| 307 | "a.-": 1, |
| 308 | "a.b": 0, |
| 309 | }, |
| 310 | ], |
| 311 | [ |
| 312 | "special case: the combination of range and set", |
| 313 | ["*.[a-z01]"], |
| 314 | { |
| 315 | "a.a": 1, |
| 316 | "a.b": 1, |
| 317 | "a.z": 1, |
| 318 | "a.0": 1, |
| 319 | "a.1": 1, |
| 320 | "a.2": 0, |
| 321 | }, |
| 322 | ], |
| 323 | [ |
| 324 | "special case: 1 step range", |
| 325 | ["*.[0-0]"], |
| 326 | { |
| 327 | "a.0": 1, |
| 328 | "a.1": 0, |
| 329 | "a.-": 0, |
| 330 | }, |
| 331 | ], |
| 332 | [ |
| 333 | "special case: similar, but not a character set", |
| 334 | ["*.[a-"], |
| 335 | { |
| 336 | "a.": 0, |
| 337 | "a.[": 0, |
| 338 | "a.a": 0, |
| 339 | "a.-": 0, |
| 340 | }, |
| 341 | ], |
| 342 | [ |
| 343 | "related to #38", |
| 344 | ["*", "!abc*"], |
| 345 | { |
| 346 | a: 1, |
| 347 | abc: 0, |
| 348 | abcd: 0, |
| 349 | }, |
| 350 | ], |
| 351 | [ |
| 352 | "#38", |
| 353 | ["*", "!*/", "!foo/bar"], |
| 354 | { |
| 355 | a: 1, |
| 356 | "b/c": 1, |
| 357 | "foo/bar": 0, |
| 358 | "foo/e": 1, |
| 359 | }, |
| 360 | ], |
| 361 | [ |
| 362 | 'intermediate "\\ " should be unescaped to " "', |
| 363 | ["abc\\ d", "abc e", "a\\ b\\ c"], |
| 364 | { |
| 365 | "abc d": 1, |
| 366 | "abc e": 1, |
| 367 | "abc/abc d": 1, |
| 368 | "abc/abc e": 1, |
| 369 | "abc/a b c": 1, |
| 370 | }, |
| 371 | ], |
| 372 | [ |
| 373 | "#25", |
| 374 | [".git/*", "!.git/config", ".ftpconfig"], |
| 375 | { |
| 376 | ".ftpconfig": 1, |
| 377 | ".git/config": 0, |
| 378 | ".git/description": 1, |
| 379 | }, |
| 380 | ], |
| 381 | [ |
| 382 | "#26: .gitignore man page sample", |
| 383 | [ |
| 384 | "# exclude everything except directory foo/bar", |
| 385 | "/*", |
| 386 | "!/foo", |
| 387 | "/foo/*", |
| 388 | "!/foo/bar", |
| 389 | ], |
| 390 | { |
| 391 | "no.js": 1, |
| 392 | "foo/no.js": 1, |
| 393 | "foo/bar/yes.js": 0, |
| 394 | "foo/bar/baz/yes.js": 0, |
| 395 | "boo/no.js": 1, |
| 396 | }, |
| 397 | ], |
| 398 | [ |
| 399 | "wildcard: special case, escaped wildcard", |
| 400 | ["*.html", "!a/b/\\*/index.html"], |
| 401 | { |
| 402 | "a/b/*/index.html": 0, |
| 403 | "a/b/index.html": 1, |
| 404 | }, |
| 405 | ], |
| 406 | [ |
| 407 | "wildcard: treated as a shell glob suitable for consumption by fnmatch(3)", |
| 408 | ["*.html", "!b/\\*/index.html"], |
| 409 | { |
| 410 | "a/b/*/index.html": 1, |
| 411 | "a/b/index.html": 1, |
| 412 | }, |
| 413 | ], |
| 414 | [ |
| 415 | "wildcard: with no escape", |
| 416 | ["*.html", "!a/b/*/index.html"], |
| 417 | { |
| 418 | "a/b/*/index.html": 0, |
| 419 | "a/b/index.html": 1, |
| 420 | }, |
| 421 | ], |
| 422 | [ |
| 423 | "#24: a negative pattern without a trailing wildcard", |
| 424 | ["/node_modules/*", "!/node_modules", "!/node_modules/package"], |
| 425 | { |
| 426 | "node_modules/a/a.js": 1, |
| 427 | "node_modules/package/a.js": 0, |
| 428 | }, |
| 429 | ], |
| 430 | [ |
| 431 | "#21: unignore with 1 globstar, reversed order", |
| 432 | ["!foo/bar.js", "foo/*"], |
| 433 | { |
| 434 | "foo/bar.js": 1, |
| 435 | "foo/bar2.js": 1, |
| 436 | "foo/bar/bar.js": 1, |
| 437 | }, |
| 438 | ], |
| 439 | |
| 440 | [ |
| 441 | "#21: unignore with 2 globstars, reversed order", |
| 442 | ["!foo/bar.js", "foo/**"], |
| 443 | { |
| 444 | "foo/bar.js": 1, |
| 445 | "foo/bar2.js": 1, |
| 446 | "foo/bar/bar.js": 1, |
| 447 | }, |
| 448 | ], |
| 449 | |
| 450 | [ |
| 451 | "#21: unignore with several groups of 2 globstars, reversed order", |
| 452 | ["!foo/bar.js", "foo/**/**"], |
| 453 | { |
| 454 | "foo/bar.js": 1, |
| 455 | "foo/bar2.js": 1, |
| 456 | "foo/bar/bar.js": 1, |
| 457 | }, |
| 458 | ], |
| 459 | |
| 460 | [ |
| 461 | "#21: unignore with 1 globstar", |
| 462 | ["foo/*", "!foo/bar.js"], |
| 463 | { |
| 464 | "foo/bar.js": 0, |
| 465 | "foo/bar2.js": 1, |
| 466 | "foo/bar/bar.js": 1, |
| 467 | }, |
| 468 | ], |
| 469 | |
| 470 | [ |
| 471 | "#21: unignore with 2 globstars", |
| 472 | ["foo/**", "!foo/bar.js"], |
| 473 | { |
| 474 | "foo/bar.js": 0, |
| 475 | "foo/bar2.js": 1, |
| 476 | "foo/bar/bar.js": 1, |
| 477 | }, |
| 478 | ], |
| 479 | |
| 480 | [ |
| 481 | "related to #21: several groups of 2 globstars", |
| 482 | ["foo/**/**", "!foo/bar.js"], |
| 483 | { |
| 484 | "foo/bar.js": 0, |
| 485 | "foo/bar2.js": 1, |
| 486 | "foo/bar/bar.js": 1, |
| 487 | }, |
| 488 | ], |
| 489 | |
| 490 | // description patterns paths/expect only |
| 491 | [ |
| 492 | "ignore dot files", |
| 493 | [".*"], |
| 494 | { |
| 495 | ".a": 1, |
| 496 | ".gitignore": 1, |
| 497 | }, |
| 498 | ], |
| 499 | |
| 500 | [ |
| 501 | "#14, README example broken in `3.0.3`", |
| 502 | [".abc/*", "!.abc/d/"], |
| 503 | { |
| 504 | ".abc/a.js": 1, |
| 505 | ".abc/d/e.js": 0, |
| 506 | }, |
| 507 | ], |
| 508 | |
| 509 | [ |
| 510 | "#14, README example broken in `3.0.3`, not negate parent folder", |
| 511 | [ |
| 512 | ".abc/*", |
| 513 | // .abc/d will be ignored |
| 514 | "!.abc/d/*", |
| 515 | ], |
| 516 | { |
| 517 | ".abc/a.js": 1, |
| 518 | // so '.abc/d/e.js' will be ignored |
| 519 | ".abc/d/e.js": 1, |
| 520 | }, |
| 521 | ], |
| 522 | |
| 523 | [ |
| 524 | "A blank line matches no files", |
| 525 | [""], |
| 526 | { |
| 527 | a: 0, |
| 528 | "a/b/c": 0, |
| 529 | }, |
| 530 | ], |
| 531 | [ |
| 532 | "A line starting with # serves as a comment.", |
| 533 | ["#abc"], |
| 534 | { |
| 535 | "#abc": 0, |
| 536 | }, |
| 537 | ], |
| 538 | [ |
| 539 | 'Put a backslash ("\\") in front of the first hash for patterns that begin with a hash.', |
| 540 | ["\\#abc"], |
| 541 | { |
| 542 | "#abc": 1, |
| 543 | }, |
| 544 | ], |
| 545 | [ |
| 546 | 'Trailing spaces are ignored unless they are quoted with backslash ("\\")', |
| 547 | [ |
| 548 | "abc\\ ", // only one space left -> (abc ) |
| 549 | "bcd ", // no space left -> (bcd) |
| 550 | "cde \\ ", // two spaces -> (cde ) |
| 551 | "def ", // no space left -> (def) |
| 552 | ], |
| 553 | { |
| 554 | // nothing to do with backslashes |
| 555 | "abc\\ ": 0, |
| 556 | "abc ": 0, |
| 557 | "abc ": 1, |
| 558 | "abc ": 0, |
| 559 | bcd: 1, |
| 560 | "bcd ": 0, |
| 561 | "bcd ": 0, |
| 562 | "cde ": 1, |
| 563 | "cde ": 0, |
| 564 | "cde ": 0, |
| 565 | def: 1, |
| 566 | "def ": 0, |
| 567 | }, |
| 568 | false, |
| 569 | true, |
| 570 | ], |
| 571 | [ |
| 572 | 'An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again', |
| 573 | ["abc", "!abc"], |
| 574 | { |
| 575 | // the parent folder is included again |
| 576 | "abc/a.js": 0, |
| 577 | "abc/": 0, |
| 578 | }, |
| 579 | ], |
| 580 | [ |
| 581 | "issue #10: It is not possible to re-include a file if a parent directory of that file is excluded", |
| 582 | ["/abc/", "!/abc/a.js"], |
| 583 | { |
| 584 | "abc/a.js": 1, |
| 585 | "abc/d/e.js": 1, |
| 586 | }, |
| 587 | ], |
| 588 | [ |
| 589 | "we did not know whether the rule is a dir first", |
| 590 | ["abc", "!bcd/abc/a.js"], |
| 591 | { |
| 592 | "abc/a.js": 1, |
| 593 | "bcd/abc/a.js": 1, |
| 594 | }, |
| 595 | ], |
| 596 | [ |
| 597 | 'Put a backslash ("\\") in front of the first "!" for patterns that begin with a literal "!"', |
| 598 | ["\\!abc", "\\!important!.txt"], |
| 599 | { |
| 600 | "!abc": 1, |
| 601 | abc: 0, |
| 602 | "b/!important!.txt": 1, |
| 603 | "!important!.txt": 1, |
| 604 | }, |
| 605 | ], |
| 606 | |
| 607 | [ |
| 608 | "If the pattern ends with a slash, it is removed for the purpose of the following description, but it would only find a match with a directory", |
| 609 | ["abc/"], |
| 610 | { |
| 611 | // actually, node-ignore have no idea about fs.Stat, |
| 612 | // you should `glob({mark: true})` |
| 613 | abc: 0, |
| 614 | "abc/": 1, |
| 615 | |
| 616 | // Actually, if there is only a trailing slash, git also treats it as a shell glob pattern |
| 617 | // 'abc/' should make 'bcd/abc/' ignored. |
| 618 | "bcd/abc/": 1, |
| 619 | }, |
| 620 | ], |
| 621 | |
| 622 | [ |
| 623 | "If the pattern does not contain a slash /, Git treats it as a shell glob pattern", |
| 624 | ["a.js", "f/"], |
| 625 | { |
| 626 | "a.js": 1, |
| 627 | "b/a/a.js": 1, |
| 628 | "a/a.js": 1, |
| 629 | "b/a.jsa": 0, |
| 630 | "f/": 1, |
| 631 | "g/f/": 1, |
| 632 | }, |
| 633 | ], |
| 634 | [ |
| 635 | "Otherwise, Git treats the pattern as a shell glob suitable for consumption by fnmatch(3) with the FNM_PATHNAME flag", |
| 636 | ["a/a.js"], |
| 637 | { |
| 638 | "a/a.js": 1, |
| 639 | "a/a.jsa": 0, |
| 640 | "b/a/a.js": 0, |
| 641 | "c/a/a.js": 0, |
| 642 | }, |
| 643 | ], |
| 644 | |
| 645 | [ |
| 646 | "wildcards in the pattern will not match a / in the pathname.", |
| 647 | ["Documentation/*.html"], |
| 648 | { |
| 649 | "Documentation/git.html": 1, |
| 650 | "Documentation/ppc/ppc.html": 0, |
| 651 | "tools/perf/Documentation/perf.html": 0, |
| 652 | }, |
| 653 | ], |
| 654 | |
| 655 | [ |
| 656 | "A leading slash matches the beginning of the pathname", |
| 657 | ["/*.c"], |
| 658 | { |
| 659 | "cat-file.c": 1, |
| 660 | "mozilla-sha1/sha1.c": 0, |
| 661 | }, |
| 662 | ], |
| 663 | |
| 664 | [ |
| 665 | 'A leading "**" followed by a slash means match in all directories', |
| 666 | ["**/foo"], |
| 667 | { |
| 668 | foo: 1, |
| 669 | "a/foo": 1, |
| 670 | "foo/a": 1, |
| 671 | "a/foo/a": 1, |
| 672 | "a/b/c/foo/a": 1, |
| 673 | }, |
| 674 | ], |
| 675 | |
| 676 | [ |
| 677 | '"**/foo/bar" matches file or directory "bar" anywhere that is directly under directory "foo"', |
| 678 | ["**/foo/bar"], |
| 679 | { |
| 680 | "foo/bar": 1, |
| 681 | "abc/foo/bar": 1, |
| 682 | "abc/foo/bar/": 1, |
| 683 | }, |
| 684 | ], |
| 685 | |
| 686 | [ |
| 687 | 'A trailing "/**" matches everything inside', |
| 688 | ["abc/**"], |
| 689 | { |
| 690 | "abc/a/": 1, |
| 691 | "abc/b": 1, |
| 692 | "abc/d/e/f/g": 1, |
| 693 | "bcd/abc/a": 0, |
| 694 | abc: 0, |
| 695 | }, |
| 696 | ], |
| 697 | |
| 698 | [ |
| 699 | "A slash followed by two consecutive asterisks then a slash matches zero or more directories", |
| 700 | ["a/**/b"], |
| 701 | { |
| 702 | "a/b": 1, |
| 703 | "a/x/b": 1, |
| 704 | "a/x/y/b": 1, |
| 705 | "b/a/b": 0, |
| 706 | }, |
| 707 | ], |
| 708 | |
| 709 | [ |
| 710 | "add a file content", |
| 711 | readPatterns(".aignore"), |
| 712 | { |
| 713 | "abc/a.js": 1, |
| 714 | "abc/b/b.js": 1, |
| 715 | "#e": 0, |
| 716 | "#f": 1, |
| 717 | }, |
| 718 | ], |
| 719 | |
| 720 | // old test cases |
| 721 | [ |
| 722 | "should excape metacharacters of regular expressions", |
| 723 | [ |
| 724 | "*.js", |
| 725 | "!\\*.js", |
| 726 | "!a#b.js", |
| 727 | "!?.js", |
| 728 | |
| 729 | // comments |
| 730 | "#abc", |
| 731 | |
| 732 | "\\#abc", |
| 733 | ], |
| 734 | { |
| 735 | "*.js": 0, |
| 736 | "abc.js": 1, |
| 737 | "a#b.js": 0, |
| 738 | abc: 0, |
| 739 | "#abc": 1, |
| 740 | "?.js": 0, |
| 741 | }, |
| 742 | ], |
| 743 | |
| 744 | [ |
| 745 | "issue #2: question mark should not break all things", |
| 746 | readPatterns(".ignore-issue-2"), |
| 747 | { |
| 748 | ".project": 1, |
| 749 | // remain |
| 750 | "abc/.project": 0, |
| 751 | ".a.sw": 0, |
| 752 | ".a.sw?": 1, |
| 753 | "thumbs.db": 1, |
| 754 | }, |
| 755 | ], |
| 756 | [ |
| 757 | 'dir ended with "*"', |
| 758 | ["abc/*"], |
| 759 | { |
| 760 | abc: 0, |
| 761 | }, |
| 762 | ], |
| 763 | [ |
| 764 | 'file ended with "*"', |
| 765 | ["abc.js*"], |
| 766 | { |
| 767 | "abc.js/": 1, |
| 768 | "abc.js/abc": 1, |
| 769 | "abc.jsa/": 1, |
| 770 | "abc.jsa/abc": 1, |
| 771 | }, |
| 772 | ], |
| 773 | [ |
| 774 | "wildcard as filename", |
| 775 | ["*.b"], |
| 776 | { |
| 777 | "b/a.b": 1, |
| 778 | "b/.b": 1, |
| 779 | "b/.ba": 0, |
| 780 | "b/c/a.b": 1, |
| 781 | }, |
| 782 | ], |
| 783 | [ |
| 784 | "slash at the beginning and come with a wildcard", |
| 785 | ["/*.c"], |
| 786 | { |
| 787 | ".c": 1, |
| 788 | "c.c": 1, |
| 789 | "c/c.c": 0, |
| 790 | "c/d": 0, |
| 791 | }, |
| 792 | ], |
| 793 | [ |
| 794 | "dot file", |
| 795 | [".d"], |
| 796 | { |
| 797 | ".d": 1, |
| 798 | ".dd": 0, |
| 799 | "d.d": 0, |
| 800 | "d/.d": 1, |
| 801 | "d/d.d": 0, |
| 802 | "d/e": 0, |
| 803 | }, |
| 804 | ], |
| 805 | [ |
| 806 | "dot dir", |
| 807 | [".e"], |
| 808 | { |
| 809 | ".e/": 1, |
| 810 | ".ee/": 0, |
| 811 | "e.e/": 0, |
| 812 | ".e/e": 1, |
| 813 | "e/.e": 1, |
| 814 | "e/e.e": 0, |
| 815 | "e/f": 0, |
| 816 | }, |
| 817 | ], |
| 818 | [ |
| 819 | "node modules: once", |
| 820 | ["node_modules/"], |
| 821 | { |
| 822 | "node_modules/gulp/node_modules/abc.md": 1, |
| 823 | "node_modules/gulp/node_modules/abc.json": 1, |
| 824 | }, |
| 825 | ], |
| 826 | [ |
| 827 | "node modules: sub directories", |
| 828 | ["node_modules"], |
| 829 | { |
| 830 | "a/b/node_modules/abc.md": 1, |
| 831 | }, |
| 832 | ], |
| 833 | [ |
| 834 | "node modules: twice", |
| 835 | ["node_modules/", "node_modules/"], |
| 836 | { |
| 837 | "node_modules/gulp/node_modules/abc.md": 1, |
| 838 | "node_modules/gulp/node_modules/abc.json": 1, |
| 839 | }, |
| 840 | ], |
| 841 | [ |
| 842 | "unicode characters in windows paths", |
| 843 | ["test"], |
| 844 | { |
| 845 | "some/path/to/test/ignored.js": 1, |
| 846 | "some/special/path/to/目录/test/ignored.js": 1, |
| 847 | }, |
| 848 | false, |
| 849 | true, // git-check-ignore fails as git converts special chars to escaped unicode before printing |
| 850 | ], |
| 851 | ]; |
| 852 | |
| 853 | if (!SHOULD_TEST_WINDOWS) { |
| 854 | caseDefinitions.push( |
| 855 | [ |
| 856 | "#68: ignore test for files named ...", |
| 857 | ["/...", "/....."], |
| 858 | { |
| 859 | "...": 1, |
| 860 | "....": 0, |
| 861 | ".....": 1, |
| 862 | "......": 0, |
| 863 | }, |
| 864 | ], |
| 865 | [ |
| 866 | "#68: file named ...", |
| 867 | [], |
| 868 | { |
| 869 | "...": 0, |
| 870 | "....": 0, |
| 871 | ".....": 0, |
| 872 | }, |
| 873 | ], |
| 874 | [ |
| 875 | "#130: consequent escaped backslashes with whitespaces", |
| 876 | ["a\\\\ ", "a\\\\ b", "a\\\\\\ b"], |
| 877 | { |
| 878 | "a\\": 1, |
| 879 | "a\\ b": 1, |
| 880 | "a\\\\ b": 0, |
| 881 | "a\\\\\\ b": 0, |
| 882 | }, |
| 883 | ], |
| 884 | [ |
| 885 | "#81: invalid trailing backslash at the end should not throw, test non-windows env only", |
| 886 | [ |
| 887 | "test\\", |
| 888 | "testa\\\\", |
| 889 | "\\", |
| 890 | "foo/*", |
| 891 | // test negative pattern |
| 892 | "!foo/test\\", |
| 893 | ], |
| 894 | { |
| 895 | test: 0, |
| 896 | "test\\": 0, |
| 897 | "testa\\": 1, |
| 898 | "\\": 0, |
| 899 | "foo/test\\": 1, |
| 900 | }, |
| 901 | ], |
| 902 | [ |
| 903 | "linux: back slashes on paths", |
| 904 | ["a", "b\\\\c"], |
| 905 | { |
| 906 | "b\\c/a.md": 1, |
| 907 | "a\\b/a.js": 0, |
| 908 | "a\\b/a": 1, |
| 909 | "a/a.js": 1, |
| 910 | }, |
| 911 | ], |
| 912 | [ |
| 913 | "#59: test cases for linux only", |
| 914 | [ |
| 915 | "src/\\[foo\\]", // 1 -> 0 |
| 916 | "src/\\[foo2\\\\]", // 2 -> 1 |
| 917 | "src/\\[foo3\\\\\\]", // 3 -> 1 |
| 918 | "src/\\[foo4\\\\\\\\]", // 4 -> 2 |
| 919 | "src/\\[foo5\\\\\\\\\\]", // 5 -> 2 |
| 920 | "src/\\[foo6\\\\\\\\\\\\]", // 6 -> 3 |
| 921 | |
| 922 | "src/\\[bar]", |
| 923 | |
| 924 | "src/[e\\\\]", |
| 925 | "s/[f\\\\\\\\]", |
| 926 | |
| 927 | "s/[a-z0-9]", |
| 928 | ], |
| 929 | { |
| 930 | "src/[foo]": 1, |
| 931 | "src/[foo2\\]": 1, |
| 932 | |
| 933 | // Seems the followings are side-effects, |
| 934 | // however, we will implement these |
| 935 | "src/[foo3\\]": 1, |
| 936 | "src/[foo4\\\\]": 1, |
| 937 | "src/[foo5\\\\]": 1, |
| 938 | "src/[foo6\\\\\\]": 1, |
| 939 | |
| 940 | "src/[bar]": 1, |
| 941 | |
| 942 | "src/e": 1, |
| 943 | "src/\\": 1, |
| 944 | "s/f": 1, |
| 945 | "s/\\": 1, |
| 946 | |
| 947 | "s/a": 1, |
| 948 | |
| 949 | "s/0": 1, |
| 950 | }, |
| 951 | ], |
| 952 | ); |
| 953 | } |
| 954 | |
| 955 | const cases_to_test_only = caseDefinitions.filter((c) => c[3]); |
| 956 | |
| 957 | const real_cases = cases_to_test_only.length |
| 958 | ? cases_to_test_only |
| 959 | : caseDefinitions; |
| 960 | |
| 961 | export const cases = (iteratee) => { |
| 962 | real_cases.forEach((single) => { |
| 963 | const [ |
| 964 | description, |
| 965 | patterns, |
| 966 | paths_object, |
| 967 | test_only, |
| 968 | skip_test_fixture, |
| 969 | scopes = false, |
| 970 | ] = single; |
| 971 | |
| 972 | // All paths to test |
| 973 | const paths = Object.keys(paths_object); |
| 974 | |
| 975 | // paths that NOT ignored |
| 976 | let expected = paths.filter((p) => !paths_object[p]).sort(); |
| 977 | |
| 978 | function expect_result(t, result, mapper) { |
| 979 | if (mapper) { |
| 980 | expected = expected.map(mapper); |
| 981 | } |
| 982 | |
| 983 | t.same(result.sort(), expected.sort()); |
| 984 | } |
| 985 | |
| 986 | iteratee({ |
| 987 | description, |
| 988 | patterns, |
| 989 | paths_object, |
| 990 | test_only, |
| 991 | skip_test_fixture, |
| 992 | paths, |
| 993 | scopes, |
| 994 | expected, |
| 995 | expect_result, |
| 996 | }); |
| 997 | }); |
| 998 | }; |
| 999 | |
| 1000 | // For local testing purpose |
| 1001 | const ENV_KEYS = [ |
| 1002 | "IGNORE_ONLY_FILTER", |
| 1003 | "IGNORE_ONLY_CREATE_FILTER", |
| 1004 | "IGNORE_ONLY_IGNORES", |
| 1005 | "IGNORE_ONLY_CHECK_IGNORE", |
| 1006 | "IGNORE_ONLY_WIN32", |
| 1007 | "IGNORE_ONLY_FIXTURES", |
| 1008 | "IGNORE_ONLY_OTHERS", |
| 1009 | ]; |
| 1010 | |
| 1011 | const envs = {}; |
| 1012 | let hasOnly = false; |
| 1013 | ENV_KEYS.forEach((key) => { |
| 1014 | const value = !!process.env[key]; |
| 1015 | envs[key] = value; |
| 1016 | |
| 1017 | if (value) { |
| 1018 | hasOnly = true; |
| 1019 | } |
| 1020 | }); |
| 1021 | |
| 1022 | export const checkEnv = (key) => (hasOnly ? !!envs[key] : true); |
| 1023 | |
| 1024 | export { IS_WINDOWS }; |
| 1025 | export { SHOULD_TEST_WINDOWS }; |
| 1026 | |
| 1027 | export { debug }; |