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-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-09-12 06:19:05 +00:00
|
|
|
import { extname as posixUnstableExtname } from "./posix/unstable_extname.ts";
|
|
|
|
import { extname as windowsUnstableExtname } from "./windows/unstable_extname.ts";
|
2018-12-19 02:29:39 +00:00
|
|
|
|
|
|
|
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./", "."],
|
2020-03-28 17:03:49 +00:00
|
|
|
["file.//", "."],
|
2024-03-14 03:42:03 +00:00
|
|
|
] as const;
|
2018-12-19 02:29:39 +00:00
|
|
|
|
2023-11-01 07:42:29 +00:00
|
|
|
Deno.test("posix.extname()", function () {
|
2020-03-28 17:03:49 +00:00
|
|
|
pairs.forEach(function (p) {
|
2018-12-19 02:29:39 +00:00
|
|
|
const input = p[0];
|
|
|
|
const expected = p[1];
|
2024-01-03 21:05:20 +00:00
|
|
|
assertEquals(expected, posix.extname(input));
|
2018-12-19 02:29:39 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// On *nix, backslash is a valid name component like any other character.
|
2024-01-03 21:05:20 +00:00
|
|
|
assertEquals(posix.extname(".\\"), "");
|
|
|
|
assertEquals(posix.extname("..\\"), ".\\");
|
|
|
|
assertEquals(posix.extname("file.ext\\"), ".ext\\");
|
|
|
|
assertEquals(posix.extname("file.ext\\\\"), ".ext\\\\");
|
|
|
|
assertEquals(posix.extname("file\\"), "");
|
|
|
|
assertEquals(posix.extname("file\\\\"), "");
|
|
|
|
assertEquals(posix.extname("file.\\"), ".\\");
|
|
|
|
assertEquals(posix.extname("file.\\\\"), ".\\\\");
|
2024-08-26 07:33:53 +00:00
|
|
|
|
|
|
|
assertEquals(
|
2024-09-12 06:19:05 +00:00
|
|
|
posixUnstableExtname(new URL("file:///home/user/Documents/image.png")),
|
2024-08-26 07:33:53 +00:00
|
|
|
".png",
|
|
|
|
);
|
|
|
|
assertEquals(
|
2024-09-12 06:19:05 +00:00
|
|
|
posixUnstableExtname(new URL("file:///home/user/Documents")),
|
2024-08-26 07:33:53 +00:00
|
|
|
"",
|
|
|
|
);
|
2018-12-19 02:29:39 +00:00
|
|
|
});
|
|
|
|
|
2024-01-03 21:05:20 +00:00
|
|
|
Deno.test("windows.extname()", function () {
|
2020-03-28 17:03:49 +00:00
|
|
|
pairs.forEach(function (p) {
|
2018-12-19 02:29:39 +00:00
|
|
|
const input = p[0].replace(slashRE, "\\");
|
|
|
|
const expected = p[1];
|
2024-01-03 21:05:20 +00:00
|
|
|
assertEquals(expected, windows.extname(input));
|
|
|
|
assertEquals(expected, windows.extname("C:" + input));
|
2018-12-19 02:29:39 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// On Windows, backslash is a path separator.
|
2024-01-03 21:05:20 +00:00
|
|
|
assertEquals(windows.extname(".\\"), "");
|
|
|
|
assertEquals(windows.extname("..\\"), "");
|
|
|
|
assertEquals(windows.extname("file.ext\\"), ".ext");
|
|
|
|
assertEquals(windows.extname("file.ext\\\\"), ".ext");
|
|
|
|
assertEquals(windows.extname("file\\"), "");
|
|
|
|
assertEquals(windows.extname("file\\\\"), "");
|
|
|
|
assertEquals(windows.extname("file.\\"), ".");
|
|
|
|
assertEquals(windows.extname("file.\\\\"), ".");
|
2024-08-26 07:33:53 +00:00
|
|
|
|
|
|
|
assertEquals(
|
2024-09-12 06:19:05 +00:00
|
|
|
windowsUnstableExtname(new URL("file:///C:/home/user/Documents/image.png")),
|
2024-08-26 07:33:53 +00:00
|
|
|
".png",
|
|
|
|
);
|
|
|
|
assertEquals(
|
2024-09-12 06:19:05 +00:00
|
|
|
windowsUnstableExtname(new URL("file:///C:/home/user/Documents")),
|
2024-08-26 07:33:53 +00:00
|
|
|
"",
|
|
|
|
);
|
2018-12-19 02:29:39 +00:00
|
|
|
});
|