Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import {
3 fileBasename,
4 isWslSharePath,
5 workspaceRelativeFilePath,
6 newProjectFolderName,
7 normalizeProjectPathForComparison,
8 normalizeProjectPathForDispatch,
9 resolvePastedPath,
10 resolveWorkspaceFilePath,
11} from "./path.ts";
12
13describe("path helpers", () => {
14 it("splits basenames on backslashes only outside absolute POSIX paths", () => {
15 for (const [path, name] of [
16 ["/repo/build\\log.txt", "build\\log.txt"],
17 ["/repo/build\\/", "build\\"],
18 ["C:\\repo\\build\\log.txt\\", "log.txt"],
19 ["/C:/repo/build\\log.txt", "log.txt"],
20 ["//server/share/build\\log.txt", "log.txt"],
21 ["build\\log.txt", "log.txt"],
22 ] as const) {
23 expect(fileBasename(path)).toBe(name);
24 }
25 });
26
27 it("recognizes WSL shares and nothing else", () => {
28 expect(isWslSharePath("\\\\wsl.localhost\\Ubuntu\\home")).toBe(true);
29 expect(isWslSharePath("\\\\wsl$\\Ubuntu")).toBe(true);
30 expect(isWslSharePath(" //WSL.LOCALHOST/Ubuntu/home")).toBe(true);
31 expect(isWslSharePath("\\\\?\\UNC\\wsl.localhost\\Ubuntu\\repo")).toBe(true);
32 expect(isWslSharePath("\\\\.\\UNC\\wsl$\\Ubuntu\\repo")).toBe(true);
33 expect(isWslSharePath("\\\\?\\C:\\work\\repo")).toBe(false);
34 expect(isWslSharePath("\\\\wslserver\\share")).toBe(false);
35 expect(isWslSharePath("\\\\fileserver\\wsl.localhost")).toBe(false);
36 expect(isWslSharePath("C:\\work\\repo")).toBe(false);
37 });
38
39 // regression: #17923 literal colons in names are kept, not parsed as positions
40 it.each([
41 ["report:123", "/workspace", "/workspace/report:123"],
42 ["src/file", "C:\\Workspace\\", "C:\\Workspace\\src\\file"],
43 ["~/report:123", "/workspace", "~/report:123"],
44 ["note.txt", "/tmp/photos\\", "/tmp/photos\\/note.txt"],
45 ])("resolves literal path %s within %s", (path, root, expected) => {
46 expect(resolveWorkspaceFilePath(path, root)).toBe(expected);
47 });
48
49 it("normalizes a bare Windows drive root the same as one with a trailing separator", () => {
50 expect(normalizeProjectPathForDispatch("C:")).toBe("C:\\");
51 expect(normalizeProjectPathForComparison("C:")).toBe("c:\\");
52 expect(normalizeProjectPathForComparison("C:")).toBe(normalizeProjectPathForComparison("C:\\"));
53 expect(normalizeProjectPathForComparison("C:")).toBe(normalizeProjectPathForComparison("C:/"));
54 expect(normalizeProjectPathForDispatch("C:\\repo\\")).toBe("C:\\repo");
55 });
56
57 it("names a new project's folder from any typed name", () => {
58 expect(newProjectFolderName("Pinball Stats")).toBe("pinball-stats");
59 expect(newProjectFolderName(" Café & Crème! ")).toBe("cafe-creme");
60 expect(newProjectFolderName("Đặt tên dự án")).toBe("dat-ten-du-an");
61 expect(newProjectFolderName("Straßenbahn GROẞ")).toBe("strassenbahn-gross");
62 expect(newProjectFolderName("Łódź sklep")).toBe("lodz-sklep");
63 expect(newProjectFolderName("Øl på Þingvellir ı")).toBe("ol-pa-thingvellir-i");
64 expect(newProjectFolderName("Aʼna")).toBe("ana");
65 expect(newProjectFolderName("../../etc")).toBe("etc");
66 expect(newProjectFolderName("🎱🎱")).toBe("project");
67 expect(newProjectFolderName(`${"a".repeat(63)} b`)).toBe("a".repeat(63));
68 expect(newProjectFolderName("Con")).toBe("con-project");
69 expect(newProjectFolderName("LPT1")).toBe("lpt1-project");
70 expect(newProjectFolderName("console")).toBe("console");
71 });
72});
73
74describe("resolvePastedPath", () => {
75 it.each([
76 ["Finder, a space", "", "'/Users/me/Monarch Money'", "/Users/me/Monarch Money"],
77 ["Finder, an apostrophe", "", "/Users/me/Bob\\'s\\ Stuff", "/Users/me/Bob's Stuff"],
78 ["Finder, a backslash", "", "/Users/me/a\\\\\\ b", "/Users/me/a\\ b"],
79 [
80 "Finder, apostrophes around a name",
81 "",
82 "/Users/me/\\'My\\ Folder\\'",
83 "/Users/me/'My Folder'",
84 ],
85 ["Explorer, a drive", "", '"C:\\Users\\me\\My Code"', "C:\\Users\\me\\My Code"],
86 ["Explorer, a share", "", '"\\\\host\\share\\My Code"', "\\\\host\\share\\My Code"],
87 ["an apostrophe in double quotes", "", '"/Users/me/Bob\'s Stuff"', "/Users/me/Bob's Stuff"],
88 ["a shell, home-relative", "", "~/Bob\\'s\\ Stuff", "~/Bob's Stuff"],
89 ["surrounding whitespace", "", " '/Users/me/My Code'\n", "/Users/me/My Code"],
90 ["over a selection", "/Users/me/old", "'/Users/me/My Code'", "/Users/me/My Code"],
91 ["after a prefilled folder", "~/", "~/'/Users/me/My Code'", "/Users/me/My Code"],
92 ["plain, after a prefilled folder", "~/", "~//Users/me/code", "/Users/me/code"],
93 ["backslash home, after a folder", "~/", "~/~\\folder\\ child", "~/~\\folder\\ child"],
94 ["after a Windows folder", "C:\\Users\\", 'C:\\Users\\"D:\\My Code"', "D:\\My Code"],
95 ["a shell's quoted apostrophe", "", "'/Users/me/Bob'\\''s Work'", "/Users/me/Bob's Work"],
96 ["after a clone destination", "~/code/repo", "~/code/repo'/a b'", "/a b"],
97 ["over part of the box", "~/code/old", "~/code/'/a b'", "/a b"],
98 ])("reads the path copied from %s", (_source, previous, next, expected) => {
99 expect(resolvePastedPath(previous, next)).toBe(expected);
100 });
101
102 it.each([
103 ["typing a separator", "/Users/me", "/Users/me/"],
104 ["typing a separator after a folder", "~/", "~//"],
105 ["typing a trailing space", "/Users/My", "/Users/My "],
106 ["deleting", "/Users/me/", "/Users/me"],
107 ["pasting a relative folder", "~/", "~/Documents/code"],
108 ["a longer path over a selection", "/Users/me", "/Users/me/code"],
109 ["a longer path after a folder", "/Users/me/", "/Users/me/code"],
110 ["quoted text that is no path", "", "'hello world'"],
111 ["a quoted relative path", "", "'code/my app'"],
112 ["a search ending in a quoted path", "", "find '/tmp/repo'"],
113 ["a quote left open", "", "'/Users/me/My Code"],
114 ["a quote inside the quotes", "", "'/Users/me/it's here'"],
115 ["Windows separators", "", "C:\\Users\\me\\code"],
116 ["a Windows dot folder", "", "C:\\Users\\me\\.config"],
117 ["a Windows dot folder below home", "", "~/code\\.vscode"],
118 ["a share", "", "\\\\host\\share\\code"],
119 ["a backslash before a letter", "", "/Users/me/a\\b"],
120 ])("keeps %s", (_case, previous, next) => {
121 expect(resolvePastedPath(previous, next)).toBe(next);
122 });
123
124 it.each(["/Users/me/a\\ b", "~/code\\.vscode", "'/Users/me/My Code'"])(
125 "leaves %s typed one character at a time",
126 (typed) => {
127 const box = [...typed].reduce((text, char) => resolvePastedPath(text, text + char), "");
128 expect(box).toBe(typed);
129 },
130 );
131});
132
133describe("workspaceRelativeFilePath", () => {
134 // regression: #9309 POSIX paths compare case-sensitively, Windows paths do not
135 it.each([
136 ["/Repo/Project/src/main.ts", "/repo/project", null],
137 ["C:/USERS/mike/t3code/main.ts", "c:/users/MIKE/t3code", "main.ts"],
138 ["/repo/project-two/a.ts", "/repo/project", null],
139 // regression: #16354 a `..` that leaves the root is a host path, not a workspace path
140 ["/repo/project/../other/notes.md", "/repo/project", null],
141 ["C:\\repo\\project\\src\\..\\..\\notes.md", "C:\\repo\\project", null],
142 ["/repo/project/docs/a/../b.md", "/repo/project", "docs/a/../b.md"],
143 ["/repo/a\\b.txt", "/repo", "a\\b.txt"],
144 ["/repo/a/b.txt", "/repo", "a/b.txt"],
145 ["/repo//src/app.ts", "/repo", "src/app.ts"],
146 ["C:\\repo\\\\src\\app.ts", "C:\\repo", "src/app.ts"],
147 ["/repo/a\\", "/repo", "a\\"],
148 ["/repo/..\\notes.md", "/repo", "..\\notes.md"],
149 ["/repo/a\\/../../notes.md", "/repo", null],
150 ["/repo\\project/a.txt", "/repo\\project/", "a.txt"],
151 ["/repo/project/a.txt", "/repo\\project", null],
152 ["/repo\\project/a.txt", "/repo/project", null],
153 ["C:\\Repo\\src\\main.ts", "c:/repo/", "src/main.ts"],
154 ["/C:/Repo/src/main.ts", "c:\\repo", "src/main.ts"],
155 ["C:\\Repo\\src\\main.ts", "/c:/repo", "src/main.ts"],
156 ["\\\\SERVER\\Share\\Repo\\src\\main.ts", "\\\\server\\share\\repo", "src/main.ts"],
157 ["\\\\SERVER\\Share\\Repo\\src\\main.ts", "//server/share/repo", "src/main.ts"],
158 ["//server/share/a\\b.txt", "//server/share", "a\\b.txt"],
159 ["\\\\server\\share\\repo\\..\\notes.md", "\\\\server\\share\\repo", null],
160 ])("relates %s to %s", (path, workspaceRoot, relativePath) => {
161 expect(workspaceRelativeFilePath(path, workspaceRoot)).toBe(relativePath);
162 });
163});