* Configure the mock workspace folders + active editor for a single test. * Returns a cleanup callback that restores the previous state.
(opts: {
folders?: Array<{ uri: { fsPath: string }; name: string; index: number }> | undefined
getWorkspaceFolder?: (...args: unknown[]) => { uri: { fsPath: string } } | null | undefined
activeEditor?: { document: { uri: { fsPath: string } } } | null
getConfiguration?: (section?: string) => { get: (key: string, defaultValue?: unknown) => unknown }
})
| 51 | * Returns a cleanup callback that restores the previous state. |
| 52 | */ |
| 53 | function withWorkspaceMock(opts: { |
| 54 | folders?: Array<{ uri: { fsPath: string }; name: string; index: number }> | undefined |
| 55 | getWorkspaceFolder?: (...args: unknown[]) => { uri: { fsPath: string } } | null | undefined |
| 56 | activeEditor?: { document: { uri: { fsPath: string } } } | null |
| 57 | getConfiguration?: (section?: string) => { get: (key: string, defaultValue?: unknown) => unknown } |
| 58 | }): () => void { |
| 59 | const previousFolders = mockWorkspace.workspaceFolders |
| 60 | const previousGetWorkspaceFolder = mockWorkspace.getWorkspaceFolder |
| 61 | const previousActiveEditor = mockWindow.activeTextEditor |
| 62 | // Capture getConfiguration too: tests that mutate it must not leak a broken |
| 63 | // mock into sibling tests if the outer beforeEach is ever changed or moved. |
| 64 | const previousGetConfiguration = mockWorkspace.getConfiguration |
| 65 | |
| 66 | if ("folders" in opts) mockWorkspace.workspaceFolders = opts.folders |
| 67 | if (opts.getWorkspaceFolder) mockWorkspace.getWorkspaceFolder = opts.getWorkspaceFolder |
| 68 | if ("activeEditor" in opts) mockWindow.activeTextEditor = opts.activeEditor ?? null |
| 69 | if (opts.getConfiguration) mockWorkspace.getConfiguration = opts.getConfiguration |
| 70 | |
| 71 | return () => { |
| 72 | mockWorkspace.workspaceFolders = previousFolders |
| 73 | mockWorkspace.getWorkspaceFolder = previousGetWorkspaceFolder |
| 74 | mockWindow.activeTextEditor = previousActiveEditor |
| 75 | mockWorkspace.getConfiguration = previousGetConfiguration |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | describe("Path Utilities", () => { |
| 80 | const originalPlatform = process.platform |