|
| 1 | +import { afterEach, describe, expect, test } from "bun:test"; |
| 2 | +import { mkdtempSync, realpathSync, rmSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { buildSlDiffArgs, runSlText } from "./sl"; |
| 6 | + |
| 7 | +const slAvailable = |
| 8 | + Bun.spawnSync(["sl", "version"], { stdin: "ignore", stdout: "ignore", stderr: "ignore" }) |
| 9 | + .exitCode === 0; |
| 10 | +const tempDirs: string[] = []; |
| 11 | + |
| 12 | +function cleanupTempDirs() { |
| 13 | + while (tempDirs.length > 0) { |
| 14 | + const dir = tempDirs.pop(); |
| 15 | + if (dir) { |
| 16 | + rmSync(dir, { recursive: true, force: true }); |
| 17 | + } |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +function createTempDir(prefix: string) { |
| 22 | + const dir = realpathSync(mkdtempSync(join(tmpdir(), prefix))); |
| 23 | + tempDirs.push(dir); |
| 24 | + return dir; |
| 25 | +} |
| 26 | + |
| 27 | +function sl(cwd: string, ...cmd: string[]) { |
| 28 | + const proc = Bun.spawnSync(["sl", "--noninteractive", "--color", "never", ...cmd], { |
| 29 | + cwd, |
| 30 | + stdout: "pipe", |
| 31 | + stderr: "pipe", |
| 32 | + stdin: "ignore", |
| 33 | + }); |
| 34 | + |
| 35 | + if (proc.exitCode !== 0) { |
| 36 | + const stderr = Buffer.from(proc.stderr).toString("utf8"); |
| 37 | + throw new Error(stderr.trim() || `sl ${cmd.join(" ")} failed`); |
| 38 | + } |
| 39 | + |
| 40 | + return Buffer.from(proc.stdout).toString("utf8"); |
| 41 | +} |
| 42 | + |
| 43 | +function createTempSlRepo(prefix: string) { |
| 44 | + const dir = createTempDir(prefix); |
| 45 | + |
| 46 | + sl(dir, "init", "--git"); |
| 47 | + |
| 48 | + return dir; |
| 49 | +} |
| 50 | + |
| 51 | +afterEach(() => { |
| 52 | + cleanupTempDirs(); |
| 53 | +}); |
| 54 | + |
| 55 | +describe("sl command helpers", () => { |
| 56 | + test("reports a friendly error when sl is not installed or not on PATH", () => { |
| 57 | + expect(() => |
| 58 | + runSlText({ |
| 59 | + input: { |
| 60 | + kind: "vcs", |
| 61 | + staged: false, |
| 62 | + options: { mode: "auto", vcs: "sl" }, |
| 63 | + }, |
| 64 | + args: ["root"], |
| 65 | + slExecutable: "definitely-not-a-real-sl-binary", |
| 66 | + }), |
| 67 | + ).toThrow( |
| 68 | + 'Sapling is required for `hunk diff` when `vcs = "sl"`, but `definitely-not-a-real-sl-binary` was not found in PATH.', |
| 69 | + ); |
| 70 | + }); |
| 71 | + |
| 72 | + test.skipIf(!slAvailable)("reports a friendly error outside a sl repository", () => { |
| 73 | + const dir = createTempDir("hunk-sl-nonrepo-"); |
| 74 | + |
| 75 | + expect(() => |
| 76 | + runSlText({ |
| 77 | + input: { |
| 78 | + kind: "vcs", |
| 79 | + staged: false, |
| 80 | + options: { mode: "auto", vcs: "sl" }, |
| 81 | + }, |
| 82 | + args: ["root"], |
| 83 | + cwd: dir, |
| 84 | + }), |
| 85 | + ).toThrow('`hunk diff` must be run inside a Sapling repository when `vcs = "sl"`.'); |
| 86 | + }); |
| 87 | + |
| 88 | + test.skipIf(!slAvailable)("reports a friendly error for invalid revsets", () => { |
| 89 | + const dir = createTempSlRepo("hunk-sl-invalid-revset-"); |
| 90 | + const input = { |
| 91 | + kind: "vcs" as const, |
| 92 | + range: "missing_revision", |
| 93 | + staged: false, |
| 94 | + options: { mode: "auto" as const, vcs: "sl" as const }, |
| 95 | + }; |
| 96 | + |
| 97 | + expect(() => |
| 98 | + runSlText({ |
| 99 | + input, |
| 100 | + args: buildSlDiffArgs(input), |
| 101 | + cwd: dir, |
| 102 | + }), |
| 103 | + ).toThrow("`hunk diff missing_revision` could not resolve Sapling revset `missing_revision`."); |
| 104 | + }); |
| 105 | +}); |
0 commit comments