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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import {
test,
testPerm,
assert,
assertEquals,
assertStrContains
} from "./test_util.ts";
const { kill, run, readFile, open, makeTempDir, writeFile } = Deno;
test(function runPermissions(): void {
let caughtError = false;
try {
Deno.run({ args: ["python", "-c", "print('hello world')"] });
} catch (e) {
caughtError = true;
assert(e instanceof Deno.errors.PermissionDenied);
}
assert(caughtError);
});
testPerm({ run: true }, async function runSuccess(): Promise<void> {
const p = run({
args: ["python", "-c", "print('hello world')"]
});
const status = await p.status();
console.log("status", status);
assertEquals(status.success, true);
assertEquals(status.code, 0);
assertEquals(status.signal, undefined);
p.close();
});
testPerm({ run: true }, async function runCommandFailedWithCode(): Promise<
void
> {
const p = run({
args: ["python", "-c", "import sys;sys.exit(41 + 1)"]
});
const status = await p.status();
assertEquals(status.success, false);
assertEquals(status.code, 42);
assertEquals(status.signal, undefined);
p.close();
});
testPerm({ run: true }, async function runCommandFailedWithSignal(): Promise<
void
> {
if (Deno.build.os === "win") {
return; // No signals on windows.
}
const p = run({
args: ["python", "-c", "import os;os.kill(os.getpid(), 9)"]
});
const status = await p.status();
assertEquals(status.success, false);
assertEquals(status.code, undefined);
assertEquals(status.signal, 9);
p.close();
});
testPerm({ run: true }, function runNotFound(): void {
let error;
try {
run({ args: ["this file hopefully doesn't exist"] });
} catch (e) {
error = e;
}
assert(error !== undefined);
assert(error instanceof Deno.errors.NotFound);
});
testPerm(
{ write: true, run: true },
async function runWithCwdIsAsync(): Promise<void> {
const enc = new TextEncoder();
const cwd = await makeTempDir({ prefix: "deno_command_test" });
const exitCodeFile = "deno_was_here";
const pyProgramFile = "poll_exit.py";
const pyProgram = `
from sys import exit
from time import sleep
while True:
try:
with open("${exitCodeFile}", "r") as f:
line = f.readline()
code = int(line)
exit(code)
except IOError:
# Retry if we got here before deno wrote the file.
sleep(0.01)
pass
`;
Deno.writeFileSync(`${cwd}/${pyProgramFile}.py`, enc.encode(pyProgram));
const p = run({
cwd,
args: ["python", `${pyProgramFile}.py`]
});
// Write the expected exit code *after* starting python.
// This is how we verify that `run()` is actually asynchronous.
const code = 84;
Deno.writeFileSync(`${cwd}/${exitCodeFile}`, enc.encode(`${code}`));
const status = await p.status();
assertEquals(status.success, false);
assertEquals(status.code, code);
assertEquals(status.signal, undefined);
p.close();
}
);
testPerm({ run: true }, async function runStdinPiped(): Promise<void> {
const p = run({
args: ["python", "-c", "import sys; assert 'hello' == sys.stdin.read();"],
stdin: "piped"
});
assert(p.stdin);
assert(!p.stdout);
assert(!p.stderr);
const msg = new TextEncoder().encode("hello");
const n = await p.stdin.write(msg);
assertEquals(n, msg.byteLength);
p.stdin!.close();
const status = await p.status();
assertEquals(status.success, true);
assertEquals(status.code, 0);
assertEquals(status.signal, undefined);
p.close();
});
testPerm({ run: true }, async function runStdoutPiped(): Promise<void> {
const p = run({
args: ["python", "-c", "import sys; sys.stdout.write('hello')"],
stdout: "piped"
});
assert(!p.stdin);
assert(!p.stderr);
const data = new Uint8Array(10);
let r = await p.stdout!.read(data);
if (r === Deno.EOF) {
throw new Error("p.stdout.read(...) should not be EOF");
}
assertEquals(r, 5);
const s = new TextDecoder().decode(data.subarray(0, r));
assertEquals(s, "hello");
r = await p.stdout!.read(data);
assertEquals(r, Deno.EOF);
p.stdout!.close();
const status = await p.status();
assertEquals(status.success, true);
assertEquals(status.code, 0);
assertEquals(status.signal, undefined);
p.close();
});
testPerm({ run: true }, async function runStderrPiped(): Promise<void> {
const p = run({
args: ["python", "-c", "import sys; sys.stderr.write('hello')"],
stderr: "piped"
});
assert(!p.stdin);
assert(!p.stdout);
const data = new Uint8Array(10);
let r = await p.stderr!.read(data);
if (r === Deno.EOF) {
throw new Error("p.stderr.read should not return EOF here");
}
assertEquals(r, 5);
const s = new TextDecoder().decode(data.subarray(0, r));
assertEquals(s, "hello");
r = await p.stderr!.read(data);
assertEquals(r, Deno.EOF);
p.stderr!.close();
const status = await p.status();
assertEquals(status.success, true);
assertEquals(status.code, 0);
assertEquals(status.signal, undefined);
p.close();
});
testPerm({ run: true }, async function runOutput(): Promise<void> {
const p = run({
args: ["python", "-c", "import sys; sys.stdout.write('hello')"],
stdout: "piped"
});
const output = await p.output();
const s = new TextDecoder().decode(output);
assertEquals(s, "hello");
p.close();
});
testPerm({ run: true }, async function runStderrOutput(): Promise<void> {
const p = run({
args: ["python", "-c", "import sys; sys.stderr.write('error')"],
stderr: "piped"
});
const error = await p.stderrOutput();
const s = new TextDecoder().decode(error);
assertEquals(s, "error");
p.close();
});
testPerm(
{ run: true, write: true, read: true },
async function runRedirectStdoutStderr(): Promise<void> {
const tempDir = await makeTempDir();
const fileName = tempDir + "/redirected_stdio.txt";
const file = await open(fileName, "w");
const p = run({
args: [
"python",
"-c",
"import sys; sys.stderr.write('error\\n'); sys.stdout.write('output\\n');"
],
stdout: file.rid,
stderr: file.rid
});
await p.status();
p.close();
file.close();
const fileContents = await readFile(fileName);
const decoder = new TextDecoder();
const text = decoder.decode(fileContents);
assertStrContains(text, "error");
assertStrContains(text, "output");
}
);
testPerm(
{ run: true, write: true, read: true },
async function runRedirectStdin(): Promise<void> {
const tempDir = await makeTempDir();
const fileName = tempDir + "/redirected_stdio.txt";
const encoder = new TextEncoder();
await writeFile(fileName, encoder.encode("hello"));
const file = await open(fileName, "r");
const p = run({
args: ["python", "-c", "import sys; assert 'hello' == sys.stdin.read();"],
stdin: file.rid
});
const status = await p.status();
assertEquals(status.code, 0);
p.close();
file.close();
}
);
testPerm({ run: true }, async function runEnv(): Promise<void> {
const p = run({
args: [
"python",
"-c",
"import os, sys; sys.stdout.write(os.environ.get('FOO', '') + os.environ.get('BAR', ''))"
],
env: {
FOO: "0123",
BAR: "4567"
},
stdout: "piped"
});
const output = await p.output();
const s = new TextDecoder().decode(output);
assertEquals(s, "01234567");
p.close();
});
testPerm({ run: true }, async function runClose(): Promise<void> {
const p = run({
args: [
"python",
"-c",
"from time import sleep; import sys; sleep(10000); sys.stderr.write('error')"
],
stderr: "piped"
});
assert(!p.stdin);
assert(!p.stdout);
p.close();
const data = new Uint8Array(10);
const r = await p.stderr!.read(data);
assertEquals(r, Deno.EOF);
});
test(function signalNumbers(): void {
if (Deno.build.os === "mac") {
assertEquals(Deno.Signal.SIGSTOP, 17);
} else if (Deno.build.os === "linux") {
assertEquals(Deno.Signal.SIGSTOP, 19);
}
});
// Ignore signal tests on windows for now...
if (Deno.build.os !== "win") {
test(function killPermissions(): void {
let caughtError = false;
try {
// Unlike the other test cases, we don't have permission to spawn a
// subprocess we can safely kill. Instead we send SIGCONT to the current
// process - assuming that Deno does not have a special handler set for it
// and will just continue even if a signal is erroneously sent.
Deno.kill(Deno.pid, Deno.Signal.SIGCONT);
} catch (e) {
caughtError = true;
assert(e instanceof Deno.errors.PermissionDenied);
}
assert(caughtError);
});
testPerm({ run: true }, async function killSuccess(): Promise<void> {
const p = run({
args: ["python", "-c", "from time import sleep; sleep(10000)"]
});
assertEquals(Deno.Signal.SIGINT, 2);
kill(p.pid, Deno.Signal.SIGINT);
const status = await p.status();
assertEquals(status.success, false);
// TODO(ry) On Linux, status.code is sometimes undefined and sometimes 1.
// The following assert is causing this test to be flaky. Investigate and
// re-enable when it can be made deterministic.
// assertEquals(status.code, 1);
// assertEquals(status.signal, Deno.Signal.SIGINT);
});
testPerm({ run: true }, async function killFailed(): Promise<void> {
const p = run({
args: ["python", "-c", "from time import sleep; sleep(10000)"]
});
assert(!p.stdin);
assert(!p.stdout);
let err;
try {
kill(p.pid, 12345);
} catch (e) {
err = e;
}
assert(!!err);
assert(err instanceof TypeError);
p.close();
});
}
|