Merge 7cb17860c6 into 28f8d00e9f
This commit is contained in:
commit
4959dd1efa
@ -1,34 +1,86 @@
|
|||||||
import { describe, expect, it, vi } from "vitest";
|
import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";
|
||||||
|
|
||||||
import type { runExec } from "../process/exec.js";
|
import type { runExec } from "../process/exec.js";
|
||||||
import type { RuntimeEnv } from "../runtime.js";
|
import type { RuntimeEnv } from "../runtime.js";
|
||||||
import { ensureBinary } from "./binaries.js";
|
import { ensureBinary } from "./binaries.js";
|
||||||
|
|
||||||
|
const originalPlatform = process.platform;
|
||||||
|
|
||||||
describe("ensureBinary", () => {
|
describe("ensureBinary", () => {
|
||||||
it("passes through when binary exists", async () => {
|
afterEach(() => {
|
||||||
const exec: typeof runExec = vi.fn().mockResolvedValue({
|
// Restore original platform after each test
|
||||||
stdout: "",
|
Object.defineProperty(process, "platform", {
|
||||||
stderr: "",
|
value: originalPlatform,
|
||||||
});
|
});
|
||||||
const runtime: RuntimeEnv = {
|
|
||||||
log: vi.fn(),
|
|
||||||
error: vi.fn(),
|
|
||||||
exit: vi.fn(),
|
|
||||||
};
|
|
||||||
await ensureBinary("node", exec, runtime);
|
|
||||||
expect(exec).toHaveBeenCalledWith("which", ["node"]);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("logs and exits when missing", async () => {
|
describe("on Unix", () => {
|
||||||
const exec: typeof runExec = vi.fn().mockRejectedValue(new Error("missing"));
|
beforeEach(() => {
|
||||||
const error = vi.fn();
|
Object.defineProperty(process, "platform", {
|
||||||
const exit = vi.fn(() => {
|
value: "linux",
|
||||||
throw new Error("exit");
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("passes through when binary exists", async () => {
|
||||||
|
const exec: typeof runExec = vi.fn().mockResolvedValue({
|
||||||
|
stdout: "",
|
||||||
|
stderr: "",
|
||||||
|
});
|
||||||
|
const runtime: RuntimeEnv = {
|
||||||
|
log: vi.fn(),
|
||||||
|
error: vi.fn(),
|
||||||
|
exit: vi.fn(),
|
||||||
|
};
|
||||||
|
await ensureBinary("node", exec, runtime);
|
||||||
|
expect(exec).toHaveBeenCalledWith("which", ["node"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("logs and exits when missing", async () => {
|
||||||
|
const exec: typeof runExec = vi.fn().mockRejectedValue(new Error("missing"));
|
||||||
|
const error = vi.fn();
|
||||||
|
const exit = vi.fn(() => {
|
||||||
|
throw new Error("exit");
|
||||||
|
});
|
||||||
|
await expect(ensureBinary("ghost", exec, { log: vi.fn(), error, exit })).rejects.toThrow(
|
||||||
|
"exit",
|
||||||
|
);
|
||||||
|
expect(error).toHaveBeenCalledWith("Missing required binary: ghost. Please install it.");
|
||||||
|
expect(exit).toHaveBeenCalledWith(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("on Windows", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
Object.defineProperty(process, "platform", {
|
||||||
|
value: "win32",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("passes through when binary exists", async () => {
|
||||||
|
const exec: typeof runExec = vi.fn().mockResolvedValue({
|
||||||
|
stdout: "",
|
||||||
|
stderr: "",
|
||||||
|
});
|
||||||
|
const runtime: RuntimeEnv = {
|
||||||
|
log: vi.fn(),
|
||||||
|
error: vi.fn(),
|
||||||
|
exit: vi.fn(),
|
||||||
|
};
|
||||||
|
await ensureBinary("node", exec, runtime);
|
||||||
|
expect(exec).toHaveBeenCalledWith("where", ["node"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("logs and exits when missing", async () => {
|
||||||
|
const exec: typeof runExec = vi.fn().mockRejectedValue(new Error("missing"));
|
||||||
|
const error = vi.fn();
|
||||||
|
const exit = vi.fn(() => {
|
||||||
|
throw new Error("exit");
|
||||||
|
});
|
||||||
|
await expect(ensureBinary("ghost", exec, { log: vi.fn(), error, exit })).rejects.toThrow(
|
||||||
|
"exit",
|
||||||
|
);
|
||||||
|
expect(error).toHaveBeenCalledWith("Missing required binary: ghost. Please install it.");
|
||||||
|
expect(exit).toHaveBeenCalledWith(1);
|
||||||
});
|
});
|
||||||
await expect(ensureBinary("ghost", exec, { log: vi.fn(), error, exit })).rejects.toThrow(
|
|
||||||
"exit",
|
|
||||||
);
|
|
||||||
expect(error).toHaveBeenCalledWith("Missing required binary: ghost. Please install it.");
|
|
||||||
expect(exit).toHaveBeenCalledWith(1);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -7,7 +7,9 @@ export async function ensureBinary(
|
|||||||
runtime: RuntimeEnv = defaultRuntime,
|
runtime: RuntimeEnv = defaultRuntime,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
// Abort early if a required CLI tool is missing.
|
// Abort early if a required CLI tool is missing.
|
||||||
await exec("which", [name]).catch(() => {
|
// Windows uses 'where', Unix uses 'which'
|
||||||
|
const command = process.platform === "win32" ? "where" : "which";
|
||||||
|
await exec(command, [name]).catch(() => {
|
||||||
runtime.error(`Missing required binary: ${name}. Please install it.`);
|
runtime.error(`Missing required binary: ${name}. Please install it.`);
|
||||||
runtime.exit(1);
|
runtime.exit(1);
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user