mirror of
https://github.com/oven-sh/bun
synced 2026-02-10 02:48:50 +00:00
53 lines
1.8 KiB
JavaScript
53 lines
1.8 KiB
JavaScript
import { fileURLToPath, pathToFileURL } from "bun";
|
|
import { describe, expect, it } from "bun:test";
|
|
import { isWindows } from "harness";
|
|
|
|
describe("pathToFileURL", () => {
|
|
it("should convert a path to a file url", () => {
|
|
expect(pathToFileURL("/path/to/file.js").href).toBe("file:///path/to/file.js");
|
|
});
|
|
});
|
|
|
|
describe("fileURLToPath", () => {
|
|
const absoluteErrorMessage = "File URL path must be an absolute";
|
|
it("should convert a file url to a path", () => {
|
|
if (isWindows) {
|
|
expect(() => fileURLToPath("file:///path/to/file.js")).toThrow(absoluteErrorMessage);
|
|
} else {
|
|
expect(fileURLToPath("file:///path/to/file.js")).toBe("/path/to/file.js");
|
|
}
|
|
});
|
|
|
|
it("should convert a URL to a path", () => {
|
|
if (isWindows) {
|
|
expect(() => fileURLToPath(new URL("file:///path/to/file.js"))).toThrow(absoluteErrorMessage);
|
|
} else {
|
|
expect(fileURLToPath(new URL("file:///path/to/file.js"))).toBe("/path/to/file.js");
|
|
}
|
|
});
|
|
|
|
it("should fail on non-file: URLs", () => {
|
|
expect(() => fileURLToPath(new URL("http:///path/to/file.js"))).toThrow();
|
|
});
|
|
|
|
describe("should fail on non URLs", () => {
|
|
const fuzz = [1, true, Symbol("foo"), {}, [], () => {}, null, undefined, NaN, Infinity, -Infinity, new Boolean()];
|
|
fuzz.forEach(value => {
|
|
it(`${String(value)}`, () => {
|
|
expect(() => fileURLToPath(value)).toThrow();
|
|
});
|
|
});
|
|
});
|
|
|
|
it("should add absolute part to relative file (#6456)", () => {
|
|
const url = pathToFileURL("foo.txt");
|
|
expect(url.href).toBe(`${pathToFileURL(process.cwd())}/foo.txt`);
|
|
});
|
|
|
|
it("should roundtrip", () => {
|
|
const url = pathToFileURL(import.meta.path);
|
|
expect(fileURLToPath(url)).toBe(import.meta.path);
|
|
expect(fileURLToPath(import.meta.url)).toBe(import.meta.path);
|
|
});
|
|
});
|