Skip to content

Commit

Permalink
refactor: switch the Deno.spawn (#2161)
Browse files Browse the repository at this point in the history
  • Loading branch information
crowlKats committed May 25, 2022
1 parent 662f27c commit 852968f
Show file tree
Hide file tree
Showing 31 changed files with 319 additions and 483 deletions.
9 changes: 4 additions & 5 deletions _deno_unstable_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,16 @@ Deno.test({
const checkedCode = `// AUTOGENERATED\n${code.replace("@ts-nocheck ", "")}`;
try {
await Deno.writeTextFile(denoUnstableCheckedUrl, checkedCode);
const process = Deno.run({
cmd: [
Deno.execPath(),
const { status } = await Deno.spawn(Deno.execPath(), {
args: [
"run",
"--quiet",
"--unstable",
denoUnstableCheckedUrl.href,
],
stdout: "inherit",
stderr: "inherit",
});
const status = await process.status();
process.close();
assertEquals(status.code, 0);
} finally {
// TODO(nayeemrmn): Uncomment (https://github.com/denoland/deno_std/pull/1819#issuecomment-1011136991).
Expand Down
43 changes: 21 additions & 22 deletions _wasm_crypto/_build.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env -S deno run --allow-run --allow-read --allow-write --allow-env
#!/usr/bin/env -S deno run --allow-run --allow-read --allow-write --allow-env --unstable
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import * as base64 from "../encoding/base64.ts";

Expand All @@ -15,33 +15,30 @@ if (new URL(import.meta.url).protocol === "file:") {

// Format the Rust code.
if (
!((await Deno.run({
cmd: [
"cargo",
"fmt",
],
}).status()).success)
!((await Deno.spawn("cargo", {
args: ["fmt"],
stdout: "inherit",
stderr: "inherit",
})).status.success)
) {
console.error(`Failed to format the Rust code.`);
Deno.exit(1);
}

// Compile the Rust code to WASM.
if (
!((await Deno.run({
cmd: [
"cargo",
"build",
"--release",
],
!((await Deno.spawn("cargo", {
args: ["build", "--release"],
env: {
// eliminate some potential sources of non-determinism
SOURCE_DATE_EPOCH: "1600000000",
TZ: "UTC",
LC_ALL: "C",
RUSTFLAGS: `--remap-path-prefix=${root}=. --remap-path-prefix=${home}=~`,
},
}).status()).success)
stdout: "inherit",
stderr: "inherit",
})).status.success)
) {
console.error(`Failed to compile the Rust code to WASM.`);
Deno.exit(1);
Expand All @@ -53,17 +50,18 @@ const copyrightLine = `// Copyright 2018-${

// Generate JavaScript bindings from WASM.
if (
!((await Deno.run({
cmd: [
"wasm-bindgen",
!((await Deno.spawn("wasm-bindgen", {
args: [
"./target/wasm32-unknown-unknown/release/deno_std_wasm_crypto.wasm",
"--target",
"deno",
"--weak-refs",
"--out-dir",
"./target/wasm32-bindgen-deno-js",
],
}).status()).success)
stdout: "inherit",
stderr: "inherit",
})).status.success)
) {
console.error(`Failed to generate JavaScript bindings from WASM.`);
Deno.exit(1);
Expand Down Expand Up @@ -133,14 +131,15 @@ await Deno.writeTextFile("./crypto.mjs", bindingsJs);

// Format the generated files.
if (
!(await Deno.run({
cmd: [
"deno",
!(await Deno.spawn("deno", {
args: [
"fmt",
"./crypto.wasm.mjs",
"./crypto.mjs",
],
}).status()).success
stdout: "inherit",
stderr: "inherit",
})).status.success
) {
console.error(
`Failed to format generated code.`,
Expand Down
35 changes: 18 additions & 17 deletions crypto/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,14 @@ Deno.test("[crypto/digest] Should not ignore length option", async () => {
});

Deno.test("[crypto/digest] Memory use should remain reasonable even with large inputs", async () => {
const process = Deno.run({
cmd: [Deno.execPath(), "--quiet", "run", "--no-check", "-"],
const process = Deno.spawnChild(Deno.execPath(), {
args: ["--quiet", "run", "--no-check", "-"],
cwd: moduleDir,
stdout: "piped",
stdin: "piped",
});

await process.stdin.write(
const writer = process.stdin.getWriter();
await writer.write(
new TextEncoder().encode(`
import { crypto as stdCrypto } from "./mod.ts";
import { _wasm } from "../_wasm_crypto/crypto.mjs";
Expand Down Expand Up @@ -188,13 +188,13 @@ Deno.test("[crypto/digest] Memory use should remain reasonable even with large i
));
`),
);
process.stdin.close();
writer.releaseLock();
await process.stdin.close();

const stdout = new TextDecoder().decode(await process.output());
const status = await process.status();
process.close();
const res = await process.output();
const stdout = new TextDecoder().decode(res.stdout);

assertEquals(status.success, true, "test subprocess failed");
assertEquals(res.status.success, true, "test subprocess failed");
const {
heapBytesInitial,
smallDigest,
Expand Down Expand Up @@ -242,14 +242,15 @@ Deno.test("[crypto/digest] Memory use should remain reasonable even with large i
});

Deno.test("[crypto/digest] Memory use should remain reasonable even with many calls", async () => {
const process = Deno.run({
cmd: [Deno.execPath(), "--quiet", "run", "--no-check", "-"],
const process = Deno.spawnChild(Deno.execPath(), {
args: ["--quiet", "run", "--no-check", "-"],
cwd: moduleDir,
stdout: "piped",
stdin: "piped",
});

await process.stdin.write(
const writer = process.stdin.getWriter();
await writer.write(
new TextEncoder().encode(`
import { crypto as stdCrypto } from "./mod.ts";
import { _wasm } from "../_wasm_crypto/crypto.mjs";
Expand Down Expand Up @@ -284,13 +285,13 @@ Deno.test("[crypto/digest] Memory use should remain reasonable even with many ca
));
`),
);
process.stdin.close();
writer.releaseLock();
await process.stdin.close();

const stdout = new TextDecoder().decode(await process.output());
const status = await process.status();
process.close();
const res = await process.output();
const stdout = new TextDecoder().decode(res.stdout);

assertEquals(status.success, true, "test subprocess failed");
assertEquals(res.status.success, true, "test subprocess failed");
const {
heapBytesInitial,
heapBytesFinal,
Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@
"node:unit": "DENO_FUTURE_CHECK=1 deno test --unstable --allow-all node/ --ignore=node/_tools/test.ts,node/_tools/versions/",
"node:test": "deno test --unstable --allow-all node/_tools/test.ts",
"node:setup": "deno run --allow-read --allow-net --allow-write node/_tools/setup.ts",
"node:check-unstable-api": "deno check ./node/module_all.ts"
"node:check-unstable-api": "deno check --unstable ./node/module_all.ts"
}
}
20 changes: 6 additions & 14 deletions dotenv/load_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,41 @@ const testdataDir = path.resolve(moduleDir, "testdata");
Deno.test({
name: "load",
async fn() {
const p = Deno.run({
cmd: [
Deno.execPath(),
const { stdout } = await Deno.spawn(Deno.execPath(), {
args: [
"run",
"--allow-read",
"--allow-env",
path.join(testdataDir, "./app_load.ts"),
],
cwd: testdataDir,
stdout: "piped",
});

const decoder = new TextDecoder();
const rawOutput = await p.output();
assertEquals(
decoder.decode(rawOutput).trim(),
decoder.decode(stdout).trim(),
"hello world",
);
p.close();
},
});

Deno.test({
name: "load when multiple files",
async fn() {
const p = Deno.run({
cmd: [
Deno.execPath(),
const { stdout } = await Deno.spawn(Deno.execPath(), {
args: [
"run",
"--allow-read",
"--allow-env",
path.join(testdataDir, "./app_load_parent.ts"),
],
cwd: testdataDir,
stdout: "piped",
});

const decoder = new TextDecoder();
const rawOutput = await p.output();
assertEquals(
decoder.decode(rawOutput).trim(),
decoder.decode(stdout).trim(),
"hello world",
);
p.close();
},
});
10 changes: 3 additions & 7 deletions dotenv/mod_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,22 +411,18 @@ Deno.test("configureSafe async", async () => {
});

Deno.test("config defaults", async () => {
const p = Deno.run({
cmd: [
Deno.execPath(),
const { stdout } = await Deno.spawn(Deno.execPath(), {
args: [
"run",
"--allow-read",
"--allow-env",
path.join(testdataDir, "./app_defaults.ts"),
],
cwd: testdataDir,
stdout: "piped",
});

const decoder = new TextDecoder();
const rawOutput = await p.output();
const conf = JSON.parse(decoder.decode(rawOutput).trim());
p.close();
const conf = JSON.parse(decoder.decode(stdout).trim());

assertEquals(conf.GREETING, "hello world", "fetches .env by default");

Expand Down
10 changes: 4 additions & 6 deletions examples/cat_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ const moduleDir = dirname(fromFileUrl(import.meta.url));

Deno.test("[examples/cat] print multiple files", async () => {
const decoder = new TextDecoder();
const process = Deno.run({
cmd: [
Deno.execPath(),
const { stdout } = await Deno.spawn(Deno.execPath(), {
args: [
"run",
"--quiet",
"--allow-read",
Expand All @@ -21,10 +20,9 @@ Deno.test("[examples/cat] print multiple files", async () => {
});

try {
const output = await process.output();
const actual = decoder.decode(output).trim();
const actual = decoder.decode(stdout).trim();
assertStrictEquals(actual, "Hello\nWorld");
} finally {
process.close();
//
}
});
Loading

0 comments on commit 852968f

Please sign in to comment.