Skip to content

Commit 0395403

Browse files
committed
normalize windows path
1 parent a5d90ca commit 0395403

4 files changed

Lines changed: 66 additions & 5 deletions

File tree

packages/fresh/src/dev/dev_build_cache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const WINDOWS_SEPARATOR = pathWin32.SEPARATOR;
2020
/** Normalize a path to use forward slashes so that generated files
2121
* are portable across operating systems (e.g. build on Windows,
2222
* deploy on Linux). */
23-
function toPosix(p: string): string {
23+
export function toPosix(p: string): string {
2424
return p.replaceAll(WINDOWS_SEPARATOR, "/");
2525
}
2626

packages/fresh/src/dev/fs_crawl.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { type FsAdapter, fsAdapter } from "../fs.ts";
22
import type { WalkEntry } from "@std/fs/walk";
3-
import type { FsRouteFileNoMod } from "./dev_build_cache.ts";
3+
import { type FsRouteFileNoMod, toPosix } from "./dev_build_cache.ts";
44
import * as path from "@std/path";
55
import { pathToPattern } from "../router.ts";
66
import { CommandType } from "../commands.ts";
@@ -86,7 +86,7 @@ export async function crawlRouteDir<State>(
8686

8787
files.push({
8888
id,
89-
filePath: entry.path,
89+
filePath: toPosix(entry.path),
9090
type,
9191
pattern,
9292
routePattern,

packages/fresh/src/dev/fs_crawl_test.ts

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect } from "@std/expect/expect";
22
import { createFakeFs } from "../test_utils.ts";
3-
import { walkDir } from "./fs_crawl.ts";
3+
import { crawlRouteDir, walkDir } from "./fs_crawl.ts";
44

55
Deno.test("walkDir - ", async () => {
66
const fs = createFakeFs({
@@ -43,3 +43,60 @@ Deno.test("walkDir - respects skip patterns", async () => {
4343
"routes/api/users.ts",
4444
]);
4545
});
46+
47+
Deno.test("crawlRouteDir.filePath - normalized Windows paths", async () => {
48+
const fs = createFakeFs({
49+
"foo\\bar\\baz.txt": "foo",
50+
"D:\\foo\\bar.tsx": "foo",
51+
});
52+
53+
const rawFiles = await crawlRouteDir(fs, "foo", [], () => {});
54+
55+
if (Deno.build.os === "windows") {
56+
expect(rawFiles).toEqual(expect.arrayContaining([
57+
{
58+
id: "/foo/bar",
59+
filePath: "D:/foo/bar.tsx",
60+
type: "route",
61+
pattern: "/foo/bar",
62+
routePattern: "/foo/bar",
63+
lazy: true,
64+
css: [],
65+
overrideConfig: { methods: "ALL" },
66+
},
67+
{
68+
id: "/bar/baz",
69+
filePath: "foo/bar/baz.txt",
70+
type: "route",
71+
pattern: "/bar/baz",
72+
routePattern: "/bar/baz",
73+
lazy: true,
74+
css: [],
75+
overrideConfig: { methods: "ALL" },
76+
},
77+
]));
78+
} else {
79+
expect(rawFiles).toEqual(expect.arrayContaining([
80+
{
81+
id: "/D:/foo/bar",
82+
filePath: "D:/foo/bar.tsx",
83+
type: "route",
84+
pattern: "/D:/foo/bar",
85+
routePattern: "/D:/foo/bar",
86+
lazy: true,
87+
css: [],
88+
overrideConfig: { methods: "ALL" },
89+
},
90+
{
91+
id: "/foo/bar/baz",
92+
filePath: "foo/bar/baz.txt",
93+
type: "route",
94+
pattern: "/foo/bar/baz",
95+
routePattern: "/foo/bar/baz",
96+
lazy: true,
97+
css: [],
98+
overrideConfig: { methods: "ALL" },
99+
},
100+
]));
101+
}
102+
});

packages/fresh/src/test_utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { DEFAULT_CONN_INFO } from "./app.ts";
77
import type { Command } from "./commands.ts";
88
import { fsItemsToCommands, type FsRouteFile } from "./fs_routes.ts";
99
import * as path from "@std/path";
10+
import { toPosix } from "./dev/dev_build_cache.ts";
1011

1112
const STUB = {} as unknown as Deno.ServeHandlerInfo;
1213

@@ -123,7 +124,10 @@ export function createFakeFs(files: Record<string, unknown>): FsAdapter {
123124
},
124125
// deno-lint-ignore require-await
125126
async isDirectory(dir) {
126-
return Object.keys(files).some((file) => file.startsWith(dir + "/"));
127+
return Object.keys(files).some((file) =>
128+
// normalize path to posix before comparing
129+
toPosix(file).startsWith(dir + "/")
130+
);
127131
},
128132
async mkdirp(_dir: string) {
129133
},

0 commit comments

Comments
 (0)