forked from coder/code-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchild_process.test.ts
More file actions
103 lines (84 loc) · 2.66 KB
/
Copy pathchild_process.test.ts
File metadata and controls
103 lines (84 loc) · 2.66 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { ChildProcess } from "child_process";
import * as path from "path";
import { Readable } from "stream";
import * as util from "util";
import { createClient } from "@coder/protocol/test";
import { Module } from "../src/common/proxy";
describe("child_process", () => {
const client = createClient();
const cp = client.modules[Module.ChildProcess];
const getStdout = async (proc: ChildProcess): Promise<string> => {
return new Promise((r): Readable => proc.stdout!.once("data", r))
.then((s) => s.toString());
};
describe("exec", () => {
it("should get exec stdout", async () => {
await expect(util.promisify(cp.exec)("echo test", { encoding: "utf8" }))
.resolves.toEqual({
stdout: "test\n",
stderr: "",
});
});
});
describe("spawn", () => {
it("should get spawn stdout", async () => {
const proc = cp.spawn("echo", ["test"]);
await expect(Promise.all([
getStdout(proc),
new Promise((r): ChildProcess => proc.on("exit", r)),
]).then((values) => values[0])).resolves.toEqual("test\n");
});
it("should cat", async () => {
const proc = cp.spawn("cat", []);
expect(proc.pid).toBe(-1);
proc.stdin!.write("banana");
await expect(getStdout(proc)).resolves.toBe("banana");
proc.stdin!.end();
proc.kill();
expect(proc.pid).toBeGreaterThan(-1);
await new Promise((r): ChildProcess => proc.on("exit", r));
});
it("should print env", async () => {
const proc = cp.spawn("env", [], {
env: { hi: "donkey" },
});
await expect(getStdout(proc)).resolves.toContain("hi=donkey\n");
});
it("should eval", async () => {
const proc = cp.spawn("node", ["-e", "console.log('foo')"]);
await expect(getStdout(proc)).resolves.toContain("foo");
});
});
describe("fork", () => {
it("should echo messages", async () => {
const proc = cp.fork(path.join(__dirname, "forker.js"));
proc.send({ bananas: true });
await expect(new Promise((r): ChildProcess => proc.on("message", r)))
.resolves.toMatchObject({
bananas: true,
});
proc.kill();
await new Promise((r): ChildProcess => proc.on("exit", r));
});
});
it("should dispose", (done) => {
setTimeout(() => {
client.dispose();
done();
}, 100);
});
it("should disconnect", async () => {
const client = createClient();
const cp = client.modules[Module.ChildProcess];
const proc = cp.fork(path.join(__dirname, "forker.js"));
const fn = jest.fn();
proc.on("error", fn);
proc.send({ bananas: true });
await expect(new Promise((r): ChildProcess => proc.on("message", r)))
.resolves.toMatchObject({
bananas: true,
});
client.dispose();
expect(fn).toHaveBeenCalledWith(new Error("disconnected"));
});
});