Skip to content

Commit

Permalink
within tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Nov 7, 2024
1 parent 5fd5703 commit fb6b7fb
Showing 1 changed file with 19 additions and 1 deletion.
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 fb6b7fb

Please sign in to comment.