Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

wslPathParsing.test.ts

193 lines6.3 KB

1import { describe, it, expect } from "vite-plus/test";
2​
3import {
4 DISTRO_NAME_PATTERN,
5 isSameWslDistro,
6 isValidDistroName,
7 parseWslDistroList,
8 parseWslUncPath,
9 resolveWslHomeUncPath,
10 resolveWslPickFolderDefaultPath,
11} from "./wslPathParsing.ts";
12​
13function makeUtf16LeBuffer(text: string): Buffer {
14 return Buffer.from("" + text, "utf16le");
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​
81​
82describe("parseWslUncPath", () => {
83 it("preserves distro identity with the Linux path", () => {
84 expect(parseWslUncPath("\\\\wsl.localhost\\Debian\\home\\alice\\repo")).toEqual({
85 distro: "Debian",
86 linuxPath: "/home/alice/repo",
87 });
88 });
89​
90 it("treats distro names case-insensitively without changing path text", () => {
91 expect(isSameWslDistro("ubuntu-22.04", "Ubuntu-22.04")).toBe(true);
92 expect(isSameWslDistro("Ubuntu", "Debian")).toBe(false);
93 });
94});
95​
96describe("resolveWslHomeUncPath", () => {
97 const distros = [
98 { name: "Debian", isDefault: true, version: 2 as const },
99 { name: "Ubuntu", isDefault: false, version: 2 as const },
100 ];
101​
102 it("uses the configured distro when one is selected", () => {
103 expect(resolveWslHomeUncPath({ distro: "Ubuntu" }, distros)).toBe(
104 "\\\\wsl.localhost\\Ubuntu\\home",
105 );
106 });
107​
108 it("uses the actual default distro when config uses the WSL default", () => {
109 expect(resolveWslHomeUncPath({ distro: null }, distros)).toBe(
110 "\\\\wsl.localhost\\Debian\\home",
111 );
112 });
113​
114 it("omits the default path when no default distro is known", () => {
115 expect(resolveWslHomeUncPath({ distro: null }, [])).toBeNull();
116 });
117});
118​
119describe("resolveWslPickFolderDefaultPath", () => {
120 const config = { distro: null };
121 const distros = [{ name: "Debian", isDefault: true, version: 2 as const }];
122​
123 it("uses WSL home when no initial path is provided", () => {
124 expect(resolveWslPickFolderDefaultPath(undefined, config, distros)).toBe(
125 "\\\\wsl.localhost\\Debian\\home",
126 );
127 });
128​
129 it("maps Linux initial paths to WSL UNC paths", () => {
130 expect(
131 resolveWslPickFolderDefaultPath({ initialPath: "/home/josh/project" }, config, distros),
132 ).toBe("\\\\wsl.localhost\\Debian\\home\\josh\\project");
133 });
134​
135 it("expands ~/path against the user's home dir when known", () => {
136 expect(
137 resolveWslPickFolderDefaultPath({ initialPath: "~/project" }, config, distros, "/home/josh"),
138 ).toBe("\\\\wsl.localhost\\Debian\\home\\josh\\project");
139 });
140​
141 it("resolves bare ~ to the user's home dir when known", () => {
142 expect(
143 resolveWslPickFolderDefaultPath({ initialPath: "~" }, config, distros, "/home/josh"),
144 ).toBe("\\\\wsl.localhost\\Debian\\home\\josh");
145 });
146​
147 it("falls back to /home parent when the user's home dir isn't known", () => {
148 expect(resolveWslPickFolderDefaultPath({ initialPath: "~/project" }, config, distros)).toBe(
149 "\\\\wsl.localhost\\Debian\\home\\project",
150 );
151 });
152​
153 it("preserves existing UNC initial paths only for the target distro", () => {
154 expect(
155 resolveWslPickFolderDefaultPath(
156 { initialPath: "\\\\wsl.localhost\\debian\\home\\josh" },
157 config,
158 distros,
159 ),
160 ).toBe("\\\\wsl.localhost\\debian\\home\\josh");
161 });
162​
163 it("does not seed a target-distro picker with another distro's UNC path", () => {
164 expect(
165 resolveWslPickFolderDefaultPath(
166 { initialPath: "\\\\wsl.localhost\\Ubuntu\\home\\josh" },
167 config,
168 distros,
169 ),
170 ).toBe("\\\\wsl.localhost\\Debian\\home");
171 });
172});
173​
174describe("DISTRO_NAME_PATTERN / isValidDistroName", () => {
175 it("accepts common distro names", () => {
176 for (const name of ["Ubuntu", "Ubuntu-22.04", "kali-linux", "Debian", "Ubuntu 22.04"]) {
177 expect(DISTRO_NAME_PATTERN.test(name)).toBe(true);
178 expect(isValidDistroName(name)).toBe(true);
179 }
180 });
181​
182 it("rejects names with trailing whitespace, hyphen, or dot", () => {
183 for (const name of ["Ubuntu ", "Ubuntu-", "Ubuntu."]) {
184 expect(DISTRO_NAME_PATTERN.test(name)).toBe(false);
185 }
186 });
187​
188 it("rejects names containing control or shell-meta characters", () => {
189 for (const name of ["bad\nname", "bad\tname", "bad/name", "bad!name", "bad;name"]) {
190 expect(DISTRO_NAME_PATTERN.test(name)).toBe(false);
191 }
192 });
193});