2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-04-29 02:57:30 +00:00
|
|
|
import { assertEquals } from "@std/assert";
|
2024-01-03 21:05:20 +00:00
|
|
|
import * as posix from "./posix/mod.ts";
|
|
|
|
import * as windows from "./windows/mod.ts";
|
2024-01-10 21:34:53 +00:00
|
|
|
import { join } from "./join.ts";
|
2018-12-19 02:29:39 +00:00
|
|
|
|
|
|
|
const backslashRE = /\\/g;
|
|
|
|
|
2024-08-30 04:18:12 +00:00
|
|
|
type TestCase = [string[] | [URL, ...string[]], string];
|
|
|
|
|
|
|
|
const joinTests: TestCase[] =
|
2018-12-19 02:29:39 +00:00
|
|
|
// 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"],
|
2020-03-28 17:03:49 +00:00
|
|
|
[["", "/", "/foo"], "/foo"],
|
2024-08-30 04:18:12 +00:00
|
|
|
|
|
|
|
// URLs
|
|
|
|
[[new URL("file:///"), "x/b", "..", "/b/c.js"], "/x/b/c.js"],
|
|
|
|
[[new URL("file:///foo"), "../../../bar"], "/bar"],
|
|
|
|
[
|
|
|
|
[new URL("file:///foo"), "bar", "baz/asdf", "quux", ".."],
|
|
|
|
"/foo/bar/baz/asdf",
|
|
|
|
],
|
2018-12-19 02:29:39 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
// Windows-specific join tests
|
2024-08-30 04:18:12 +00:00
|
|
|
const windowsJoinTests: TestCase[] = [
|
2018-12-19 02:29:39 +00:00
|
|
|
// 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:\\"],
|
2020-03-28 17:03:49 +00:00
|
|
|
[["c:", "file"], "c:\\file"],
|
2024-08-30 04:18:12 +00:00
|
|
|
// URLs
|
|
|
|
[[new URL("file:///c:")], "c:\\"],
|
|
|
|
[[new URL("file:///c:"), "file"], "c:\\file"],
|
|
|
|
[[new URL("file:///c:/"), "file"], "c:\\file"],
|
2018-12-19 02:29:39 +00:00
|
|
|
];
|
|
|
|
|
2023-11-01 07:42:29 +00:00
|
|
|
Deno.test("posix.join()", function () {
|
2020-03-28 17:03:49 +00:00
|
|
|
joinTests.forEach(function (p) {
|
2024-08-30 04:18:12 +00:00
|
|
|
const _p = p[0];
|
2024-01-03 21:05:20 +00:00
|
|
|
const actual = posix.join.apply(null, _p);
|
2019-03-07 00:42:24 +00:00
|
|
|
assertEquals(actual, p[1]);
|
2018-12-19 02:29:39 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-01-03 21:05:20 +00:00
|
|
|
Deno.test("windows.join()", function () {
|
2020-03-28 17:03:49 +00:00
|
|
|
joinTests.forEach(function (p) {
|
2024-08-30 04:18:12 +00:00
|
|
|
const _p = p[0];
|
2024-01-03 21:05:20 +00:00
|
|
|
const actual = windows.join.apply(null, _p).replace(backslashRE, "/");
|
2019-03-07 00:42:24 +00:00
|
|
|
assertEquals(actual, p[1]);
|
2018-12-19 02:29:39 +00:00
|
|
|
});
|
2020-03-28 17:03:49 +00:00
|
|
|
windowsJoinTests.forEach(function (p) {
|
2024-08-30 04:18:12 +00:00
|
|
|
const _p = p[0];
|
2024-01-03 21:05:20 +00:00
|
|
|
const actual = windows.join.apply(null, _p);
|
2019-03-07 00:42:24 +00:00
|
|
|
assertEquals(actual, p[1]);
|
2018-12-19 02:29:39 +00:00
|
|
|
});
|
|
|
|
});
|
2024-01-10 21:34:53 +00:00
|
|
|
|
|
|
|
Deno.test(`join() returns "." if input is empty`, function () {
|
|
|
|
assertEquals(join(""), ".");
|
|
|
|
assertEquals(join("", ""), ".");
|
|
|
|
|
|
|
|
const pwd = Deno.cwd();
|
|
|
|
assertEquals(join(pwd), pwd);
|
|
|
|
assertEquals(join(pwd, ""), pwd);
|
|
|
|
});
|