The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 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 | ["# exclude everything except directory foo/bar", "/*", "!/foo", "/foo/*", "!/foo/bar"], |
| 384 | { |
| 385 | "no.js": 1, |
| 386 | "foo/no.js": 1, |
| 387 | "foo/bar/yes.js": 0, |
| 388 | "foo/bar/baz/yes.js": 0, |
| 389 | "boo/no.js": 1, |
| 390 | }, |
| 391 | ], |
| 392 | [ |
| 393 | "wildcard: special case, escaped wildcard", |
| 394 | ["*.html", "!a/b/\\*/index.html"], |
| 395 | { |
| 396 | "a/b/*/index.html": 0, |
| 397 | "a/b/index.html": 1, |
| 398 | }, |
| 399 | ], |
| 400 | [ |
| 401 | "wildcard: treated as a shell glob suitable for consumption by fnmatch(3)", |
| 402 | ["*.html", "!b/\\*/index.html"], |
| 403 | { |
| 404 | "a/b/*/index.html": 1, |
| 405 | "a/b/index.html": 1, |
| 406 | }, |
| 407 | ], |
| 408 | [ |
| 409 | "wildcard: with no escape", |
| 410 | ["*.html", "!a/b/*/index.html"], |
| 411 | { |
| 412 | "a/b/*/index.html": 0, |
| 413 | "a/b/index.html": 1, |
| 414 | }, |
| 415 | ], |
| 416 | [ |
| 417 | "#24: a negative pattern without a trailing wildcard", |
| 418 | ["/node_modules/*", "!/node_modules", "!/node_modules/package"], |
| 419 | { |
| 420 | "node_modules/a/a.js": 1, |
| 421 | "node_modules/package/a.js": 0, |
| 422 | }, |
| 423 | ], |
| 424 | [ |
| 425 | "#21: unignore with 1 globstar, reversed order", |
| 426 | ["!foo/bar.js", "foo/*"], |
| 427 | { |
| 428 | "foo/bar.js": 1, |
| 429 | "foo/bar2.js": 1, |
| 430 | "foo/bar/bar.js": 1, |
| 431 | }, |
| 432 | ], |
| 433 | |
| 434 | [ |
| 435 | "#21: unignore with 2 globstars, reversed order", |
| 436 | ["!foo/bar.js", "foo/**"], |
| 437 | { |
| 438 | "foo/bar.js": 1, |
| 439 | "foo/bar2.js": 1, |
| 440 | "foo/bar/bar.js": 1, |
| 441 | }, |
| 442 | ], |
| 443 | |
| 444 | [ |
| 445 | "#21: unignore with several groups of 2 globstars, reversed order", |
| 446 | ["!foo/bar.js", "foo/**/**"], |
| 447 | { |
| 448 | "foo/bar.js": 1, |
| 449 | "foo/bar2.js": 1, |
| 450 | "foo/bar/bar.js": 1, |
| 451 | }, |
| 452 | ], |
| 453 | |
| 454 | [ |
| 455 | "#21: unignore with 1 globstar", |
| 456 | ["foo/*", "!foo/bar.js"], |
| 457 | { |
| 458 | "foo/bar.js": 0, |
| 459 | "foo/bar2.js": 1, |
| 460 | "foo/bar/bar.js": 1, |
| 461 | }, |
| 462 | ], |
| 463 | |
| 464 | [ |
| 465 | "#21: unignore with 2 globstars", |
| 466 | ["foo/**", "!foo/bar.js"], |
| 467 | { |
| 468 | "foo/bar.js": 0, |
| 469 | "foo/bar2.js": 1, |
| 470 | "foo/bar/bar.js": 1, |
| 471 | }, |
| 472 | ], |
| 473 | |
| 474 | [ |
| 475 | "related to #21: several groups of 2 globstars", |
| 476 | ["foo/**/**", "!foo/bar.js"], |
| 477 | { |
| 478 | "foo/bar.js": 0, |
| 479 | "foo/bar2.js": 1, |
| 480 | "foo/bar/bar.js": 1, |
| 481 | }, |
| 482 | ], |
| 483 | |
| 484 | // description patterns paths/expect only |
| 485 | [ |
| 486 | "ignore dot files", |
| 487 | [".*"], |
| 488 | { |
| 489 | ".a": 1, |
| 490 | ".gitignore": 1, |
| 491 | }, |
| 492 | ], |
| 493 | |
| 494 | [ |
| 495 | "#14, README example broken in `3.0.3`", |
| 496 | [".abc/*", "!.abc/d/"], |
| 497 | { |
| 498 | ".abc/a.js": 1, |
| 499 | ".abc/d/e.js": 0, |
| 500 | }, |
| 501 | ], |
| 502 | |
| 503 | [ |
| 504 | "#14, README example broken in `3.0.3`, not negate parent folder", |
| 505 | [ |
| 506 | ".abc/*", |
| 507 | // .abc/d will be ignored |
| 508 | "!.abc/d/*", |
| 509 | ], |
| 510 | { |
| 511 | ".abc/a.js": 1, |
| 512 | // so '.abc/d/e.js' will be ignored |
| 513 | ".abc/d/e.js": 1, |
| 514 | }, |
| 515 | ], |
| 516 | |
| 517 | [ |
| 518 | "A blank line matches no files", |
| 519 | [""], |
| 520 | { |
| 521 | a: 0, |
| 522 | "a/b/c": 0, |
| 523 | }, |
| 524 | ], |
| 525 | [ |
| 526 | "A line starting with # serves as a comment.", |
| 527 | ["#abc"], |
| 528 | { |
| 529 | "#abc": 0, |
| 530 | }, |
| 531 | ], |
| 532 | [ |
| 533 | 'Put a backslash ("\\") in front of the first hash for patterns that begin with a hash.', |
| 534 | ["\\#abc"], |
| 535 | { |
| 536 | "#abc": 1, |
| 537 | }, |
| 538 | ], |
| 539 | [ |
| 540 | 'Trailing spaces are ignored unless they are quoted with backslash ("\\")', |
| 541 | [ |
| 542 | "abc\\ ", // only one space left -> (abc ) |
| 543 | "bcd ", // no space left -> (bcd) |
| 544 | "cde \\ ", // two spaces -> (cde ) |
| 545 | "def ", // no space left -> (def) |
| 546 | ], |
| 547 | { |
| 548 | // nothing to do with backslashes |
| 549 | "abc\\ ": 0, |
| 550 | "abc ": 0, |
| 551 | "abc ": 1, |
| 552 | "abc ": 0, |
| 553 | bcd: 1, |
| 554 | "bcd ": 0, |
| 555 | "bcd ": 0, |
| 556 | "cde ": 1, |
| 557 | "cde ": 0, |
| 558 | "cde ": 0, |
| 559 | def: 1, |
| 560 | "def ": 0, |
| 561 | }, |
| 562 | false, |
| 563 | true, |
| 564 | ], |
| 565 | [ |
| 566 | 'An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again', |
| 567 | ["abc", "!abc"], |
| 568 | { |
| 569 | // the parent folder is included again |
| 570 | "abc/a.js": 0, |
| 571 | "abc/": 0, |
| 572 | }, |
| 573 | ], |
| 574 | [ |
| 575 | "issue #10: It is not possible to re-include a file if a parent directory of that file is excluded", |
| 576 | ["/abc/", "!/abc/a.js"], |
| 577 | { |
| 578 | "abc/a.js": 1, |
| 579 | "abc/d/e.js": 1, |
| 580 | }, |
| 581 | ], |
| 582 | [ |
| 583 | "we did not know whether the rule is a dir first", |
| 584 | ["abc", "!bcd/abc/a.js"], |
| 585 | { |
| 586 | "abc/a.js": 1, |
| 587 | "bcd/abc/a.js": 1, |
| 588 | }, |
| 589 | ], |
| 590 | [ |
| 591 | 'Put a backslash ("\\") in front of the first "!" for patterns that begin with a literal "!"', |
| 592 | ["\\!abc", "\\!important!.txt"], |
| 593 | { |
| 594 | "!abc": 1, |
| 595 | abc: 0, |
| 596 | "b/!important!.txt": 1, |
| 597 | "!important!.txt": 1, |
| 598 | }, |
| 599 | ], |
| 600 | |
| 601 | [ |
| 602 | "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", |
| 603 | ["abc/"], |
| 604 | { |
| 605 | // actually, node-ignore have no idea about fs.Stat, |
| 606 | // you should `glob({mark: true})` |
| 607 | abc: 0, |
| 608 | "abc/": 1, |
| 609 | |
| 610 | // Actually, if there is only a trailing slash, git also treats it as a shell glob pattern |
| 611 | // 'abc/' should make 'bcd/abc/' ignored. |
| 612 | "bcd/abc/": 1, |
| 613 | }, |
| 614 | ], |
| 615 | |
| 616 | [ |
| 617 | "If the pattern does not contain a slash /, Git treats it as a shell glob pattern", |
| 618 | ["a.js", "f/"], |
| 619 | { |
| 620 | "a.js": 1, |
| 621 | "b/a/a.js": 1, |
| 622 | "a/a.js": 1, |
| 623 | "b/a.jsa": 0, |
| 624 | "f/": 1, |
| 625 | "g/f/": 1, |
| 626 | }, |
| 627 | ], |
| 628 | [ |
| 629 | "Otherwise, Git treats the pattern as a shell glob suitable for consumption by fnmatch(3) with the FNM_PATHNAME flag", |
| 630 | ["a/a.js"], |
| 631 | { |
| 632 | "a/a.js": 1, |
| 633 | "a/a.jsa": 0, |
| 634 | "b/a/a.js": 0, |
| 635 | "c/a/a.js": 0, |
| 636 | }, |
| 637 | ], |
| 638 | |
| 639 | [ |
| 640 | "wildcards in the pattern will not match a / in the pathname.", |
| 641 | ["Documentation/*.html"], |
| 642 | { |
| 643 | "Documentation/git.html": 1, |
| 644 | "Documentation/ppc/ppc.html": 0, |
| 645 | "tools/perf/Documentation/perf.html": 0, |
| 646 | }, |
| 647 | ], |
| 648 | |
| 649 | [ |
| 650 | "A leading slash matches the beginning of the pathname", |
| 651 | ["/*.c"], |
| 652 | { |
| 653 | "cat-file.c": 1, |
| 654 | "mozilla-sha1/sha1.c": 0, |
| 655 | }, |
| 656 | ], |
| 657 | |
| 658 | [ |
| 659 | 'A leading "**" followed by a slash means match in all directories', |
| 660 | ["**/foo"], |
| 661 | { |
| 662 | foo: 1, |
| 663 | "a/foo": 1, |
| 664 | "foo/a": 1, |
| 665 | "a/foo/a": 1, |
| 666 | "a/b/c/foo/a": 1, |
| 667 | }, |
| 668 | ], |
| 669 | |
| 670 | [ |
| 671 | '"**/foo/bar" matches file or directory "bar" anywhere that is directly under directory "foo"', |
| 672 | ["**/foo/bar"], |
| 673 | { |
| 674 | "foo/bar": 1, |
| 675 | "abc/foo/bar": 1, |
| 676 | "abc/foo/bar/": 1, |
| 677 | }, |
| 678 | ], |
| 679 | |
| 680 | [ |
| 681 | 'A trailing "/**" matches everything inside', |
| 682 | ["abc/**"], |
| 683 | { |
| 684 | "abc/a/": 1, |
| 685 | "abc/b": 1, |
| 686 | "abc/d/e/f/g": 1, |
| 687 | "bcd/abc/a": 0, |
| 688 | abc: 0, |
| 689 | }, |
| 690 | ], |
| 691 | |
| 692 | [ |
| 693 | "A slash followed by two consecutive asterisks then a slash matches zero or more directories", |
| 694 | ["a/**/b"], |
| 695 | { |
| 696 | "a/b": 1, |
| 697 | "a/x/b": 1, |
| 698 | "a/x/y/b": 1, |
| 699 | "b/a/b": 0, |
| 700 | }, |
| 701 | ], |
| 702 | |
| 703 | [ |
| 704 | "add a file content", |
| 705 | readPatterns(".aignore"), |
| 706 | { |
| 707 | "abc/a.js": 1, |
| 708 | "abc/b/b.js": 1, |
| 709 | "#e": 0, |
| 710 | "#f": 1, |
| 711 | }, |
| 712 | ], |
| 713 | |
| 714 | // old test cases |
| 715 | [ |
| 716 | "should excape metacharacters of regular expressions", |
| 717 | [ |
| 718 | "*.js", |
| 719 | "!\\*.js", |
| 720 | "!a#b.js", |
| 721 | "!?.js", |
| 722 | |
| 723 | // comments |
| 724 | "#abc", |
| 725 | |
| 726 | "\\#abc", |
| 727 | ], |
| 728 | { |
| 729 | "*.js": 0, |
| 730 | "abc.js": 1, |
| 731 | "a#b.js": 0, |
| 732 | abc: 0, |
| 733 | "#abc": 1, |
| 734 | "?.js": 0, |
| 735 | }, |
| 736 | ], |
| 737 | |
| 738 | [ |
| 739 | "issue #2: question mark should not break all things", |
| 740 | readPatterns(".ignore-issue-2"), |
| 741 | { |
| 742 | ".project": 1, |
| 743 | // remain |
| 744 | "abc/.project": 0, |
| 745 | ".a.sw": 0, |
| 746 | ".a.sw?": 1, |
| 747 | "thumbs.db": 1, |
| 748 | }, |
| 749 | ], |
| 750 | [ |
| 751 | 'dir ended with "*"', |
| 752 | ["abc/*"], |
| 753 | { |
| 754 | abc: 0, |
| 755 | }, |
| 756 | ], |
| 757 | [ |
| 758 | 'file ended with "*"', |
| 759 | ["abc.js*"], |
| 760 | { |
| 761 | "abc.js/": 1, |
| 762 | "abc.js/abc": 1, |
| 763 | "abc.jsa/": 1, |
| 764 | "abc.jsa/abc": 1, |
| 765 | }, |
| 766 | ], |
| 767 | [ |
| 768 | "wildcard as filename", |
| 769 | ["*.b"], |
| 770 | { |
| 771 | "b/a.b": 1, |
| 772 | "b/.b": 1, |
| 773 | "b/.ba": 0, |
| 774 | "b/c/a.b": 1, |
| 775 | }, |
| 776 | ], |
| 777 | [ |
| 778 | "slash at the beginning and come with a wildcard", |
| 779 | ["/*.c"], |
| 780 | { |
| 781 | ".c": 1, |
| 782 | "c.c": 1, |
| 783 | "c/c.c": 0, |
| 784 | "c/d": 0, |
| 785 | }, |
| 786 | ], |
| 787 | [ |
| 788 | "dot file", |
| 789 | [".d"], |
| 790 | { |
| 791 | ".d": 1, |
| 792 | ".dd": 0, |
| 793 | "d.d": 0, |
| 794 | "d/.d": 1, |
| 795 | "d/d.d": 0, |
| 796 | "d/e": 0, |
| 797 | }, |
| 798 | ], |
| 799 | [ |
| 800 | "dot dir", |
| 801 | [".e"], |
| 802 | { |
| 803 | ".e/": 1, |
| 804 | ".ee/": 0, |
| 805 | "e.e/": 0, |
| 806 | ".e/e": 1, |
| 807 | "e/.e": 1, |
| 808 | "e/e.e": 0, |
| 809 | "e/f": 0, |
| 810 | }, |
| 811 | ], |
| 812 | [ |
| 813 | "node modules: once", |
| 814 | ["node_modules/"], |
| 815 | { |
| 816 | "node_modules/gulp/node_modules/abc.md": 1, |
| 817 | "node_modules/gulp/node_modules/abc.json": 1, |
| 818 | }, |
| 819 | ], |
| 820 | [ |
| 821 | "node modules: sub directories", |
| 822 | ["node_modules"], |
| 823 | { |
| 824 | "a/b/node_modules/abc.md": 1, |
| 825 | }, |
| 826 | ], |
| 827 | [ |
| 828 | "node modules: twice", |
| 829 | ["node_modules/", "node_modules/"], |
| 830 | { |
| 831 | "node_modules/gulp/node_modules/abc.md": 1, |
| 832 | "node_modules/gulp/node_modules/abc.json": 1, |
| 833 | }, |
| 834 | ], |
| 835 | [ |
| 836 | "unicode characters in windows paths", |
| 837 | ["test"], |
| 838 | { |
| 839 | "some/path/to/test/ignored.js": 1, |
| 840 | "some/special/path/to/目录/test/ignored.js": 1, |
| 841 | }, |
| 842 | false, |
| 843 | true, // git-check-ignore fails as git converts special chars to escaped unicode before printing |
| 844 | ], |
| 845 | ]; |
| 846 | |
| 847 | if (!SHOULD_TEST_WINDOWS) { |
| 848 | caseDefinitions.push( |
| 849 | [ |
| 850 | "#68: ignore test for files named ...", |
| 851 | ["/...", "/....."], |
| 852 | { |
| 853 | "...": 1, |
| 854 | "....": 0, |
| 855 | ".....": 1, |
| 856 | "......": 0, |
| 857 | }, |
| 858 | ], |
| 859 | [ |
| 860 | "#68: file named ...", |
| 861 | [], |
| 862 | { |
| 863 | "...": 0, |
| 864 | "....": 0, |
| 865 | ".....": 0, |
| 866 | }, |
| 867 | ], |
| 868 | [ |
| 869 | "#130: consequent escaped backslashes with whitespaces", |
| 870 | ["a\\\\ ", "a\\\\ b", "a\\\\\\ b"], |
| 871 | { |
| 872 | "a\\": 1, |
| 873 | "a\\ b": 1, |
| 874 | "a\\\\ b": 0, |
| 875 | "a\\\\\\ b": 0, |
| 876 | }, |
| 877 | ], |
| 878 | [ |
| 879 | "#81: invalid trailing backslash at the end should not throw, test non-windows env only", |
| 880 | [ |
| 881 | "test\\", |
| 882 | "testa\\\\", |
| 883 | "\\", |
| 884 | "foo/*", |
| 885 | // test negative pattern |
| 886 | "!foo/test\\", |
| 887 | ], |
| 888 | { |
| 889 | test: 0, |
| 890 | "test\\": 0, |
| 891 | "testa\\": 1, |
| 892 | "\\": 0, |
| 893 | "foo/test\\": 1, |
| 894 | }, |
| 895 | ], |
| 896 | [ |
| 897 | "linux: back slashes on paths", |
| 898 | ["a", "b\\\\c"], |
| 899 | { |
| 900 | "b\\c/a.md": 1, |
| 901 | "a\\b/a.js": 0, |
| 902 | "a\\b/a": 1, |
| 903 | "a/a.js": 1, |
| 904 | }, |
| 905 | ], |
| 906 | [ |
| 907 | "#59: test cases for linux only", |
| 908 | [ |
| 909 | "src/\\[foo\\]", // 1 -> 0 |
| 910 | "src/\\[foo2\\\\]", // 2 -> 1 |
| 911 | "src/\\[foo3\\\\\\]", // 3 -> 1 |
| 912 | "src/\\[foo4\\\\\\\\]", // 4 -> 2 |
| 913 | "src/\\[foo5\\\\\\\\\\]", // 5 -> 2 |
| 914 | "src/\\[foo6\\\\\\\\\\\\]", // 6 -> 3 |
| 915 | |
| 916 | "src/\\[bar]", |
| 917 | |
| 918 | "src/[e\\\\]", |
| 919 | "s/[f\\\\\\\\]", |
| 920 | |
| 921 | "s/[a-z0-9]", |
| 922 | ], |
| 923 | { |
| 924 | "src/[foo]": 1, |
| 925 | "src/[foo2\\]": 1, |
| 926 | |
| 927 | // Seems the followings are side-effects, |
| 928 | // however, we will implement these |
| 929 | "src/[foo3\\]": 1, |
| 930 | "src/[foo4\\\\]": 1, |
| 931 | "src/[foo5\\\\]": 1, |
| 932 | "src/[foo6\\\\\\]": 1, |
| 933 | |
| 934 | "src/[bar]": 1, |
| 935 | |
| 936 | "src/e": 1, |
| 937 | "src/\\": 1, |
| 938 | "s/f": 1, |
| 939 | "s/\\": 1, |
| 940 | |
| 941 | "s/a": 1, |
| 942 | |
| 943 | "s/0": 1, |
| 944 | }, |
| 945 | ], |
| 946 | ); |
| 947 | } |
| 948 | |
| 949 | const cases_to_test_only = caseDefinitions.filter((c) => c[3]); |
| 950 | |
| 951 | const real_cases = cases_to_test_only.length ? cases_to_test_only : caseDefinitions; |
| 952 | |
| 953 | export const cases = (iteratee) => { |
| 954 | real_cases.forEach((single) => { |
| 955 | const [description, patterns, paths_object, test_only, skip_test_fixture, scopes = false] = |
| 956 | single; |
| 957 | |
| 958 | // All paths to test |
| 959 | const paths = Object.keys(paths_object); |
| 960 | |
| 961 | // paths that NOT ignored |
| 962 | let expected = paths.filter((p) => !paths_object[p]).sort(); |
| 963 | |
| 964 | function expect_result(t, result, mapper) { |
| 965 | if (mapper) { |
| 966 | expected = expected.map(mapper); |
| 967 | } |
| 968 | |
| 969 | t.same(result.sort(), expected.sort()); |
| 970 | } |
| 971 | |
| 972 | iteratee({ |
| 973 | description, |
| 974 | patterns, |
| 975 | paths_object, |
| 976 | test_only, |
| 977 | skip_test_fixture, |
| 978 | paths, |
| 979 | scopes, |
| 980 | expected, |
| 981 | expect_result, |
| 982 | }); |
| 983 | }); |
| 984 | }; |
| 985 | |
| 986 | // For local testing purpose |
| 987 | const ENV_KEYS = [ |
| 988 | "IGNORE_ONLY_FILTER", |
| 989 | "IGNORE_ONLY_CREATE_FILTER", |
| 990 | "IGNORE_ONLY_IGNORES", |
| 991 | "IGNORE_ONLY_CHECK_IGNORE", |
| 992 | "IGNORE_ONLY_WIN32", |
| 993 | "IGNORE_ONLY_FIXTURES", |
| 994 | "IGNORE_ONLY_OTHERS", |
| 995 | ]; |
| 996 | |
| 997 | const envs = {}; |
| 998 | let hasOnly = false; |
| 999 | ENV_KEYS.forEach((key) => { |
| 1000 | const value = !!process.env[key]; |
| 1001 | envs[key] = value; |
| 1002 | |
| 1003 | if (value) { |
| 1004 | hasOnly = true; |
| 1005 | } |
| 1006 | }); |
| 1007 | |
| 1008 | export const checkEnv = (key) => (hasOnly ? !!envs[key] : true); |
| 1009 | |
| 1010 | export { IS_WINDOWS }; |
| 1011 | export { SHOULD_TEST_WINDOWS }; |
| 1012 | |
| 1013 | export { debug }; |