migrate deno_path to deno_std (denoland/deno_std#26)

Previously https://github.com/zhmushan/deno_path

Original: 1a35f9daf5
This commit is contained in:
木杉 2018-12-19 10:29:39 +08:00 committed by Ryan Dahl
parent 3c8f564ab8
commit 2d58da520f
16 changed files with 2292 additions and 3 deletions

21
LICENSE Normal file
View file

@ -0,0 +1,21 @@
MIT License
Copyright 2018 the Deno authors.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View file

@ -108,7 +108,7 @@ test(async function bufioBufReader() {
for (let i = 0; i < texts.length - 1; i++) {
texts[i] = str + "\n";
all += texts[i];
str += String.fromCharCode(i % 26 + 97);
str += String.fromCharCode((i % 26) + 97);
}
texts[texts.length - 1] = all;
@ -293,7 +293,7 @@ test(async function bufioWriter() {
const data = new Uint8Array(8192);
for (let i = 0; i < data.byteLength; i++) {
data[i] = charCode(" ") + i % (charCode("~") - charCode(" "));
data[i] = charCode(" ") + (i % (charCode("~") - charCode(" ")));
}
const w = new Buffer();

7
path/README.md Normal file
View file

@ -0,0 +1,7 @@
# Deno Path Manipulation Libraries
Usage:
```ts
import * as path from 'https://deno.land/x/path/index.ts'
```

75
path/basename_test.ts Normal file
View file

@ -0,0 +1,75 @@
// Copyright the Browserify authors. MIT License.
// Ported from https://github.com/browserify/path-browserify/
import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
import * as path from "./index";
test(function basename() {
assertEqual(path.basename(".js", ".js"), "");
assertEqual(path.basename(""), "");
assertEqual(path.basename("/dir/basename.ext"), "basename.ext");
assertEqual(path.basename("/basename.ext"), "basename.ext");
assertEqual(path.basename("basename.ext"), "basename.ext");
assertEqual(path.basename("basename.ext/"), "basename.ext");
assertEqual(path.basename("basename.ext//"), "basename.ext");
assertEqual(path.basename("aaa/bbb", "/bbb"), "bbb");
assertEqual(path.basename("aaa/bbb", "a/bbb"), "bbb");
assertEqual(path.basename("aaa/bbb", "bbb"), "bbb");
assertEqual(path.basename("aaa/bbb//", "bbb"), "bbb");
assertEqual(path.basename("aaa/bbb", "bb"), "b");
assertEqual(path.basename("aaa/bbb", "b"), "bb");
assertEqual(path.basename("/aaa/bbb", "/bbb"), "bbb");
assertEqual(path.basename("/aaa/bbb", "a/bbb"), "bbb");
assertEqual(path.basename("/aaa/bbb", "bbb"), "bbb");
assertEqual(path.basename("/aaa/bbb//", "bbb"), "bbb");
assertEqual(path.basename("/aaa/bbb", "bb"), "b");
assertEqual(path.basename("/aaa/bbb", "b"), "bb");
assertEqual(path.basename("/aaa/bbb"), "bbb");
assertEqual(path.basename("/aaa/"), "aaa");
assertEqual(path.basename("/aaa/b"), "b");
assertEqual(path.basename("/a/b"), "b");
assertEqual(path.basename("//a"), "a");
// On unix a backslash is just treated as any other character.
assertEqual(
path.posix.basename("\\dir\\basename.ext"),
"\\dir\\basename.ext"
);
assertEqual(path.posix.basename("\\basename.ext"), "\\basename.ext");
assertEqual(path.posix.basename("basename.ext"), "basename.ext");
assertEqual(path.posix.basename("basename.ext\\"), "basename.ext\\");
assertEqual(path.posix.basename("basename.ext\\\\"), "basename.ext\\\\");
assertEqual(path.posix.basename("foo"), "foo");
// POSIX filenames may include control characters
const controlCharFilename = "Icon" + String.fromCharCode(13);
assertEqual(
path.posix.basename("/a/b/" + controlCharFilename),
controlCharFilename
);
});
test(function basenameWin32() {
assertEqual(path.win32.basename("\\dir\\basename.ext"), "basename.ext");
assertEqual(path.win32.basename("\\basename.ext"), "basename.ext");
assertEqual(path.win32.basename("basename.ext"), "basename.ext");
assertEqual(path.win32.basename("basename.ext\\"), "basename.ext");
assertEqual(path.win32.basename("basename.ext\\\\"), "basename.ext");
assertEqual(path.win32.basename("foo"), "foo");
assertEqual(path.win32.basename("aaa\\bbb", "\\bbb"), "bbb");
assertEqual(path.win32.basename("aaa\\bbb", "a\\bbb"), "bbb");
assertEqual(path.win32.basename("aaa\\bbb", "bbb"), "bbb");
assertEqual(path.win32.basename("aaa\\bbb\\\\\\\\", "bbb"), "bbb");
assertEqual(path.win32.basename("aaa\\bbb", "bb"), "b");
assertEqual(path.win32.basename("aaa\\bbb", "b"), "bb");
assertEqual(path.win32.basename("C:"), "");
assertEqual(path.win32.basename("C:."), ".");
assertEqual(path.win32.basename("C:\\"), "");
assertEqual(path.win32.basename("C:\\dir\\base.ext"), "base.ext");
assertEqual(path.win32.basename("C:\\basename.ext"), "basename.ext");
assertEqual(path.win32.basename("C:basename.ext"), "basename.ext");
assertEqual(path.win32.basename("C:basename.ext\\"), "basename.ext");
assertEqual(path.win32.basename("C:basename.ext\\\\"), "basename.ext");
assertEqual(path.win32.basename("C:foo"), "foo");
assertEqual(path.win32.basename("file:stream"), "file:stream");
});

53
path/constants.ts Normal file
View file

@ -0,0 +1,53 @@
// Copyright the Browserify authors. MIT License.
// Ported from https://github.com/browserify/path-browserify/
import { platform } from "deno";
const isWindows = platform.os === "win";
// Alphabet chars.
export const CHAR_UPPERCASE_A = 65; /* A */
export const CHAR_LOWERCASE_A = 97; /* a */
export const CHAR_UPPERCASE_Z = 90; /* Z */
export const CHAR_LOWERCASE_Z = 122; /* z */
// Non-alphabetic chars.
export const CHAR_DOT = 46; /* . */
export const CHAR_FORWARD_SLASH = 47; /* / */
export const CHAR_BACKWARD_SLASH = 92; /* \ */
export const CHAR_VERTICAL_LINE = 124; /* | */
export const CHAR_COLON = 58; /* : */
export const CHAR_QUESTION_MARK = 63; /* ? */
export const CHAR_UNDERSCORE = 95; /* _ */
export const CHAR_LINE_FEED = 10; /* \n */
export const CHAR_CARRIAGE_RETURN = 13; /* \r */
export const CHAR_TAB = 9; /* \t */
export const CHAR_FORM_FEED = 12; /* \f */
export const CHAR_EXCLAMATION_MARK = 33; /* ! */
export const CHAR_HASH = 35; /* # */
export const CHAR_SPACE = 32; /* */
export const CHAR_NO_BREAK_SPACE = 160; /* \u00A0 */
export const CHAR_ZERO_WIDTH_NOBREAK_SPACE = 65279; /* \uFEFF */
export const CHAR_LEFT_SQUARE_BRACKET = 91; /* [ */
export const CHAR_RIGHT_SQUARE_BRACKET = 93; /* ] */
export const CHAR_LEFT_ANGLE_BRACKET = 60; /* < */
export const CHAR_RIGHT_ANGLE_BRACKET = 62; /* > */
export const CHAR_LEFT_CURLY_BRACKET = 123; /* { */
export const CHAR_RIGHT_CURLY_BRACKET = 125; /* } */
export const CHAR_HYPHEN_MINUS = 45; /* - */
export const CHAR_PLUS = 43; /* + */
export const CHAR_DOUBLE_QUOTE = 34; /* " */
export const CHAR_SINGLE_QUOTE = 39; /* ' */
export const CHAR_PERCENT = 37; /* % */
export const CHAR_SEMICOLON = 59; /* ; */
export const CHAR_CIRCUMFLEX_ACCENT = 94; /* ^ */
export const CHAR_GRAVE_ACCENT = 96; /* ` */
export const CHAR_AT = 64; /* @ */
export const CHAR_AMPERSAND = 38; /* & */
export const CHAR_EQUAL = 61; /* = */
// Digits
export const CHAR_0 = 48; /* 0 */
export const CHAR_9 = 57; /* 9 */
export const EOL = isWindows ? "\r\n" : "\n";

61
path/dirname_test.ts Normal file
View file

@ -0,0 +1,61 @@
// Copyright the Browserify authors. MIT License.
// Ported from https://github.com/browserify/path-browserify/
import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
import * as path from "./index";
test(function dirname() {
assertEqual(path.posix.dirname("/a/b/"), "/a");
assertEqual(path.posix.dirname("/a/b"), "/a");
assertEqual(path.posix.dirname("/a"), "/");
assertEqual(path.posix.dirname(""), ".");
assertEqual(path.posix.dirname("/"), "/");
assertEqual(path.posix.dirname("////"), "/");
assertEqual(path.posix.dirname("//a"), "//");
assertEqual(path.posix.dirname("foo"), ".");
});
test(function dirnameWin32() {
assertEqual(path.win32.dirname("c:\\"), "c:\\");
assertEqual(path.win32.dirname("c:\\foo"), "c:\\");
assertEqual(path.win32.dirname("c:\\foo\\"), "c:\\");
assertEqual(path.win32.dirname("c:\\foo\\bar"), "c:\\foo");
assertEqual(path.win32.dirname("c:\\foo\\bar\\"), "c:\\foo");
assertEqual(path.win32.dirname("c:\\foo\\bar\\baz"), "c:\\foo\\bar");
assertEqual(path.win32.dirname("\\"), "\\");
assertEqual(path.win32.dirname("\\foo"), "\\");
assertEqual(path.win32.dirname("\\foo\\"), "\\");
assertEqual(path.win32.dirname("\\foo\\bar"), "\\foo");
assertEqual(path.win32.dirname("\\foo\\bar\\"), "\\foo");
assertEqual(path.win32.dirname("\\foo\\bar\\baz"), "\\foo\\bar");
assertEqual(path.win32.dirname("c:"), "c:");
assertEqual(path.win32.dirname("c:foo"), "c:");
assertEqual(path.win32.dirname("c:foo\\"), "c:");
assertEqual(path.win32.dirname("c:foo\\bar"), "c:foo");
assertEqual(path.win32.dirname("c:foo\\bar\\"), "c:foo");
assertEqual(path.win32.dirname("c:foo\\bar\\baz"), "c:foo\\bar");
assertEqual(path.win32.dirname("file:stream"), ".");
assertEqual(path.win32.dirname("dir\\file:stream"), "dir");
assertEqual(path.win32.dirname("\\\\unc\\share"), "\\\\unc\\share");
assertEqual(path.win32.dirname("\\\\unc\\share\\foo"), "\\\\unc\\share\\");
assertEqual(path.win32.dirname("\\\\unc\\share\\foo\\"), "\\\\unc\\share\\");
assertEqual(
path.win32.dirname("\\\\unc\\share\\foo\\bar"),
"\\\\unc\\share\\foo"
);
assertEqual(
path.win32.dirname("\\\\unc\\share\\foo\\bar\\"),
"\\\\unc\\share\\foo"
);
assertEqual(
path.win32.dirname("\\\\unc\\share\\foo\\bar\\baz"),
"\\\\unc\\share\\foo\\bar"
);
assertEqual(path.win32.dirname("/a/b/"), "/a");
assertEqual(path.win32.dirname("/a/b"), "/a");
assertEqual(path.win32.dirname("/a"), "/");
assertEqual(path.win32.dirname(""), ".");
assertEqual(path.win32.dirname("/"), "/");
assertEqual(path.win32.dirname("////"), "/");
assertEqual(path.win32.dirname("foo"), ".");
});

89
path/extname_test.ts Normal file
View file

@ -0,0 +1,89 @@
// Copyright the Browserify authors. MIT License.
// Ported from https://github.com/browserify/path-browserify/
import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
import * as path from "./index";
const slashRE = /\//g;
const pairs = [
["", ""],
["/path/to/file", ""],
["/path/to/file.ext", ".ext"],
["/path.to/file.ext", ".ext"],
["/path.to/file", ""],
["/path.to/.file", ""],
["/path.to/.file.ext", ".ext"],
["/path/to/f.ext", ".ext"],
["/path/to/..ext", ".ext"],
["/path/to/..", ""],
["file", ""],
["file.ext", ".ext"],
[".file", ""],
[".file.ext", ".ext"],
["/file", ""],
["/file.ext", ".ext"],
["/.file", ""],
["/.file.ext", ".ext"],
[".path/file.ext", ".ext"],
["file.ext.ext", ".ext"],
["file.", "."],
[".", ""],
["./", ""],
[".file.ext", ".ext"],
[".file", ""],
[".file.", "."],
[".file..", "."],
["..", ""],
["../", ""],
["..file.ext", ".ext"],
["..file", ".file"],
["..file.", "."],
["..file..", "."],
["...", "."],
["...ext", ".ext"],
["....", "."],
["file.ext/", ".ext"],
["file.ext//", ".ext"],
["file/", ""],
["file//", ""],
["file./", "."],
["file.//", "."]
];
test(function extname() {
pairs.forEach(function(p) {
const input = p[0];
const expected = p[1];
assertEqual(expected, path.posix.extname(input));
});
// On *nix, backslash is a valid name component like any other character.
assertEqual(path.posix.extname(".\\"), "");
assertEqual(path.posix.extname("..\\"), ".\\");
assertEqual(path.posix.extname("file.ext\\"), ".ext\\");
assertEqual(path.posix.extname("file.ext\\\\"), ".ext\\\\");
assertEqual(path.posix.extname("file\\"), "");
assertEqual(path.posix.extname("file\\\\"), "");
assertEqual(path.posix.extname("file.\\"), ".\\");
assertEqual(path.posix.extname("file.\\\\"), ".\\\\");
});
test(function extnameWin32() {
pairs.forEach(function(p) {
const input = p[0].replace(slashRE, "\\");
const expected = p[1];
assertEqual(expected, path.win32.extname(input));
assertEqual(expected, path.win32.extname("C:" + input));
});
// On Windows, backslash is a path separator.
assertEqual(path.win32.extname(".\\"), "");
assertEqual(path.win32.extname("..\\"), "");
assertEqual(path.win32.extname("file.ext\\"), ".ext");
assertEqual(path.win32.extname("file.ext\\\\"), ".ext");
assertEqual(path.win32.extname("file\\"), "");
assertEqual(path.win32.extname("file\\\\"), "");
assertEqual(path.win32.extname("file.\\"), ".");
assertEqual(path.win32.extname("file.\\\\"), ".");
});

1425
path/index.ts Normal file

File diff suppressed because it is too large Load diff

47
path/interface.ts Normal file
View file

@ -0,0 +1,47 @@
/**
* A parsed path object generated by path.parse() or consumed by path.format().
*/
export interface ParsedPath {
/**
* The root of the path such as '/' or 'c:\'
*/
root: string;
/**
* The full directory path such as '/home/user/dir' or 'c:\path\dir'
*/
dir: string;
/**
* The file name including extension (if any) such as 'index.html'
*/
base: string;
/**
* The file extension (if any) such as '.html'
*/
ext: string;
/**
* The file name without extension (if any) such as 'index'
*/
name: string;
}
export interface FormatInputPathObject {
/**
* The root of the path such as '/' or 'c:\'
*/
root?: string;
/**
* The full directory path such as '/home/user/dir' or 'c:\path\dir'
*/
dir?: string;
/**
* The file name including extension (if any) such as 'index.html'
*/
base?: string;
/**
* The file extension (if any) such as '.html'
*/
ext?: string;
/**
* The file name without extension (if any) such as 'index'
*/
name?: string;
}

33
path/isabsolute_test.ts Normal file
View file

@ -0,0 +1,33 @@
// Copyright the Browserify authors. MIT License.
// Ported from https://github.com/browserify/path-browserify/
import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
import * as path from "./index";
test(function isAbsolute() {
assertEqual(path.posix.isAbsolute("/home/foo"), true);
assertEqual(path.posix.isAbsolute("/home/foo/.."), true);
assertEqual(path.posix.isAbsolute("bar/"), false);
assertEqual(path.posix.isAbsolute("./baz"), false);
});
test(function isAbsoluteWin32() {
assertEqual(path.win32.isAbsolute("/"), true);
assertEqual(path.win32.isAbsolute("//"), true);
assertEqual(path.win32.isAbsolute("//server"), true);
assertEqual(path.win32.isAbsolute("//server/file"), true);
assertEqual(path.win32.isAbsolute("\\\\server\\file"), true);
assertEqual(path.win32.isAbsolute("\\\\server"), true);
assertEqual(path.win32.isAbsolute("\\\\"), true);
assertEqual(path.win32.isAbsolute("c"), false);
assertEqual(path.win32.isAbsolute("c:"), false);
assertEqual(path.win32.isAbsolute("c:\\"), true);
assertEqual(path.win32.isAbsolute("c:/"), true);
assertEqual(path.win32.isAbsolute("c://"), true);
assertEqual(path.win32.isAbsolute("C:/Users/"), true);
assertEqual(path.win32.isAbsolute("C:\\Users\\"), true);
assertEqual(path.win32.isAbsolute("C:cwd/another"), false);
assertEqual(path.win32.isAbsolute("C:cwd\\another"), false);
assertEqual(path.win32.isAbsolute("directory/directory"), false);
assertEqual(path.win32.isAbsolute("directory\\directory"), false);
});

124
path/join_test.ts Normal file
View file

@ -0,0 +1,124 @@
import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
import * as path from "./index";
const backslashRE = /\\/g;
const joinTests =
// arguments result
[
[[".", "x/b", "..", "/b/c.js"], "x/b/c.js"],
[[], "."],
[["/.", "x/b", "..", "/b/c.js"], "/x/b/c.js"],
[["/foo", "../../../bar"], "/bar"],
[["foo", "../../../bar"], "../../bar"],
[["foo/", "../../../bar"], "../../bar"],
[["foo/x", "../../../bar"], "../bar"],
[["foo/x", "./bar"], "foo/x/bar"],
[["foo/x/", "./bar"], "foo/x/bar"],
[["foo/x/", ".", "bar"], "foo/x/bar"],
[["./"], "./"],
[[".", "./"], "./"],
[[".", ".", "."], "."],
[[".", "./", "."], "."],
[[".", "/./", "."], "."],
[[".", "/////./", "."], "."],
[["."], "."],
[["", "."], "."],
[["", "foo"], "foo"],
[["foo", "/bar"], "foo/bar"],
[["", "/foo"], "/foo"],
[["", "", "/foo"], "/foo"],
[["", "", "foo"], "foo"],
[["foo", ""], "foo"],
[["foo/", ""], "foo/"],
[["foo", "", "/bar"], "foo/bar"],
[["./", "..", "/foo"], "../foo"],
[["./", "..", "..", "/foo"], "../../foo"],
[[".", "..", "..", "/foo"], "../../foo"],
[["", "..", "..", "/foo"], "../../foo"],
[["/"], "/"],
[["/", "."], "/"],
[["/", ".."], "/"],
[["/", "..", ".."], "/"],
[[""], "."],
[["", ""], "."],
[[" /foo"], " /foo"],
[[" ", "foo"], " /foo"],
[[" ", "."], " "],
[[" ", "/"], " /"],
[[" ", ""], " "],
[["/", "foo"], "/foo"],
[["/", "/foo"], "/foo"],
[["/", "//foo"], "/foo"],
[["/", "", "/foo"], "/foo"],
[["", "/", "foo"], "/foo"],
[["", "/", "/foo"], "/foo"]
];
// Windows-specific join tests
const windowsJoinTests = [
// arguments result
// UNC path expected
[["//foo/bar"], "\\\\foo\\bar\\"],
[["\\/foo/bar"], "\\\\foo\\bar\\"],
[["\\\\foo/bar"], "\\\\foo\\bar\\"],
// UNC path expected - server and share separate
[["//foo", "bar"], "\\\\foo\\bar\\"],
[["//foo/", "bar"], "\\\\foo\\bar\\"],
[["//foo", "/bar"], "\\\\foo\\bar\\"],
// UNC path expected - questionable
[["//foo", "", "bar"], "\\\\foo\\bar\\"],
[["//foo/", "", "bar"], "\\\\foo\\bar\\"],
[["//foo/", "", "/bar"], "\\\\foo\\bar\\"],
// UNC path expected - even more questionable
[["", "//foo", "bar"], "\\\\foo\\bar\\"],
[["", "//foo/", "bar"], "\\\\foo\\bar\\"],
[["", "//foo/", "/bar"], "\\\\foo\\bar\\"],
// No UNC path expected (no double slash in first component)
[["\\", "foo/bar"], "\\foo\\bar"],
[["\\", "/foo/bar"], "\\foo\\bar"],
[["", "/", "/foo/bar"], "\\foo\\bar"],
// No UNC path expected (no non-slashes in first component -
// questionable)
[["//", "foo/bar"], "\\foo\\bar"],
[["//", "/foo/bar"], "\\foo\\bar"],
[["\\\\", "/", "/foo/bar"], "\\foo\\bar"],
[["//"], "\\"],
// No UNC path expected (share name missing - questionable).
[["//foo"], "\\foo"],
[["//foo/"], "\\foo\\"],
[["//foo", "/"], "\\foo\\"],
[["//foo", "", "/"], "\\foo\\"],
// No UNC path expected (too many leading slashes - questionable)
[["///foo/bar"], "\\foo\\bar"],
[["////foo", "bar"], "\\foo\\bar"],
[["\\\\\\/foo/bar"], "\\foo\\bar"],
// Drive-relative vs drive-absolute paths. This merely describes the
// status quo, rather than being obviously right
[["c:"], "c:."],
[["c:."], "c:."],
[["c:", ""], "c:."],
[["", "c:"], "c:."],
[["c:.", "/"], "c:.\\"],
[["c:.", "file"], "c:file"],
[["c:", "/"], "c:\\"],
[["c:", "file"], "c:\\file"]
];
test(function join() {
joinTests.forEach(function(p) {
const actual = path.posix.join.apply(null, p[0]);
assertEqual(actual, p[1]);
});
});
test(function joinWin32() {
joinTests.forEach(function(p) {
const actual = path.win32.join.apply(null, p[0]).replace(backslashRE, "/");
assertEqual(actual, p[1]);
});
windowsJoinTests.forEach(function(p) {
const actual = path.win32.join.apply(null, p[0]);
assertEqual(actual, p[1]);
});
});

176
path/parse_format_test.ts Normal file
View file

@ -0,0 +1,176 @@
// Copyright the Browserify authors. MIT License.
// Ported from https://github.com/browserify/path-browserify/
import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
import * as path from "./index";
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",
"\\\\server two\\shared folder\\"
],
["\\\\teela\\admin$\\system32", "\\\\teela\\admin$\\"],
["\\\\?\\UNC\\server\\share", "\\\\?\\UNC\\"]
];
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"],
[{}, ""]
];
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", "/"],
["/foo/bar.baz", "/"]
];
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"],
[{}, ""]
];
test(function parseWin32() {
checkParseFormat(path.win32, winPaths);
checkSpecialCaseParseFormat(path.win32, winSpecialCaseParseTests);
});
test(function parse() {
checkParseFormat(path.posix, unixPaths);
});
test(function formatWin32() {
checkFormat(path.win32, winSpecialCaseFormatTests);
});
test(function format() {
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\\\\\\",
{ root: "c:\\", dir: "c:\\", base: "foo", ext: "", name: "foo" }
],
[
"D:\\foo\\\\\\bar.baz",
{
root: "D:\\",
dir: "D:\\foo\\\\",
base: "bar.baz",
ext: ".baz",
name: "bar"
}
]
];
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",
{ root: "/", dir: "/foo//", base: "bar.baz", ext: ".baz", name: "bar" }
]
];
function checkParseFormat(path, paths) {
paths.forEach(function(p) {
const element = p[0];
const output = path.parse(element);
assertEqual(typeof output.root, "string");
assertEqual(typeof output.dir, "string");
assertEqual(typeof output.base, "string");
assertEqual(typeof output.ext, "string");
assertEqual(typeof output.name, "string");
assertEqual(path.format(output), element);
assertEqual(output.rooroot, undefined);
assertEqual(output.dir, output.dir ? path.dirname(element) : "");
assertEqual(output.base, path.basename(element));
});
}
function checkSpecialCaseParseFormat(path, testCases) {
testCases.forEach(function(testCase) {
const element = testCase[0];
const expect = testCase[1];
const output = path.parse(element);
Object.keys(expect).forEach(function(key) {
assertEqual(output[key], expect[key]);
});
});
}
function checkFormat(path, testCases) {
testCases.forEach(function(testCase) {
assertEqual(path.format(testCase[0]), testCase[1]);
});
}
test(function parseTrailingWin32() {
windowsTrailingTests.forEach(function(p) {
const actual = path.win32.parse(p[0]);
const expected = p[1];
assertEqual(actual, expected);
});
});
test(function parseTrailing() {
posixTrailingTests.forEach(function(p) {
const actual = path.posix.parse(p[0]);
const expected = p[1];
assertEqual(actual, expected);
});
});

