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,
|
2019-06-21 19:00:14 -04:00
|
|
|
} from "./test_util.ts";
|
2018-11-15 23:07:40 -05:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
|
|
|
{ permissions: { read: true, run: false } },
|
|
|
|
function runPermissions() {
|
|
|
|
assertThrows(() => {
|
|
|
|
Deno.run({
|
|
|
|
cmd: [Deno.execPath(), "eval", "console.log('hello world')"],
|
|
|
|
});
|
|
|
|
}, Deno.errors.PermissionDenied);
|
|
|
|
},
|
|
|
|
);
|
2018-11-15 23:07:40 -05:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { 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
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { 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
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { run: true, read: true } },
|
2020-12-21 07:13:09 -05:00
|
|
|
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
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { 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
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { 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
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2020-03-04 11:31:14 -05:00
|
|
|
{
|
2021-09-22 19:50:50 -04:00
|
|
|
permissions: { 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",
|
|
|
|
"Deno.kill(Deno.pid, 'SIGKILL')",
|
|
|
|
],
|
2020-03-04 11:31:14 -05:00
|
|
|
});
|
|
|
|
const status = await p.status();
|
|
|
|
assertEquals(status.success, false);
|
2021-09-27 06:18:02 -04:00
|
|
|
if (Deno.build.os === "windows") {
|
|
|
|
assertEquals(status.code, 1);
|
|
|
|
assertEquals(status.signal, undefined);
|
|
|
|
} else {
|
|
|
|
assertEquals(status.code, 128 + 9);
|
|
|
|
assertEquals(status.signal, 9);
|
|
|
|
}
|
2020-03-04 11:31:14 -05:00
|
|
|
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-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { 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
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { 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
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { run: true, read: true } },
|
2020-12-21 07:13:09 -05:00
|
|
|
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
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { run: true, read: true } },
|
2020-12-21 07:13:09 -05:00
|
|
|
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
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { run: true, read: true } },
|
2020-12-21 07:13:09 -05:00
|
|
|
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
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { 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
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { run: true, read: true } },
|
2020-12-21 07:13:09 -05:00
|
|
|
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
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { 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
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { 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
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { 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
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { 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
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { 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 ||
|
2021-09-27 06:18:02 -04:00
|
|
|
// 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.
|
2021-01-13 23:52:12 -05:00
|
|
|
(Deno.build.os === "windows" &&
|
2021-09-27 06:18:02 -04:00
|
|
|
error instanceof Deno.errors.PermissionDenied),
|
2020-09-09 16:40:16 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
p.close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { run: false } }, 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
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-27 06:18:02 -04:00
|
|
|
{ ignore: Deno.build.os !== "windows", permissions: { run: true } },
|
|
|
|
function negativePidInvalidWindows() {
|
|
|
|
assertThrows(() => {
|
2021-10-07 10:31:02 -04:00
|
|
|
Deno.kill(-1, "SIGTERM");
|
2021-09-27 06:18:02 -04:00
|
|
|
}, TypeError);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-27 06:18:02 -04:00
|
|
|
{ ignore: Deno.build.os !== "windows", permissions: { run: true } },
|
|
|
|
function invalidSignalNameWindows() {
|
|
|
|
assertThrows(() => {
|
|
|
|
Deno.kill(Deno.pid, "SIGUSR1");
|
|
|
|
}, TypeError);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { 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
|
|
|
|
2020-12-21 07:13:09 -05:00
|
|
|
try {
|
2021-10-07 10:31:02 -04:00
|
|
|
Deno.kill(p.pid, "SIGKILL");
|
2021-09-27 06:18:02 -04:00
|
|
|
const status = await p.status();
|
|
|
|
|
|
|
|
assertEquals(status.success, false);
|
2021-09-07 18:45:13 -04:00
|
|
|
if (Deno.build.os === "windows") {
|
2021-09-27 06:18:02 -04:00
|
|
|
assertEquals(status.code, 1);
|
|
|
|
assertEquals(status.signal, undefined);
|
2021-09-07 18:45:13 -04:00
|
|
|
} else {
|
2021-10-07 10:31:02 -04:00
|
|
|
assertEquals(status.code, 137);
|
|
|
|
assertEquals(status.signal, 9);
|
2021-09-07 18:45:13 -04:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
p.close();
|
2020-12-21 07:13:09 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2019-04-21 21:26:56 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ permissions: { 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");
|
2021-09-27 06:18:02 -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
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { run: true, read: true, env: true } },
|
2021-08-04 15:47:43 -04:00
|
|
|
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();
|
|
|
|
},
|
|
|
|
);
|
2021-09-13 13:26:23 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{
|
|
|
|
permissions: { run: true, read: true },
|
|
|
|
ignore: Deno.build.os === "windows",
|
|
|
|
},
|
2021-09-13 13:26:23 -04:00
|
|
|
async function uid(): Promise<void> {
|
|
|
|
const p = Deno.run({
|
|
|
|
cmd: [
|
|
|
|
"id",
|
|
|
|
"-u",
|
|
|
|
],
|
|
|
|
stdout: "piped",
|
|
|
|
});
|
|
|
|
|
|
|
|
const currentUid = new TextDecoder().decode(await p.output());
|
|
|
|
p.close();
|
|
|
|
|
|
|
|
if (currentUid !== "0") {
|
|
|
|
assertThrows(() => {
|
|
|
|
Deno.run({
|
|
|
|
cmd: [
|
|
|
|
"echo",
|
|
|
|
"fhqwhgads",
|
|
|
|
],
|
|
|
|
uid: 0,
|
|
|
|
});
|
|
|
|
}, Deno.errors.PermissionDenied);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
{
|
|
|
|
permissions: { run: true, read: true },
|
|
|
|
ignore: Deno.build.os === "windows",
|
|
|
|
},
|
2021-09-13 13:26:23 -04:00
|
|
|
async function gid(): Promise<void> {
|
|
|
|
const p = Deno.run({
|
|
|
|
cmd: [
|
|
|
|
"id",
|
|
|
|
"-g",
|
|
|
|
],
|
|
|
|
stdout: "piped",
|
|
|
|
});
|
|
|
|
|
|
|
|
const currentGid = new TextDecoder().decode(await p.output());
|
|
|
|
p.close();
|
|
|
|
|
|
|
|
if (currentGid !== "0") {
|
|
|
|
assertThrows(() => {
|
|
|
|
Deno.run({
|
|
|
|
cmd: [
|
|
|
|
"echo",
|
|
|
|
"fhqwhgads",
|
|
|
|
],
|
|
|
|
gid: 0,
|
|
|
|
});
|
|
|
|
}, Deno.errors.PermissionDenied);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|