2019-01-21 14:03:30 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-03-06 20:48:46 -05:00
|
|
|
import { test, testPerm, assert, assertEquals } from "./test_util.ts";
|
2019-02-12 10:08:56 -05:00
|
|
|
const { run, DenoError, ErrorKind } = Deno;
|
2018-11-15 23:07:40 -05:00
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(function runPermissions(): void {
|
2018-11-15 23:07:40 -05:00
|
|
|
let caughtError = false;
|
|
|
|
try {
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.run({ args: ["python", "-c", "print('hello world')"] });
|
2018-11-15 23:07:40 -05:00
|
|
|
} catch (e) {
|
|
|
|
caughtError = true;
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(e.kind, Deno.ErrorKind.PermissionDenied);
|
|
|
|
assertEquals(e.name, "PermissionDenied");
|
2018-11-15 23:07:40 -05:00
|
|
|
}
|
|
|
|
assert(caughtError);
|
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ run: true }, async function runSuccess(): Promise<void> {
|
2018-11-15 23:07:40 -05:00
|
|
|
const p = run({
|
|
|
|
args: ["python", "-c", "print('hello world')"]
|
|
|
|
});
|
|
|
|
const status = await p.status();
|
|
|
|
console.log("status", status);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(status.success, true);
|
|
|
|
assertEquals(status.code, 0);
|
|
|
|
assertEquals(status.signal, undefined);
|
2018-11-15 23:07:40 -05:00
|
|
|
p.close();
|
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ run: true }, async function runCommandFailedWithCode(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
2018-11-15 23:07:40 -05:00
|
|
|
let p = run({
|
|
|
|
args: ["python", "-c", "import sys;sys.exit(41 + 1)"]
|
|
|
|
});
|
|
|
|
let status = await p.status();
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(status.success, false);
|
|
|
|
assertEquals(status.code, 42);
|
|
|
|
assertEquals(status.signal, undefined);
|
2018-11-15 23:07:40 -05:00
|
|
|
p.close();
|
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ run: true }, async function runCommandFailedWithSignal(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
2019-03-06 16:54:58 -05:00
|
|
|
if (Deno.build.os === "win") {
|
2018-11-15 23:07:40 -05:00
|
|
|
return; // No signals on windows.
|
|
|
|
}
|
|
|
|
const p = run({
|
|
|
|
args: ["python", "-c", "import os;os.kill(os.getpid(), 9)"]
|
|
|
|
});
|
|
|
|
const status = await p.status();
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(status.success, false);
|
|
|
|
assertEquals(status.code, undefined);
|
|
|
|
assertEquals(status.signal, 9);
|
2018-11-15 23:07:40 -05:00
|
|
|
p.close();
|
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ run: true }, function runNotFound(): void {
|
2018-11-15 23:07:40 -05:00
|
|
|
let error;
|
|
|
|
try {
|
|
|
|
run({ args: ["this file hopefully doesn't exist"] });
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
assert(error !== undefined);
|
|
|
|
assert(error instanceof DenoError);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(error.kind, ErrorKind.NotFound);
|
2018-11-15 23:07:40 -05:00
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm(
|
|
|
|
{ write: true, run: true },
|
|
|
|
async function runWithCwdIsAsync(): Promise<void> {
|
|
|
|
const enc = new TextEncoder();
|
|
|
|
const cwd = Deno.makeTempDirSync({ 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";
|
|
|
|
const pyProgramFile = "poll_exit.py";
|
|
|
|
const pyProgram = `
|
2018-11-15 23:07:40 -05:00
|
|
|
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
|
|
|
|
`;
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
);
|
2018-11-15 23:07:40 -05:00
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ run: true }, async function runStdinPiped(): Promise<void> {
|
2018-11-15 23:07:40 -05:00
|
|
|
const p = run({
|
|
|
|
args: ["python", "-c", "import sys; assert 'hello' == sys.stdin.read();"],
|
|
|
|
stdin: "piped"
|
|
|
|
});
|
|
|
|
assert(!p.stdout);
|
|
|
|
assert(!p.stderr);
|
|
|
|
|
|
|
|
let msg = new TextEncoder().encode("hello");
|
|
|
|
let n = await p.stdin.write(msg);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(n, msg.byteLength);
|
2018-11-15 23:07:40 -05:00
|
|
|
|
|
|
|
p.stdin.close();
|
|
|
|
|
|
|
|
const status = await p.status();
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(status.success, true);
|
|
|
|
assertEquals(status.code, 0);
|
|
|
|
assertEquals(status.signal, undefined);
|
2018-11-15 23:07:40 -05:00
|
|
|
p.close();
|
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ run: true }, async function runStdoutPiped(): Promise<void> {
|
2018-11-15 23:07:40 -05:00
|
|
|
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);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(r.nread, 5);
|
|
|
|
assertEquals(r.eof, false);
|
2018-11-15 23:07:40 -05:00
|
|
|
const s = new TextDecoder().decode(data.subarray(0, r.nread));
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(s, "hello");
|
2018-11-15 23:07:40 -05:00
|
|
|
r = await p.stdout.read(data);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(r.nread, 0);
|
|
|
|
assertEquals(r.eof, true);
|
2018-11-15 23:07:40 -05:00
|
|
|
p.stdout.close();
|
|
|
|
|
|
|
|
const status = await p.status();
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(status.success, true);
|
|
|
|
assertEquals(status.code, 0);
|
|
|
|
assertEquals(status.signal, undefined);
|
2018-11-15 23:07:40 -05:00
|
|
|
p.close();
|
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ run: true }, async function runStderrPiped(): Promise<void> {
|
2018-11-15 23:07:40 -05:00
|
|
|
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);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(r.nread, 5);
|
|
|
|
assertEquals(r.eof, false);
|
2018-11-15 23:07:40 -05:00
|
|
|
const s = new TextDecoder().decode(data.subarray(0, r.nread));
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(s, "hello");
|
2018-11-15 23:07:40 -05:00
|
|
|
r = await p.stderr.read(data);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(r.nread, 0);
|
|
|
|
assertEquals(r.eof, true);
|
2018-11-15 23:07:40 -05:00
|
|
|
p.stderr.close();
|
|
|
|
|
|
|
|
const status = await p.status();
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(status.success, true);
|
|
|
|
assertEquals(status.code, 0);
|
|
|
|
assertEquals(status.signal, undefined);
|
2018-11-15 23:07:40 -05:00
|
|
|
p.close();
|
|
|
|
});
|
2018-11-30 13:44:05 -05:00
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ run: true }, async function runOutput(): Promise<void> {
|
2018-11-30 13:44:05 -05:00
|
|
|
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);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(s, "hello");
|
2018-11-30 13:44:05 -05:00
|
|
|
p.close();
|
|
|
|
});
|
2019-02-15 10:37:04 -05:00
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ run: true }, async function runStderrOutput(): Promise<void> {
|
2019-03-28 16:09:46 -04:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ run: true }, async function runEnv(): Promise<void> {
|
2019-02-15 10:37:04 -05:00
|
|
|
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);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(s, "01234567");
|
2019-02-15 10:37:04 -05:00
|
|
|
p.close();
|
|
|
|
});
|