Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 3 additions & 29 deletions apps/desktop/src/wsl/wslPathParsing.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { describe, it, expect } from "vite-plus/test";

import {
DISTRO_NAME_PATTERN,
extractDistroFromUncPath,
isValidDistroName,
parseWslDistroList,
resolveWslHomeUncPath,
resolveWslPickFolderDefaultPath,
wslUncPathToLinuxPath,
} from "./wslPathParsing.ts";
Expand Down Expand Up @@ -116,29 +114,6 @@ describe("wslUncPathToLinuxPath", () => {
});
});

describe("resolveWslHomeUncPath", () => {
const distros = [
{ name: "Debian", isDefault: true, version: 2 as const },
{ name: "Ubuntu", isDefault: false, version: 2 as const },
];

it("uses the configured distro when one is selected", () => {
expect(resolveWslHomeUncPath({ distro: "Ubuntu" }, distros)).toBe(
"\\\\wsl.localhost\\Ubuntu\\home",
);
});

it("uses the actual default distro when config uses the WSL default", () => {
expect(resolveWslHomeUncPath({ distro: null }, distros)).toBe(
"\\\\wsl.localhost\\Debian\\home",
);
});

it("omits the default path when no default distro is known", () => {
expect(resolveWslHomeUncPath({ distro: null }, [])).toBeNull();
});
});

describe("resolveWslPickFolderDefaultPath", () => {
const config = { distro: null };
const distros = [{ name: "Debian", isDefault: true, version: 2 as const }];
Expand Down Expand Up @@ -184,23 +159,22 @@ describe("resolveWslPickFolderDefaultPath", () => {
});
});

describe("DISTRO_NAME_PATTERN / isValidDistroName", () => {
describe("isValidDistroName", () => {
it("accepts common distro names", () => {
for (const name of ["Ubuntu", "Ubuntu-22.04", "kali-linux", "Debian", "Ubuntu 22.04"]) {
expect(DISTRO_NAME_PATTERN.test(name)).toBe(true);
expect(isValidDistroName(name)).toBe(true);
}
});

it("rejects names with trailing whitespace, hyphen, or dot", () => {
for (const name of ["Ubuntu ", "Ubuntu-", "Ubuntu."]) {
expect(DISTRO_NAME_PATTERN.test(name)).toBe(false);
expect(isValidDistroName(name)).toBe(false);
}
});

it("rejects names containing control or shell-meta characters", () => {
for (const name of ["bad\nname", "bad\tname", "bad/name", "bad!name", "bad;name"]) {
expect(DISTRO_NAME_PATTERN.test(name)).toBe(false);
expect(isValidDistroName(name)).toBe(false);
}
});
});
7 changes: 2 additions & 5 deletions apps/desktop/src/wsl/wslPathParsing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface WslConfig {

// Literal space — \s would also match \n/\t/\r and corrupt UNC paths like \\wsl.localhost\<distro>\...
// Trailing char must also be \w so hand-edited config like "Ubuntu " / "Ubuntu-" / "Ubuntu." rejects.
export const DISTRO_NAME_PATTERN = /^\w(?:[\w \-.]*\w)?$/;
const DISTRO_NAME_PATTERN = /^\w(?:[\w \-.]*\w)?$/;

export function parseWslDistroList(stdout: Buffer): readonly WslDistro[] {
const hasUtf16Bom = stdout.length >= 2 && stdout[0] === 0xff && stdout[1] === 0xfe;
Expand Down Expand Up @@ -61,10 +61,7 @@ export function wslUncPathToLinuxPath(windowsPath: string): string | null {
return `/${rest.split("\\").filter(Boolean).join("/")}`;
}

export function resolveWslHomeUncPath(
config: WslConfig,
distros: readonly WslDistro[],
): string | null {
function resolveWslHomeUncPath(config: WslConfig, distros: readonly WslDistro[]): string | null {
const distroName = config.distro ?? distros.find((distro) => distro.isDefault)?.name ?? null;
return distroName ? `\\\\wsl.localhost\\${distroName}\\home` : null;
}
Expand Down
Loading