-
Notifications
You must be signed in to change notification settings - Fork 574
/
child_process_test.ts
252 lines (228 loc) · 6.99 KB
/
child_process_test.ts
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import {
assert,
assertNotStrictEquals,
assertStrictEquals,
} from "../testing/asserts.ts";
import { spawn } from "./child_process.ts";
import { Deferred, deferred } from "../async/deferred.ts";
const isWindows = Deno.build.os === "windows";
function withTimeout(timeoutInMS: number): Deferred<void> {
const promise = deferred<void>();
const timer = setTimeout(() => {
promise.reject("Timeout");
}, timeoutInMS);
promise.then(() => {
clearTimeout(timer);
});
return promise;
}
// TODO(uki00a): Once Node.js's `parallel/test-child-process-spawn-error.js` works, this test case should be removed.
Deno.test("[node/child_process spawn] The 'error' event is emitted when no binary is found", async () => {
const promise = withTimeout(1000);
const childProcess = spawn("no-such-cmd");
childProcess.on("error", (_err) => {
// TODO Assert an error message.
promise.resolve();
});
await promise;
});
Deno.test("[node/child_process spawn] The 'exit' event is emitted with an exit code after the child process ends", async () => {
const promise = withTimeout(3000);
const childProcess = spawn(Deno.execPath(), ["--help"], {
env: { NO_COLOR: "true" },
});
try {
let exitCode = null;
childProcess.on("exit", (code) => {
promise.resolve();
exitCode = code;
});
await promise;
assertStrictEquals(exitCode, 0);
assertStrictEquals(childProcess.exitCode, exitCode);
} finally {
childProcess.kill();
}
});
Deno.test({
name: "[node/child_process spawn] Verify that stdin and stdout work",
fn: async () => {
const promise = withTimeout(3000);
const childProcess = spawn(Deno.execPath(), ["fmt", "-"], {
env: { NO_COLOR: "true" },
stdio: ["pipe", "pipe"],
});
try {
assert(childProcess.stdin, "stdin should be defined");
assert(childProcess.stdout, "stdout should be defined");
let data = "";
childProcess.stdout.on("data", (chunk) => {
data += chunk;
});
childProcess.stdin.write(" console.log('hello')", "utf-8");
childProcess.stdin.end();
childProcess.on("exit", () => {
promise.resolve();
});
await promise;
assertStrictEquals(data, `console.log("hello");\n`);
} finally {
childProcess.kill();
}
},
});
/* Start of ported part */
// Copyright Joyent and Node contributors. All rights reserved. MIT license.
// Ported from Node 15.5.1
// TODO(uki00a): Remove this case once Node's `parallel/test-child-process-spawn-event.js` works.
Deno.test("[child_process spawn] 'spawn' event", async () => {
const timeout = withTimeout(3000);
const subprocess = spawn("echo", ["ok"]);
let didSpawn = false;
subprocess.on("spawn", function () {
didSpawn = true;
});
function mustNotBeCalled() {
timeout.reject(new Error("function should not have been called"));
}
const promises = [] as Promise<void>[];
function mustBeCalledAfterSpawn() {
const promise = deferred<void>();
promises.push(promise);
return () => {
if (didSpawn) {
promise.resolve();
} else {
promise.reject(
new Error("function should be called after the 'spawn' event"),
);
}
};
}
subprocess.on("error", mustNotBeCalled);
subprocess.stdout!.on("data", mustBeCalledAfterSpawn());
subprocess.stdout!.on("end", mustBeCalledAfterSpawn());
subprocess.stdout!.on("close", mustBeCalledAfterSpawn());
subprocess.stderr!.on("data", mustNotBeCalled);
subprocess.stderr!.on("end", mustBeCalledAfterSpawn());
subprocess.stderr!.on("close", mustBeCalledAfterSpawn());
subprocess.on("exit", mustBeCalledAfterSpawn());
subprocess.on("close", mustBeCalledAfterSpawn());
try {
await Promise.race([Promise.all(promises), timeout]);
timeout.resolve();
} finally {
subprocess.kill();
}
});
// TODO(uki00a): Remove this case once Node's `parallel/test-child-process-spawn-shell.js` works.
Deno.test("[child_process spawn] Verify that a shell is executed", async () => {
const promise = withTimeout(3000);
const doesNotExist = spawn("does-not-exist", { shell: true });
try {
assertNotStrictEquals(doesNotExist.spawnfile, "does-not-exist");
doesNotExist.on("error", () => {
promise.reject("The 'error' event must not be emitted.");
});
doesNotExist.on("exit", (code, signal) => {
assertStrictEquals(signal, null);
if (isWindows) {
assertStrictEquals(code, 1); // Exit code of cmd.exe
} else {
assertStrictEquals(code, 127); // Exit code of /bin/sh });
}
promise.resolve();
});
await promise;
} finally {
doesNotExist.kill();
}
});
// TODO(uki00a): Remove this case once Node's `parallel/test-child-process-spawn-shell.js` works.
Deno.test({
ignore: isWindows,
name: "[node/child_process spawn] Verify that passing arguments works",
async fn() {
const promise = withTimeout(3000);
const echo = spawn("echo", ["foo"], {
shell: true,
});
let echoOutput = "";
try {
assertStrictEquals(
echo.spawnargs[echo.spawnargs.length - 1].replace(/"/g, ""),
"echo foo",
);
assert(echo.stdout);
echo.stdout.on("data", (data) => {
echoOutput += data;
});
echo.on("close", () => {
assertStrictEquals(echoOutput.trim(), "foo");
promise.resolve();
});
await promise;
} finally {
echo.kill();
}
},
});
// TODO(uki00a): Remove this case once Node's `parallel/test-child-process-spawn-shell.js` works.
Deno.test({
ignore: isWindows,
name: "[node/child_process spawn] Verity that shell features can be used",
async fn() {
const promise = withTimeout(3000);
const cmd = "echo bar | cat";
const command = spawn(cmd, {
shell: true,
});
try {
let commandOutput = "";
assert(command.stdout);
command.stdout.on("data", (data) => {
commandOutput += data;
});
command.on("close", () => {
assertStrictEquals(commandOutput.trim(), "bar");
promise.resolve();
});
await promise;
} finally {
command.kill();
}
},
});
// TODO(uki00a): Remove this case once Node's `parallel/test-child-process-spawn-shell.js` works.
Deno.test({
ignore: isWindows,
name:
"[node/child_process spawn] Verity that environment is properly inherited",
async fn() {
const promise = withTimeout(3000);
const env = spawn(
`"${Deno.execPath()}" eval -p "Deno.env.toObject().BAZ"`,
{
env: { BAZ: "buzz", NO_COLOR: "true" },
shell: true,
},
);
try {
let envOutput = "";
assert(env.stdout);
env.on("error", (err) => promise.reject(err));
env.stdout.on("data", (data) => {
envOutput += data;
});
env.on("close", () => {
assertStrictEquals(envOutput.trim(), "buzz");
promise.resolve();
});
await promise;
} finally {
env.kill();
}
},
});
/* End of ported part */