Skip to content

Commit

Permalink
fix within on Windows (#1804)
Browse files Browse the repository at this point in the history
* within tests

* fix within on windows
  • Loading branch information
mbostock authored Nov 7, 2024
1 parent d084983 commit 324c7eb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
9 changes: 2 additions & 7 deletions src/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,8 @@ import {cwd} from "node:process";
import {fileURLToPath} from "node:url";
import {isEnoent} from "./error.js";

export function toOsPath(path: string): string {
return path.split(sep).join(op.sep);
}

export function fromOsPath(path: string): string {
return path.split(op.sep).join(sep);
}
export const toOsPath = sep === op.sep ? (path: string) => path : (path: string) => path.split(sep).join(op.sep);
export const fromOsPath = sep === op.sep ? (path: string) => path : (path: string) => path.split(op.sep).join(sep);

/**
* Returns the relative path from the current working directory to the given
Expand Down
4 changes: 3 additions & 1 deletion src/path.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {dirname, isAbsolute, join, normalize, relative, resolve} from "node:path/posix";
import op from "node:path";
import {dirname, join} from "node:path/posix";

/**
* Returns the normalized relative path from "/file/path/to/a" to
Expand Down Expand Up @@ -86,6 +87,7 @@ export function parseRelativeUrl(url: string): {pathname: string; search: string
}

export function within(root: string, path: string): boolean {
const {relative, normalize, resolve, isAbsolute} = op;
path = relative(normalize(resolve(root)), normalize(resolve(path)));
return !path.startsWith("..") && !isAbsolute(path);
}
20 changes: 19 additions & 1 deletion test/path-test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from "node:assert";
import {isPathImport, parseRelativeUrl, relativePath, resolveLocalPath, resolvePath} from "../src/path.js";
import {isPathImport, parseRelativeUrl, relativePath, resolveLocalPath, resolvePath, within} from "../src/path.js";

describe("resolvePath(source, target)", () => {
it("returns the path to the specified target within the source root", () => {
Expand Down Expand Up @@ -151,3 +151,21 @@ describe("parseRelativeUrl(url)", () => {
assert.deepStrictEqual(parseRelativeUrl("foo?bar#baz"), {pathname: "foo", search: "?bar", hash: "#baz"});
});
});

describe("within(root, path)", () => {
it("returns true for paths within the current working directory", () => {
assert.strictEqual(within(process.cwd(), "dist"), true, "dist");
assert.strictEqual(within(process.cwd(), "./dist"), true, "./dist");
assert.strictEqual(within(process.cwd(), "dist/"), true, "dist/");
assert.strictEqual(within(process.cwd(), "./dist/"), true, "./dist/");
assert.strictEqual(within(process.cwd(), "foo/../dist"), true, "foo/../dist");
assert.strictEqual(within(process.cwd(), "foo/../dist/"), true, "foo/../dist/");
assert.strictEqual(within(process.cwd(), "./foo/../dist/"), true, "./foo/../dist/");
assert.strictEqual(within(process.cwd(), "foo/bar"), true, "foo/bar");
assert.strictEqual(within(process.cwd(), "foo/bar"), true, "foo/bar");
assert.strictEqual(within(process.cwd(), "../framework/dist"), true, "../framework/dist");
assert.strictEqual(within(process.cwd(), "../framework2/dist"), false, "../framework2/dist");
assert.strictEqual(within(process.cwd(), "../dist"), false, "../dist");
assert.strictEqual(within(process.cwd(), "/dist"), false, "/dist");
});
});

0 comments on commit 324c7eb

Please sign in to comment.