From 2fb8606e9e0bac9b2a7fda6c726349e395f2a4f5 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Mon, 3 Jun 2024 15:03:48 +0200 Subject: [PATCH] fix: path handling inconsistencies (#2482) --- init/src/init_test.ts | 33 ++++++++++++++++++++++++++------ src/config.ts | 8 ++++++-- src/config_test.ts | 17 ++++++++-------- src/dev/file_transformer_test.ts | 1 + src/fs.ts | 2 ++ src/plugins/fs_routes/mod.ts | 4 +++- src/test_utils.ts | 1 + 7 files changed, 49 insertions(+), 17 deletions(-) diff --git a/init/src/init_test.ts b/init/src/init_test.ts index 6163624c..40337317 100644 --- a/init/src/init_test.ts +++ b/init/src/init_test.ts @@ -6,7 +6,9 @@ import { waitForText } from "../../tests/test_utils.tsx"; import { withChildProcessServer } from "../../tests/test_utils.tsx"; async function withTmpDir(fn: (dir: string) => void | Promise) { - const dir = await Deno.makeTempDir(); + const hash = crypto.randomUUID().replaceAll(/-/g, ""); + const dir = path.join(import.meta.dirname!, "..", "..", `tmp-${hash}`); + await Deno.mkdir(dir, { recursive: true }); try { await fn(dir); @@ -15,6 +17,25 @@ async function withTmpDir(fn: (dir: string) => void | Promise) { } } +// TODO: Patch project dependencies until there is an easier way +// to link JSR dependencies +async function patchProject(dir: string): Promise { + const jsonPath = path.join(dir, "deno.json"); + const json = JSON.parse(await Deno.readTextFile(jsonPath)); + const rootJson = JSON.parse( + await Deno.readTextFile( + path.join(import.meta.dirname!, "..", "..", "deno.json"), + ), + ); + + json.imports = rootJson.imports; + json.imports["@fresh/core"] = "../src/mod.ts"; + json.imports["@fresh/core/dev"] = "../src/dev/mod.ts"; + json.imports["@fresh/plugin-tailwind"] = "../plugin-tailwindcss/mod.ts"; + + await Deno.writeTextFile(jsonPath, JSON.stringify(json, null, 2)); +} + function mockUserInput(steps: Record) { const errorOutput: unknown[][] = []; const tty: MockTTY = { @@ -104,8 +125,7 @@ Deno.test("init - with vscode", async () => { }); }); -// TODO: Testing this with JSR isn't as easy anymore as it was before -Deno.test.ignore("init - can start dev server", async () => { +Deno.test("init - can start dev server", async () => { await withTmpDir(async (dir) => { const mock = mockUserInput({ [InitStep.ProjectName]: ".", @@ -114,6 +134,7 @@ Deno.test.ignore("init - can start dev server", async () => { await expectProjectFile(dir, "main.ts"); await expectProjectFile(dir, "dev.ts"); + await patchProject(dir); await withChildProcessServer( dir, path.join(dir, "dev.ts"), @@ -128,8 +149,7 @@ Deno.test.ignore("init - can start dev server", async () => { }); }); -// TODO: Testing this with JSR isn't as easy anymore as it was before -Deno.test.ignore("init - can start build project", async () => { +Deno.test("init - can start build project", async () => { await withTmpDir(async (dir) => { const mock = mockUserInput({ [InitStep.ProjectName]: ".", @@ -138,6 +158,8 @@ Deno.test.ignore("init - can start build project", async () => { await expectProjectFile(dir, "main.ts"); await expectProjectFile(dir, "dev.ts"); + await patchProject(dir); + // Build await new Deno.Command(Deno.execPath(), { args: ["run", "-A", path.join(dir, "dev.ts"), "build"], @@ -151,7 +173,6 @@ Deno.test.ignore("init - can start build project", async () => { dir, path.join(dir, "main.ts"), async (address) => { - console.log({ address }); await withBrowser(async (page) => { await page.goto(address); await page.locator("button").click(); diff --git a/src/config.ts b/src/config.ts index 9db284b2..2f46fd76 100644 --- a/src/config.ts +++ b/src/config.ts @@ -39,9 +39,11 @@ export interface ResolvedFreshConfig { mode: Mode; } -export function parseRootPath(root: string): string { +export function parseRootPath(root: string, cwd: string): string { if (root.startsWith("file://")) { root = path.fromFileUrl(root); + } else if (!path.isAbsolute(root)) { + root = path.join(cwd, root); } const ext = path.extname(root); @@ -56,7 +58,9 @@ export function parseRootPath(root: string): string { } export function normalizeConfig(options: FreshConfig): ResolvedFreshConfig { - const root = options.root ? parseRootPath(options.root) : Deno.cwd(); + const root = options.root + ? parseRootPath(options.root, Deno.cwd()) + : Deno.cwd(); return { root, diff --git a/src/config_test.ts b/src/config_test.ts index 1233c3d7..b99d05ee 100644 --- a/src/config_test.ts +++ b/src/config_test.ts @@ -3,12 +3,13 @@ import { parseRootPath } from "./config.ts"; // FIXME: Windows Deno.test.ignore("parseRootPath", () => { - expect(parseRootPath("file:///foo/bar")).toEqual("/foo/bar"); - expect(parseRootPath("file:///foo/bar.ts")).toEqual("/foo"); - expect(parseRootPath("/foo/bar")).toEqual("/foo/bar"); - expect(parseRootPath("/foo/bar.ts")).toEqual("/foo"); - expect(parseRootPath("/foo/bar.tsx")).toEqual("/foo"); - expect(parseRootPath("/foo/bar.js")).toEqual("/foo"); - expect(parseRootPath("/foo/bar.jsx")).toEqual("/foo"); - expect(parseRootPath("/foo/bar.mjs")).toEqual("/foo"); + const cwd = Deno.cwd(); + expect(parseRootPath("file:///foo/bar", cwd)).toEqual("/foo/bar"); + expect(parseRootPath("file:///foo/bar.ts", cwd)).toEqual("/foo"); + expect(parseRootPath("/foo/bar", cwd)).toEqual("/foo/bar"); + expect(parseRootPath("/foo/bar.ts", cwd)).toEqual("/foo"); + expect(parseRootPath("/foo/bar.tsx", cwd)).toEqual("/foo"); + expect(parseRootPath("/foo/bar.js", cwd)).toEqual("/foo"); + expect(parseRootPath("/foo/bar.jsx", cwd)).toEqual("/foo"); + expect(parseRootPath("/foo/bar.mjs", cwd)).toEqual("/foo"); }); diff --git a/src/dev/file_transformer_test.ts b/src/dev/file_transformer_test.ts index cc25e3de..e8537fa2 100644 --- a/src/dev/file_transformer_test.ts +++ b/src/dev/file_transformer_test.ts @@ -8,6 +8,7 @@ import { delay } from "../test_utils.ts"; function testTransformer(files: Record) { const mockFs: FsAdapter = { + cwd: () => "/", isDirectory: () => Promise.resolve(false), mkdirp: () => Promise.resolve(), walk: async function* foo() { diff --git a/src/fs.ts b/src/fs.ts index 35c4d149..7ef6ea3d 100644 --- a/src/fs.ts +++ b/src/fs.ts @@ -6,6 +6,7 @@ export interface FreshFile { } export interface FsAdapter { + cwd(): string; walk( root: string | URL, options?: WalkOptions, @@ -17,6 +18,7 @@ export interface FsAdapter { export const fsAdapter: FsAdapter = { walk, + cwd: Deno.cwd, async isDirectory(path) { try { const stat = await Deno.stat(path); diff --git a/src/plugins/fs_routes/mod.ts b/src/plugins/fs_routes/mod.ts index f52f002a..28a5d634 100644 --- a/src/plugins/fs_routes/mod.ts +++ b/src/plugins/fs_routes/mod.ts @@ -65,7 +65,9 @@ export async function fsRoutes( const ignore = options.ignoreFilePattern ?? [TEST_FILE_PATTERN]; const fs = options._fs ?? fsAdapter; - const dir = options.dir ? parseRootPath(options.dir) : app.config.root; + const dir = options.dir + ? parseRootPath(options.dir, fs.cwd()) + : app.config.root; const islandDir = path.join(dir, "islands"); const routesDir = path.join(dir, "routes"); diff --git a/src/test_utils.ts b/src/test_utils.ts index c522e868..59bab7f9 100644 --- a/src/test_utils.ts +++ b/src/test_utils.ts @@ -91,6 +91,7 @@ export function serveMiddleware( export function createFakeFs(files: Record): FsAdapter { return { + cwd: () => ".", async *walk(_root) { // FIXME: ignore for (const file of Object.keys(files)) {