|
| 1 | +/** |
| 2 | + * Tests for the worktree "copy ignored files" step: pattern parsing, |
| 3 | + * gitignore-style matching against `git ls-files` output, and the actual |
| 4 | + * copy into a freshly created worktree. The copy test uses a real git repo |
| 5 | + * in a temp directory so git decides which entries are actually ignored. |
| 6 | + */ |
| 7 | + |
| 8 | +import { mkdtemp, mkdir, readFile, rm, writeFile, stat } from "node:fs/promises"; |
| 9 | +import { tmpdir } from "node:os"; |
| 10 | +import { execFile } from "node:child_process"; |
| 11 | +import { join } from "node:path"; |
| 12 | +import { promisify } from "node:util"; |
| 13 | +import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 14 | +import { parseCopyPatterns } from "@/shared/worktree"; |
| 15 | +import { copyIgnoredFilesIntoWorktree, matchIgnoredCopyEntries } from "./copyIgnoredFiles"; |
| 16 | + |
| 17 | +const execFileAsync = promisify(execFile); |
| 18 | + |
| 19 | +describe("parseCopyPatterns", () => { |
| 20 | + it("splits lines, trims, and drops blanks and comments", () => { |
| 21 | + expect(parseCopyPatterns(" .env \n\n# comment\n.env.*\r\n")).toEqual([".env", ".env.*"]); |
| 22 | + }); |
| 23 | + |
| 24 | + it("returns empty array for empty input", () => { |
| 25 | + expect(parseCopyPatterns("")).toEqual([]); |
| 26 | + expect(parseCopyPatterns("\n# only a comment\n")).toEqual([]); |
| 27 | + }); |
| 28 | +}); |
| 29 | + |
| 30 | +describe("matchIgnoredCopyEntries", () => { |
| 31 | + it("matches files with gitignore-style wildcards", () => { |
| 32 | + expect(matchIgnoredCopyEntries([".env.local", "node_modules/"], [".env.*"])).toEqual([ |
| 33 | + ".env.local", |
| 34 | + ]); |
| 35 | + }); |
| 36 | + |
| 37 | + it("matches nested files for patterns without a slash", () => { |
| 38 | + expect(matchIgnoredCopyEntries(["packages/app/.env", "dist/"], [".env"])).toEqual([ |
| 39 | + "packages/app/.env", |
| 40 | + ]); |
| 41 | + }); |
| 42 | + |
| 43 | + it("matches collapsed directory entries", () => { |
| 44 | + expect(matchIgnoredCopyEntries(["secrets/", ".env"], ["secrets"])).toEqual(["secrets/"]); |
| 45 | + }); |
| 46 | + |
| 47 | + it("matches collapsed directory entries with trailing-slash patterns", () => { |
| 48 | + expect(matchIgnoredCopyEntries(["secrets/", ".env"], ["secrets/"])).toEqual(["secrets/"]); |
| 49 | + }); |
| 50 | + |
| 51 | + it("returns nothing when patterns are empty", () => { |
| 52 | + expect(matchIgnoredCopyEntries([".env"], [])).toEqual([]); |
| 53 | + }); |
| 54 | +}); |
| 55 | + |
| 56 | +describe("copyIgnoredFilesIntoWorktree", () => { |
| 57 | + let repoDir: string; |
| 58 | + let worktreeDir: string; |
| 59 | + |
| 60 | + beforeEach(async () => { |
| 61 | + repoDir = await mkdtemp(join(tmpdir(), "lightcode-copy-src-")); |
| 62 | + worktreeDir = await mkdtemp(join(tmpdir(), "lightcode-copy-dest-")); |
| 63 | + |
| 64 | + await execFileAsync("git", ["init"], { cwd: repoDir }); |
| 65 | + await writeFile(join(repoDir, ".gitignore"), ".env*\nsecrets/\n"); |
| 66 | + await writeFile(join(repoDir, ".env"), "ROOT=1\n"); |
| 67 | + await writeFile(join(repoDir, ".env.local"), "LOCAL=1\n"); |
| 68 | + await mkdir(join(repoDir, "packages", "app"), { recursive: true }); |
| 69 | + await writeFile(join(repoDir, "packages", "app", ".env"), "NESTED=1\n"); |
| 70 | + await mkdir(join(repoDir, "secrets"), { recursive: true }); |
| 71 | + await writeFile(join(repoDir, "secrets", "key.pem"), "KEY\n"); |
| 72 | + await writeFile(join(repoDir, "tracked.ts"), "export {};\n"); |
| 73 | + }); |
| 74 | + |
| 75 | + afterEach(async () => { |
| 76 | + await rm(repoDir, { recursive: true, force: true }); |
| 77 | + await rm(worktreeDir, { recursive: true, force: true }); |
| 78 | + }); |
| 79 | + |
| 80 | + function location() { |
| 81 | + return { kind: "posix" as const, path: repoDir }; |
| 82 | + } |
| 83 | + |
| 84 | + it("copies matching ignored files preserving relative paths", async () => { |
| 85 | + await copyIgnoredFilesIntoWorktree(location(), worktreeDir, [".env", ".env.*"]); |
| 86 | + |
| 87 | + expect(await readFile(join(worktreeDir, ".env"), "utf8")).toBe("ROOT=1\n"); |
| 88 | + expect(await readFile(join(worktreeDir, ".env.local"), "utf8")).toBe("LOCAL=1\n"); |
| 89 | + expect(await readFile(join(worktreeDir, "packages", "app", ".env"), "utf8")).toBe("NESTED=1\n"); |
| 90 | + await expect(stat(join(worktreeDir, "secrets"))).rejects.toThrow(/ENOENT/); |
| 91 | + await expect(stat(join(worktreeDir, "tracked.ts"))).rejects.toThrow(/ENOENT/); |
| 92 | + }); |
| 93 | + |
| 94 | + it("copies matched directories recursively", async () => { |
| 95 | + await copyIgnoredFilesIntoWorktree(location(), worktreeDir, ["secrets"]); |
| 96 | + |
| 97 | + expect(await readFile(join(worktreeDir, "secrets", "key.pem"), "utf8")).toBe("KEY\n"); |
| 98 | + await expect(stat(join(worktreeDir, ".env"))).rejects.toThrow(/ENOENT/); |
| 99 | + }); |
| 100 | + |
| 101 | + it("never overwrites files that already exist in the worktree", async () => { |
| 102 | + await writeFile(join(worktreeDir, ".env"), "EXISTING=1\n"); |
| 103 | + |
| 104 | + await copyIgnoredFilesIntoWorktree(location(), worktreeDir, [".env", ".env.*"]); |
| 105 | + |
| 106 | + expect(await readFile(join(worktreeDir, ".env"), "utf8")).toBe("EXISTING=1\n"); |
| 107 | + expect(await readFile(join(worktreeDir, ".env.local"), "utf8")).toBe("LOCAL=1\n"); |
| 108 | + }); |
| 109 | + |
| 110 | + it("is a no-op when patterns are empty", async () => { |
| 111 | + await copyIgnoredFilesIntoWorktree(location(), worktreeDir, []); |
| 112 | + |
| 113 | + await expect(stat(join(worktreeDir, ".env"))).rejects.toThrow(/ENOENT/); |
| 114 | + }); |
| 115 | +}); |
0 commit comments