Open History to see the last commit
1import { describe, it, expect } from "vite-plus/test";
2
3import {
4 DISTRO_NAME_PATTERN,
5 extractDistroFromUncPath,
6 isValidDistroName,
7 parseWslDistroList,
8 resolveWslHomeUncPath,
9 resolveWslPickFolderDefaultPath,
10 wslUncPathToLinuxPath,
11} from "./wslPathParsing.ts";
12
13function makeUtf16LeBuffer(text: string): Buffer {
14 return Buffer.from("" + text, "utf16le");
15}
16
17describe("parseWslDistroList", () => {
18 it("parses standard output with default distro marked", () => {
19 const output = makeUtf16LeBuffer(
20 [
21 " NAME STATE VERSION",
22 "* Ubuntu Running 2",
23 " Debian Stopped 2",
24 " Ubuntu-22.04 Running 1",
25 ].join("\r\n"),
26 );
27 const distros = parseWslDistroList(output);
28 expect(distros).toEqual([
29 { name: "Ubuntu", isDefault: true, version: 2 },
30 { name: "Debian", isDefault: false, version: 2 },
31 { name: "Ubuntu-22.04", isDefault: false, version: 1 },
32 ]);
33 });
34
35 it("returns empty array for empty buffer", () => {
36 expect(parseWslDistroList(Buffer.alloc(0))).toEqual([]);
37 });
38
39 it("returns empty array for header-only output", () => {
40 const output = makeUtf16LeBuffer(" NAME STATE VERSION\r\n");
41 expect(parseWslDistroList(output)).toEqual([]);
42 });
43
44 it("skips malformed lines", () => {
45 const output = makeUtf16LeBuffer(
46 [
47 " NAME STATE VERSION",
48 "* Ubuntu Running 2",
49 " bad line",
50 "",
51 " Debian Stopped 2",
52 ].join("\r\n"),
53 );
54 const distros = parseWslDistroList(output);
55 expect(distros).toEqual([
56 { name: "Ubuntu", isDefault: true, version: 2 },
57 { name: "Debian", isDefault: false, version: 2 },
58 ]);
59 });
60
61 it("handles output without BOM", () => {
62 const text = [
63 " NAME STATE VERSION",
64 "* Ubuntu Running 2",
65 ].join("\r\n");
66 const output = Buffer.from(text, "utf16le");
67 const distros = parseWslDistroList(output);
68 expect(distros).toEqual([{ name: "Ubuntu", isDefault: true, version: 2 }]);
69 });
70
71 it("handles UTF-8 output", () => {
72 const text = [
73 " NAME STATE VERSION",
74 "* Debian Running 2",
75 ].join("\r\n");
76 const distros = parseWslDistroList(Buffer.from(text, "utf8"));
77 expect(distros).toEqual([{ name: "Debian", isDefault: true, version: 2 }]);
78 });
79});
80
81describe("extractDistroFromUncPath", () => {
82 it("extracts the distro from \\\\wsl.localhost UNC paths", () => {
83 expect(extractDistroFromUncPath("\\\\wsl.localhost\\Ubuntu-22.04\\home\\josh")).toBe(
84 "Ubuntu-22.04",
85 );
86 });
87
88 it("extracts the distro from the legacy \\\\wsl$ UNC paths", () => {
89 expect(extractDistroFromUncPath("\\\\wsl$\\Debian\\home\\josh")).toBe("Debian");
90 });
91
92 it("returns null for non-UNC Windows paths", () => {
93 expect(extractDistroFromUncPath("C:\\Users\\Josh\\project")).toBeNull();
94 });
95
96 it("returns null when the segment is not a valid distro name", () => {
97 expect(extractDistroFromUncPath("\\\\wsl.localhost\\bad name!\\home")).toBeNull();
98 });
99});
100
101describe("wslUncPathToLinuxPath", () => {
102 it("maps WSL UNC paths back to Linux absolute paths", () => {
103 expect(wslUncPathToLinuxPath("\\\\wsl.localhost\\Ubuntu-22.04\\home\\josh\\repo")).toBe(
104 "/home/josh/repo",
105 );
106 });
107
108 it("maps a distro UNC root to Linux root", () => {
109 expect(wslUncPathToLinuxPath("\\\\wsl$\\Debian")).toBe("/");
110 expect(wslUncPathToLinuxPath("\\\\wsl.localhost\\Debian\\")).toBe("/");
111 });
112
113 it("rejects invalid distro names and non-WSL paths", () => {
114 expect(wslUncPathToLinuxPath("\\\\wsl.localhost\\bad!name\\home")).toBeNull();
115 expect(wslUncPathToLinuxPath("C:\\Users\\Josh\\repo")).toBeNull();
116 });
117});
118
119describe("resolveWslHomeUncPath", () => {
120 const distros = [
121 { name: "Debian", isDefault: true, version: 2 as const },
122 { name: "Ubuntu", isDefault: false, version: 2 as const },
123 ];
124
125 it("uses the configured distro when one is selected", () => {
126 expect(resolveWslHomeUncPath({ distro: "Ubuntu" }, distros)).toBe(
127 "\\\\wsl.localhost\\Ubuntu\\home",
128 );
129 });
130
131 it("uses the actual default distro when config uses the WSL default", () => {
132 expect(resolveWslHomeUncPath({ distro: null }, distros)).toBe(
133 "\\\\wsl.localhost\\Debian\\home",
134 );
135 });
136
137 it("omits the default path when no default distro is known", () => {
138 expect(resolveWslHomeUncPath({ distro: null }, [])).toBeNull();
139 });
140});
141
142describe("resolveWslPickFolderDefaultPath", () => {
143 const config = { distro: null };
144 const distros = [{ name: "Debian", isDefault: true, version: 2 as const }];
145
146 it("uses WSL home when no initial path is provided", () => {
147 expect(resolveWslPickFolderDefaultPath(undefined, config, distros)).toBe(
148 "\\\\wsl.localhost\\Debian\\home",
149 );
150 });
151
152 it("maps Linux initial paths to WSL UNC paths", () => {
153 expect(
154 resolveWslPickFolderDefaultPath({ initialPath: "/home/josh/project" }, config, distros),
155 ).toBe("\\\\wsl.localhost\\Debian\\home\\josh\\project");
156 });
157
158 it("expands ~/path against the user's home dir when known", () => {
159 expect(
160 resolveWslPickFolderDefaultPath({ initialPath: "~/project" }, config, distros, "/home/josh"),
161 ).toBe("\\\\wsl.localhost\\Debian\\home\\josh\\project");
162 });
163
164 it("resolves bare ~ to the user's home dir when known", () => {
165 expect(
166 resolveWslPickFolderDefaultPath({ initialPath: "~" }, config, distros, "/home/josh"),
167 ).toBe("\\\\wsl.localhost\\Debian\\home\\josh");
168 });
169
170 it("falls back to /home parent when the user's home dir isn't known", () => {
171 expect(resolveWslPickFolderDefaultPath({ initialPath: "~/project" }, config, distros)).toBe(
172 "\\\\wsl.localhost\\Debian\\home\\project",
173 );
174 });
175
176 it("preserves existing UNC initial paths", () => {
177 expect(
178 resolveWslPickFolderDefaultPath(
179 { initialPath: "\\\\wsl.localhost\\Ubuntu\\home\\josh" },
180 config,
181 distros,
182 ),
183 ).toBe("\\\\wsl.localhost\\Ubuntu\\home\\josh");
184 });
185});
186
187describe("DISTRO_NAME_PATTERN / isValidDistroName", () => {
188 it("accepts common distro names", () => {
189 for (const name of ["Ubuntu", "Ubuntu-22.04", "kali-linux", "Debian", "Ubuntu 22.04"]) {
190 expect(DISTRO_NAME_PATTERN.test(name)).toBe(true);
191 expect(isValidDistroName(name)).toBe(true);
192 }
193 });
194
195 it("rejects names with trailing whitespace, hyphen, or dot", () => {
196 for (const name of ["Ubuntu ", "Ubuntu-", "Ubuntu."]) {
197 expect(DISTRO_NAME_PATTERN.test(name)).toBe(false);
198 }
199 });
200
201 it("rejects names containing control or shell-meta characters", () => {
202 for (const name of ["bad\nname", "bad\tname", "bad/name", "bad!name", "bad;name"]) {
203 expect(DISTRO_NAME_PATTERN.test(name)).toBe(false);
204 }
205 });
206});