-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexec.test.ts
More file actions
58 lines (47 loc) · 1.59 KB
/
Copy pathexec.test.ts
File metadata and controls
58 lines (47 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { EventEmitter } from "events";
import { describe, expect, it, vi } from "vitest";
import { spawn } from "child_process";
import { runCommand } from "./exec.js";
vi.mock("child_process", () => ({
spawn: vi.fn(),
}));
function fakeChild() {
const emitter = new EventEmitter();
return emitter;
}
describe("runCommand", () => {
it("spawns the command with inherited stdio and resolves on a zero exit code", async () => {
const child = fakeChild();
vi.mocked(spawn).mockReturnValue(child as never);
const promise = runCommand("echo", ["hi"], "/tmp/proj");
child.emit("close", 0);
await expect(promise).resolves.toBeUndefined();
expect(spawn).toHaveBeenCalledWith("echo", ["hi"], {
stdio: "inherit",
cwd: "/tmp/proj",
shell: true,
env: process.env,
});
});
it("uses a supplied env instead of process.env", async () => {
const child = fakeChild();
vi.mocked(spawn).mockReturnValue(child as never);
const customEnv = { CUSTOM: "1" };
const promise = runCommand("echo", [], "/tmp/proj", customEnv);
child.emit("close", 0);
await promise;
expect(spawn).toHaveBeenCalledWith("echo", [], {
stdio: "inherit",
cwd: "/tmp/proj",
shell: true,
env: customEnv,
});
});
it("rejects with the command name when the exit code is non-zero", async () => {
const child = fakeChild();
vi.mocked(spawn).mockReturnValue(child as never);
const promise = runCommand("failing-cmd", [], "/tmp/proj");
child.emit("close", 1);
await expect(promise).rejects.toThrow("failing-cmd failed");
});
});