72
path/relative_test.ts Normal file
View file

@ -0,0 +1,72 @@
// Copyright the Browserify authors. MIT License.
// Ported from https://github.com/browserify/path-browserify/
import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
import * as path from "./index";
const relativeTests = {
win32:
// arguments result
[
["c:/blah\\blah", "d:/games", "d:\\games"],
["c:/aaaa/bbbb", "c:/aaaa", ".."],
["c:/aaaa/bbbb", "c:/cccc", "..\\..\\cccc"],
["c:/aaaa/bbbb", "c:/aaaa/bbbb", ""],
["c:/aaaa/bbbb", "c:/aaaa/cccc", "..\\cccc"],
["c:/aaaa/", "c:/aaaa/cccc", "cccc"],
["c:/", "c:\\aaaa\\bbbb", "aaaa\\bbbb"],
["c:/aaaa/bbbb", "d:\\", "d:\\"],
["c:/AaAa/bbbb", "c:/aaaa/bbbb", ""],
["c:/aaaaa/", "c:/aaaa/cccc", "..\\aaaa\\cccc"],
["C:\\foo\\bar\\baz\\quux", "C:\\", "..\\..\\..\\.."],
[
"C:\\foo\\test",
"C:\\foo\\test\\bar\\package.json",
"bar\\package.json"
],
["C:\\foo\\bar\\baz-quux", "C:\\foo\\bar\\baz", "..\\baz"],
["C:\\foo\\bar\\baz", "C:\\foo\\bar\\baz-quux", "..\\baz-quux"],
["\\\\foo\\bar", "\\\\foo\\bar\\baz", "baz"],
["\\\\foo\\bar\\baz", "\\\\foo\\bar", ".."],
["\\\\foo\\bar\\baz-quux", "\\\\foo\\bar\\baz", "..\\baz"],
["\\\\foo\\bar\\baz", "\\\\foo\\bar\\baz-quux", "..\\baz-quux"],
["C:\\baz-quux", "C:\\baz", "..\\baz"],
["C:\\baz", "C:\\baz-quux", "..\\baz-quux"],
["\\\\foo\\baz-quux", "\\\\foo\\baz", "..\\baz"],
["\\\\foo\\baz", "\\\\foo\\baz-quux", "..\\baz-quux"],
["C:\\baz", "\\\\foo\\bar\\baz", "\\\\foo\\bar\\baz"],
["\\\\foo\\bar\\baz", "C:\\baz", "C:\\baz"]
],
posix:
// arguments result
[
["/var/lib", "/var", ".."],
["/var/lib", "/bin", "../../bin"],
["/var/lib", "/var/lib", ""],
["/var/lib", "/var/apache", "../apache"],
["/var/", "/var/lib", "lib"],
["/", "/var/lib", "var/lib"],
["/foo/test", "/foo/test/bar/package.json", "bar/package.json"],
["/Users/a/web/b/test/mails", "/Users/a/web/b", "../.."],
["/foo/bar/baz-quux", "/foo/bar/baz", "../baz"],
["/foo/bar/baz", "/foo/bar/baz-quux", "../baz-quux"],
["/baz-quux", "/baz", "../baz"],
["/baz", "/baz-quux", "../baz-quux"]
]
};
test(function relative() {
relativeTests.posix.forEach(function(p) {
const expected = p[2];
const actual = path.posix.relative(p[0], p[1]);
assertEqual(actual, expected);
});
});
test(function relativeWin32() {
relativeTests.win32.forEach(function(p) {
const expected = p[2];
const actual = path.win32.relative(p[0], p[1]);
assertEqual(actual, expected);
});
});

