The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { describe, expect, it } from "bun:test"; |
| 2 | import { execFileSync, spawnSync } from "node:child_process"; |
| 3 | import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 4 | import { tmpdir } from "node:os"; |
| 5 | import { dirname, join } from "node:path"; |
| 6 | import { cases, IS_WINDOWS } from "./fixtures/cases.ts"; |
| 7 | |
| 8 | const gitAvailable = spawnSync("git", ["--version"]).status === 0; |
| 9 | |
| 10 | const withoutEndingSlash = (value: string) => value.replace(/\/$/, ""); |
| 11 | |
| 12 | const containsAnotherPath = (candidate: string, index: number, paths: string[]) => { |
| 13 | const path = withoutEndingSlash(candidate); |
| 14 | return paths.some( |
| 15 | (other, otherIndex) => |
| 16 | otherIndex !== index && |
| 17 | (other === path || (other.startsWith(path) && other[path.length] === "/")), |
| 18 | ); |
| 19 | }; |
| 20 | |
| 21 | const nativeGitResult = ( |
| 22 | patterns: Array<string | { pattern: string }> | string, |
| 23 | paths: string[], |
| 24 | ) => { |
| 25 | const root = mkdtempSync(join(tmpdir(), "alchemy-node-ignore-")); |
| 26 | try { |
| 27 | const contents = |
| 28 | typeof patterns === "string" |
| 29 | ? patterns |
| 30 | : patterns.map((rule) => (typeof rule === "string" ? rule : rule.pattern)).join("\n"); |
| 31 | writeFileSync(join(root, ".gitignore"), contents); |
| 32 | |
| 33 | paths.forEach((path, index) => { |
| 34 | if (path === ".gitignore" || containsAnotherPath(path, index, paths)) return; |
| 35 | const target = join(root, path); |
| 36 | if (path.endsWith("/")) { |
| 37 | mkdirSync(target, { recursive: true }); |
| 38 | } else { |
| 39 | mkdirSync(dirname(target), { recursive: true }); |
| 40 | writeFileSync(target, ""); |
| 41 | } |
| 42 | }); |
| 43 | |
| 44 | execFileSync("git", ["init", "--quiet"], { cwd: root }); |
| 45 | return paths.filter((path) => { |
| 46 | const result = spawnSync("git", ["check-ignore", "--no-index", path], { |
| 47 | cwd: root, |
| 48 | encoding: "utf8", |
| 49 | }); |
| 50 | return result.status !== 0; |
| 51 | }); |
| 52 | } finally { |
| 53 | rmSync(root, { recursive: true, force: true }); |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | describe.skipIf(IS_WINDOWS || !gitAvailable)("parity with git check-ignore", () => { |
| 58 | cases(({ description, patterns, skip_test_fixture, paths, expected }) => { |
| 59 | if ( |
| 60 | skip_test_fixture || |
| 61 | !paths.some(Boolean) || |
| 62 | !expected.every((path: string) => !path.startsWith(".git/")) |
| 63 | ) { |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | it(description, () => { |
| 68 | expect(nativeGitResult(patterns, paths).sort()).toEqual(expected.sort()); |
| 69 | }); |
| 70 | }); |
| 71 | }); |