From 7e72fa236c1abc183aae31e5532af914ed060ec5 Mon Sep 17 00:00:00 2001 From: Sylvain Cau <10268458+sylc@users.noreply.github.com> Date: Fri, 10 Mar 2023 03:57:16 +0800 Subject: [PATCH] refactor: remove deno.run (#1036) Co-authored-by: Luca Casonato --- src/dev/mod.ts | 15 ++++----- tests/cli_test.ts | 77 ++++++++++++++++--------------------------- tests/islands_test.ts | 18 +++++----- tests/main_test.ts | 19 +++++------ tests/plugin_test.ts | 9 +++-- www/main_test.ts | 13 +++++--- 6 files changed, 63 insertions(+), 88 deletions(-) diff --git a/src/dev/mod.ts b/src/dev/mod.ts index d874977e..37bf5aaa 100644 --- a/src/dev/mod.ts +++ b/src/dev/mod.ts @@ -134,24 +134,23 @@ const manifest = { export default manifest; `; - const proc = Deno.run({ - cmd: [Deno.execPath(), "fmt", "-"], + const proc = new Deno.Command(Deno.execPath(), { + args: ["fmt", "-"], stdin: "piped", stdout: "piped", stderr: "null", - }); + }).spawn(); + const raw = new ReadableStream({ start(controller) { controller.enqueue(new TextEncoder().encode(output)); controller.close(); }, }); - await raw.pipeTo(proc.stdin.writable); - const out = await proc.output(); - await proc.status(); - proc.close(); + await raw.pipeTo(proc.stdin); + const { stdout } = await proc.output(); - const manifestStr = new TextDecoder().decode(out); + const manifestStr = new TextDecoder().decode(stdout); const manifestPath = join(directory, "./fresh.gen.ts"); await Deno.writeTextFile(manifestPath, manifestStr); diff --git a/tests/cli_test.ts b/tests/cli_test.ts index 49ef4947..4f26f90f 100644 --- a/tests/cli_test.ts +++ b/tests/cli_test.ts @@ -42,9 +42,8 @@ Deno.test({ const tmpDirName = await Deno.makeTempDir(); await t.step("execute init command", async () => { - const cliProcess = Deno.run({ - cmd: [ - "deno", + const cliProcess = new Deno.Command(Deno.execPath(), { + args: [ "run", "-A", "init.ts", @@ -53,8 +52,7 @@ Deno.test({ stdin: "null", stdout: "null", }); - const { code } = await cliProcess.status(); - cliProcess.close(); + const { code } = await cliProcess.output(); assertEquals(code, 0); }); @@ -113,15 +111,15 @@ Deno.test({ }); await t.step("start up the server and access the root page", async () => { - const serverProcess = Deno.run({ - cmd: ["deno", "run", "-A", "--check", "main.ts"], + const serverProcess = new Deno.Command(Deno.execPath(), { + args: ["run", "-A", "--check", "main.ts"], stdin: "null", stdout: "piped", stderr: "inherit", cwd: tmpDirName, - }); + }).spawn(); - const lines = serverProcess.stdout.readable + const lines = serverProcess.stdout .pipeThrough(new TextDecoderStream()) .pipeThrough(new TextLineStream()); @@ -166,13 +164,11 @@ Deno.test({ await lines.cancel(); serverProcess.kill("SIGTERM"); - await serverProcess.status(); - serverProcess.close(); + await delay(100); }); await retry(() => Deno.remove(tmpDirName, { recursive: true })); }, - sanitizeOps: false, sanitizeResources: false, }); @@ -183,9 +179,8 @@ Deno.test({ const tmpDirName = await Deno.makeTempDir(); await t.step("execute init command", async () => { - const cliProcess = Deno.run({ - cmd: [ - "deno", + const cliProcess = new Deno.Command(Deno.execPath(), { + args: [ "run", "-A", "init.ts", @@ -196,8 +191,7 @@ Deno.test({ stdin: "null", stdout: "null", }); - const { code } = await cliProcess.status(); - cliProcess.close(); + const { code } = await cliProcess.output(); assertEquals(code, 0); }); @@ -265,15 +259,15 @@ Deno.test({ }); await t.step("start up the server and access the root page", async () => { - const serverProcess = Deno.run({ - cmd: ["deno", "run", "-A", "--check", "main.ts"], + const serverProcess = new Deno.Command(Deno.execPath(), { + args: ["run", "-A", "--check", "main.ts"], stdin: "null", stdout: "piped", stderr: "inherit", cwd: tmpDirName, - }); + }).spawn(); - const lines = serverProcess.stdout.readable + const lines = serverProcess.stdout .pipeThrough(new TextDecoderStream()) .pipeThrough(new TextLineStream()); @@ -322,13 +316,11 @@ Deno.test({ await lines.cancel(); serverProcess.kill("SIGTERM"); - await serverProcess.status(); - serverProcess.close(); + await delay(100); }); await retry(() => Deno.remove(tmpDirName, { recursive: true })); }, - sanitizeOps: false, sanitizeResources: false, }); @@ -338,18 +330,15 @@ Deno.test("fresh-init error(help)", async function (t) { await t.step( "execute invalid init command (deno run -A init.ts)", async () => { - const cliProcess = Deno.run({ - cmd: ["deno", "run", "-A", "init.ts"], + const cliProcess = new Deno.Command(Deno.execPath(), { + args: ["run", "-A", "init.ts"], stdin: "null", stderr: "piped", }); - const { code } = await cliProcess.status(); - cliProcess.close(); + const { code, stderr } = await cliProcess.output(); assertEquals(code, 1); - const rawError = await cliProcess.stderrOutput(); - const errorString = new TextDecoder().decode(rawError); - + const errorString = new TextDecoder().decode(stderr); assertStringIncludes(errorString, includeText); }, ); @@ -357,18 +346,13 @@ Deno.test("fresh-init error(help)", async function (t) { await t.step( "execute invalid init command (deno run -A init.ts -f)", async () => { - const cliProcess = Deno.run({ - cmd: ["deno", "run", "-A", "init.ts", "-f"], - stdin: "null", - stderr: "piped", + const cliProcess = new Deno.Command(Deno.execPath(), { + args: ["run", "-A", "init.ts", "-f"], }); - const { code } = await cliProcess.status(); - cliProcess.close(); + const { code, stderr } = await cliProcess.output(); assertEquals(code, 1); - const rawError = await cliProcess.stderrOutput(); - const errorString = new TextDecoder().decode(rawError); - + const errorString = new TextDecoder().decode(stderr); assertStringIncludes(errorString, includeText); }, ); @@ -376,18 +360,13 @@ Deno.test("fresh-init error(help)", async function (t) { await t.step( "execute invalid init command (deno run -A init.ts --foo)", async () => { - const cliProcess = Deno.run({ - cmd: ["deno", "run", "-A", "init.ts", "--foo"], - stdin: "null", - stderr: "piped", + const cliProcess = new Deno.Command(Deno.execPath(), { + args: ["run", "-A", "init.ts", "--foo"], }); - const { code } = await cliProcess.status(); - cliProcess.close(); + const { code, stderr } = await cliProcess.output(); assertEquals(code, 1); - const rawError = await cliProcess.stderrOutput(); - const errorString = new TextDecoder().decode(rawError); - + const errorString = new TextDecoder().decode(stderr); assertStringIncludes(errorString, includeText); }, ); diff --git a/tests/islands_test.ts b/tests/islands_test.ts index 41db1599..bad8d121 100644 --- a/tests/islands_test.ts +++ b/tests/islands_test.ts @@ -10,14 +10,14 @@ Deno.test({ name: "island tests", async fn(t) { // Preparation - const serverProcess = Deno.run({ - cmd: ["deno", "run", "-A", "./tests/fixture/main.ts"], + const serverProcess = new Deno.Command(Deno.execPath(), { + args: ["run", "-A", "./tests/fixture/main.ts"], stdout: "piped", stderr: "inherit", - }); + }).spawn(); const decoder = new TextDecoderStream(); - const lines = serverProcess.stdout.readable + const lines = serverProcess.stdout .pipeThrough(decoder) .pipeThrough(new TextLineStream()); @@ -77,7 +77,6 @@ Deno.test({ await lines.cancel(); serverProcess.kill("SIGTERM"); - serverProcess.close(); }, sanitizeOps: false, sanitizeResources: false, @@ -87,14 +86,14 @@ Deno.test({ name: "island tests with ", async fn(t) { // Preparation - const serverProcess = Deno.run({ - cmd: ["deno", "run", "-A", "./tests/fixture/main.ts"], + const serverProcess = new Deno.Command(Deno.execPath(), { + args: ["run", "-A", "./tests/fixture/main.ts"], stdout: "piped", stderr: "inherit", - }); + }).spawn(); const decoder = new TextDecoderStream(); - const lines = serverProcess.stdout.readable + const lines = serverProcess.stdout .pipeThrough(decoder) .pipeThrough(new TextLineStream()); @@ -136,7 +135,6 @@ Deno.test({ await lines.cancel(); serverProcess.kill("SIGTERM"); - serverProcess.close(); }, sanitizeOps: false, sanitizeResources: false, diff --git a/tests/main_test.ts b/tests/main_test.ts index ea4c3199..252ebb44 100644 --- a/tests/main_test.ts +++ b/tests/main_test.ts @@ -500,9 +500,8 @@ Deno.test("experimental Deno.serve", { ignore: Deno.build.os === "windows", // TODO: Deno.serve hang on Windows? }, async (t) => { // Preparation - const serverProcess = Deno.run({ - cmd: [ - "deno", + const serverProcess = new Deno.Command(Deno.execPath(), { + args: [ "run", "-A", "--unstable", @@ -511,10 +510,10 @@ Deno.test("experimental Deno.serve", { ], stdout: "piped", stderr: "inherit", - }); + }).spawn(); const decoder = new TextDecoderStream(); - const lines = serverProcess.stdout.readable + const lines = serverProcess.stdout .pipeThrough(decoder) .pipeThrough(new TextLineStream()); @@ -562,7 +561,6 @@ Deno.test("experimental Deno.serve", { await lines.cancel(); serverProcess.kill("SIGTERM"); - serverProcess.close(); }); Deno.test("jsx pragma works", { @@ -570,14 +568,14 @@ Deno.test("jsx pragma works", { sanitizeResources: false, }, async (t) => { // Preparation - const serverProcess = Deno.run({ - cmd: ["deno", "run", "-A", "./tests/fixture_jsx_pragma/main.ts"], + const serverProcess = new Deno.Command(Deno.execPath(), { + args: ["run", "-A", "./tests/fixture_jsx_pragma/main.ts"], stdout: "piped", stderr: "inherit", - }); + }).spawn(); const decoder = new TextDecoderStream(); - const lines = serverProcess.stdout.readable + const lines = serverProcess.stdout .pipeThrough(decoder) .pipeThrough(new TextLineStream()); @@ -617,5 +615,4 @@ Deno.test("jsx pragma works", { await lines.cancel(); serverProcess.kill("SIGTERM"); - serverProcess.close(); }); diff --git a/tests/plugin_test.ts b/tests/plugin_test.ts index e7920e46..0f1f067e 100644 --- a/tests/plugin_test.ts +++ b/tests/plugin_test.ts @@ -54,14 +54,14 @@ Deno.test({ name: "/with-island hydration", async fn(t) { // Preparation - const serverProcess = Deno.run({ - cmd: ["deno", "run", "-A", "./tests/fixture_plugin/main.ts"], + const serverProcess = new Deno.Command(Deno.execPath(), { + args: ["run", "-A", "./tests/fixture_plugin/main.ts"], stdout: "piped", stderr: "inherit", - }); + }).spawn(); const decoder = new TextDecoderStream(); - const lines = serverProcess.stdout.readable + const lines = serverProcess.stdout .pipeThrough(decoder) .pipeThrough(new TextLineStream()); @@ -98,7 +98,6 @@ Deno.test({ await lines.cancel(); serverProcess.kill("SIGTERM"); - serverProcess.close(); }, sanitizeOps: false, sanitizeResources: false, diff --git a/www/main_test.ts b/www/main_test.ts index 98db2d11..000f7f68 100644 --- a/www/main_test.ts +++ b/www/main_test.ts @@ -1,17 +1,19 @@ import { assertEquals } from "$std/testing/asserts.ts"; import { TextLineStream } from "$std/streams/delimiter.ts"; +import { delay } from "$std/async/delay.ts"; Deno.test("CORS should not set on GET /fresh-badge.svg", { sanitizeResources: false, }, async () => { - const serverProcess = Deno.run({ - cmd: ["deno", "run", "-A", "./main.ts"], + const serverProcess = new Deno.Command(Deno.execPath(), { + args: ["run", "-A", "./main.ts"], stdin: "null", stdout: "piped", stderr: "inherit", - }); + }).spawn(); + const decoder = new TextDecoderStream(); - const lines = serverProcess.stdout.readable + const lines = serverProcess.stdout .pipeThrough(decoder) .pipeThrough(new TextLineStream()); @@ -33,5 +35,6 @@ Deno.test("CORS should not set on GET /fresh-badge.svg", { await lines.cancel(); serverProcess.kill("SIGTERM"); - serverProcess.close(); + // await for the server to close + await delay(100); });