Skip to content

Commit 324c7eb

Browse files
authored
fix within on Windows (#1804)
* within tests * fix within on windows
1 parent d084983 commit 324c7eb

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

src/files.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,8 @@ import {cwd} from "node:process";
77
import {fileURLToPath} from "node:url";
88
import {isEnoent} from "./error.js";
99

10-
export function toOsPath(path: string): string {
11-
return path.split(sep).join(op.sep);
12-
}
13-
14-
export function fromOsPath(path: string): string {
15-
return path.split(op.sep).join(sep);
16-
}
10+
export const toOsPath = sep === op.sep ? (path: string) => path : (path: string) => path.split(sep).join(op.sep);
11+
export const fromOsPath = sep === op.sep ? (path: string) => path : (path: string) => path.split(op.sep).join(sep);
1712

1813
/**
1914
* Returns the relative path from the current working directory to the given

src/path.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import {dirname, isAbsolute, join, normalize, relative, resolve} from "node:path/posix";
1+
import op from "node:path";
2+
import {dirname, join} from "node:path/posix";
23

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

8889
export function within(root: string, path: string): boolean {
90+
const {relative, normalize, resolve, isAbsolute} = op;
8991
path = relative(normalize(resolve(root)), normalize(resolve(path)));
9092
return !path.startsWith("..") && !isAbsolute(path);
9193
}

test/path-test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import assert from "node:assert";
2-
import {isPathImport, parseRelativeUrl, relativePath, resolveLocalPath, resolvePath} from "../src/path.js";
2+
import {isPathImport, parseRelativeUrl, relativePath, resolveLocalPath, resolvePath, within} from "../src/path.js";
33

44
describe("resolvePath(source, target)", () => {
55
it("returns the path to the specified target within the source root", () => {
@@ -151,3 +151,21 @@ describe("parseRelativeUrl(url)", () => {
151151
assert.deepStrictEqual(parseRelativeUrl("foo?bar#baz"), {pathname: "foo", search: "?bar", hash: "#baz"});
152152
});
153153
});
154+
155+
describe("within(root, path)", () => {
156+
it("returns true for paths within the current working directory", () => {
157+
assert.strictEqual(within(process.cwd(), "dist"), true, "dist");
158+
assert.strictEqual(within(process.cwd(), "./dist"), true, "./dist");
159+
assert.strictEqual(within(process.cwd(), "dist/"), true, "dist/");
160+
assert.strictEqual(within(process.cwd(), "./dist/"), true, "./dist/");
161+
assert.strictEqual(within(process.cwd(), "foo/../dist"), true, "foo/../dist");
162+
assert.strictEqual(within(process.cwd(), "foo/../dist/"), true, "foo/../dist/");
163+
assert.strictEqual(within(process.cwd(), "./foo/../dist/"), true, "./foo/../dist/");
164+
assert.strictEqual(within(process.cwd(), "foo/bar"), true, "foo/bar");
165+
assert.strictEqual(within(process.cwd(), "foo/bar"), true, "foo/bar");
166+
assert.strictEqual(within(process.cwd(), "../framework/dist"), true, "../framework/dist");
167+
assert.strictEqual(within(process.cwd(), "../framework2/dist"), false, "../framework2/dist");
168+
assert.strictEqual(within(process.cwd(), "../dist"), false, "../dist");
169+
assert.strictEqual(within(process.cwd(), "/dist"), false, "/dist");
170+
});
171+
});

0 commit comments

Comments
 (0)