|
| 1 | +import { test, expect, describe } from "vitest"; |
| 2 | +import path from "node:path"; |
| 3 | +import fs from "fs-extra"; |
| 4 | +import tmp from "tmp"; |
| 5 | +import { execFile } from "node:child_process"; |
| 6 | +import { promisify } from "node:util"; |
| 7 | +import { realpathSync } from "node:fs"; |
| 8 | + |
| 9 | +import toplevel from "./index.js"; |
| 10 | + |
| 11 | +const execFileAsync = promisify(execFile); |
| 12 | + |
| 13 | +/** |
| 14 | + * Normalize a path for cross-platform comparison. |
| 15 | + * On Windows, tmp paths may use short names (e.g., RUNNER~1) while git returns long names. |
| 16 | + * This resolves symlinks and normalizes the path format. |
| 17 | + */ |
| 18 | +function normalizePath(p: string): string { |
| 19 | + return realpathSync(p).replace(/\\/g, "/"); |
| 20 | +} |
| 21 | + |
| 22 | +async function initGitRepo(cwd: string): Promise<void> { |
| 23 | + await execFileAsync("git", ["init"], { cwd }); |
| 24 | + await execFileAsync("git", ["config", "user.email", "test@example.com"], { |
| 25 | + cwd, |
| 26 | + }); |
| 27 | + await execFileAsync("git", ["config", "user.name", "test"], { cwd }); |
| 28 | + await execFileAsync("git", ["config", "commit.gpgsign", "false"], { cwd }); |
| 29 | +} |
| 30 | + |
| 31 | +describe("toplevel", () => { |
| 32 | + test("should return git root for a regular repository", async () => { |
| 33 | + const tmpDir = tmp.dirSync({ keep: false, unsafeCleanup: true }); |
| 34 | + const repoDir = tmpDir.name; |
| 35 | + |
| 36 | + await initGitRepo(repoDir); |
| 37 | + |
| 38 | + const result = await toplevel(repoDir); |
| 39 | + expect(normalizePath(result!)).toBe(normalizePath(repoDir)); |
| 40 | + }); |
| 41 | + |
| 42 | + test("should return git root from a subdirectory", async () => { |
| 43 | + const tmpDir = tmp.dirSync({ keep: false, unsafeCleanup: true }); |
| 44 | + const repoDir = tmpDir.name; |
| 45 | + |
| 46 | + await initGitRepo(repoDir); |
| 47 | + |
| 48 | + const subDir = path.join(repoDir, "sub", "dir"); |
| 49 | + await fs.mkdirp(subDir); |
| 50 | + |
| 51 | + const result = await toplevel(subDir); |
| 52 | + expect(normalizePath(result!)).toBe(normalizePath(repoDir)); |
| 53 | + }); |
| 54 | + |
| 55 | + test("should return undefined for a non-git directory", async () => { |
| 56 | + const tmpDir = tmp.dirSync({ keep: false, unsafeCleanup: true }); |
| 57 | + |
| 58 | + const result = await toplevel(tmpDir.name); |
| 59 | + expect(result).toBeUndefined(); |
| 60 | + }); |
| 61 | + |
| 62 | + test("should work with git worktrees", async () => { |
| 63 | + const tmpDir = tmp.dirSync({ keep: false, unsafeCleanup: true }); |
| 64 | + const mainRepoDir = path.join(tmpDir.name, "main"); |
| 65 | + const worktreeDir = path.join(tmpDir.name, "worktree"); |
| 66 | + |
| 67 | + await fs.mkdirp(mainRepoDir); |
| 68 | + await initGitRepo(mainRepoDir); |
| 69 | + |
| 70 | + // Create an initial commit (required for worktree) |
| 71 | + await fs.writeFile(path.join(mainRepoDir, "file.txt"), "content"); |
| 72 | + await execFileAsync("git", ["add", "."], { cwd: mainRepoDir }); |
| 73 | + await execFileAsync("git", ["commit", "-m", "initial"], { |
| 74 | + cwd: mainRepoDir, |
| 75 | + }); |
| 76 | + |
| 77 | + // Create a new branch for the worktree |
| 78 | + await execFileAsync("git", ["branch", "worktree-branch"], { |
| 79 | + cwd: mainRepoDir, |
| 80 | + }); |
| 81 | + |
| 82 | + // Create the worktree |
| 83 | + await execFileAsync( |
| 84 | + "git", |
| 85 | + ["worktree", "add", worktreeDir, "worktree-branch"], |
| 86 | + { cwd: mainRepoDir }, |
| 87 | + ); |
| 88 | + |
| 89 | + // toplevel should return the worktree directory, not the main repo |
| 90 | + const result = await toplevel(worktreeDir); |
| 91 | + expect(normalizePath(result!)).toBe(normalizePath(worktreeDir)); |
| 92 | + }); |
| 93 | + |
| 94 | + test("should work from a subdirectory of a git worktree", async () => { |
| 95 | + const tmpDir = tmp.dirSync({ keep: false, unsafeCleanup: true }); |
| 96 | + const mainRepoDir = path.join(tmpDir.name, "main"); |
| 97 | + const worktreeDir = path.join(tmpDir.name, "worktree"); |
| 98 | + |
| 99 | + await fs.mkdirp(mainRepoDir); |
| 100 | + await initGitRepo(mainRepoDir); |
| 101 | + |
| 102 | + // Create an initial commit |
| 103 | + await fs.writeFile(path.join(mainRepoDir, "file.txt"), "content"); |
| 104 | + await execFileAsync("git", ["add", "."], { cwd: mainRepoDir }); |
| 105 | + await execFileAsync("git", ["commit", "-m", "initial"], { |
| 106 | + cwd: mainRepoDir, |
| 107 | + }); |
| 108 | + |
| 109 | + // Create a new branch and worktree |
| 110 | + await execFileAsync("git", ["branch", "worktree-branch"], { |
| 111 | + cwd: mainRepoDir, |
| 112 | + }); |
| 113 | + await execFileAsync( |
| 114 | + "git", |
| 115 | + ["worktree", "add", worktreeDir, "worktree-branch"], |
| 116 | + { cwd: mainRepoDir }, |
| 117 | + ); |
| 118 | + |
| 119 | + // Create a subdirectory in the worktree |
| 120 | + const subDir = path.join(worktreeDir, "sub", "dir"); |
| 121 | + await fs.mkdirp(subDir); |
| 122 | + |
| 123 | + // toplevel from subdirectory should return the worktree root |
| 124 | + const result = await toplevel(subDir); |
| 125 | + expect(normalizePath(result!)).toBe(normalizePath(worktreeDir)); |
| 126 | + }); |
| 127 | +}); |
0 commit comments