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 | import { describe, expect, it } from "bun:test"; |
| 2 | import ignore, { isPathValid } from "../src/ignore.ts"; |
| 3 | |
| 4 | describe("ignore v7.0.5 options and public methods", () => { |
| 5 | it("adds another Ignore instance", () => { |
| 6 | const a = ignore().add([".abc/*", "!.abc/d/"]); |
| 7 | const b = ignore().add(a).add("!.abc/e/"); |
| 8 | const paths = [".abc/a.js", ".abc/d/e.js", ".abc/e/e.js"]; |
| 9 | |
| 10 | expect(a.filter(paths)).toEqual([".abc/d/e.js"]); |
| 11 | expect(b.filter(paths)).toEqual([".abc/d/e.js", ".abc/e/e.js"]); |
| 12 | }); |
| 13 | |
| 14 | it("cannot invoke the instance constructor without new", () => { |
| 15 | const { constructor } = ignore(); |
| 16 | expect(() => (constructor as () => unknown)()).toThrow(); |
| 17 | }); |
| 18 | |
| 19 | it("accepts an Ignore-shaped instance from another module copy", () => { |
| 20 | const source = ignore().add([".abc/*", "!.abc/d/"]); |
| 21 | const compatible = { |
| 22 | _rules: { |
| 23 | _rules: (source as any)._rules._rules.slice(), |
| 24 | }, |
| 25 | [Symbol.for("node-ignore")]: true, |
| 26 | }; |
| 27 | const matcher = ignore() |
| 28 | .add(compatible as any) |
| 29 | .add("!.abc/e/"); |
| 30 | |
| 31 | expect(matcher.filter([".abc/a.js", ".abc/d/e.js", ".abc/e/e.js"])).toEqual([ |
| 32 | ".abc/d/e.js", |
| 33 | ".abc/e/e.js", |
| 34 | ]); |
| 35 | }); |
| 36 | |
| 37 | it("respects ignorecase", () => { |
| 38 | const matcher = ignore({ ignorecase: false }).add("*.[jJ][pP]g"); |
| 39 | expect(matcher.ignores("a.jpg")).toBe(true); |
| 40 | expect(matcher.ignores("a.JPg")).toBe(true); |
| 41 | expect(matcher.ignores("a.JPG")).toBe(false); |
| 42 | }); |
| 43 | |
| 44 | it("respects ignorecase without leaking the compiled-rule cache", () => { |
| 45 | const rule = "*.[jJ][pP]g"; |
| 46 | expect(ignore({ ignorecase: false }).add(rule).ignores("a.JPG")).toBe(false); |
| 47 | expect(ignore({ ignorecase: true }).add(rule).ignores("a.JPG")).toBe(true); |
| 48 | }); |
| 49 | |
| 50 | it("rejects invalid paths", () => { |
| 51 | const matcher = ignore(); |
| 52 | expect(() => matcher.ignores("")).toThrow(); |
| 53 | expect(() => matcher.ignores("/a")).toThrow("path.relative"); |
| 54 | expect(() => matcher.filter([""])).toThrow(); |
| 55 | expect(() => [""].filter(matcher.createFilter())).toThrow(); |
| 56 | }); |
| 57 | |
| 58 | it("validates paths", () => { |
| 59 | expect([".", "./foo", "../foo", "/foo", "foo"].filter(isPathValid)).toEqual(["foo"]); |
| 60 | }); |
| 61 | |
| 62 | it("returns ignored and unignored state from test", () => { |
| 63 | const cases: Array<[string | string[] | undefined, string, boolean, boolean]> = [ |
| 64 | [undefined, "foo", false, false], |
| 65 | ["bar", "foo", false, false], |
| 66 | ["!foo", "foo", false, true], |
| 67 | [["foo", "!foo"], "foo", false, true], |
| 68 | [["foo", "!foo"], "foo/bar", false, false], |
| 69 | [["*.js", "!a/a.js"], "a/a.js", false, true], |
| 70 | ...(process.platform === "win32" |
| 71 | ? [] |
| 72 | : ([[undefined, "...", false, false]] as Array<[undefined, string, boolean, boolean]>)), |
| 73 | ]; |
| 74 | |
| 75 | for (const [patterns, path, ignored, unignored] of cases) { |
| 76 | const matcher = ignore(); |
| 77 | if (patterns) matcher.add(patterns); |
| 78 | expect(matcher.test(path)).toEqual({ ignored, unignored }); |
| 79 | } |
| 80 | }); |
| 81 | |
| 82 | it("optionally permits relative paths", () => { |
| 83 | expect(ignore({ allowRelativePaths: true }).add("foo").ignores("../foo/bar.js")).toBe(true); |
| 84 | expect(() => ignore().ignores("../foo/bar.js")).toThrow("path.relative"); |
| 85 | expect(() => ignore().add("foo").ignores("/foo/bar.js")).toThrow("path.relative"); |
| 86 | }); |
| 87 | }); |