Skip to content

Commit

Permalink
fix: path handling inconsistencies (#2482)
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister authored Jun 3, 2024
1 parent 9347b84 commit 2fb8606
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 17 deletions.
33 changes: 27 additions & 6 deletions init/src/init_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 = {
Expand Down Expand Up @@ -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]: ".",
Expand All @@ -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"),
Expand All @@ -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]: ".",
Expand All @@ -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"],
Expand All @@ -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();
Expand Down
8 changes: 6 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,
Expand Down
17 changes: 9 additions & 8 deletions src/config_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 change: 1 addition & 0 deletions src/dev/file_transformer_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 2 additions & 0 deletions src/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface FreshFile {
}

export interface FsAdapter {
cwd(): string;
walk(
root: string | URL,
options?: WalkOptions,
Expand All @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion src/plugins/fs_routes/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
1 change: 1 addition & 0 deletions src/test_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down

0 comments on commit 2fb8606

Please sign in to comment.