chore: simplify folder tests (#1337)

This commit is contained in:
Marvin Hagemeister
2023-06-21 14:02:25 +02:00
committed by GitHub
parent 87b15885aa
commit 3a83c5974d
+38 -164
View File
@@ -11,29 +11,13 @@ import {
} from "./deps.ts"; } from "./deps.ts";
import { startFreshServer } from "./test_utils.ts"; import { startFreshServer } from "./test_utils.ts";
type FileTree = { const assertFileExistence = async (files: string[], dirname: string) => {
type: "file"; for (const filePath of files) {
name: string; const parts = filePath.split("/").slice(1);
} | {
type: "directory";
name: string;
contents: FileTree[];
};
const assertFileExistence = async (tree: FileTree[], dirname?: string) => { const osFilePath = path.join(dirname, ...parts);
for (const t of tree) { const stat = await Deno.stat(osFilePath);
const stat = await Deno.stat( assert(stat.isFile, `Could not find file ${osFilePath}`);
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,
);
}
} }
}; };
@@ -58,63 +42,20 @@ Deno.test({
assertEquals(code, 0); assertEquals(code, 0);
}); });
// NOTE: generated by `tree -J <dir>` const files = [
const targetFileTree: FileTree[] = [ `/README.md`,
{ `/fresh.gen.ts`,
"type": "directory", `/components/Button.tsx`,
"name": tmpDirName, `/islands/Counter.tsx`,
"contents": [ `/main.ts`,
{ "type": "file", "name": "README.md" }, `/routes/greet/[name].tsx`,
{ "type": "file", "name": "fresh.gen.ts" }, `/routes/api/joke.ts`,
{ `/routes/index.tsx`,
"type": "directory", `/static/logo.svg`,
"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" },
],
},
],
},
]; ];
await t.step("check generated files", async () => { 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 () => { await t.step("start up the server and access the root page", async () => {
@@ -185,73 +126,24 @@ Deno.test({
assertEquals(code, 0); assertEquals(code, 0);
}); });
// NOTE: generated by `tree -J <dir>` const files = [
const targetFileTree: FileTree[] = [ "/README.md",
{ "/fresh.gen.ts",
"type": "directory", "/twind.config.ts",
"name": tmpDirName, "/components/Button.tsx",
"contents": [ "/islands/Counter.tsx",
{ "type": "file", "name": "README.md" }, "/main.ts",
{ "type": "file", "name": "fresh.gen.ts" }, "/routes/greet/[name].tsx",
{ "type": "file", "name": "twind.config.ts" }, "/routes/api/joke.ts",
{ "/routes/index.tsx",
"type": "directory", "/static/logo.svg",
"name": "components", "/.vscode/settings.json",
"contents": [ "/.vscode/extensions.json",
{ "type": "file", "name": "Button.tsx" }, "/.gitignore",
],
},
{
"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" },
],
},
]; ];
await t.step("check generated files", async () => { 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 () => { await t.step("start up the server and access the root page", async () => {
@@ -404,33 +296,15 @@ Deno.test({
path.join(tmpDirName, "deno.json"), path.join(tmpDirName, "deno.json"),
); );
const targetFileTree: FileTree[] = [ const files = [
{ "/deno.json",
"type": "directory", "/subdirectory/subsubdirectory/main.ts",
"name": tmpDirName, "/subdirectory/subsubdirectory/dev.ts",
"contents": [ "/subdirectory/subsubdirectory/fresh.gen.ts",
{ "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" },
],
},
],
},
],
},
]; ];
await t.step("check generated files", async () => { await t.step("check generated files", async () => {
await assertFileExistence(targetFileTree); await assertFileExistence(files, tmpDirName);
}); });
await t.step("start up the server", async () => { await t.step("start up the server", async () => {