Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(path): Enable and fix file URL tests #804

Merged
merged 2 commits into from
Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions path/_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,18 @@ export function _format(
if (dir === pathObject.root) return dir + base;
return dir + sep + base;
}

const WHITESPACE_ENCODINGS: Record<string, string> = {
"\u0009": "%09",
"\u000A": "%0A",
"\u000B": "%0B",
"\u000C": "%0C",
"\u000D": "%0D",
"\u0020": "%20",
};

export function encodeWhitespace(string: string): string {
return string.replaceAll(/[\s]/g, (c) => {
return WHITESPACE_ENCODINGS[c] ?? c;
});
}
8 changes: 2 additions & 6 deletions path/from_file_url_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,14 @@ Deno.test("[path] fromFileUrl", function () {
);
});

const isCanary = /\d\.\d\.\d\+[0-9a-f]{7}/.test(Deno.version.deno);

Deno.test("[path] fromFileUrl (win32)", function () {
assertEquals(win32.fromFileUrl(new URL("file:///home/foo")), "\\home\\foo");
assertEquals(win32.fromFileUrl("file:///"), "\\");
assertEquals(win32.fromFileUrl("file:///home/foo"), "\\home\\foo");
assertEquals(win32.fromFileUrl("file:///home/foo%20bar"), "\\home\\foo bar");
assertEquals(win32.fromFileUrl("file:///%"), "\\%");
// TODO(kt3k): Enable this assertion again in stable deno when 1.8.0 is landed.
if (isCanary) {
assertEquals(win32.fromFileUrl("file://localhost/foo"), "\\foo");
}
assertEquals(win32.fromFileUrl("file://127.0.0.1/foo"), "\\\\127.0.0.1\\foo");
assertEquals(win32.fromFileUrl("file://localhost/foo"), "\\foo");
assertEquals(win32.fromFileUrl("file:///C:"), "C:\\");
assertEquals(win32.fromFileUrl("file:///C:/"), "C:\\");
// Drop the hostname if a drive letter is parsed.
Expand Down
5 changes: 4 additions & 1 deletion path/posix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { CHAR_DOT, CHAR_FORWARD_SLASH } from "./_constants.ts";
import {
_format,
assertPath,
encodeWhitespace,
isPosixPathSeparator,
normalizeString,
} from "./_util.ts";
Expand Down Expand Up @@ -499,6 +500,8 @@ export function toFileUrl(path: string): URL {
throw new TypeError("Must be an absolute path.");
}
const url = new URL("file:///");
url.pathname = path.replace(/%/g, "%25").replace(/\\/g, "%5C");
url.pathname = encodeWhitespace(
path.replace(/%/g, "%25").replace(/\\/g, "%5C"),
);
return url;
}
50 changes: 18 additions & 32 deletions path/to_file_url_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@
import { posix, win32 } from "./mod.ts";
import { assertEquals, assertThrows } from "../testing/asserts.ts";

const isCanary = /\d\.\d\.\d\+[0-9a-f]{7}/.test(Deno.version.deno);

Deno.test("[path] toFileUrl", function () {
assertEquals(posix.toFileUrl("/home/foo").href, "file:///home/foo");
// TODO(kt3k): Enable this assertion again in stable deno when 1.8.0 is landed.
if (isCanary) {
assertEquals(posix.toFileUrl("/home/ ").href, "file:///home/");
}
assertEquals(posix.toFileUrl("/home/ ").href, "file:///home/%20");
assertEquals(posix.toFileUrl("/home/%20").href, "file:///home/%2520");
assertEquals(posix.toFileUrl("/home\\foo").href, "file:///home%5Cfoo");
assertThrows(
Expand All @@ -22,23 +17,17 @@ Deno.test("[path] toFileUrl", function () {
TypeError,
"Must be an absolute path.",
);
// TODO(kt3k): Enable this assertion again in stable deno when 1.8.0 is landed.
if (isCanary) {
assertEquals(
posix.toFileUrl("//localhost/home/foo").href,
"file:///localhost/home/foo",
);
assertEquals(posix.toFileUrl("//localhost/").href, "file:///localhost/");
assertEquals(posix.toFileUrl("//:/home/foo").href, "file:///:/home/foo");
}
assertEquals(
posix.toFileUrl("//localhost/home/foo").href,
"file:///localhost/home/foo",
);
assertEquals(posix.toFileUrl("//localhost/").href, "file:///localhost/");
assertEquals(posix.toFileUrl("//:/home/foo").href, "file:///:/home/foo");
});

Deno.test("[path] toFileUrl (win32)", function () {
assertEquals(win32.toFileUrl("/home/foo").href, "file:///home/foo");
// TODO(kt3k): Enable this assertion again in stable deno when 1.8.0 is landed.
if (isCanary) {
assertEquals(win32.toFileUrl("/home/ ").href, "file:///home/");
}
assertEquals(win32.toFileUrl("/home/ ").href, "file:///home/%20");
assertEquals(win32.toFileUrl("/home/%20").href, "file:///home/%2520");
assertEquals(win32.toFileUrl("/home\\foo").href, "file:///home/foo");
assertThrows(
Expand All @@ -47,19 +36,16 @@ Deno.test("[path] toFileUrl (win32)", function () {
"Must be an absolute path.",
);
assertEquals(win32.toFileUrl("C:/").href, "file:///C:/");
// TODO(kt3k): Enable this assertion again in stable deno when 1.8.0 is landed.
if (isCanary) {
assertThrows(
() =>
assertEquals(
win32.toFileUrl("//localhost/home/foo").href,
"file://localhost/home/foo",
),
TypeError,
"Invalid hostname.",
);
assertEquals(win32.toFileUrl("//localhost/").href, "file:///localhost/");
}
assertEquals(
win32.toFileUrl("//localhost/home/foo").href,
"file:///home/foo",
);
assertEquals(
win32.toFileUrl("//127.0.0.1/home/foo").href,
"file://127.0.0.1/home/foo",
);
assertEquals(win32.toFileUrl("//localhost/").href, "file:///");
assertEquals(win32.toFileUrl("//127.0.0.1/").href, "file://127.0.0.1/");
assertThrows(
() => win32.toFileUrl("//:/home/foo").href,
TypeError,
Expand Down
9 changes: 5 additions & 4 deletions path/win32.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import {
_format,
assertPath,
encodeWhitespace,
isPathSeparator,
isWindowsDeviceRoot,
normalizeString,
Expand Down Expand Up @@ -979,19 +980,19 @@ export function fromFileUrl(url: string | URL): string {
*
* toFileUrl("\\home\\foo"); // new URL("file:///home/foo")
* toFileUrl("C:\\Users\\foo"); // new URL("file:///C:/Users/foo")
* toFileUrl("\\\\localhost\\home\\foo"); // new URL("file://localhost/home/foo")
* toFileUrl("\\\\127.0.0.1\\home\\foo"); // new URL("file://127.0.0.1/home/foo")
* @param path to convert to file URL
*/
export function toFileUrl(path: string): URL {
if (!isAbsolute(path)) {
throw new TypeError("Must be an absolute path.");
}
const [, hostname, pathname] = path.match(
/^(?:[/\\]{2}([^/\\]+)(?=[/\\][^/\\]))?(.*)/,
/^(?:[/\\]{2}([^/\\]+)(?=[/\\](?:[^/\\]|$)))?(.*)/,
)!;
const url = new URL("file:///");
url.pathname = pathname.replace(/%/g, "%25");
if (hostname != null) {
url.pathname = encodeWhitespace(pathname.replace(/%/g, "%25"));
if (hostname != null && hostname != "localhost") {
url.hostname = hostname;
if (!url.hostname) {
throw new TypeError("Invalid hostname.");
Expand Down