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

wslPathParsing.test.ts

121 lines4.0 KB

1import { describe, it, expect } from "vite-plus/test";
2​
3import {
4 extractDistroFromUncPath,
5 isValidDistroName,
6 parseWslDistroList,
7 resolveWslPickFolderDefaultPath,
8 wslUncPathToLinuxPath,
9} from "./wslPathParsing.ts";
10​
11function makeUtf16LeBuffer(text: string): Buffer {
12 return Buffer.from("" + text, "utf16le");
14​
15describe("parseWslDistroList", () => {
16 it("parses standard output with default distro marked", () => {
17 const output = makeUtf16LeBuffer(
18 [
19 " NAME STATE VERSION",
20 "* Ubuntu Running 2",
21 " Debian Stopped 2",
22 " Ubuntu-22.04 Running 1",
23 ].join("\r\n"),
24 );
25 const distros = parseWslDistroList(output);
26 expect(distros).toEqual([
27 { name: "Ubuntu", isDefault: true, version: 2 },
28 { name: "Debian", isDefault: false, version: 2 },
29 { name: "Ubuntu-22.04", isDefault: false, version: 1 },
30 ]);
31 });
32​
33 it("skips malformed lines", () => {
34 const output = makeUtf16LeBuffer(
35 [
36 " NAME STATE VERSION",
37 "* Ubuntu Running 2",
38 " bad line",
39 "",
40 " Debian Stopped 2",
41 ].join("\r\n"),
42 );
43 const distros = parseWslDistroList(output);
44 expect(distros).toEqual([
45 { name: "Ubuntu", isDefault: true, version: 2 },
46 { name: "Debian", isDefault: false, version: 2 },
47 ]);
48 });
49​
50 it("handles UTF-8 output", () => {
51 const text = [
52 " NAME STATE VERSION",
53 "* Debian Running 2",
54 ].join("\r\n");
55 const distros = parseWslDistroList(Buffer.from(text, "utf8"));
56 expect(distros).toEqual([{ name: "Debian", isDefault: true, version: 2 }]);
57 });
58});
59​
60describe("extractDistroFromUncPath", () => {
61 it("extracts the distro from \\\\wsl.localhost UNC paths", () => {
62 expect(extractDistroFromUncPath("\\\\wsl.localhost\\Ubuntu-22.04\\home\\josh")).toBe(
63 "Ubuntu-22.04",
64 );
65 });
66});
67​
68describe("wslUncPathToLinuxPath", () => {
69 it("maps WSL UNC paths back to Linux absolute paths", () => {
70 expect(wslUncPathToLinuxPath("\\\\wsl.localhost\\Ubuntu-22.04\\home\\josh\\repo")).toBe(
71 "/home/josh/repo",
72 );
73 });
74​
75 it("maps a distro UNC root to Linux root", () => {
76 expect(wslUncPathToLinuxPath("\\\\wsl$\\Debian")).toBe("/");
77 expect(wslUncPathToLinuxPath("\\\\wsl.localhost\\Debian\\")).toBe("/");
78 });
79​
80 it("rejects invalid distro names and non-WSL paths", () => {
81 expect(wslUncPathToLinuxPath("\\\\wsl.localhost\\bad!name\\home")).toBeNull();
82 expect(wslUncPathToLinuxPath("C:\\Users\\Josh\\repo")).toBeNull();
83 });
84});
85​
86describe("resolveWslPickFolderDefaultPath", () => {
87 const config = { distro: null };
88 const distros = [{ name: "Debian", isDefault: true, version: 2 as const }];
89​
90 it("maps Linux initial paths to WSL UNC paths", () => {
91 expect(
92 resolveWslPickFolderDefaultPath({ initialPath: "/home/josh/project" }, config, distros),
93 ).toBe("\\\\wsl.localhost\\Debian\\home\\josh\\project");
94 });
95​
96 it("expands ~/path against the user's home dir when known", () => {
97 expect(
98 resolveWslPickFolderDefaultPath({ initialPath: "~/project" }, config, distros, "/home/josh"),
99 ).toBe("\\\\wsl.localhost\\Debian\\home\\josh\\project");
100 });
101​
102 it("falls back to /home parent when the user's home dir isn't known", () => {
103 expect(resolveWslPickFolderDefaultPath({ initialPath: "~/project" }, config, distros)).toBe(
104 "\\\\wsl.localhost\\Debian\\home\\project",
105 );
106 });
107});
108​
109describe("isValidDistroName", () => {
110 it("rejects names with trailing whitespace, hyphen, or dot", () => {
111 for (const name of ["Ubuntu ", "Ubuntu-", "Ubuntu."]) {
112 expect(isValidDistroName(name)).toBe(false);
113 }
114 });
115​
116 it("rejects names containing control or shell-meta characters", () => {
117 for (const name of ["bad\nname", "bad\tname", "bad/name", "bad!name", "bad;name"]) {
118 expect(isValidDistroName(name)).toBe(false);
119 }
120 });
121});