49
path/resolve_test.ts Normal file
View file

@ -0,0 +1,49 @@
// Copyright the Browserify authors. MIT License.
// Ported from https://github.com/browserify/path-browserify/
import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
import * as path from "./index";
import { cwd } from "deno";
const windowsTests =
// arguments result
[
[["c:/blah\\blah", "d:/games", "c:../a"], "c:\\blah\\a"],
[["c:/ignore", "d:\\a/b\\c/d", "\\e.exe"], "d:\\e.exe"],
[["c:/ignore", "c:/some/file"], "c:\\some\\file"],
[["d:/ignore", "d:some/dir//"], "d:\\ignore\\some\\dir"],
[["//server/share", "..", "relative\\"], "\\\\server\\share\\relative"],
[["c:/", "//"], "c:\\"],
[["c:/", "//dir"], "c:\\dir"],
[["c:/", "//server/share"], "\\\\server\\share\\"],
[["c:/", "//server//share"], "\\\\server\\share\\"],
[["c:/", "///some//dir"], "c:\\some\\dir"],
[
["C:\\foo\\tmp.3\\", "..\\tmp.3\\cycles\\root.js"],
"C:\\foo\\tmp.3\\cycles\\root.js"
]
];
const posixTests =
// arguments result
[
[["/var/lib", "../", "file/"], "/var/file"],
[["/var/lib", "/../", "file/"], "/file"],
[["a/b/c/", "../../.."], cwd()],
[["."], cwd()],
[["/some/dir", ".", "/absolute/"], "/absolute"],
[["/foo/tmp.3/", "../tmp.3/cycles/root.js"], "/foo/tmp.3/cycles/root.js"]
];
test(function resolve() {
posixTests.forEach(function(p) {
const actual = path.posix.resolve.apply(null, p[0]);
assertEqual(actual, p[1]);
});
});
test(function resolveWin32() {
windowsTests.forEach(function(p) {
const actual = path.win32.resolve.apply(null, p[0]);
assertEqual(actual, p[1]);
});
});

