fix: path handling inconsistencies (#2482)

This commit is contained in:
Marvin Hagemeister
2024-06-03 15:03:48 +02:00
committed by GitHub
parent 9347b84cba
commit 2fb8606e9e
7 changed files with 49 additions and 17 deletions
+27 -6
View File
@@ -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<void>) {
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<void>) {
}
}
// TODO: Patch project dependencies until there is an easier way
// to link JSR dependencies
async function patchProject(dir: string): Promise<void> {
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<string, unknown>) {
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();
+6 -2
View File
@@ -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,
+9 -8
View File
@@ -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");
});
+1
View File
@@ -8,6 +8,7 @@ import { delay } from "../test_utils.ts";
function testTransformer(files: Record<string, string>) {
const mockFs: FsAdapter = {
cwd: () => "/",
isDirectory: () => Promise.resolve(false),
mkdirp: () => Promise.resolve(),
walk: async function* foo() {
+2
View File
@@ -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);
+3 -1
View File
@@ -65,7 +65,9 @@ export async function fsRoutes<State>(
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");
+1
View File
@@ -91,6 +91,7 @@ export function serveMiddleware<T>(
export function createFakeFs(files: Record<string, unknown>): FsAdapter {
return {
cwd: () => ".",
async *walk(_root) {
// FIXME: ignore
for (const file of Object.keys(files)) {