|
| 1 | +import { describe, test, expect } from "bun:test"; |
| 2 | +import { resolveProjectRoot, isRootPath } from "./paths.js"; |
| 3 | + |
| 4 | +describe("isRootPath", () => { |
| 5 | + test("detects Unix root /", () => { |
| 6 | + expect(isRootPath("/")).toBe(true); |
| 7 | + }); |
| 8 | + |
| 9 | + test("detects backslash root (Windows)", () => { |
| 10 | + expect(isRootPath("\\")).toBe(true); |
| 11 | + }); |
| 12 | + |
| 13 | + test("detects Windows drive roots", () => { |
| 14 | + expect(isRootPath("C:\\")).toBe(true); |
| 15 | + expect(isRootPath("D:\\")).toBe(true); |
| 16 | + expect(isRootPath("C:")).toBe(true); |
| 17 | + }); |
| 18 | + |
| 19 | + test("detects empty string as root-like", () => { |
| 20 | + expect(isRootPath("")).toBe(true); |
| 21 | + }); |
| 22 | + |
| 23 | + test("detects multiple slashes as root", () => { |
| 24 | + expect(isRootPath("//")).toBe(true); |
| 25 | + expect(isRootPath("\\\\")).toBe(true); |
| 26 | + }); |
| 27 | + |
| 28 | + test("rejects normal directories", () => { |
| 29 | + expect(isRootPath("/home/user/project")).toBe(false); |
| 30 | + expect(isRootPath("/Users/clodpod/projects")).toBe(false); |
| 31 | + expect(isRootPath("E:\\code\\commander")).toBe(false); |
| 32 | + }); |
| 33 | +}); |
| 34 | + |
| 35 | +describe("resolveProjectRoot", () => { |
| 36 | + test("uses dirname(dirname(Bun.main)) for source .ts files", () => { |
| 37 | + const result = resolveProjectRoot( |
| 38 | + "/home/user/commander/src/commander.ts", |
| 39 | + "/usr/bin/bun", |
| 40 | + "/home/user" |
| 41 | + ); |
| 42 | + expect(result).toBe("/home/user/commander"); |
| 43 | + }); |
| 44 | + |
| 45 | + test("uses dirname(dirname(Bun.main)) for source .js files", () => { |
| 46 | + const result = resolveProjectRoot( |
| 47 | + "/home/user/commander/src/commander.js", |
| 48 | + "/usr/bin/bun", |
| 49 | + "/home/user" |
| 50 | + ); |
| 51 | + expect(result).toBe("/home/user/commander"); |
| 52 | + }); |
| 53 | + |
| 54 | + test("uses execPath for compiled binary", () => { |
| 55 | + const result = resolveProjectRoot( |
| 56 | + "/internal/bundle", // Bun.main in compiled binary (not .ts/.js) |
| 57 | + "/home/user/commander/commander-linux-x64", |
| 58 | + "/home/user" |
| 59 | + ); |
| 60 | + expect(result).toBe("/home/user/commander"); |
| 61 | + }); |
| 62 | + |
| 63 | + test("BUG REPRO: does NOT return root when Bun.main is '/' in compiled binary", () => { |
| 64 | + // This is the exact bug: on Windows compiled binaries, Bun.main returns "/" |
| 65 | + // which causes dirname("/") -> "\" on Windows, leading to mkdir("\") -> EPERM |
| 66 | + const result = resolveProjectRoot( |
| 67 | + "/", // Bun.main returns "/" in compiled binary on Windows |
| 68 | + "/home/user/commander/commander-exe", // but execPath is valid |
| 69 | + "/home/user" |
| 70 | + ); |
| 71 | + // Should NOT be "/" or "\" — should use execPath instead |
| 72 | + expect(isRootPath(result)).toBe(false); |
| 73 | + expect(result).toBe("/home/user/commander"); |
| 74 | + }); |
| 75 | + |
| 76 | + test("falls back to cwd when both Bun.main and execPath are root paths", () => { |
| 77 | + const result = resolveProjectRoot( |
| 78 | + "/", |
| 79 | + "/", |
| 80 | + "/home/user/commander" |
| 81 | + ); |
| 82 | + expect(result).toBe("/home/user/commander"); |
| 83 | + }); |
| 84 | + |
| 85 | + test("falls back to cwd when both paths are empty", () => { |
| 86 | + const result = resolveProjectRoot( |
| 87 | + "", |
| 88 | + "", |
| 89 | + "/home/user/commander" |
| 90 | + ); |
| 91 | + expect(result).toBe("/home/user/commander"); |
| 92 | + }); |
| 93 | +}); |
0 commit comments