View file

@ -0,0 +1,46 @@
// Copyright the Browserify authors. MIT License.
// Ported from https://github.com/browserify/path-browserify/
import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
import * as path from "./index";
import { cwd } from "deno";
const pwd = cwd();
test(function joinZeroLength() {
// join will internally ignore all the zero-length strings and it will return
// '.' if the joined string is a zero-length string.
assertEqual(path.posix.join(""), ".");
assertEqual(path.posix.join("", ""), ".");
if (path.win32) assertEqual(path.win32.join(""), ".");
if (path.win32) assertEqual(path.win32.join("", ""), ".");
assertEqual(path.join(pwd), pwd);
assertEqual(path.join(pwd, ""), pwd);
});
test(function normalizeZeroLength() {
// normalize will return '.' if the input is a zero-length string
assertEqual(path.posix.normalize(""), ".");
if (path.win32) assertEqual(path.win32.normalize(""), ".");
assertEqual(path.normalize(pwd), pwd);
});
test(function isAbsoluteZeroLength() {
// Since '' is not a valid path in any of the common environments, return false
assertEqual(path.posix.isAbsolute(""), false);
if (path.win32) assertEqual(path.win32.isAbsolute(""), false);
});
test(function resolveZeroLength() {
// resolve, internally ignores all the zero-length strings and returns the
// current working directory
assertEqual(path.resolve(""), pwd);
assertEqual(path.resolve("", ""), pwd);
});
test(function relativeZeroLength() {
// relative, internally calls resolve. So, '' is actually the current directory
assertEqual(path.relative("", pwd), "");
assertEqual(path.relative(pwd, ""), "");
assertEqual(path.relative(pwd, pwd), "");
});

13
test.ts
View file

@ -1,5 +1,5 @@
#!/usr/bin/env deno --allow-run --allow-net
import { run } from "deno";
import { run, exit } from "deno";
import "net/bufio_test.ts";
import "net/http_test.ts";
@ -10,6 +10,17 @@ import { runTests, completePromise } from "net/file_server_test.ts";
const fileServer = run({
args: ["deno", "--allow-net", "net/file_server.ts", "."]
});
// path test
import "path/basename_test.ts";
import "path/dirname_test.ts";
import "path/extname_test.ts";
import "path/isabsolute_test.ts";
import "path/join_test.ts";
import "path/parse_format_test.ts";
import "path/relative_test.ts";
import "path/resolve_test.ts";
import "path/zero_length_strings_test.ts";
// I am also too lazy to do this properly LOL
runTests(new Promise(res => setTimeout(res, 5000)));
(async () => {