repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by bugabinga
extensions/llmiterate/__tests__/gitignore.test.ts
RawFile outline
- m line 4
- GitIgnoreMatcher — basics line 8
- empty matcher ignores nothing line 9
- blank lines are skipped line 16
- comments starting with # are skipped line 21
- escaped hash at start is treated as literal line 27
- trailing spaces are ignored line 33
- escaped trailing space is literal line 39
- add returns this for chaining line 45
- GitIgnoreMatcher — glob * and ? line 51
- * matches single path component line 52
- * does not match slash line 59
- ? matches single char except slash line 66
- multiple * in pattern line 74
- GitIgnoreMatcher — character classes line 82
- [abc] matches single char from set line 83
- [a-z] range matches line 90
- negated class [!a-z] matches outside range line 97
- multiple ranges in one class line 103
- unclosed [ is treated as literal line 110
- GitIgnoreMatcher — escaping line 117
- escaped specials are literals line 118
- escaped backslash is literal line 126
- backslash escapes any character line 131
- trailing backslash is invalid and never matches line 136
- literal asterisk via escape line 145
- literal question mark via escape line 151
- GitIgnoreMatcher — anchoring / line 158
- leading / anchors to context root line 159
- anchored file pattern line 166
- middle slash makes pattern relative to gitignore dir line 172
- GitIgnoreMatcher — directory-only trailing / line 179
- build/ ignores only directories line 180
- pattern without trailing slash ignores files and directories line 187
- frotz/ matches frotz and a/frotz directories line 195
- GitIgnoreMatcher — double-star ** line 203
- **/foo matches foo anywhere line 204
- **/foo/bar matches bar directly under foo anywhere line 212
- abc/** matches everything inside abc (relative) line 219
- a/**/b matches zero or more directories (relative) line 229
- foo/**/bar/baz matches nested line 239
- ** alone matches everything line 248
- GitIgnoreMatcher — negation ! line 255
- !important.log negates previous ignore line 256
- multiple negations in sequence line 262
- negation cannot re-include if parent dir is excluded line 269
- anchored negation !/build line 274
- negation with directory-only line 281
- GitIgnoreMatcher — parent directory propagation line 289
- parent directory ignored ignores children line 290
- deep parent directory ignored line 297
- GitIgnoreMatcher — real-world patterns line 304
- node_modules line 305
- dotenv files line 312
- IDE directories line 320
- build artifacts line 328
- OS files line 337
- lockfiles line 343
- python artifacts line 350
- Rust target line 357
- GitIgnoreMatcher — brutal edge cases line 364
- pattern with only slash is invalid line 365
- pattern . is invalid line 370
- pattern .. is invalid line 375
- pattern starting with ./ is invalid line 380
- pattern starting with ../ is invalid line 385
- consecutive slashes in pattern line 390
- pattern with spaces in filename line 395
- unicode in pattern line 400
- very long pattern still works line 405
- CRLF line endings line 411
- mixed line endings line 417
- empty string add is no-op line 424
- pattern ending with just / after stripping is invalid line 429
- negation of non-ignored file is no-op line 434
- pattern that matches after being negated then re-ignored line 439
- directory check on file pattern line 444
- multiple add calls accumulate line 451
- foo*bar matches foobar and fooXbar line 457
- **/ prefix matches basename anywhere line 464
- deep nested dirs with ** line 472
- pattern with all special chars escaped line 479
- GitIgnoreMatcher — git docs compliance line 485
- Remaining entries omitted.
import { describe, expect, test } from "vitest";
import { GitIgnoreMatcher } from "../gitignore";
function m(): GitIgnoreMatcher {
return new GitIgnoreMatcher();
}
describe("GitIgnoreMatcher — basics", () => {
test("empty matcher ignores nothing", () => {
const matcher = m();
expect(matcher.ignores("a.ts")).toBe(false);
expect(matcher.ignores("src/a.ts")).toBe(false);
expect(matcher.ignores("")).toBe(false);
});
test("blank lines are skipped", () => {
const matcher = m().add("\n\n \n\t\n*.log\n");
expect(matcher.ignores("a.log")).toBe(true);
});
test("comments starting with # are skipped", () => {
const matcher = m().add("# ignore logs\n*.log\n");
expect(matcher.ignores("a.log")).toBe(true);
expect(matcher.ignores("# ignore logs")).toBe(false);
});
test("escaped hash at start is treated as literal", () => {
const matcher = m().add("\\#secret\n");
expect(matcher.ignores("#secret")).toBe(true);
expect(matcher.ignores("secret")).toBe(false);
});
test("trailing spaces are ignored", () => {
const matcher = m().add("*.log \n");
expect(matcher.ignores("a.log")).toBe(true);
expect(matcher.ignores("a.log ")).toBe(false);
});
test("escaped trailing space is literal", () => {
const matcher = m().add("foo\\ \n");
expect(matcher.ignores("foo ")).toBe(true);
expect(matcher.ignores("foo")).toBe(false);
});
test("add returns this for chaining", () => {
const matcher = m();
expect(matcher.add("*.log")).toBe(matcher);
});
});
describe("GitIgnoreMatcher — glob * and ?", () => {
test("* matches single path component", () => {
const matcher = m().add("*\n");
expect(matcher.ignores("a")).toBe(true);
expect(matcher.ignores("abc")).toBe(true);
expect(matcher.ignores("a/b")).toBe(true); // matches component b
});
test("* does not match slash", () => {
// Pattern * alone matches any single path component at any depth
const matcher = m().add("foo*\n");
expect(matcher.ignores("foobar")).toBe(true);
expect(matcher.ignores("foo/bar")).toBe(false);
});
test("? matches single char except slash", () => {
const matcher = m().add("?.tmp\n");
expect(matcher.ignores("a.tmp")).toBe(true);
expect(matcher.ignores("ab.tmp")).toBe(false);
// Basename pattern at any depth: ? matches b in a/b.tmp
expect(matcher.ignores("a/b.tmp")).toBe(true);
});
test("multiple * in pattern", () => {
const matcher = m().add("*.*\n");
expect(matcher.ignores("a.b")).toBe(true);
expect(matcher.ignores("a.b.c")).toBe(true);
expect(matcher.ignores("nodots")).toBe(false);
});
});
describe("GitIgnoreMatcher — character classes", () => {
test("[abc] matches single char from set", () => {
const matcher = m().add("*.[to]mp\n");
expect(matcher.ignores("a.tmp")).toBe(true);
expect(matcher.ignores("a.omp")).toBe(true);
expect(matcher.ignores("a.xmp")).toBe(false);
});
test("[a-z] range matches", () => {
const matcher = m().add("file[0-9].txt\n");
expect(matcher.ignores("file1.txt")).toBe(true);
expect(matcher.ignores("file9.txt")).toBe(true);
expect(matcher.ignores("filea.txt")).toBe(false);
});
test("negated class [!a-z] matches outside range", () => {
const matcher = m().add("file[!0-9].txt\n");
expect(matcher.ignores("filea.txt")).toBe(true);
expect(matcher.ignores("file1.txt")).toBe(false);
});
test("multiple ranges in one class", () => {
const matcher = m().add("file[a-zA-Z].txt\n");
expect(matcher.ignores("filea.txt")).toBe(true);
expect(matcher.ignores("fileZ.txt")).toBe(true);
expect(matcher.ignores("file1.txt")).toBe(false);
});
test("unclosed [ is treated as literal", () => {
const matcher = m().add("a[b\n");
expect(matcher.ignores("a[b")).toBe(true);
expect(matcher.ignores("ab")).toBe(false);
});
});
describe("GitIgnoreMatcher — escaping", () => {
test("escaped specials are literals", () => {
const matcher = m().add("\\#file\n\\!important\nstar\\*.txt\n");
expect(matcher.ignores("#file")).toBe(true);
expect(matcher.ignores("!important")).toBe(true);
expect(matcher.ignores("star*.txt")).toBe(true);
expect(matcher.ignores("#other")).toBe(false);
});
test("escaped backslash is literal", () => {
const matcher = m().add("a\\\\b\n");
expect(matcher.ignores("a\\b")).toBe(true);
});
test("backslash escapes any character", () => {
const matcher = m().add("\\a\n");
expect(matcher.ignores("a")).toBe(true);
});
test("trailing backslash is invalid and never matches", () => {
const matcher = m().add("foo\\\n");
expect(matcher.ignores("foo")).toBe(false);
expect(matcher.ignores("foo\\")).toBe(false);
// Verify a valid pattern still works in same matcher
const m2 = m().add("foo\\\nbar\n");
expect(m2.ignores("bar")).toBe(true);
});
test("literal asterisk via escape", () => {
const matcher = m().add("\\*.tmp\n");
expect(matcher.ignores("*.tmp")).toBe(true);
expect(matcher.ignores("a.tmp")).toBe(false);
});
test("literal question mark via escape", () => {
const matcher = m().add("\\?.tmp\n");
expect(matcher.ignores("?.tmp")).toBe(true);
expect(matcher.ignores("a.tmp")).toBe(false);
});
});
describe("GitIgnoreMatcher — anchoring /", () => {
test("leading / anchors to context root", () => {
const matcher = m().add("/build/\n");
expect(matcher.ignores("build/")).toBe(true);
expect(matcher.ignores("build/output.js")).toBe(true);
expect(matcher.ignores("src/build/")).toBe(false);
});
test("anchored file pattern", () => {
const matcher = m().add("/Makefile\n");
expect(matcher.ignores("Makefile")).toBe(true);
expect(matcher.ignores("src/Makefile")).toBe(false);
});
test("middle slash makes pattern relative to gitignore dir", () => {
const matcher = m().add("doc/frotz/\n");
expect(matcher.ignores("doc/frotz/")).toBe(true);
expect(matcher.ignores("a/doc/frotz/")).toBe(false);
});
});
describe("GitIgnoreMatcher — directory-only trailing /", () => {
test("build/ ignores only directories", () => {
const matcher = m().add("build/\n");
expect(matcher.ignores("build/")).toBe(true);
expect(matcher.ignores("build/output.js")).toBe(true);
expect(matcher.ignores("build")).toBe(false);
});
test("pattern without trailing slash ignores files and directories", () => {
const matcher = m().add("build\n");
expect(matcher.ignores("build")).toBe(true);
expect(matcher.ignores("build/")).toBe(true);
expect(matcher.ignores("src/build")).toBe(true);
expect(matcher.ignores("src/build/")).toBe(true);
});
test("frotz/ matches frotz and a/frotz directories", () => {
const matcher = m().add("frotz/\n");
expect(matcher.ignores("frotz/")).toBe(true);
expect(matcher.ignores("a/frotz/")).toBe(true);
expect(matcher.ignores("frotz")).toBe(false);
});
});
describe("GitIgnoreMatcher — double-star **", () => {
test("**/foo matches foo anywhere", () => {
const matcher = m().add("**/foo\n");
expect(matcher.ignores("foo")).toBe(true);
expect(matcher.ignores("a/foo")).toBe(true);
expect(matcher.ignores("a/b/foo")).toBe(true);
expect(matcher.ignores("a/foobar")).toBe(false);
});
test("**/foo/bar matches bar directly under foo anywhere", () => {
const matcher = m().add("**/foo/bar\n");
expect(matcher.ignores("foo/bar")).toBe(true);
expect(matcher.ignores("a/foo/bar")).toBe(true);
expect(matcher.ignores("a/foo/b/bar")).toBe(false);
});
test("abc/** matches everything inside abc (relative)", () => {
const matcher = m().add("abc/**\n");
expect(matcher.ignores("abc/foo")).toBe(true);
expect(matcher.ignores("abc/foo/bar")).toBe(true);
expect(matcher.ignores("abc/")).toBe(false);
expect(matcher.ignores("abc")).toBe(false);
// Relative pattern: does NOT match x/abc/foo
expect(matcher.ignores("x/abc/foo")).toBe(false);
});
test("a/**/b matches zero or more directories (relative)", () => {
const matcher = m().add("a/**/b\n");
expect(matcher.ignores("a/b")).toBe(true);
expect(matcher.ignores("a/x/b")).toBe(true);
expect(matcher.ignores("a/x/y/b")).toBe(true);
expect(matcher.ignores("a/c")).toBe(false);
// Relative pattern: does NOT match z/a/x/b
expect(matcher.ignores("z/a/x/b")).toBe(false);
});
test("foo/**/bar/baz matches nested", () => {
const matcher = m().add("foo/**/bar/baz\n");
expect(matcher.ignores("foo/bar/baz")).toBe(true);
expect(matcher.ignores("foo/x/bar/baz")).toBe(true);
expect(matcher.ignores("foo/x/y/bar/baz")).toBe(true);
expect(matcher.ignores("foo/bar/baz/qux")).toBe(true);
expect(matcher.ignores("foo/bar/qux")).toBe(false);
});
test("** alone matches everything", () => {
const matcher = m().add("**\n");
expect(matcher.ignores("a")).toBe(true);
expect(matcher.ignores("a/b/c")).toBe(true);
});
});
describe("GitIgnoreMatcher — negation !", () => {
test("!important.log negates previous ignore", () => {
const matcher = m().add("*.log\n!important.log\n");
expect(matcher.ignores("debug.log")).toBe(true);
expect(matcher.ignores("important.log")).toBe(false);
});
test("multiple negations in sequence", () => {
const matcher = m().add("*.log\n!a.log\n!b.log\n");
expect(matcher.ignores("c.log")).toBe(true);
expect(matcher.ignores("a.log")).toBe(false);
expect(matcher.ignores("b.log")).toBe(false);
});
test("negation cannot re-include if parent dir is excluded", () => {
const matcher = m().add("dist/\n!dist/keep.txt\n");
expect(matcher.ignores("dist/keep.txt")).toBe(true);
});
test("anchored negation !/build", () => {
const matcher = m().add("/build/\n!/build/keep/\n");
expect(matcher.ignores("build/")).toBe(true);
// Cannot re-include if parent is excluded per gitignore spec
expect(matcher.ignores("build/keep/")).toBe(true);
});
test("negation with directory-only", () => {
const matcher = m().add("logs/\n!logs/important/\n");
expect(matcher.ignores("logs/debug/")).toBe(true);
// Cannot re-include if parent is excluded per gitignore spec
expect(matcher.ignores("logs/important/")).toBe(true);
});
});
describe("GitIgnoreMatcher — parent directory propagation", () => {
test("parent directory ignored ignores children", () => {
const matcher = m().add("dist/\n");
expect(matcher.ignores("dist/bundle.js")).toBe(true);
expect(matcher.ignores("dist/assets/logo.png")).toBe(true);
expect(matcher.ignores("dist/sub/deep/file")).toBe(true);
});
test("deep parent directory ignored", () => {
const matcher = m().add("node_modules/\n");
expect(matcher.ignores("node_modules/.package-lock.json")).toBe(true);
expect(matcher.ignores("a/b/node_modules/c/d")).toBe(true);
});
});
describe("GitIgnoreMatcher — real-world patterns", () => {
test("node_modules", () => {
const matcher = m().add("node_modules/\n");
expect(matcher.ignores("node_modules/lodash/index.js")).toBe(true);
expect(matcher.ignores("node_modules/.package-lock.json")).toBe(true);
expect(matcher.ignores("src/node_modules/")).toBe(true);
});
test("dotenv files", () => {
const matcher = m().add(".env\n.env.*\n!.env.example\n");
expect(matcher.ignores(".env")).toBe(true);
expect(matcher.ignores(".env.local")).toBe(true);
expect(matcher.ignores(".env.production")).toBe(true);
expect(matcher.ignores(".env.example")).toBe(false);
});
test("IDE directories", () => {
const matcher = m().add(".idea/\n.vscode/\n*.swp\n*~\n");
expect(matcher.ignores(".idea/workspace.xml")).toBe(true);
expect(matcher.ignores(".vscode/settings.json")).toBe(true);
expect(matcher.ignores("file.swp")).toBe(true);
expect(matcher.ignores("backup~")).toBe(true);
});
test("build artifacts", () => {
const matcher = m().add("dist/\nbuild/\ncoverage/\n*.min.js\n*.map\n");
expect(matcher.ignores("dist/app.js")).toBe(true);
expect(matcher.ignores("build/index.html")).toBe(true);
expect(matcher.ignores("coverage/lcov-report")).toBe(true);
expect(matcher.ignores("app.min.js")).toBe(true);
expect(matcher.ignores("app.js.map")).toBe(true);
});
test("OS files", () => {
const matcher = m().add(".DS_Store\nThumbs.db\n");
expect(matcher.ignores(".DS_Store")).toBe(true);
expect(matcher.ignores("src/.DS_Store")).toBe(true);
});
test("lockfiles", () => {
const matcher = m().add("package-lock.json\nyarn.lock\n");
expect(matcher.ignores("package-lock.json")).toBe(true);
expect(matcher.ignores("yarn.lock")).toBe(true);
expect(matcher.ignores("package.json")).toBe(false);
});
test("python artifacts", () => {
const matcher = m().add("__pycache__/\n*.pyc\n*.pyo\n.pytest_cache/\n");
expect(matcher.ignores("__pycache__/foo.cpython-311.pyc")).toBe(true);
expect(matcher.ignores("module.pyc")).toBe(true);
expect(matcher.ignores(".pytest_cache/v/cache/nodeids")).toBe(true);
});
test("Rust target", () => {
const matcher = m().add("target/\n");
expect(matcher.ignores("target/debug/app")).toBe(true);
expect(matcher.ignores("target/release/libfoo.so")).toBe(true);
});
});
describe("GitIgnoreMatcher — brutal edge cases", () => {
test("pattern with only slash is invalid", () => {
const matcher = m().add("/\n");
expect(matcher.ignores("a")).toBe(false);
});
test("pattern . is invalid", () => {
const matcher = m().add(".\n");
expect(matcher.ignores(".")).toBe(false);
});
test("pattern .. is invalid", () => {
const matcher = m().add("..\n");
expect(matcher.ignores("..")).toBe(false);
});
test("pattern starting with ./ is invalid", () => {
const matcher = m().add("./foo\n");
expect(matcher.ignores("foo")).toBe(false);
});
test("pattern starting with ../ is invalid", () => {
const matcher = m().add("../foo\n");
expect(matcher.ignores("foo")).toBe(false);
});
test("consecutive slashes in pattern", () => {
const matcher = m().add("a//b\n");
expect(matcher.ignores("a//b")).toBe(true);
});
test("pattern with spaces in filename", () => {
const matcher = m().add("my\\ file.txt\n");
expect(matcher.ignores("my file.txt")).toBe(true);
});
test("unicode in pattern", () => {
const matcher = m().add("*.äöü\n");
expect(matcher.ignores("file.äöü")).toBe(true);
});
test("very long pattern still works", () => {
const longName = "a".repeat(200);
const matcher = m().add(`${longName}\n`);
expect(matcher.ignores(longName)).toBe(true);
});
test("CRLF line endings", () => {
const matcher = m().add("*.log\r\n*.tmp\r\n");
expect(matcher.ignores("a.log")).toBe(true);
expect(matcher.ignores("a.tmp")).toBe(true);
});
test("mixed line endings", () => {
const matcher = m().add("*.log\n*.tmp\r\n*.bak\n");
expect(matcher.ignores("a.log")).toBe(true);
expect(matcher.ignores("a.tmp")).toBe(true);
expect(matcher.ignores("a.bak")).toBe(true);
});
test("empty string add is no-op", () => {
const matcher = m().add("");
expect(matcher.ignores("a")).toBe(false);
});
test("pattern ending with just / after stripping is invalid", () => {
const matcher = m().add("/\n");
expect(matcher.ignores("/")).toBe(false);
});
test("negation of non-ignored file is no-op", () => {
const matcher = m().add("!keep.txt\n");
expect(matcher.ignores("keep.txt")).toBe(false);
});
test("pattern that matches after being negated then re-ignored", () => {
const matcher = m().add("*.log\n!important.log\n*.log\n");
expect(matcher.ignores("important.log")).toBe(true);
});
test("directory check on file pattern", () => {
const matcher = m().add("build\n");
expect(matcher.ignores("build")).toBe(true);
expect(matcher.ignores("build/")).toBe(true);
expect(matcher.ignores("src/build/")).toBe(true);
});
test("multiple add calls accumulate", () => {
const matcher = m().add("*.log\n").add("*.tmp\n");
expect(matcher.ignores("a.log")).toBe(true);
expect(matcher.ignores("a.tmp")).toBe(true);
});
test("foo*bar matches foobar and fooXbar", () => {
const matcher = m().add("foo*bar\n");
expect(matcher.ignores("foobar")).toBe(true);
expect(matcher.ignores("fooXbar")).toBe(true);
expect(matcher.ignores("foo/bar")).toBe(false);
});
test("**/ prefix matches basename anywhere", () => {
const matcher = m().add("**/Makefile\n");
expect(matcher.ignores("Makefile")).toBe(true);
expect(matcher.ignores("src/Makefile")).toBe(true);
// Does NOT match Makefile.am — different basename
expect(matcher.ignores("src/Makefile.am")).toBe(false);
});
test("deep nested dirs with **", () => {
const matcher = m().add("**/node_modules/**\n");
expect(matcher.ignores("node_modules/foo")).toBe(true);
expect(matcher.ignores("a/node_modules/foo")).toBe(true);
expect(matcher.ignores("a/node_modules/foo/bar")).toBe(true);
});
test("pattern with all special chars escaped", () => {
const matcher = m().add("\\.\\[\\*\\?\\#\\!\\/\\]\n");
expect(matcher.ignores(".[*?#!/]")).toBe(true);
});
});
describe("GitIgnoreMatcher — git docs compliance", () => {
test("doc/frotz/ matches doc/frotz but not a/doc/frotz", () => {
const matcher = m().add("doc/frotz/\n");
expect(matcher.ignores("doc/frotz/")).toBe(true);
expect(matcher.ignores("a/doc/frotz/")).toBe(false);
});
test("frotz/ matches frotz and a/frotz directories", () => {
const matcher = m().add("frotz/\n");
expect(matcher.ignores("frotz/")).toBe(true);
expect(matcher.ignores("a/frotz/")).toBe(true);
expect(matcher.ignores("frotz")).toBe(false);
});
test("**/foo same as foo", () => {
const m1 = m().add("**/foo\n");
const m2 = m().add("foo\n");
const paths = ["foo", "a/foo", "a/b/foo", "foobar"];
for (const p of paths) {
expect(m1.ignores(p)).toBe(m2.ignores(p));
}
});
test("**/foo/bar matches bar directly under foo anywhere", () => {
const matcher = m().add("**/foo/bar\n");
expect(matcher.ignores("foo/bar")).toBe(true);
expect(matcher.ignores("a/foo/bar")).toBe(true);
expect(matcher.ignores("a/foo/b/bar")).toBe(false);
expect(matcher.ignores("foo/bar/baz")).toBe(true);
});
test("abc/** matches all files inside abc with infinite depth", () => {
const matcher = m().add("abc/**\n");
expect(matcher.ignores("abc/foo")).toBe(true);
expect(matcher.ignores("abc/foo/bar")).toBe(true);
expect(matcher.ignores("abc/foo/bar/baz")).toBe(true);
expect(matcher.ignores("abc/")).toBe(false);
expect(matcher.ignores("abc")).toBe(false);
});
test("a/**/b matches a/b a/x/b a/x/y/b", () => {
const matcher = m().add("a/**/b\n");
expect(matcher.ignores("a/b")).toBe(true);
expect(matcher.ignores("a/x/b")).toBe(true);
expect(matcher.ignores("a/x/y/b")).toBe(true);
});
});