|
| 1 | +import { describe, it, expect, beforeEach, afterEach, vi } from "vitest" |
| 2 | +import * as fs from "fs" |
| 3 | +import * as os from "os" |
| 4 | +import { isWSL, getWindowsHomeFromWSL } from "../wsl" |
| 5 | + |
| 6 | +// Mock fs module |
| 7 | +vi.mock("fs", () => ({ |
| 8 | + readFileSync: vi.fn(), |
| 9 | + accessSync: vi.fn(), |
| 10 | + constants: { |
| 11 | + F_OK: 0, |
| 12 | + }, |
| 13 | +})) |
| 14 | + |
| 15 | +// Mock os module |
| 16 | +vi.mock("os", () => ({ |
| 17 | + userInfo: vi.fn(() => ({ username: "testuser" })), |
| 18 | +})) |
| 19 | + |
| 20 | +describe("WSL Detection", () => { |
| 21 | + const originalEnv = process.env |
| 22 | + const originalPlatform = process.platform |
| 23 | + |
| 24 | + beforeEach(() => { |
| 25 | + // Reset environment variables |
| 26 | + process.env = { ...originalEnv } |
| 27 | + // Reset platform |
| 28 | + Object.defineProperty(process, "platform", { |
| 29 | + value: originalPlatform, |
| 30 | + writable: true, |
| 31 | + }) |
| 32 | + // Clear all mocks |
| 33 | + vi.clearAllMocks() |
| 34 | + }) |
| 35 | + |
| 36 | + afterEach(() => { |
| 37 | + // Restore original environment |
| 38 | + process.env = originalEnv |
| 39 | + Object.defineProperty(process, "platform", { |
| 40 | + value: originalPlatform, |
| 41 | + writable: false, |
| 42 | + }) |
| 43 | + }) |
| 44 | + |
| 45 | + describe("isWSL", () => { |
| 46 | + it("should detect WSL when WSL_DISTRO_NAME is set", () => { |
| 47 | + process.env.WSL_DISTRO_NAME = "Ubuntu" |
| 48 | + expect(isWSL()).toBe(true) |
| 49 | + }) |
| 50 | + |
| 51 | + it("should detect WSL when WSL_INTEROP is set", () => { |
| 52 | + process.env.WSL_INTEROP = "/run/WSL/123_interop" |
| 53 | + expect(isWSL()).toBe(true) |
| 54 | + }) |
| 55 | + |
| 56 | + it("should detect WSL when /proc/version contains Microsoft", () => { |
| 57 | + Object.defineProperty(process, "platform", { value: "linux", writable: true }) |
| 58 | + vi.mocked(fs.readFileSync).mockReturnValue( |
| 59 | + "Linux version 5.10.16.3-microsoft-standard-WSL2 (gcc version 9.3.0)", |
| 60 | + ) |
| 61 | + expect(isWSL()).toBe(true) |
| 62 | + }) |
| 63 | + |
| 64 | + it("should detect WSL when /proc/version contains WSL", () => { |
| 65 | + Object.defineProperty(process, "platform", { value: "linux", writable: true }) |
| 66 | + vi.mocked(fs.readFileSync).mockReturnValue("Linux version 5.10.16.3-WSL2") |
| 67 | + expect(isWSL()).toBe(true) |
| 68 | + }) |
| 69 | + |
| 70 | + it("should detect WSL when /proc/sys/fs/binfmt_misc/WSLInterop exists", () => { |
| 71 | + Object.defineProperty(process, "platform", { value: "linux", writable: true }) |
| 72 | + vi.mocked(fs.readFileSync).mockImplementation(() => { |
| 73 | + throw new Error("File not found") |
| 74 | + }) |
| 75 | + // accessSync should not throw for WSLInterop file |
| 76 | + vi.mocked(fs.accessSync).mockImplementation((path) => { |
| 77 | + if (path === "/proc/sys/fs/binfmt_misc/WSLInterop") { |
| 78 | + return undefined |
| 79 | + } |
| 80 | + throw new Error("File not found") |
| 81 | + }) |
| 82 | + expect(isWSL()).toBe(true) |
| 83 | + }) |
| 84 | + |
| 85 | + it("should detect WSL when PATH contains /mnt/c/", () => { |
| 86 | + Object.defineProperty(process, "platform", { value: "linux", writable: true }) |
| 87 | + process.env.PATH = "/usr/bin:/mnt/c/Windows/System32:/mnt/c/Windows" |
| 88 | + vi.mocked(fs.readFileSync).mockImplementation(() => { |
| 89 | + throw new Error("File not found") |
| 90 | + }) |
| 91 | + vi.mocked(fs.accessSync).mockImplementation(() => { |
| 92 | + throw new Error("File not found") |
| 93 | + }) |
| 94 | + expect(isWSL()).toBe(true) |
| 95 | + }) |
| 96 | + |
| 97 | + it("should detect WSL when WSLENV is set", () => { |
| 98 | + Object.defineProperty(process, "platform", { value: "linux", writable: true }) |
| 99 | + process.env.WSLENV = "WT_SESSION:WT_PROFILE_ID" |
| 100 | + vi.mocked(fs.readFileSync).mockImplementation(() => { |
| 101 | + throw new Error("File not found") |
| 102 | + }) |
| 103 | + vi.mocked(fs.accessSync).mockImplementation(() => { |
| 104 | + throw new Error("File not found") |
| 105 | + }) |
| 106 | + expect(isWSL()).toBe(true) |
| 107 | + }) |
| 108 | + |
| 109 | + it("should return false on native Windows", () => { |
| 110 | + Object.defineProperty(process, "platform", { value: "win32", writable: true }) |
| 111 | + vi.mocked(fs.readFileSync).mockImplementation(() => { |
| 112 | + throw new Error("File not found") |
| 113 | + }) |
| 114 | + vi.mocked(fs.accessSync).mockImplementation(() => { |
| 115 | + throw new Error("File not found") |
| 116 | + }) |
| 117 | + expect(isWSL()).toBe(false) |
| 118 | + }) |
| 119 | + |
| 120 | + it("should return false on native Linux without WSL indicators", () => { |
| 121 | + Object.defineProperty(process, "platform", { value: "linux", writable: true }) |
| 122 | + process.env.PATH = "/usr/bin:/usr/local/bin" |
| 123 | + vi.mocked(fs.readFileSync).mockReturnValue("Linux version 5.10.0-generic") |
| 124 | + vi.mocked(fs.accessSync).mockImplementation(() => { |
| 125 | + throw new Error("File not found") |
| 126 | + }) |
| 127 | + expect(isWSL()).toBe(false) |
| 128 | + }) |
| 129 | + |
| 130 | + it("should return false on macOS", () => { |
| 131 | + Object.defineProperty(process, "platform", { value: "darwin", writable: true }) |
| 132 | + vi.mocked(fs.readFileSync).mockImplementation(() => { |
| 133 | + throw new Error("File not found") |
| 134 | + }) |
| 135 | + vi.mocked(fs.accessSync).mockImplementation(() => { |
| 136 | + throw new Error("File not found") |
| 137 | + }) |
| 138 | + expect(isWSL()).toBe(false) |
| 139 | + }) |
| 140 | + }) |
| 141 | + |
| 142 | + describe("getWindowsHomeFromWSL", () => { |
| 143 | + beforeEach(() => { |
| 144 | + // Set up WSL environment |
| 145 | + Object.defineProperty(process, "platform", { value: "linux", writable: true }) |
| 146 | + process.env.WSL_DISTRO_NAME = "Ubuntu" |
| 147 | + }) |
| 148 | + |
| 149 | + it("should return null when not in WSL", () => { |
| 150 | + delete process.env.WSL_DISTRO_NAME |
| 151 | + delete process.env.WSL_INTEROP |
| 152 | + Object.defineProperty(process, "platform", { value: "win32", writable: true }) |
| 153 | + expect(getWindowsHomeFromWSL()).toBe(null) |
| 154 | + }) |
| 155 | + |
| 156 | + it("should find Windows home directory at /mnt/c/Users/username", () => { |
| 157 | + process.env.USER = "testuser" |
| 158 | + vi.mocked(fs.accessSync).mockImplementation((path) => { |
| 159 | + if (path === "/mnt/c/Users/testuser") { |
| 160 | + return undefined |
| 161 | + } |
| 162 | + throw new Error("File not found") |
| 163 | + }) |
| 164 | + expect(getWindowsHomeFromWSL()).toBe("/mnt/c/Users/testuser") |
| 165 | + }) |
| 166 | + |
| 167 | + it("should find Windows home directory at /mnt/c/users/username (lowercase)", () => { |
| 168 | + process.env.USER = "testuser" |
| 169 | + vi.mocked(fs.accessSync).mockImplementation((path) => { |
| 170 | + if (path === "/mnt/c/users/testuser") { |
| 171 | + return undefined |
| 172 | + } |
| 173 | + throw new Error("File not found") |
| 174 | + }) |
| 175 | + expect(getWindowsHomeFromWSL()).toBe("/mnt/c/users/testuser") |
| 176 | + }) |
| 177 | + |
| 178 | + it("should check D: drive if C: drive not found", () => { |
| 179 | + process.env.USER = "testuser" |
| 180 | + vi.mocked(fs.accessSync).mockImplementation((path) => { |
| 181 | + if (path === "/mnt/d/Users/testuser") { |
| 182 | + return undefined |
| 183 | + } |
| 184 | + throw new Error("File not found") |
| 185 | + }) |
| 186 | + expect(getWindowsHomeFromWSL()).toBe("/mnt/d/Users/testuser") |
| 187 | + }) |
| 188 | + |
| 189 | + it("should use WSL_USER_NAME if available", () => { |
| 190 | + process.env.WSL_USER_NAME = "wsluser" |
| 191 | + vi.mocked(fs.accessSync).mockImplementation((path) => { |
| 192 | + if (path === "/mnt/c/Users/wsluser") { |
| 193 | + return undefined |
| 194 | + } |
| 195 | + throw new Error("File not found") |
| 196 | + }) |
| 197 | + expect(getWindowsHomeFromWSL()).toBe("/mnt/c/Users/wsluser") |
| 198 | + }) |
| 199 | + |
| 200 | + it("should convert USERPROFILE Windows path to WSL path", () => { |
| 201 | + process.env.USERPROFILE = "C:\\Users\\winuser" |
| 202 | + vi.mocked(fs.accessSync).mockImplementation((path) => { |
| 203 | + if (path === "/mnt/c/Users/winuser") { |
| 204 | + return undefined |
| 205 | + } |
| 206 | + throw new Error("File not found") |
| 207 | + }) |
| 208 | + expect(getWindowsHomeFromWSL()).toBe("/mnt/c/Users/winuser") |
| 209 | + }) |
| 210 | + |
| 211 | + it("should handle USERPROFILE with different drive letter", () => { |
| 212 | + process.env.USERPROFILE = "D:\\Users\\winuser" |
| 213 | + vi.mocked(fs.accessSync).mockImplementation((path) => { |
| 214 | + if (path === "/mnt/d/Users/winuser") { |
| 215 | + return undefined |
| 216 | + } |
| 217 | + throw new Error("File not found") |
| 218 | + }) |
| 219 | + expect(getWindowsHomeFromWSL()).toBe("/mnt/d/Users/winuser") |
| 220 | + }) |
| 221 | + |
| 222 | + it("should return null when no Windows home directory is found", () => { |
| 223 | + process.env.USER = "testuser" |
| 224 | + delete process.env.USERPROFILE |
| 225 | + vi.mocked(fs.accessSync).mockImplementation(() => { |
| 226 | + throw new Error("File not found") |
| 227 | + }) |
| 228 | + expect(getWindowsHomeFromWSL()).toBe(null) |
| 229 | + }) |
| 230 | + }) |
| 231 | +}) |
0 commit comments