2018-12-18 21:29:39 -05:00
|
|
|
// Copyright the Browserify authors. MIT License.
|
|
|
|
// Ported from https://github.com/browserify/path-browserify/
|
2019-10-16 14:39:33 -04:00
|
|
|
import { assertEquals } from "../testing/asserts.ts";
|
2019-01-10 17:11:44 -05:00
|
|
|
import * as path from "./mod.ts";
|
2018-12-18 21:29:39 -05:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
// TODO(kt3k): fix any types in this file
|
|
|
|
|
2018-12-18 21:29:39 -05:00
|
|
|
const winPaths = [
|
|
|
|
// [path, root]
|
|
|
|
["C:\\path\\dir\\index.html", "C:\\"],
|
|
|
|
["C:\\another_path\\DIR\\1\\2\\33\\\\index", "C:\\"],
|
|
|
|
["another_path\\DIR with spaces\\1\\2\\33\\index", ""],
|
|
|
|
["\\", "\\"],
|
|
|
|
["\\foo\\C:", "\\"],
|
|
|
|
["file", ""],
|
|
|
|
["file:stream", ""],
|
|
|
|
[".\\file", ""],
|
|
|
|
["C:", "C:"],
|
|
|
|
["C:.", "C:"],
|
|
|
|
["C:..", "C:"],
|
|
|
|
["C:abc", "C:"],
|
|
|
|
["C:\\", "C:\\"],
|
|
|
|
["C:\\abc", "C:\\"],
|
|
|
|
["", ""],
|
|
|
|
// unc
|
|
|
|
["\\\\server\\share\\file_path", "\\\\server\\share\\"],
|
|
|
|
[
|
|
|
|
"\\\\server two\\shared folder\\file path.zip",
|
2020-03-28 13:03:49 -04:00
|
|
|
"\\\\server two\\shared folder\\",
|
2018-12-18 21:29:39 -05:00
|
|
|
],
|
|
|
|
["\\\\teela\\admin$\\system32", "\\\\teela\\admin$\\"],
|
2020-03-28 13:03:49 -04:00
|
|
|
["\\\\?\\UNC\\server\\share", "\\\\?\\UNC\\"],
|
2018-12-18 21:29:39 -05:00
|
|
|
];
|
|
|
|
|
|
|
|
const winSpecialCaseParseTests = [["/foo/bar", { root: "/" }]];
|
|
|
|
|
|
|
|
const winSpecialCaseFormatTests = [
|
|
|
|
[{ dir: "some\\dir" }, "some\\dir\\"],
|
|
|
|
[{ base: "index.html" }, "index.html"],
|
|
|
|
[{ root: "C:\\" }, "C:\\"],
|
|
|
|
[{ name: "index", ext: ".html" }, "index.html"],
|
|
|
|
[{ dir: "some\\dir", name: "index", ext: ".html" }, "some\\dir\\index.html"],
|
|
|
|
[{ root: "C:\\", name: "index", ext: ".html" }, "C:\\index.html"],
|
2020-03-28 13:03:49 -04:00
|
|
|
[{}, ""],
|
2018-12-18 21:29:39 -05:00
|
|
|
];
|
|
|
|
|
|
|
|
const unixPaths = [
|
|
|
|
// [path, root]
|
|
|
|
["/home/user/dir/file.txt", "/"],
|
|
|
|
["/home/user/a dir/another File.zip", "/"],
|
|
|
|
["/home/user/a dir//another&File.", "/"],
|
|
|
|
["/home/user/a$$$dir//another File.zip", "/"],
|
|
|
|
["user/dir/another File.zip", ""],
|
|
|
|
["file", ""],
|
|
|
|
[".\\file", ""],
|
|
|
|
["./file", ""],
|
|
|
|
["C:\\foo", ""],
|
|
|
|
["/", "/"],
|
|
|
|
["", ""],
|
|
|
|
[".", ""],
|
|
|
|
["..", ""],
|
|
|
|
["/foo", "/"],
|
|
|
|
["/foo.", "/"],
|
|
|
|
["/foo.bar", "/"],
|
|
|
|
["/.", "/"],
|
|
|
|
["/.foo", "/"],
|
|
|
|
["/.foo.bar", "/"],
|
2020-03-28 13:03:49 -04:00
|
|
|
["/foo/bar.baz", "/"],
|
2018-12-18 21:29:39 -05:00
|
|
|
];
|
|
|
|
|
|
|
|
const unixSpecialCaseFormatTests = [
|
|
|
|
[{ dir: "some/dir" }, "some/dir/"],
|
|
|
|
[{ base: "index.html" }, "index.html"],
|
|
|
|
[{ root: "/" }, "/"],
|
|
|
|
[{ name: "index", ext: ".html" }, "index.html"],
|
|
|
|
[{ dir: "some/dir", name: "index", ext: ".html" }, "some/dir/index.html"],
|
|
|
|
[{ root: "/", name: "index", ext: ".html" }, "/index.html"],
|
2020-03-28 13:03:49 -04:00
|
|
|
[{}, ""],
|
2018-12-18 21:29:39 -05:00
|
|
|
];
|
|
|
|
|
2020-06-12 09:19:29 -04:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2019-05-30 08:59:30 -04:00
|
|
|
function checkParseFormat(path: any, paths: any): void {
|
2020-03-28 13:03:49 -04:00
|
|
|
paths.forEach(function (p: Array<Record<string, unknown>>) {
|
2019-03-12 01:51:51 -04:00
|
|
|
const element = p[0];
|
|
|
|
const output = path.parse(element);
|
|
|
|
assertEquals(typeof output.root, "string");
|
|
|
|
assertEquals(typeof output.dir, "string");
|
|
|
|
assertEquals(typeof output.base, "string");
|
|
|
|
assertEquals(typeof output.ext, "string");
|
|
|
|
assertEquals(typeof output.name, "string");
|
|
|
|
assertEquals(path.format(output), element);
|
|
|
|
assertEquals(output.rooroot, undefined);
|
|
|
|
assertEquals(output.dir, output.dir ? path.dirname(element) : "");
|
|
|
|
assertEquals(output.base, path.basename(element));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-12 09:19:29 -04:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2019-05-30 08:59:30 -04:00
|
|
|
function checkSpecialCaseParseFormat(path: any, testCases: any): void {
|
2020-03-28 13:03:49 -04:00
|
|
|
testCases.forEach(function (testCase: Array<Record<string, unknown>>) {
|
2019-03-12 01:51:51 -04:00
|
|
|
const element = testCase[0];
|
|
|
|
const expect = testCase[1];
|
|
|
|
const output = path.parse(element);
|
2020-03-28 13:03:49 -04:00
|
|
|
Object.keys(expect).forEach(function (key) {
|
2019-03-12 01:51:51 -04:00
|
|
|
assertEquals(output[key], expect[key]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-12 09:19:29 -04:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2019-05-30 08:59:30 -04:00
|
|
|
function checkFormat(path: any, testCases: unknown[][]): void {
|
2020-03-28 13:03:49 -04:00
|
|
|
testCases.forEach(function (testCase) {
|
2019-03-12 01:51:51 -04:00
|
|
|
assertEquals(path.format(testCase[0]), testCase[1]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("parseWin32", function () {
|
2018-12-18 21:29:39 -05:00
|
|
|
checkParseFormat(path.win32, winPaths);
|
|
|
|
checkSpecialCaseParseFormat(path.win32, winSpecialCaseParseTests);
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("parse", function () {
|
2018-12-18 21:29:39 -05:00
|
|
|
checkParseFormat(path.posix, unixPaths);
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("formatWin32", function () {
|
2018-12-18 21:29:39 -05:00
|
|
|
checkFormat(path.win32, winSpecialCaseFormatTests);
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("format", function () {
|
2018-12-18 21:29:39 -05:00
|
|
|
checkFormat(path.posix, unixSpecialCaseFormatTests);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Test removal of trailing path separators
|
|
|
|
const windowsTrailingTests = [
|
|
|
|
[".\\", { root: "", dir: "", base: ".", ext: "", name: "." }],
|
|
|
|
["\\\\", { root: "\\", dir: "\\", base: "", ext: "", name: "" }],
|
|
|
|
["\\\\", { root: "\\", dir: "\\", base: "", ext: "", name: "" }],
|
|
|
|
[
|
|
|
|
"c:\\foo\\\\\\",
|
2020-03-28 13:03:49 -04:00
|
|
|
{ root: "c:\\", dir: "c:\\", base: "foo", ext: "", name: "foo" },
|
2018-12-18 21:29:39 -05:00
|
|
|
],
|
|
|
|
[
|
|
|
|
"D:\\foo\\\\\\bar.baz",
|
|
|
|
{
|
|
|
|
root: "D:\\",
|
|
|
|
dir: "D:\\foo\\\\",
|
|
|
|
base: "bar.baz",
|
|
|
|
ext: ".baz",
|
2020-03-28 13:03:49 -04:00
|
|
|
name: "bar",
|
|
|
|
},
|
|
|
|
],
|
2018-12-18 21:29:39 -05:00
|
|
|
];
|
2019-03-12 01:51:51 -04:00
|
|
|
|
2018-12-18 21:29:39 -05:00
|
|
|
const posixTrailingTests = [
|
|
|
|
["./", { root: "", dir: "", base: ".", ext: "", name: "." }],
|
|
|
|
["//", { root: "/", dir: "/", base: "", ext: "", name: "" }],
|
|
|
|
["///", { root: "/", dir: "/", base: "", ext: "", name: "" }],
|
|
|
|
["/foo///", { root: "/", dir: "/", base: "foo", ext: "", name: "foo" }],
|
|
|
|
[
|
|
|
|
"/foo///bar.baz",
|
2020-03-28 13:03:49 -04:00
|
|
|
{ root: "/", dir: "/foo//", base: "bar.baz", ext: ".baz", name: "bar" },
|
|
|
|
],
|
2018-12-18 21:29:39 -05:00
|
|
|
];
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("parseTrailingWin32", function () {
|
2020-03-28 13:03:49 -04:00
|
|
|
windowsTrailingTests.forEach(function (p) {
|
2019-01-07 10:39:36 -05:00
|
|
|
const actual = path.win32.parse(p[0] as string);
|
2018-12-18 21:29:39 -05:00
|
|
|
const expected = p[1];
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(actual, expected);
|
2018-12-18 21:29:39 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("parseTrailing", function () {
|
2020-03-28 13:03:49 -04:00
|
|
|
posixTrailingTests.forEach(function (p) {
|
2019-01-07 10:39:36 -05:00
|
|
|
const actual = path.posix.parse(p[0] as string);
|
2018-12-18 21:29:39 -05:00
|
|
|
const expected = p[1];
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(actual, expected);
|
2018-12-18 21:29:39 -05:00
|
|
|
});
|
|
|
|
});
|