2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2019-06-21 19:00:14 -04:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
2020-10-26 11:03:30 -04:00
|
|
|
assertStringIncludes,
|
2020-09-27 06:22:32 -04:00
|
|
|
assertThrows,
|
2020-03-28 13:03:49 -04:00
|
|
|
unitTest,
|
2019-06-21 19:00:14 -04:00
|
|
|
} from "./test_util.ts";
|
2018-11-15 23:07:40 -05:00
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
unitTest({ perms: { read: true } }, function runPermissions() {
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows(() => {
|
2020-12-21 07:13:09 -05:00
|
|
|
Deno.run({ cmd: [Deno.execPath(), "eval", "console.log('hello world')"] });
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.PermissionDenied);
|
2018-11-15 23:07:40 -05:00
|
|
|
});
|
|
|
|
|
2020-12-21 07:13:09 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { run: true, read: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function runSuccess() {
|
2020-12-21 07:13:09 -05:00
|
|
|
const p = Deno.run({
|
|
|
|
cmd: [Deno.execPath(), "eval", "console.log('hello world')"],
|
|
|
|
stdout: "piped",
|
|
|
|
stderr: "null",
|
|
|
|
});
|
|
|
|
const status = await p.status();
|
|
|
|
assertEquals(status.success, true);
|
|
|
|
assertEquals(status.code, 0);
|
|
|
|
assertEquals(status.signal, undefined);
|
|
|
|
p.stdout.close();
|
|
|
|
p.close();
|
|
|
|
},
|
|
|
|
);
|
2020-08-12 14:20:34 -04:00
|
|
|
|
2020-12-21 07:13:09 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { run: true, read: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function runUrl() {
|
2020-12-21 07:13:09 -05:00
|
|
|
const p = Deno.run({
|
|
|
|
cmd: [
|
|
|
|
new URL(`file:///${Deno.execPath()}`),
|
|
|
|
"eval",
|
|
|
|
"console.log('hello world')",
|
|
|
|
],
|
|
|
|
stdout: "piped",
|
|
|
|
stderr: "null",
|
|
|
|
});
|
|
|
|
const status = await p.status();
|
|
|
|
assertEquals(status.success, true);
|
|
|
|
assertEquals(status.code, 0);
|
|
|
|
assertEquals(status.signal, undefined);
|
|
|
|
p.stdout.close();
|
|
|
|
p.close();
|
|
|
|
},
|
|
|
|
);
|
2020-08-12 14:20:34 -04:00
|
|
|
|
2020-12-21 07:13:09 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { run: true, read: true } },
|
|
|
|
async function runStdinRid0(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
|
|
|
const p = Deno.run({
|
|
|
|
cmd: [Deno.execPath(), "eval", "console.log('hello world')"],
|
|
|
|
stdin: 0,
|
|
|
|
stdout: "piped",
|
|
|
|
stderr: "null",
|
|
|
|
});
|
|
|
|
const status = await p.status();
|
|
|
|
assertEquals(status.success, true);
|
|
|
|
assertEquals(status.code, 0);
|
|
|
|
assertEquals(status.signal, undefined);
|
|
|
|
p.stdout.close();
|
|
|
|
p.close();
|
|
|
|
},
|
|
|
|
);
|
2020-06-25 12:38:19 -04:00
|
|
|
|
2020-12-21 07:13:09 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { run: true, read: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
function runInvalidStdio() {
|
2020-12-21 07:13:09 -05:00
|
|
|
assertThrows(() =>
|
|
|
|
Deno.run({
|
|
|
|
cmd: [Deno.execPath(), "eval", "console.log('hello world')"],
|
|
|
|
// @ts-expect-error because Deno.run should throw on invalid stdin.
|
|
|
|
stdin: "a",
|
|
|
|
})
|
|
|
|
);
|
|
|
|
assertThrows(() =>
|
|
|
|
Deno.run({
|
|
|
|
cmd: [Deno.execPath(), "eval", "console.log('hello world')"],
|
|
|
|
// @ts-expect-error because Deno.run should throw on invalid stdout.
|
|
|
|
stdout: "b",
|
|
|
|
})
|
|
|
|
);
|
|
|
|
assertThrows(() =>
|
|
|
|
Deno.run({
|
|
|
|
cmd: [Deno.execPath(), "eval", "console.log('hello world')"],
|
|
|
|
// @ts-expect-error because Deno.run should throw on invalid stderr.
|
|
|
|
stderr: "c",
|
|
|
|
})
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
2018-11-15 23:07:40 -05:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
2020-12-21 07:13:09 -05:00
|
|
|
{ perms: { run: true, read: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function runCommandFailedWithCode() {
|
2020-06-12 15:23:38 -04:00
|
|
|
const p = Deno.run({
|
2020-12-21 07:13:09 -05:00
|
|
|
cmd: [Deno.execPath(), "eval", "Deno.exit(41 + 1)"],
|
2020-03-04 11:31:14 -05:00
|
|
|
});
|
|
|
|
const status = await p.status();
|
|
|
|
assertEquals(status.success, false);
|
|
|
|
assertEquals(status.code, 42);
|
|
|
|
assertEquals(status.signal, undefined);
|
|
|
|
p.close();
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2018-11-15 23:07:40 -05:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{
|
|
|
|
// No signals on windows.
|
2020-04-28 12:35:23 -04:00
|
|
|
ignore: Deno.build.os === "windows",
|
2020-12-21 07:13:09 -05:00
|
|
|
perms: { run: true, read: true },
|
2020-03-04 11:31:14 -05:00
|
|
|
},
|
2021-08-05 07:08:58 -04:00
|
|
|
async function runCommandFailedWithSignal() {
|
2020-06-12 15:23:38 -04:00
|
|
|
const p = Deno.run({
|
2021-09-06 10:05:33 -04:00
|
|
|
cmd: [
|
|
|
|
Deno.execPath(),
|
|
|
|
"eval",
|
|
|
|
"--unstable",
|
|
|
|
"Deno.kill(Deno.pid, 'SIGKILL')",
|
|
|
|
],
|
2020-03-04 11:31:14 -05:00
|
|
|
});
|
|
|
|
const status = await p.status();
|
|
|
|
assertEquals(status.success, false);
|
2020-06-10 11:10:08 -04:00
|
|
|
assertEquals(status.code, 128 + 9);
|
2020-03-04 11:31:14 -05:00
|
|
|
assertEquals(status.signal, 9);
|
|
|
|
p.close();
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2018-11-15 23:07:40 -05:00
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
unitTest({ perms: { run: true } }, function runNotFound() {
|
2018-11-15 23:07:40 -05:00
|
|
|
let error;
|
|
|
|
try {
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.run({ cmd: ["this file hopefully doesn't exist"] });
|
2018-11-15 23:07:40 -05:00
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
assert(error !== undefined);
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(error instanceof Deno.errors.NotFound);
|
2018-11-15 23:07:40 -05:00
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
2020-12-21 07:13:09 -05:00
|
|
|
{ perms: { write: true, run: true, read: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function runWithCwdIsAsync() {
|
2019-04-21 16:40:10 -04:00
|
|
|
const enc = new TextEncoder();
|
2020-06-12 15:23:38 -04:00
|
|
|
const cwd = await Deno.makeTempDir({ prefix: "deno_command_test" });
|
2018-11-15 23:07:40 -05:00
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
const exitCodeFile = "deno_was_here";
|
2020-12-21 07:13:09 -05:00
|
|
|
const programFile = "poll_exit.ts";
|
|
|
|
const program = `
|
|
|
|
async function tryExit() {
|
|
|
|
try {
|
|
|
|
const code = parseInt(await Deno.readTextFile("${exitCodeFile}"));
|
|
|
|
Deno.exit(code);
|
|
|
|
} catch {
|
|
|
|
// Retry if we got here before deno wrote the file.
|
|
|
|
setTimeout(tryExit, 0.01);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tryExit();
|
2018-11-15 23:07:40 -05:00
|
|
|
`;
|
|
|
|
|
2020-12-21 07:13:09 -05:00
|
|
|
Deno.writeFileSync(`${cwd}/${programFile}`, enc.encode(program));
|
2020-06-12 15:23:38 -04:00
|
|
|
const p = Deno.run({
|
2019-04-21 16:40:10 -04:00
|
|
|
cwd,
|
2020-12-21 07:13:09 -05:00
|
|
|
cmd: [Deno.execPath(), "run", "--allow-read", programFile],
|
2019-04-21 16:40:10 -04:00
|
|
|
});
|
|
|
|
|
2020-12-21 07:13:09 -05:00
|
|
|
// Write the expected exit code *after* starting deno.
|
2019-04-21 16:40:10 -04:00
|
|
|
// This is how we verify that `run()` is actually asynchronous.
|
|
|
|
const code = 84;
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.writeFileSync(`${cwd}/${exitCodeFile}`, enc.encode(`${code}`));
|
2019-04-21 16:40:10 -04:00
|
|
|
|
|
|
|
const status = await p.status();
|
|
|
|
assertEquals(status.success, false);
|
|
|
|
assertEquals(status.code, code);
|
|
|
|
assertEquals(status.signal, undefined);
|
|
|
|
p.close();
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-04-21 16:40:10 -04:00
|
|
|
);
|
2018-11-15 23:07:40 -05:00
|
|
|
|
2020-12-21 07:13:09 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { run: true, read: true } },
|
|
|
|
async function runStdinPiped(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
|
|
|
const p = Deno.run({
|
|
|
|
cmd: [
|
|
|
|
Deno.execPath(),
|
|
|
|
"eval",
|
|
|
|
"if (new TextDecoder().decode(await Deno.readAll(Deno.stdin)) !== 'hello') throw new Error('Expected \\'hello\\'')",
|
|
|
|
],
|
|
|
|
stdin: "piped",
|
|
|
|
});
|
|
|
|
assert(p.stdin);
|
|
|
|
assert(!p.stdout);
|
|
|
|
assert(!p.stderr);
|
2018-11-15 23:07:40 -05:00
|
|
|
|
2020-12-21 07:13:09 -05:00
|
|
|
const msg = new TextEncoder().encode("hello");
|
|
|
|
const n = await p.stdin.write(msg);
|
|
|
|
assertEquals(n, msg.byteLength);
|
2018-11-15 23:07:40 -05:00
|
|
|
|
2020-12-21 07:13:09 -05:00
|
|
|
p.stdin.close();
|
2018-11-15 23:07:40 -05:00
|
|
|
|
2020-12-21 07:13:09 -05:00
|
|
|
const status = await p.status();
|
|
|
|
assertEquals(status.success, true);
|
|
|
|
assertEquals(status.code, 0);
|
|
|
|
assertEquals(status.signal, undefined);
|
|
|
|
p.close();
|
|
|
|
},
|
|
|
|
);
|
2018-11-15 23:07:40 -05:00
|
|
|
|
2020-12-21 07:13:09 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { run: true, read: true } },
|
|
|
|
async function runStdoutPiped(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
|
|
|
const p = Deno.run({
|
|
|
|
cmd: [
|
|
|
|
Deno.execPath(),
|
|
|
|
"eval",
|
|
|
|
"await Deno.stdout.write(new TextEncoder().encode('hello'))",
|
|
|
|
],
|
|
|
|
stdout: "piped",
|
|
|
|
});
|
|
|
|
assert(!p.stdin);
|
|
|
|
assert(!p.stderr);
|
|
|
|
|
|
|
|
const data = new Uint8Array(10);
|
|
|
|
let r = await p.stdout.read(data);
|
|
|
|
if (r === null) {
|
|
|
|
throw new Error("p.stdout.read(...) should not be null");
|
|
|
|
}
|
|
|
|
assertEquals(r, 5);
|
|
|
|
const s = new TextDecoder().decode(data.subarray(0, r));
|
|
|
|
assertEquals(s, "hello");
|
|
|
|
r = await p.stdout.read(data);
|
|
|
|
assertEquals(r, null);
|
|
|
|
p.stdout.close();
|
2018-11-15 23:07:40 -05:00
|
|
|
|
2020-12-21 07:13:09 -05:00
|
|
|
const status = await p.status();
|
|
|
|
assertEquals(status.success, true);
|
|
|
|
assertEquals(status.code, 0);
|
|
|
|
assertEquals(status.signal, undefined);
|
|
|
|
p.close();
|
|
|
|
},
|
|
|
|
);
|
2018-11-15 23:07:40 -05:00
|
|
|
|
2020-12-21 07:13:09 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { run: true, read: true } },
|
|
|
|
async function runStderrPiped(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
|
|
|
const p = Deno.run({
|
|
|
|
cmd: [
|
|
|
|
Deno.execPath(),
|
|
|
|
"eval",
|
|
|
|
"await Deno.stderr.write(new TextEncoder().encode('hello'))",
|
|
|
|
],
|
|
|
|
stderr: "piped",
|
|
|
|
});
|
|
|
|
assert(!p.stdin);
|
|
|
|
assert(!p.stdout);
|
|
|
|
|
|
|
|
const data = new Uint8Array(10);
|
|
|
|
let r = await p.stderr.read(data);
|
|
|
|
if (r === null) {
|
|
|
|
throw new Error("p.stderr.read should not return null here");
|
|
|
|
}
|
|
|
|
assertEquals(r, 5);
|
|
|
|
const s = new TextDecoder().decode(data.subarray(0, r));
|
|
|
|
assertEquals(s, "hello");
|
|
|
|
r = await p.stderr.read(data);
|
|
|
|
assertEquals(r, null);
|
|
|
|
p.stderr!.close();
|
2018-11-15 23:07:40 -05:00
|
|
|
|
2020-12-21 07:13:09 -05:00
|
|
|
const status = await p.status();
|
|
|
|
assertEquals(status.success, true);
|
|
|
|
assertEquals(status.code, 0);
|
|
|
|
assertEquals(status.signal, undefined);
|
|
|
|
p.close();
|
|
|
|
},
|
|
|
|
);
|
2018-11-30 13:44:05 -05:00
|
|
|
|
2020-12-21 07:13:09 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { run: true, read: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function runOutput() {
|
2020-12-21 07:13:09 -05:00
|
|
|
const p = Deno.run({
|
|
|
|
cmd: [
|
|
|
|
Deno.execPath(),
|
|
|
|
"eval",
|
|
|
|
"await Deno.stdout.write(new TextEncoder().encode('hello'))",
|
|
|
|
],
|
|
|
|
stdout: "piped",
|
|
|
|
});
|
|
|
|
const output = await p.output();
|
|
|
|
const s = new TextDecoder().decode(output);
|
|
|
|
assertEquals(s, "hello");
|
|
|
|
p.close();
|
|
|
|
},
|
|
|
|
);
|
2019-02-15 10:37:04 -05:00
|
|
|
|
2020-12-21 07:13:09 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { run: true, read: true } },
|
|
|
|
async function runStderrOutput(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
|
|
|
const p = Deno.run({
|
|
|
|
cmd: [
|
|
|
|
Deno.execPath(),
|
|
|
|
"eval",
|
|
|
|
"await Deno.stderr.write(new TextEncoder().encode('error'))",
|
|
|
|
],
|
|
|
|
stderr: "piped",
|
|
|
|
});
|
|
|
|
const error = await p.stderrOutput();
|
|
|
|
const s = new TextDecoder().decode(error);
|
|
|
|
assertEquals(s, "error");
|
|
|
|
p.close();
|
|
|
|
},
|
|
|
|
);
|
2019-03-28 16:09:46 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { run: true, write: true, read: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function runRedirectStdoutStderr() {
|
2020-06-12 15:23:38 -04:00
|
|
|
const tempDir = await Deno.makeTempDir();
|
2019-06-21 19:00:14 -04:00
|
|
|
const fileName = tempDir + "/redirected_stdio.txt";
|
2020-06-12 15:23:38 -04:00
|
|
|
const file = await Deno.open(fileName, {
|
2020-04-24 18:45:55 -04:00
|
|
|
create: true,
|
|
|
|
write: true,
|
|
|
|
});
|
2019-06-21 19:00:14 -04:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
const p = Deno.run({
|
2020-03-21 17:44:18 -04:00
|
|
|
cmd: [
|
2020-12-21 07:13:09 -05:00
|
|
|
Deno.execPath(),
|
|
|
|
"eval",
|
|
|
|
"Deno.stderr.write(new TextEncoder().encode('error\\n')); Deno.stdout.write(new TextEncoder().encode('output\\n'));",
|
2019-06-21 19:00:14 -04:00
|
|
|
],
|
|
|
|
stdout: file.rid,
|
2020-03-28 13:03:49 -04:00
|
|
|
stderr: file.rid,
|
2019-06-21 19:00:14 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
await p.status();
|
|
|
|
p.close();
|
|
|
|
file.close();
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
const fileContents = await Deno.readFile(fileName);
|
2019-06-21 19:00:14 -04:00
|
|
|
const decoder = new TextDecoder();
|
|
|
|
const text = decoder.decode(fileContents);
|
|
|
|
|
2020-10-26 11:03:30 -04:00
|
|
|
assertStringIncludes(text, "error");
|
|
|
|
assertStringIncludes(text, "output");
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-06-21 19:00:14 -04:00
|
|
|
);
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { run: true, write: true, read: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function runRedirectStdin() {
|
2020-06-12 15:23:38 -04:00
|
|
|
const tempDir = await Deno.makeTempDir();
|
2019-06-21 19:00:14 -04:00
|
|
|
const fileName = tempDir + "/redirected_stdio.txt";
|
|
|
|
const encoder = new TextEncoder();
|
2020-06-12 15:23:38 -04:00
|
|
|
await Deno.writeFile(fileName, encoder.encode("hello"));
|
|
|
|
const file = await Deno.open(fileName);
|
2019-06-21 19:00:14 -04:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
const p = Deno.run({
|
2020-12-21 07:13:09 -05:00
|
|
|
cmd: [
|
|
|
|
Deno.execPath(),
|
|
|
|
"eval",
|
|
|
|
"if (new TextDecoder().decode(await Deno.readAll(Deno.stdin)) !== 'hello') throw new Error('Expected \\'hello\\'')",
|
|
|
|
],
|
2020-03-28 13:03:49 -04:00
|
|
|
stdin: file.rid,
|
2019-06-21 19:00:14 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
const status = await p.status();
|
|
|
|
assertEquals(status.code, 0);
|
|
|
|
p.close();
|
|
|
|
file.close();
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-06-21 19:00:14 -04:00
|
|
|
);
|
|
|
|
|
2020-12-21 07:13:09 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { run: true, read: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function runEnv() {
|
2020-12-21 07:13:09 -05:00
|
|
|
const p = Deno.run({
|
|
|
|
cmd: [
|
|
|
|
Deno.execPath(),
|
|
|
|
"eval",
|
|
|
|
"Deno.stdout.write(new TextEncoder().encode(Deno.env.get('FOO') + Deno.env.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();
|
|
|
|
},
|
|
|
|
);
|
2019-04-21 21:26:56 -04:00
|
|
|
|
2020-12-21 07:13:09 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { run: true, read: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function runClose() {
|
2020-12-21 07:13:09 -05:00
|
|
|
const p = Deno.run({
|
|
|
|
cmd: [
|
|
|
|
Deno.execPath(),
|
|
|
|
"eval",
|
|
|
|
"setTimeout(() => Deno.stdout.write(new TextEncoder().encode('error')), 10000)",
|
|
|
|
],
|
|
|
|
stderr: "piped",
|
|
|
|
});
|
|
|
|
assert(!p.stdin);
|
|
|
|
assert(!p.stdout);
|
2019-04-21 21:26:56 -04:00
|
|
|
|
2020-12-21 07:13:09 -05:00
|
|
|
p.close();
|
2019-04-21 21:26:56 -04:00
|
|
|
|
2020-12-21 07:13:09 -05:00
|
|
|
const data = new Uint8Array(10);
|
|
|
|
const r = await p.stderr.read(data);
|
|
|
|
assertEquals(r, null);
|
|
|
|
p.stderr.close();
|
|
|
|
},
|
|
|
|
);
|
2019-04-21 21:26:56 -04:00
|
|
|
|
2020-09-09 16:40:16 -04:00
|
|
|
unitTest(
|
2020-12-21 07:13:09 -05:00
|
|
|
{ perms: { run: true, read: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function runKillAfterStatus() {
|
2020-09-09 16:40:16 -04:00
|
|
|
const p = Deno.run({
|
2020-12-21 07:13:09 -05:00
|
|
|
cmd: [Deno.execPath(), "eval", 'console.log("hello")'],
|
2020-09-09 16:40:16 -04:00
|
|
|
});
|
|
|
|
await p.status();
|
|
|
|
|
2021-01-13 23:52:12 -05:00
|
|
|
let error = null;
|
|
|
|
try {
|
2021-09-06 10:05:33 -04:00
|
|
|
p.kill("SIGTERM");
|
2021-01-13 23:52:12 -05:00
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(
|
|
|
|
error instanceof Deno.errors.NotFound ||
|
|
|
|
// On Windows, the underlying Windows API may return
|
|
|
|
// `ERROR_ACCESS_DENIED` when the process has exited, but hasn't been
|
|
|
|
// completely cleaned up yet and its `pid` is still valid.
|
|
|
|
(Deno.build.os === "windows" &&
|
|
|
|
error instanceof Deno.errors.PermissionDenied),
|
2020-09-09 16:40:16 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
p.close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
unitTest(function killPermissions() {
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows(() => {
|
2020-05-17 13:11:24 -04:00
|
|
|
// 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.
|
2021-09-06 10:05:33 -04:00
|
|
|
Deno.kill(Deno.pid, "SIGCONT");
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.PermissionDenied);
|
2020-05-17 13:11:24 -04:00
|
|
|
});
|
|
|
|
|
2020-12-21 07:13:09 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { run: true, read: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function killSuccess() {
|
2020-12-21 07:13:09 -05:00
|
|
|
const p = Deno.run({
|
|
|
|
cmd: [Deno.execPath(), "eval", "setTimeout(() => {}, 10000)"],
|
|
|
|
});
|
2019-08-06 01:45:36 -04:00
|
|
|
|
2021-09-06 10:05:33 -04:00
|
|
|
Deno.kill(p.pid, "SIGINT");
|
2020-12-21 07:13:09 -05:00
|
|
|
const status = await p.status();
|
2019-04-21 21:26:56 -04:00
|
|
|
|
2020-12-21 07:13:09 -05:00
|
|
|
assertEquals(status.success, false);
|
|
|
|
try {
|
2021-09-06 10:05:33 -04:00
|
|
|
assertEquals(status.signal, "SIGINT");
|
2020-12-21 07:13:09 -05:00
|
|
|
} catch {
|
|
|
|
// TODO(nayeemrmn): On Windows sometimes the following values are given
|
|
|
|
// instead. Investigate and remove this catch when fixed.
|
2021-09-06 10:05:33 -04:00
|
|
|
assertEquals(status.code, 130);
|
|
|
|
assertEquals(status.signal, 2);
|
2020-12-21 07:13:09 -05:00
|
|
|
}
|
|
|
|
p.close();
|
|
|
|
},
|
|
|
|
);
|
2019-04-21 21:26:56 -04:00
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
unitTest({ perms: { run: true, read: true } }, function killFailed() {
|
2020-06-12 15:23:38 -04:00
|
|
|
const p = Deno.run({
|
2020-12-21 07:13:09 -05:00
|
|
|
cmd: [Deno.execPath(), "eval", "setTimeout(() => {}, 10000)"],
|
2019-04-21 21:26:56 -04:00
|
|
|
});
|
2020-05-17 13:11:24 -04:00
|
|
|
assert(!p.stdin);
|
|
|
|
assert(!p.stdout);
|
2019-04-21 21:26:56 -04:00
|
|
|
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows(() => {
|
2021-09-06 10:05:33 -04:00
|
|
|
// @ts-expect-error testing runtime error of bad signal
|
|
|
|
Deno.kill(p.pid, "foobar");
|
2020-06-24 18:57:08 -04:00
|
|
|
}, TypeError);
|
2019-04-21 21:26:56 -04:00
|
|
|
|
2020-05-17 13:11:24 -04:00
|
|
|
p.close();
|
|
|
|
});
|
2021-08-04 15:47:43 -04:00
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{ perms: { run: true, read: true, env: true } },
|
|
|
|
async function clearEnv(): Promise<void> {
|
|
|
|
const p = Deno.run({
|
|
|
|
cmd: [
|
|
|
|
Deno.execPath(),
|
|
|
|
"eval",
|
|
|
|
"-p",
|
|
|
|
"JSON.stringify(Deno.env.toObject())",
|
|
|
|
],
|
|
|
|
stdout: "piped",
|
|
|
|
clearEnv: true,
|
|
|
|
env: {
|
|
|
|
FOO: "23147",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const obj = JSON.parse(new TextDecoder().decode(await p.output()));
|
|
|
|
|
|
|
|
// can't check for object equality because the OS may set additional env vars for processes
|
|
|
|
// so we check if PATH isn't present as that is a common env var across OS's and isn't set for processes.
|
|
|
|
assertEquals(obj.FOO, "23147");
|
|
|
|
assert(!("PATH" in obj));
|
|
|
|
|
|
|
|
p.close();
|
|
|
|
},
|
|
|
|
);
|