From 3d63efc7a8702cdffa4d00b3d2519ea3bd338208 Mon Sep 17 00:00:00 2001 From: Iori IKEDA Date: Tue, 29 Mar 2022 04:52:05 +0900 Subject: [PATCH] tests: add `fresh init` integration test (#141) Co-authored-by: Luca Casonato --- tests/cli_test.ts | 119 ++++++++++++++++++++++++++++++++++++++++++++++ tests/deps.ts | 5 +- 2 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 tests/cli_test.ts diff --git a/tests/cli_test.ts b/tests/cli_test.ts new file mode 100644 index 00000000..e5798790 --- /dev/null +++ b/tests/cli_test.ts @@ -0,0 +1,119 @@ +import { assert, assertEquals, TextLineStream } from "./deps.ts"; + +type FileTree = { + type: "file"; + name: string; +} | { + type: "directory"; + name: string; + contents: FileTree[]; +} | { + type: "report"; + directories: number; + files: number; +}; + +const assertFileExistence = async (tree: FileTree[], dirname?: string) => { + for (const t of tree) { + if (t.type === "report") continue; + + const stat = await Deno.stat( + dirname ? [dirname, t.name].join("/") : t.name, + ); + assertEquals(t.type === "file", stat.isFile); + + if (t.type === "directory") { + assert(stat.isDirectory); + await assertFileExistence( + t.contents, + dirname ? [dirname, t.name].join("/") : t.name, + ); + } + } +}; + +Deno.test({ + name: "fresh init", + async fn(t) { + // Preparation + const tmpDirName = await Deno.makeTempDir(); + + await t.step("execute init command", async () => { + const cliProcess = Deno.run({ + cmd: ["deno", "run", "-A", "--no-check", "cli.ts", "init", tmpDirName], + stdout: "null", + }); + const { code } = await cliProcess.status(); + cliProcess.close(); + assertEquals(code, 0); + }); + + // NOTE: generated by `tree -J ` + const targetFileTree: FileTree[] = [ + { + "type": "directory", + "name": tmpDirName, + "contents": [ + { "type": "file", "name": "README.md" }, + { "type": "file", "name": "client_deps.ts" }, + { "type": "file", "name": "fresh.gen.ts" }, + { + "type": "directory", + "name": "islands", + "contents": [ + { "type": "file", "name": "Counter.tsx" }, + ], + }, + { "type": "file", "name": "main.ts" }, + { + "type": "directory", + "name": "routes", + "contents": [ + { "type": "file", "name": "[name].tsx" }, + { + "type": "directory", + "name": "api", + "contents": [ + { "type": "file", "name": "joke.ts" }, + ], + }, + { "type": "file", "name": "index.tsx" }, + ], + }, + { "type": "file", "name": "server_deps.ts" }, + ], + }, + { "type": "report", "directories": 3, "files": 9 }, + ]; + await t.step("check generated files", async () => { + await assertFileExistence(targetFileTree); + }); + + await t.step("start up the server and access the root page", async () => { + const serverProcess = Deno.run({ + cmd: ["deno", "run", "-A", "--no-check", "main.ts"], + stdout: "piped", + cwd: tmpDirName, + }); + + const lines = serverProcess.stdout.readable + .pipeThrough(new TextDecoderStream()) + .pipeThrough(new TextLineStream()); + + for await (const line of lines) { + if (line.includes("Server listening on http://")) break; + } + await lines.cancel(); + + // Access the root page + const res = await fetch("http://localhost:8000"); + await res.body?.cancel(); + assertEquals(res.status, 200); + + serverProcess.kill("SIGTERM"); + serverProcess.close(); + }); + }, + sanitizeOps: false, + sanitizeResources: false, +}); diff --git a/tests/deps.ts b/tests/deps.ts index 4d784ec3..abdff4a8 100644 --- a/tests/deps.ts +++ b/tests/deps.ts @@ -2,4 +2,7 @@ export { assert, assertEquals, assertStringIncludes, -} from "https://deno.land/std@0.102.0/testing/asserts.ts"; +} from "https://deno.land/std@0.129.0/testing/asserts.ts"; +export { + TextLineStream, +} from "https://deno.land/std@0.129.0/streams/delimiter.ts";