Skip to content

Commit

Permalink
refactor: use spawn API across codebase (denoland#14414)
Browse files Browse the repository at this point in the history
  • Loading branch information
crowlKats committed May 18, 2022
1 parent 5ad8919 commit 4e1ca1d
Show file tree
Hide file tree
Showing 25 changed files with 266 additions and 327 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ jobs:
if: matrix.job == 'lint'
uses: denoland/setup-deno@v1
with:
deno-version: v1.19.3
deno-version: v1.x

- name: Install Python
uses: actions/setup-python@v1
Expand Down
14 changes: 10 additions & 4 deletions cli/tests/integration/run_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,9 @@ itest!(_044_bad_resource {
exit_code: 1,
});

// TODO(bartlomieju): remove --unstable once Deno.spawn is stabilized
itest!(_045_proxy {
args: "run -L debug --allow-net --allow-env --allow-run --allow-read --reload --quiet 045_proxy_test.ts",
args: "run -L debug --unstable --allow-net --allow-env --allow-run --allow-read --reload --quiet 045_proxy_test.ts",
output: "045_proxy_test.ts.out",
http_server: true,
});
Expand Down Expand Up @@ -509,8 +510,9 @@ itest!(_088_dynamic_import_already_evaluating {
output: "088_dynamic_import_already_evaluating.ts.out",
});

// TODO(bartlomieju): remove --unstable once Deno.spawn is stabilized
itest!(_089_run_allow_list {
args: "run --allow-run=curl 089_run_allow_list.ts",
args: "run --unstable --allow-run=curl 089_run_allow_list.ts",
output: "089_run_allow_list.ts.out",
});

Expand Down Expand Up @@ -588,9 +590,10 @@ itest!(lock_write_requires_lock {
exit_code: 1,
});

// TODO(bartlomieju): remove --unstable once Deno.spawn is stabilized
itest!(lock_write_fetch {
args:
"run --quiet --allow-read --allow-write --allow-env --allow-run lock_write_fetch.ts",
"run --quiet --allow-read --allow-write --allow-env --allow-run --unstable lock_write_fetch.ts",
output: "lock_write_fetch.ts.out",
http_server: true,
exit_code: 0,
Expand Down Expand Up @@ -1861,12 +1864,14 @@ fn dont_cache_on_check_fail() {
mod permissions {
use test_util as util;

// TODO(bartlomieju): remove --unstable once Deno.spawn is stabilized
#[test]
fn with_allow() {
for permission in &util::PERMISSION_VARIANTS {
let status = util::deno_cmd()
.current_dir(&util::testdata_path())
.arg("run")
.arg("--unstable")
.arg(format!("--allow-{0}", permission))
.arg("permission_test.ts")
.arg(format!("{0}Required", permission))
Expand All @@ -1878,12 +1883,13 @@ mod permissions {
}
}

// TODO(bartlomieju): remove --unstable once Deno.spawn is stabilized
#[test]
fn without_allow() {
for permission in &util::PERMISSION_VARIANTS {
let (_, err) = util::run_and_collect_output(
false,
&format!("run permission_test.ts {0}Required", permission),
&format!("run --unstable permission_test.ts {0}Required", permission),
None,
None,
false,
Expand Down
44 changes: 12 additions & 32 deletions cli/tests/testdata/045_proxy_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,105 +31,85 @@ async function handler(req: Request): Promise<Response> {
}

async function testFetch() {
const c = Deno.run({
cmd: [
Deno.execPath(),
const { status } = await Deno.spawn(Deno.execPath(), {
args: [
"run",
"--quiet",
"--reload",
"--allow-net",
"045_proxy_client.ts",
],
stdout: "piped",
env: {
HTTP_PROXY: `http:https://${addr}`,
},
});

const status = await c.status();
assertEquals(status.code, 0);
c.close();
}

async function testModuleDownload() {
const http = Deno.run({
cmd: [
Deno.execPath(),
const { status } = await Deno.spawn(Deno.execPath(), {
args: [
"cache",
"--reload",
"--quiet",
"http:https://localhost:4545/045_mod.ts",
],
stdout: "piped",
env: {
HTTP_PROXY: `http:https://${addr}`,
},
});

const httpStatus = await http.status();
assertEquals(httpStatus.code, 0);
http.close();
assertEquals(status.code, 0);
}

async function testFetchNoProxy() {
const c = Deno.run({
cmd: [
Deno.execPath(),
const { status } = await Deno.spawn(Deno.execPath(), {
args: [
"run",
"--quiet",
"--reload",
"--allow-net",
"045_proxy_client.ts",
],
stdout: "piped",
env: {
HTTP_PROXY: "http:https://not.exising.proxy.server",
NO_PROXY: "localhost",
},
});

const status = await c.status();
assertEquals(status.code, 0);
c.close();
}

async function testModuleDownloadNoProxy() {
const http = Deno.run({
cmd: [
Deno.execPath(),
const { status } = await Deno.spawn(Deno.execPath(), {
args: [
"cache",
"--reload",
"--quiet",
"http:https://localhost:4545/045_mod.ts",
],
stdout: "piped",
env: {
HTTP_PROXY: "http:https://not.exising.proxy.server",
NO_PROXY: "localhost",
},
});

const httpStatus = await http.status();
assertEquals(httpStatus.code, 0);
http.close();
assertEquals(status.code, 0);
}

async function testFetchProgrammaticProxy() {
const c = Deno.run({
cmd: [
Deno.execPath(),
const { status } = await Deno.spawn(Deno.execPath(), {
args: [
"run",
"--quiet",
"--reload",
"--allow-net=localhost:4545,localhost:4555",
"--unstable",
"045_programmatic_proxy_client.ts",
],
stdout: "piped",
});
const status = await c.status();
assertEquals(status.code, 0);
c.close();
}

proxyServer();
Expand Down
11 changes: 5 additions & 6 deletions cli/tests/testdata/089_run_allow_list.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
try {
Deno.run({
cmd: ["ls"],
});
await Deno.spawn("ls");
} catch (e) {
console.log(e);
}

const proc = Deno.run({
cmd: ["curl", "--help"],
const { status } = await Deno.spawn("curl", {
args: ["--help"],
stdout: "null",
stderr: "inherit",
});
console.log((await proc.status()).success);
console.log(status.success);
24 changes: 9 additions & 15 deletions cli/tests/testdata/lock_write_fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ try {
// pass
}

const fetchProc = Deno.run({
const fetchProc = await Deno.spawn(Deno.execPath(), {
stdout: "null",
stderr: "null",
cmd: [
Deno.execPath(),
args: [
"cache",
"--reload",
"--lock=lock_write_fetch.json",
Expand All @@ -18,31 +17,27 @@ const fetchProc = Deno.run({
],
});

const fetchCode = (await fetchProc.status()).code;
console.log(`fetch code: ${fetchCode}`);
console.log(`fetch code: ${fetchProc.status.code}`);

const fetchCheckProc = Deno.run({
const fetchCheckProc = await Deno.spawn(Deno.execPath(), {
stdout: "null",
stderr: "null",
cmd: [
Deno.execPath(),
args: [
"cache",
"--lock=lock_write_fetch.json",
"--cert=tls/RootCA.pem",
"https_import.ts",
],
});

const fetchCheckProcCode = (await fetchCheckProc.status()).code;
console.log(`fetch check code: ${fetchCheckProcCode}`);
console.log(`fetch check code: ${fetchCheckProc.status.code}`);

Deno.removeSync("./lock_write_fetch.json");

const runProc = Deno.run({
const runProc = await Deno.spawn(Deno.execPath(), {
stdout: "null",
stderr: "null",
cmd: [
Deno.execPath(),
args: [
"run",
"--lock=lock_write_fetch.json",
"--lock-write",
Expand All @@ -52,7 +47,6 @@ const runProc = Deno.run({
],
});

const runCode = (await runProc.status()).code;
console.log(`run code: ${runCode}`);
console.log(`run code: ${runProc.status.code}`);

Deno.removeSync("./lock_write_fetch.json");
5 changes: 4 additions & 1 deletion cli/tests/testdata/no_prompt.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
new Worker("data:,setTimeout(() => Deno.exit(2), 200)", { type: "module" });

try {
await Deno.run({ cmd: ["ps"] });
await Deno.spawn("ps", {
stdout: "inherit",
stderr: "inherit",
});
} catch {
Deno.exit(0);
}
9 changes: 3 additions & 6 deletions cli/tests/testdata/permission_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@ const test: { [key: string]: (...args: any[]) => void | Promise<void> } = {
netRequired() {
Deno.listen({ transport: "tcp", port: 4541 });
},
runRequired() {
const p = Deno.run({
cmd: Deno.build.os === "windows"
? ["cmd.exe", "/c", "echo hello"]
: ["printf", "hello"],
async runRequired() {
await Deno.spawn(Deno.build.os === "windows" ? "cmd.exe" : "printf", {
args: Deno.build.os === "windows" ? ["/c", "echo hello"] : ["hello"],
});
p.close();
},
};

Expand Down
20 changes: 8 additions & 12 deletions cli/tests/unit/chown_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,17 @@ import { assertEquals, assertRejects, assertThrows } from "./test_util.ts";

async function getUidAndGid(): Promise<{ uid: number; gid: number }> {
// get the user ID and group ID of the current process
const uidProc = Deno.run({
stdout: "piped",
cmd: ["id", "-u"],
const uidProc = await Deno.spawn("id", {
args: ["-u"],
});
const gidProc = Deno.run({
stdout: "piped",
cmd: ["id", "-g"],
const gidProc = await Deno.spawn("id", {
args: ["-g"],
});

assertEquals((await uidProc.status()).code, 0);
assertEquals((await gidProc.status()).code, 0);
const uid = parseInt(new TextDecoder("utf-8").decode(await uidProc.output()));
uidProc.close();
const gid = parseInt(new TextDecoder("utf-8").decode(await gidProc.output()));
gidProc.close();
assertEquals(uidProc.status.code, 0);
assertEquals(gidProc.status.code, 0);
const uid = parseInt(new TextDecoder("utf-8").decode(uidProc.stdout));
const gid = parseInt(new TextDecoder("utf-8").decode(gidProc.stdout));

return { uid, gid };
}
Expand Down
33 changes: 19 additions & 14 deletions cli/tests/unit/flock_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "./test_util.ts";
import { readAll } from "../../../test_util/std/io/util.ts";

Deno.test(
{ permissions: { read: true, run: true, hrtime: true } },
Expand Down Expand Up @@ -103,8 +102,8 @@ async function checkFirstBlocksSecond(opts: {
const secondPsTimes = await secondProcess.getTimes();
return firstPsTimes.exitTime < secondPsTimes.enterTime;
} finally {
firstProcess.close();
secondProcess.close();
await firstProcess.close();
await secondProcess.close();
}
}

Expand Down Expand Up @@ -149,14 +148,21 @@ function runFlockTestProcess(opts: { exclusive: boolean; sync: boolean }) {
console.log(JSON.stringify({ enterTime, exitTime }));
`;

const process = Deno.run({
cmd: [Deno.execPath(), "eval", "--unstable", scriptText],
stdout: "piped",
const process = Deno.spawnChild(Deno.execPath(), {
args: ["eval", "--unstable", scriptText],
stdin: "piped",
});

const waitSignal = () => process.stdout.read(new Uint8Array(1));
const signal = () => process.stdin.write(new Uint8Array(1));
const waitSignal = async () => {
const reader = process.stdout.getReader({ mode: "byob" });
await reader.read(new Uint8Array(1));
reader.releaseLock();
};
const signal = async () => {
const writer = process.stdin.getWriter();
await writer.write(new Uint8Array(1));
writer.releaseLock();
};

return {
async waitStartup() {
Expand All @@ -174,17 +180,16 @@ function runFlockTestProcess(opts: { exclusive: boolean; sync: boolean }) {
await waitSignal();
},
getTimes: async () => {
const outputBytes = await readAll(process.stdout);
const text = new TextDecoder().decode(outputBytes);
const { stdout } = await process.output();
const text = new TextDecoder().decode(stdout);
return JSON.parse(text) as {
enterTime: number;
exitTime: number;
};
},
close: () => {
process.stdout.close();
process.stdin.close();
process.close();
close: async () => {
await process.status;
await process.stdin.close();
},
};
}
Loading

0 comments on commit 4e1ca1d

Please sign in to comment.