2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2018-12-19 02:29:39 +00:00
|
|
|
// Copyright the Browserify authors. MIT License.
|
|
|
|
// Ported from https://github.com/browserify/path-browserify/
|
2024-08-21 05:28:58 +00:00
|
|
|
import { assertEquals, assertThrows } from "@std/assert";
|
2024-01-03 21:05:20 +00:00
|
|
|
import { dirname } from "./dirname.ts";
|
|
|
|
import * as posix from "./posix/mod.ts";
|
|
|
|
import * as windows from "./windows/mod.ts";
|
2024-09-12 05:33:12 +00:00
|
|
|
import { dirname as windowsUnstableDirname } from "./windows/unstable_dirname.ts";
|
|
|
|
import { dirname as posixUnstableDirname } from "./posix/unstable_dirname.ts";
|
2018-12-19 02:29:39 +00:00
|
|
|
|
2023-01-26 06:49:28 +00:00
|
|
|
// Test suite from "GNU core utilities"
|
|
|
|
// https://github.com/coreutils/coreutils/blob/master/tests/misc/dirname.pl
|
|
|
|
const COREUTILS_TESTSUITE = [
|
|
|
|
["d/f", "d"],
|
|
|
|
["/d/f", "/d"],
|
|
|
|
["d/f/", "d"],
|
|
|
|
["d/f//", "d"],
|
|
|
|
["f", "."],
|
|
|
|
["/", "/"],
|
|
|
|
["//", "/"],
|
|
|
|
["///", "/"],
|
|
|
|
["//a//", "/"],
|
|
|
|
["///a///", "/"],
|
|
|
|
["///a///b", "///a"],
|
|
|
|
["///a//b/", "///a"],
|
|
|
|
["", "."],
|
2024-03-14 03:42:03 +00:00
|
|
|
] as const;
|
2023-01-26 06:49:28 +00:00
|
|
|
|
|
|
|
const POSIX_TESTSUITE = [
|
|
|
|
["/a/b/", "/a"],
|
|
|
|
["/a/b", "/a"],
|
|
|
|
["/a", "/"],
|
|
|
|
["", "."],
|
|
|
|
["/", "/"],
|
|
|
|
["////", "/"],
|
|
|
|
["//a", "/"],
|
|
|
|
["foo", "."],
|
2024-03-14 03:42:03 +00:00
|
|
|
] as const;
|
2023-01-26 06:49:28 +00:00
|
|
|
|
2024-01-03 21:05:20 +00:00
|
|
|
const WINDOWS_TESTSUITE = [
|
2023-01-26 06:49:28 +00:00
|
|
|
["c:\\", "c:\\"],
|
|
|
|
["c:\\foo", "c:\\"],
|
|
|
|
["c:\\foo\\", "c:\\"],
|
|
|
|
["c:\\foo\\bar", "c:\\foo"],
|
|
|
|
["c:\\foo\\bar\\", "c:\\foo"],
|
|
|
|
["c:\\foo\\bar\\baz", "c:\\foo\\bar"],
|
|
|
|
["\\", "\\"],
|
|
|
|
["\\foo", "\\"],
|
|
|
|
["\\foo\\", "\\"],
|
|
|
|
["\\foo\\bar", "\\foo"],
|
|
|
|
["\\foo\\bar\\", "\\foo"],
|
|
|
|
["\\foo\\bar\\baz", "\\foo\\bar"],
|
|
|
|
["c:", "c:"],
|
|
|
|
["c:foo", "c:"],
|
|
|
|
["c:foo\\", "c:"],
|
|
|
|
["c:foo\\bar", "c:foo"],
|
|
|
|
["c:foo\\bar\\", "c:foo"],
|
|
|
|
["c:foo\\bar\\baz", "c:foo\\bar"],
|
|
|
|
["file:stream", "."],
|
|
|
|
["dir\\file:stream", "dir"],
|
|
|
|
["\\\\unc\\share", "\\\\unc\\share"],
|
|
|
|
["\\\\unc\\share\\foo", "\\\\unc\\share\\"],
|
|
|
|
["\\\\unc\\share\\foo\\", "\\\\unc\\share\\"],
|
|
|
|
["\\\\unc\\share\\foo\\bar", "\\\\unc\\share\\foo"],
|
|
|
|
["\\\\unc\\share\\foo\\bar\\", "\\\\unc\\share\\foo"],
|
|
|
|
["\\\\unc\\share\\foo\\bar\\baz", "\\\\unc\\share\\foo\\bar"],
|
|
|
|
["/a/b/", "/a"],
|
|
|
|
["/a/b", "/a"],
|
|
|
|
["/a", "/"],
|
|
|
|
["", "."],
|
|
|
|
["/", "/"],
|
|
|
|
["////", "/"],
|
|
|
|
["foo", "."],
|
2024-03-14 03:42:03 +00:00
|
|
|
] as const;
|
2023-01-26 06:49:28 +00:00
|
|
|
|
2023-11-01 07:42:29 +00:00
|
|
|
Deno.test("posix.dirname()", function () {
|
2023-01-26 06:49:28 +00:00
|
|
|
for (const [name, expected] of COREUTILS_TESTSUITE) {
|
2024-01-03 21:05:20 +00:00
|
|
|
assertEquals(dirname(name), expected);
|
2023-01-26 06:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const [name, expected] of POSIX_TESTSUITE) {
|
2024-01-03 21:05:20 +00:00
|
|
|
assertEquals(posix.dirname(name), expected);
|
2023-01-26 06:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// POSIX treats backslash as any other character.
|
2024-01-03 21:05:20 +00:00
|
|
|
assertEquals(posix.dirname("\\foo/bar"), "\\foo");
|
|
|
|
assertEquals(posix.dirname("\\/foo/bar"), "\\/foo");
|
|
|
|
assertEquals(posix.dirname("/foo/bar\\baz/qux"), "/foo/bar\\baz");
|
|
|
|
assertEquals(posix.dirname("/foo/bar/baz\\"), "/foo/bar");
|
2018-12-19 02:29:39 +00:00
|
|
|
});
|
|
|
|
|
2024-08-21 05:28:58 +00:00
|
|
|
Deno.test("posix.dirname() works with file URLs", () => {
|
|
|
|
assertEquals(
|
2024-09-12 05:33:12 +00:00
|
|
|
posixUnstableDirname(new URL("file:///home/user/Documents/image.png")),
|
2024-08-21 05:28:58 +00:00
|
|
|
"/home/user/Documents",
|
|
|
|
);
|
|
|
|
|
|
|
|
// throws with non-file URLs
|
|
|
|
assertThrows(
|
2024-09-12 05:33:12 +00:00
|
|
|
() => posixUnstableDirname(new URL("https://deno.land/")),
|
2024-08-21 05:28:58 +00:00
|
|
|
TypeError,
|
2024-08-23 03:31:01 +00:00
|
|
|
'URL must be a file URL: received "https:"',
|
2024-08-21 05:28:58 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2024-01-03 21:05:20 +00:00
|
|
|
Deno.test("windows.dirname()", function () {
|
|
|
|
for (const [name, expected] of WINDOWS_TESTSUITE) {
|
|
|
|
assertEquals(windows.dirname(name), expected);
|
2023-01-26 06:49:28 +00:00
|
|
|
}
|
|
|
|
|
2024-01-03 21:05:20 +00:00
|
|
|
// windows should pass all "forward slash" posix tests as well.
|
2023-01-26 06:49:28 +00:00
|
|
|
for (const [name, expected] of COREUTILS_TESTSUITE) {
|
2024-01-03 21:05:20 +00:00
|
|
|
assertEquals(windows.dirname(name), expected);
|
2023-01-26 06:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const [name, expected] of POSIX_TESTSUITE) {
|
2024-01-03 21:05:20 +00:00
|
|
|
assertEquals(windows.dirname(name), expected);
|
2023-01-26 06:49:28 +00:00
|
|
|
}
|
2018-12-19 02:29:39 +00:00
|
|
|
});
|
2024-08-21 05:28:58 +00:00
|
|
|
|
|
|
|
Deno.test("windows.dirname() works with file URLs", () => {
|
|
|
|
assertEquals(
|
2024-09-12 05:33:12 +00:00
|
|
|
windowsUnstableDirname(new URL("file:///C:/home/user/Documents/image.png")),
|
2024-08-21 05:28:58 +00:00
|
|
|
"C:\\home\\user\\Documents",
|
|
|
|
);
|
|
|
|
|
|
|
|
// throws with non-file URLs
|
|
|
|
assertThrows(
|
2024-09-12 05:33:12 +00:00
|
|
|
() => windowsUnstableDirname(new URL("https://deno.land/")),
|
2024-08-21 05:28:58 +00:00
|
|
|
TypeError,
|
2024-08-23 03:31:01 +00:00
|
|
|
'URL must be a file URL: received "https:"',
|
2024-08-21 05:28:58 +00:00
|
|
|
);
|
|
|
|
});
|