|
3 | 3 | // Windows-only branches of path-utils.ts and path-validation.ts run here: |
4 | 4 | // drive roots, bare drive letters, backslash conversion and UNC shares. |
5 | 5 |
|
| 6 | +import { promises as fsp, type Stats } from "fs"; |
| 7 | +import os from "os"; |
6 | 8 | import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; |
7 | 9 |
|
8 | 10 | vi.mock("path", async () => { |
9 | 11 | const actual = await vi.importActual<typeof import("path")>("path"); |
10 | 12 | return { ...actual.win32, default: actual.win32 }; |
11 | 13 | }); |
12 | 14 |
|
13 | | -const { normalizePath } = await import("../path-utils.js"); |
| 15 | +const { normalizePath, expandHome } = await import("../path-utils.js"); |
| 16 | +const { getValidRootDirectories } = await import("../roots-utils.js"); |
14 | 17 | const { isPathWithinAllowedDirectories } = |
15 | 18 | await import("../path-validation.js"); |
16 | 19 |
|
@@ -141,3 +144,43 @@ describe("isPathWithinAllowedDirectories on win32", () => { |
141 | 144 | ).toBe(false); |
142 | 145 | }); |
143 | 146 | }); |
| 147 | + |
| 148 | +// Windows users write a home-relative path with a backslash (#5180). The |
| 149 | +// home directory is stubbed to a Windows spelling, since the host's is POSIX. |
| 150 | +describe("~\\ on win32", () => { |
| 151 | + const home = "C:\\Users\\me"; |
| 152 | + |
| 153 | + it("expands ~\\ to the home directory, as ~/ is", () => { |
| 154 | + const spy = vi.spyOn(os, "homedir").mockReturnValue(home); |
| 155 | + try { |
| 156 | + expect(expandHome("~\\projects")).toBe("C:\\Users\\me\\projects"); |
| 157 | + expect(expandHome("~\\projects")).toBe(expandHome("~/projects")); |
| 158 | + } finally { |
| 159 | + spy.mockRestore(); |
| 160 | + } |
| 161 | + }); |
| 162 | + |
| 163 | + it("leaves a ~ that does not start a segment alone", () => { |
| 164 | + expect(expandHome("~user\\projects")).toBe("~user\\projects"); |
| 165 | + expect(expandHome("C:\\~\\x")).toBe("C:\\~\\x"); |
| 166 | + }); |
| 167 | + |
| 168 | + it("expands a ~\\ client Root through the same expandHome", async () => { |
| 169 | + const spies = [ |
| 170 | + vi.spyOn(os, "homedir").mockReturnValue(home), |
| 171 | + // The directory does not exist on this host: realpath and stat answer |
| 172 | + // as Windows would for an existing directory. |
| 173 | + vi.spyOn(fsp, "realpath").mockImplementation(async (p) => String(p)), |
| 174 | + vi |
| 175 | + .spyOn(fsp, "stat") |
| 176 | + .mockResolvedValue({ isDirectory: () => true } as Stats), |
| 177 | + ]; |
| 178 | + try { |
| 179 | + expect(await getValidRootDirectories([{ uri: "~\\projects" }])).toEqual([ |
| 180 | + "C:\\Users\\me\\projects", |
| 181 | + ]); |
| 182 | + } finally { |
| 183 | + for (const spy of spies) spy.mockRestore(); |
| 184 | + } |
| 185 | + }); |
| 186 | +}); |
0 commit comments