From 9177d9afc34696a7a3003da90efcb6755dd30dd9 Mon Sep 17 00:00:00 2001 From: Reed von Redwitz Date: Tue, 11 Jul 2023 14:39:48 +0200 Subject: [PATCH] feat: prevent duplicate routes when generating manifest (#1410) --- src/dev/mod.ts | 11 +++ tests/fixture_route_analysis/deno.json | 20 +++++ tests/fixture_route_analysis/dev.ts | 5 ++ tests/fixture_route_analysis/fresh.gen.ts | 17 +++++ tests/fixture_route_analysis/main.ts | 12 +++ .../routes/simple_name_conflict/users.ts | 7 ++ .../routes/simple_name_conflict/users.tsx | 3 + tests/route_analysis_test.ts | 18 +++++ tests/test_utils.ts | 74 +++++++++++++------ 9 files changed, 146 insertions(+), 21 deletions(-) create mode 100644 tests/fixture_route_analysis/deno.json create mode 100755 tests/fixture_route_analysis/dev.ts create mode 100644 tests/fixture_route_analysis/fresh.gen.ts create mode 100644 tests/fixture_route_analysis/main.ts create mode 100644 tests/fixture_route_analysis/routes/simple_name_conflict/users.ts create mode 100644 tests/fixture_route_analysis/routes/simple_name_conflict/users.tsx create mode 100644 tests/route_analysis_test.ts diff --git a/src/dev/mod.ts b/src/dev/mod.ts index 7c62a1ea..8103d1cf 100644 --- a/src/dev/mod.ts +++ b/src/dev/mod.ts @@ -39,6 +39,7 @@ async function collectDir(dir: string): Promise { } const paths = []; + const fileNames = new Set(); const routesFolder = walk(dir, { includeDirs: false, includeFiles: true, @@ -46,6 +47,16 @@ async function collectDir(dir: string): Promise { }); for await (const entry of routesFolder) { + const fileNameWithoutExt = relative(dir, entry.path).split(".").slice(0, -1) + .join("."); + + if (fileNames.has(fileNameWithoutExt)) { + throw new Error( + `Route conflict detected. Multiple files have the same name: ${dir}${fileNameWithoutExt}`, + ); + } + + fileNames.add(fileNameWithoutExt); paths.push(relative(dir, entry.path)); } diff --git a/tests/fixture_route_analysis/deno.json b/tests/fixture_route_analysis/deno.json new file mode 100644 index 00000000..9951af16 --- /dev/null +++ b/tests/fixture_route_analysis/deno.json @@ -0,0 +1,20 @@ +{ + "lock": false, + "tasks": { + "start": "deno run -A --watch=static/,routes/ dev.ts", + "update": "deno run -A -r https://fresh.deno.dev/update ." + }, + "imports": { + "$fresh/": "../../", + "preact": "https://esm.sh/preact@10.15.1", + "preact/": "https://esm.sh/preact@10.15.1/", + "preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.1.0", + "@preact/signals": "https://esm.sh/*@preact/signals@1.1.3", + "@preact/signals-core": "https://esm.sh/*@preact/signals-core@1.2.3", + "$std/": "https://deno.land/std@0.190.0/" + }, + "compilerOptions": { + "jsx": "react-jsx", + "jsxImportSource": "preact" + } +} diff --git a/tests/fixture_route_analysis/dev.ts b/tests/fixture_route_analysis/dev.ts new file mode 100755 index 00000000..2d85d6c1 --- /dev/null +++ b/tests/fixture_route_analysis/dev.ts @@ -0,0 +1,5 @@ +#!/usr/bin/env -S deno run -A --watch=static/,routes/ + +import dev from "$fresh/dev.ts"; + +await dev(import.meta.url, "./main.ts"); diff --git a/tests/fixture_route_analysis/fresh.gen.ts b/tests/fixture_route_analysis/fresh.gen.ts new file mode 100644 index 00000000..8cc887a4 --- /dev/null +++ b/tests/fixture_route_analysis/fresh.gen.ts @@ -0,0 +1,17 @@ +// DO NOT EDIT. This file is generated by fresh. +// This file SHOULD be checked into source version control. +// This file is automatically updated during development when running `dev.ts`. + +import * as $0 from "./routes/simple_name_conflict/users.ts"; +import * as $1 from "./routes/simple_name_conflict/users.tsx"; + +const manifest = { + routes: { + "./routes/simple_name_conflict/users.ts": $0, + "./routes/simple_name_conflict/users.tsx": $1, + }, + islands: {}, + baseUrl: import.meta.url, +}; + +export default manifest; diff --git a/tests/fixture_route_analysis/main.ts b/tests/fixture_route_analysis/main.ts new file mode 100644 index 00000000..8ef5d924 --- /dev/null +++ b/tests/fixture_route_analysis/main.ts @@ -0,0 +1,12 @@ +/// +/// +/// +/// +/// + +import "$std/dotenv/load.ts"; + +import { start } from "$fresh/server.ts"; +import manifest from "./fresh.gen.ts"; + +await start(manifest); diff --git a/tests/fixture_route_analysis/routes/simple_name_conflict/users.ts b/tests/fixture_route_analysis/routes/simple_name_conflict/users.ts new file mode 100644 index 00000000..90d9e898 --- /dev/null +++ b/tests/fixture_route_analysis/routes/simple_name_conflict/users.ts @@ -0,0 +1,7 @@ +import { Handlers } from "$fresh/server.ts"; + +export const handler: Handlers = { + GET(_req, _ctx) { + return new Response("hello"); + }, +}; diff --git a/tests/fixture_route_analysis/routes/simple_name_conflict/users.tsx b/tests/fixture_route_analysis/routes/simple_name_conflict/users.tsx new file mode 100644 index 00000000..9899158d --- /dev/null +++ b/tests/fixture_route_analysis/routes/simple_name_conflict/users.tsx @@ -0,0 +1,3 @@ +export default function Home() { + return
Hello
; +} diff --git a/tests/route_analysis_test.ts b/tests/route_analysis_test.ts new file mode 100644 index 00000000..ec457e0d --- /dev/null +++ b/tests/route_analysis_test.ts @@ -0,0 +1,18 @@ +import { startFreshServerExpectErrors } from "./test_utils.ts"; +import { dirname, join } from "$std/path/mod.ts"; +import { assertStringIncludes } from "./deps.ts"; + +const dir = dirname(import.meta.url); + +Deno.test({ + name: "route-conflicts", + async fn() { + const errorMessage = await startFreshServerExpectErrors({ + args: ["run", "-A", join(dir, "./fixture_route_analysis/dev.ts")], + }); + assertStringIncludes( + errorMessage, + "Error: Route conflict detected. Multiple files have the same name", + ); + }, +}); diff --git a/tests/test_utils.ts b/tests/test_utils.ts index 96b3c6f9..693a379d 100644 --- a/tests/test_utils.ts +++ b/tests/test_utils.ts @@ -1,28 +1,8 @@ import { delay, Page, puppeteer, TextLineStream } from "./deps.ts"; export async function startFreshServer(options: Deno.CommandOptions) { - const serverProcess = new Deno.Command(Deno.execPath(), { - ...options, - stdin: "null", - stdout: "piped", - stderr: "inherit", - }).spawn(); + const { serverProcess, lines, address } = await spawnServer(options); - const decoder = new TextDecoderStream(); - const lines: ReadableStream = serverProcess.stdout - .pipeThrough(decoder) - .pipeThrough(new TextLineStream(), { - preventCancel: true, - }); - - let address = ""; - for await (const line of lines) { - const match = line.match(/https?:\/\/localhost:\d+/g); - if (match) { - address = match[0]; - break; - } - } if (!address) { throw new Error("Server didn't start up"); } @@ -57,3 +37,55 @@ export async function withPageName( await serverProcess.status; } } + +export async function startFreshServerExpectErrors( + options: Deno.CommandOptions, +) { + const { serverProcess, address } = await spawnServer(options, true); + + if (address) { + throw Error("Server started correctly"); + } + + const errorDecoder = new TextDecoderStream(); + const errorLines: ReadableStream = serverProcess.stderr + .pipeThrough(errorDecoder) + .pipeThrough(new TextLineStream(), { + preventCancel: true, + }); + let output = ""; + for await (const line of errorLines) { + output += line + "\n"; + } + return output; +} + +async function spawnServer( + options: Deno.CommandOptions, + expectErrors = false, +) { + const serverProcess = new Deno.Command(Deno.execPath(), { + ...options, + stdin: "null", + stdout: "piped", + stderr: expectErrors ? "piped" : "inherit", + }).spawn(); + + const decoder = new TextDecoderStream(); + const lines: ReadableStream = serverProcess.stdout + .pipeThrough(decoder) + .pipeThrough(new TextLineStream(), { + preventCancel: true, + }); + + let address = ""; + for await (const line of lines) { + const match = line.match(/https?:\/\/localhost:\d+/g); + if (match) { + address = match[0]; + break; + } + } + + return { serverProcess, lines, address }; +}