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
+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)) {