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-01-31 09:10:15 +00:00
|
|
|
import { assertEquals } from "@std/assert";
|
2024-01-03 21:05:20 +00:00
|
|
|
import { basename } from "./basename.ts";
|
|
|
|
import * as posix from "./posix/mod.ts";
|
|
|
|
import * as windows from "./windows/mod.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/basename.pl
|
|
|
|
const COREUTILS_TESTSUITE = [
|
|
|
|
[["d/f"], "f"],
|
|
|
|
[["/d/f"], "f"],
|
|
|
|
[["d/f/"], "f"],
|
|
|
|
[["d/f//"], "f"],
|
|
|
|
[["f"], "f"],
|
|
|
|
[["/"], "/"],
|
|
|
|
[["///"], "/"],
|
|
|
|
[["///a///"], "a"],
|
|
|
|
[[""], ""],
|
|
|
|
[["aa", "a"], "a"],
|
|
|
|
[["a-a", "-a"], "a"],
|
|
|
|
[["f.s", ".s"], "f"],
|
|
|
|
[["fs", "s"], "f"],
|
|
|
|
[["fs", "fs"], "fs"],
|
|
|
|
[["fs/", "s"], "f"],
|
|
|
|
[["dir/file.suf", ".suf"], "file"],
|
|
|
|
[["fs", "x"], "fs"],
|
|
|
|
[["fs", ""], "fs"],
|
|
|
|
[["fs/", "s/"], "fs"],
|
|
|
|
];
|
|
|
|
|
|
|
|
const POSIX_TESTSUITE = [
|
|
|
|
[[""], ""],
|
|
|
|
[["/dir/basename.ext"], "basename.ext"],
|
|
|
|
[["/basename.ext"], "basename.ext"],
|
|
|
|
[["basename.ext"], "basename.ext"],
|
|
|
|
[["basename.ext/"], "basename.ext"],
|
|
|
|
[["basename.ext//"], "basename.ext"],
|
|
|
|
[["aaa/bbb", "/bbb"], "bbb"],
|
|
|
|
[["aaa/bbb", "a/bbb"], "bbb"],
|
|
|
|
[["aaa/bbb", "bbb"], "bbb"],
|
|
|
|
[["aaa/bbb//", "bbb"], "bbb"],
|
|
|
|
[["aaa/bbb", "bb"], "b"],
|
|
|
|
[["aaa/bbb", "b"], "bb"],
|
|
|
|
[["/aaa/bbb", "/bbb"], "bbb"],
|
|
|
|
[["/aaa/bbb", "a/bbb"], "bbb"],
|
|
|
|
[["/aaa/bbb", "bbb"], "bbb"],
|
|
|
|
[["/aaa/bbb//", "bbb"], "bbb"],
|
|
|
|
[["/aaa/bbb//", "a/bbb"], "bbb"],
|
|
|
|
[["/aaa/bbb", "bb"], "b"],
|
|
|
|
[["/aaa/bbb", "b"], "bb"],
|
|
|
|
[["/aaa/bbb"], "bbb"],
|
|
|
|
[["/aaa/"], "aaa"],
|
|
|
|
[["/aaa/b"], "b"],
|
|
|
|
[["/a/b"], "b"],
|
|
|
|
[["//a"], "a"],
|
|
|
|
[["///"], "/"],
|
|
|
|
[["///", "bbb"], "/"],
|
|
|
|
[["//", "bbb"], "/"],
|
|
|
|
];
|
|
|
|
|
|
|
|
const WIN32_TESTSUITE = [
|
|
|
|
[["\\dir\\basename.ext"], "basename.ext"],
|
|
|
|
[["\\basename.ext"], "basename.ext"],
|
|
|
|
[["basename.ext"], "basename.ext"],
|
|
|
|
[["basename.ext\\"], "basename.ext"],
|
|
|
|
[["basename.ext\\\\"], "basename.ext"],
|
|
|
|
[["foo"], "foo"],
|
|
|
|
[["aaa\\bbb", "\\bbb"], "bbb"],
|
|
|
|
[["aaa\\bbb", "a\\bbb"], "bbb"],
|
|
|
|
[["aaa\\bbb", "bbb"], "bbb"],
|
|
|
|
[["aaa\\bbb\\\\\\\\", "bbb"], "bbb"],
|
|
|
|
[["aaa\\bbb", "bb"], "b"],
|
|
|
|
[["aaa\\bbb", "b"], "bb"],
|
|
|
|
[["/aaa/bbb", "bb"], "b"],
|
|
|
|
[["C:"], ""],
|
|
|
|
[["C:."], "."],
|
|
|
|
[["C:\\"], "\\"],
|
|
|
|
[["C:\\dir\\base.ext"], "base.ext"],
|
|
|
|
[["C:\\basename.ext"], "basename.ext"],
|
|
|
|
[["C:basename.ext"], "basename.ext"],
|
|
|
|
[["C:basename.ext\\"], "basename.ext"],
|
|
|
|
[["C:basename.ext\\\\"], "basename.ext"],
|
|
|
|
[["C:foo"], "foo"],
|
|
|
|
[["file:stream"], "file:stream"],
|
|
|
|
];
|
|
|
|
|
2023-11-01 07:42:29 +00:00
|
|
|
Deno.test("posix.basename()", function () {
|
2023-01-26 06:49:28 +00:00
|
|
|
for (const [[name, suffix], expected] of COREUTILS_TESTSUITE) {
|
2024-01-03 21:05:20 +00:00
|
|
|
assertEquals(basename(name, suffix), expected);
|
2023-01-26 06:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const [[name, suffix], expected] of POSIX_TESTSUITE) {
|
2024-01-03 21:05:20 +00:00
|
|
|
assertEquals(posix.basename(name, suffix), expected);
|
2023-01-26 06:49:28 +00:00
|
|
|
}
|
2018-12-19 02:29:39 +00:00
|
|
|
|
|
|
|
// On unix a backslash is just treated as any other character.
|
2019-03-07 00:42:24 +00:00
|
|
|
assertEquals(
|
2024-01-03 21:05:20 +00:00
|
|
|
posix.basename("\\dir\\basename.ext"),
|
2020-07-14 19:24:17 +00:00
|
|
|
"\\dir\\basename.ext",
|
2019-03-07 00:42:24 +00:00
|
|
|
);
|
2024-01-03 21:05:20 +00:00
|
|
|
assertEquals(posix.basename("\\basename.ext"), "\\basename.ext");
|
|
|
|
assertEquals(posix.basename("basename.ext"), "basename.ext");
|
|
|
|
assertEquals(posix.basename("basename.ext\\"), "basename.ext\\");
|
|
|
|
assertEquals(posix.basename("basename.ext\\\\"), "basename.ext\\\\");
|
|
|
|
assertEquals(posix.basename("foo"), "foo");
|
2018-12-19 02:29:39 +00:00
|
|
|
|
|
|
|
// POSIX filenames may include control characters
|
|
|
|
const controlCharFilename = "Icon" + String.fromCharCode(13);
|
2019-03-07 00:42:24 +00:00
|
|
|
assertEquals(
|
2024-01-03 21:05:20 +00:00
|
|
|
posix.basename("/a/b/" + controlCharFilename),
|
2020-07-14 19:24:17 +00:00
|
|
|
controlCharFilename,
|
2018-12-19 02:29:39 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2024-01-03 21:05:20 +00:00
|
|
|
Deno.test("windows.basename()", function () {
|
2023-01-26 06:49:28 +00:00
|
|
|
for (const [[name, suffix], expected] of WIN32_TESTSUITE) {
|
2024-01-03 21:05:20 +00:00
|
|
|
assertEquals(windows.basename(name, suffix), 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, suffix], expected] of COREUTILS_TESTSUITE) {
|
2024-01-03 21:05:20 +00:00
|
|
|
assertEquals(windows.basename(name, suffix), expected);
|
2023-01-26 06:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const [[name, suffix], expected] of POSIX_TESTSUITE) {
|
2024-01-03 21:05:20 +00:00
|
|
|
assertEquals(windows.basename(name, suffix), expected);
|
2023-01-26 06:49:28 +00:00
|
|
|
}
|
2018-12-19 02:29:39 +00:00
|
|
|
});
|