diff --git a/tests/cli_test.ts b/tests/cli_test.ts index 03938a58..a833c7c7 100644 --- a/tests/cli_test.ts +++ b/tests/cli_test.ts @@ -11,29 +11,13 @@ import { } from "./deps.ts"; import { startFreshServer } from "./test_utils.ts"; -type FileTree = { - type: "file"; - name: string; -} | { - type: "directory"; - name: string; - contents: FileTree[]; -}; +const assertFileExistence = async (files: string[], dirname: string) => { + for (const filePath of files) { + const parts = filePath.split("/").slice(1); -const assertFileExistence = async (tree: FileTree[], dirname?: string) => { - for (const t of tree) { - 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, - ); - } + const osFilePath = path.join(dirname, ...parts); + const stat = await Deno.stat(osFilePath); + assert(stat.isFile, `Could not find file ${osFilePath}`); } }; @@ -58,63 +42,20 @@ Deno.test({ assertEquals(code, 0); }); - // NOTE: generated by `tree -J ` - const targetFileTree: FileTree[] = [ - { - "type": "directory", - "name": tmpDirName, - "contents": [ - { "type": "file", "name": "README.md" }, - { "type": "file", "name": "fresh.gen.ts" }, - { - "type": "directory", - "name": "components", - "contents": [ - { "type": "file", "name": "Button.tsx" }, - ], - }, - { - "type": "directory", - "name": "islands", - "contents": [ - { "type": "file", "name": "Counter.tsx" }, - ], - }, - { "type": "file", "name": "main.ts" }, - { - "type": "directory", - "name": "routes", - "contents": [ - { - "type": "directory", - "name": "greet", - "contents": [ - { "type": "file", "name": "[name].tsx" }, - ], - }, - { - "type": "directory", - "name": "api", - "contents": [ - { "type": "file", "name": "joke.ts" }, - ], - }, - { "type": "file", "name": "index.tsx" }, - ], - }, - { - "type": "directory", - "name": "static", - "contents": [ - { "type": "file", "name": "logo.svg" }, - ], - }, - ], - }, + const files = [ + `/README.md`, + `/fresh.gen.ts`, + `/components/Button.tsx`, + `/islands/Counter.tsx`, + `/main.ts`, + `/routes/greet/[name].tsx`, + `/routes/api/joke.ts`, + `/routes/index.tsx`, + `/static/logo.svg`, ]; await t.step("check generated files", async () => { - await assertFileExistence(targetFileTree); + await assertFileExistence(files, tmpDirName); }); await t.step("start up the server and access the root page", async () => { @@ -185,73 +126,24 @@ Deno.test({ assertEquals(code, 0); }); - // NOTE: generated by `tree -J ` - const targetFileTree: FileTree[] = [ - { - "type": "directory", - "name": tmpDirName, - "contents": [ - { "type": "file", "name": "README.md" }, - { "type": "file", "name": "fresh.gen.ts" }, - { "type": "file", "name": "twind.config.ts" }, - { - "type": "directory", - "name": "components", - "contents": [ - { "type": "file", "name": "Button.tsx" }, - ], - }, - { - "type": "directory", - "name": "islands", - "contents": [ - { "type": "file", "name": "Counter.tsx" }, - ], - }, - { "type": "file", "name": "main.ts" }, - { - "type": "directory", - "name": "routes", - "contents": [ - { - "type": "directory", - "name": "greet", - "contents": [ - { "type": "file", "name": "[name].tsx" }, - ], - }, - { - "type": "directory", - "name": "api", - "contents": [ - { "type": "file", "name": "joke.ts" }, - ], - }, - { "type": "file", "name": "index.tsx" }, - ], - }, - { - "type": "directory", - "name": "static", - "contents": [ - { "type": "file", "name": "logo.svg" }, - ], - }, - { - "type": "directory", - "name": ".vscode", - "contents": [ - { "type": "file", "name": "settings.json" }, - { "type": "file", "name": "extensions.json" }, - ], - }, - { "type": "file", "name": ".gitignore" }, - ], - }, + const files = [ + "/README.md", + "/fresh.gen.ts", + "/twind.config.ts", + "/components/Button.tsx", + "/islands/Counter.tsx", + "/main.ts", + "/routes/greet/[name].tsx", + "/routes/api/joke.ts", + "/routes/index.tsx", + "/static/logo.svg", + "/.vscode/settings.json", + "/.vscode/extensions.json", + "/.gitignore", ]; await t.step("check generated files", async () => { - await assertFileExistence(targetFileTree); + await assertFileExistence(files, tmpDirName); }); await t.step("start up the server and access the root page", async () => { @@ -404,33 +296,15 @@ Deno.test({ path.join(tmpDirName, "deno.json"), ); - const targetFileTree: FileTree[] = [ - { - "type": "directory", - "name": tmpDirName, - "contents": [ - { "type": "file", "name": "deno.json" }, - { - "type": "directory", - "name": "subdirectory", - contents: [ - { - "type": "directory", - "name": "subsubdirectory", - "contents": [ - { "type": "file", "name": "main.ts" }, - { "type": "file", "name": "dev.ts" }, - { "type": "file", "name": "fresh.gen.ts" }, - ], - }, - ], - }, - ], - }, + const files = [ + "/deno.json", + "/subdirectory/subsubdirectory/main.ts", + "/subdirectory/subsubdirectory/dev.ts", + "/subdirectory/subsubdirectory/fresh.gen.ts", ]; await t.step("check generated files", async () => { - await assertFileExistence(targetFileTree); + await assertFileExistence(files, tmpDirName); }); await t.step("start up the server", async () => {