|
1 | 1 | import { afterEach, beforeEach, expect, test } from "bun:test"; |
2 | | -import { mkdir, rm, writeFile } from "node:fs/promises"; |
| 2 | +import { mkdir, readFile, rm, writeFile } from "node:fs/promises"; |
3 | 3 | import { realpathSync } from "node:fs"; |
4 | 4 | import { join } from "node:path"; |
5 | 5 | import { tmpdir } from "node:os"; |
@@ -51,6 +51,73 @@ test("projectKeyFor shares nested dirs under the same git toplevel", async () => |
51 | 51 | expect(projectKeyFor(nested)).toBe(projectKeyFor(root)); |
52 | 52 | }); |
53 | 53 |
|
| 54 | +test("projectRootFor spawns git rev-parse once for the same cwd", async () => { |
| 55 | + initTemporaryGitRepo(root); |
| 56 | + await commitReadme(root); |
| 57 | + |
| 58 | + const traceFile = join(root, "git-trace2-events"); |
| 59 | + const previousTrace = process.env.GIT_TRACE2_EVENT; |
| 60 | + process.env.GIT_TRACE2_EVENT = traceFile; |
| 61 | + try { |
| 62 | + const first = projectRootFor(root); |
| 63 | + const second = projectRootFor(root); |
| 64 | + expect(second).toBe(first); |
| 65 | + expect(first).toBe(realpathSync(root)); |
| 66 | + const spawns = (await readFile(traceFile, "utf8")) |
| 67 | + .split("\n") |
| 68 | + .filter( |
| 69 | + (line) => |
| 70 | + line.includes('"event":"start"') && line.includes("rev-parse"), |
| 71 | + ).length; |
| 72 | + expect(spawns).toBe(1); |
| 73 | + } finally { |
| 74 | + if (previousTrace === undefined) { |
| 75 | + delete process.env.GIT_TRACE2_EVENT; |
| 76 | + } else { |
| 77 | + process.env.GIT_TRACE2_EVENT = previousTrace; |
| 78 | + } |
| 79 | + } |
| 80 | +}); |
| 81 | + |
| 82 | +test("projectRootFor resolves a directory created after a missing lookup", async () => { |
| 83 | + initTemporaryGitRepo(root); |
| 84 | + await commitReadme(root); |
| 85 | + |
| 86 | + const parent = realpathSync(root); |
| 87 | + const child = join(parent, "child"); |
| 88 | + expect(projectRootFor(child)).toBe(child); |
| 89 | + await mkdir(child); |
| 90 | + expect(projectRootFor(child)).toBe(parent); |
| 91 | +}); |
| 92 | + |
| 93 | +test("projectRootFor retries a nested dir after git was unavailable", async () => { |
| 94 | + initTemporaryGitRepo(root); |
| 95 | + await commitReadme(root); |
| 96 | + |
| 97 | + const nested = join(root, "nested"); |
| 98 | + await mkdir(nested); |
| 99 | + const parent = realpathSync(root); |
| 100 | + // Bun 1.3 ignores PATH rewrites and does not forward process.env mutations |
| 101 | + // unless the child is given `env`. A missing GIT_DIR is not enough: git may |
| 102 | + // keep discovering the work tree. An existing file that is not a git dir |
| 103 | + // makes rev-parse exit non-zero. |
| 104 | + const notGitDir = join(root, "not-a-git-dir"); |
| 105 | + await writeFile(notGitDir, "not a git dir\n"); |
| 106 | + |
| 107 | + const previousGitDir = process.env.GIT_DIR; |
| 108 | + process.env.GIT_DIR = notGitDir; |
| 109 | + try { |
| 110 | + expect(projectRootFor(nested)).toBe(realpathSync(nested)); |
| 111 | + } finally { |
| 112 | + if (previousGitDir === undefined) { |
| 113 | + delete process.env.GIT_DIR; |
| 114 | + } else { |
| 115 | + process.env.GIT_DIR = previousGitDir; |
| 116 | + } |
| 117 | + } |
| 118 | + expect(projectRootFor(nested)).toBe(parent); |
| 119 | +}); |
| 120 | + |
54 | 121 | test("linked worktrees have distinct project roots and keys from main and each other", async () => { |
55 | 122 | initTemporaryGitRepo(root); |
56 | 123 | await commitReadme(root); |
|
0 commit comments