|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach, Mock } from 'vitest'; |
| 2 | +import * as vscode from 'vscode'; |
| 3 | +import * as workspaceUtils from '../workspace'; |
| 4 | +import { promptOpenProject, tryGetLogicAppProjectRoot } from '../verifyIsProject'; |
| 5 | + |
| 6 | +vi.mock('../verifyIsProject'); |
| 7 | + |
| 8 | +describe('workspaceUtils.getWorkspaceFolder', () => { |
| 9 | + const originalWorkspace = { ...vscode.workspace }; |
| 10 | + |
| 11 | + const mockContext: any = { |
| 12 | + telemetry: { properties: {}, measurements: {} }, |
| 13 | + errorHandling: { issueProperties: {} }, |
| 14 | + ui: { |
| 15 | + showQuickPick: vi.fn(), |
| 16 | + showOpenDialog: vi.fn(), |
| 17 | + onDidFinishPrompt: vi.fn(), |
| 18 | + showInputBox: vi.fn(), |
| 19 | + showWarningMessage: vi.fn(), |
| 20 | + }, |
| 21 | + }; |
| 22 | + |
| 23 | + const mockFolder = (fsPath: string): vscode.WorkspaceFolder => ({ uri: { fsPath } }) as vscode.WorkspaceFolder; |
| 24 | + |
| 25 | + beforeEach(() => { |
| 26 | + // Reset workspace state and mocks before each test |
| 27 | + vi.restoreAllMocks(); |
| 28 | + (vscode.workspace as any).workspaceFolders = undefined; |
| 29 | + (vscode.workspace as any).workspaceFile = undefined; |
| 30 | + }); |
| 31 | + |
| 32 | + afterEach(() => { |
| 33 | + // Restore original workspace |
| 34 | + Object.assign(vscode, { workspace: originalWorkspace }); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should prompt to open project if no workspace folders are open', async () => { |
| 38 | + const folder1 = mockFolder('/path/one'); |
| 39 | + (vscode.workspace as any).workspaceFolders = []; |
| 40 | + |
| 41 | + const promptOpenProjectSpy = vi.fn(() => { |
| 42 | + (vscode.workspace as any).workspaceFolders = [folder1]; |
| 43 | + }); |
| 44 | + |
| 45 | + (promptOpenProject as Mock).mockImplementation(promptOpenProjectSpy); |
| 46 | + |
| 47 | + await workspaceUtils.getWorkspaceFolder(mockContext); |
| 48 | + expect(promptOpenProjectSpy).toHaveBeenCalled(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should prompt to open project if workspace folders are undefined', async () => { |
| 52 | + const folder1 = mockFolder('/path/one'); |
| 53 | + (vscode.workspace as any).workspaceFolders = undefined; |
| 54 | + |
| 55 | + const promptOpenProjectSpy = vi.fn(() => { |
| 56 | + (vscode.workspace as any).workspaceFolders = [folder1]; |
| 57 | + }); |
| 58 | + |
| 59 | + (promptOpenProject as Mock).mockImplementation(promptOpenProjectSpy); |
| 60 | + |
| 61 | + await workspaceUtils.getWorkspaceFolder(mockContext); |
| 62 | + expect(promptOpenProjectSpy).toHaveBeenCalled(); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should return the only workspace folder if there is only one', async () => { |
| 66 | + const folder1 = mockFolder('/path/one'); |
| 67 | + (vscode.workspace as any).workspaceFolders = [folder1]; |
| 68 | + |
| 69 | + const promptOpenProjectSpy = vi.fn(() => { |
| 70 | + (vscode.workspace as any).workspaceFolders = [folder1]; |
| 71 | + }); |
| 72 | + |
| 73 | + (promptOpenProject as Mock).mockImplementation(promptOpenProjectSpy); |
| 74 | + |
| 75 | + const result = await workspaceUtils.getWorkspaceFolder(mockContext); |
| 76 | + expect(result).toEqual(folder1); |
| 77 | + expect(promptOpenProjectSpy).not.toHaveBeenCalled(); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should return the only workspace folder if there is only one after prompting', async () => { |
| 81 | + const folder1 = mockFolder('/path/one'); |
| 82 | + (vscode.workspace as any).workspaceFolders = undefined; |
| 83 | + |
| 84 | + const promptOpenProjectSpy = vi.fn(() => { |
| 85 | + (vscode.workspace as any).workspaceFolders = [folder1]; |
| 86 | + }); |
| 87 | + |
| 88 | + (promptOpenProject as Mock).mockImplementation(promptOpenProjectSpy); |
| 89 | + |
| 90 | + const result = await workspaceUtils.getWorkspaceFolder(mockContext); |
| 91 | + expect(result).toEqual(folder1); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should return undefined if no logic app project is found among multiple folders', async () => { |
| 95 | + const folder1 = mockFolder('/path/one'); |
| 96 | + const folder2 = mockFolder('/path/two'); |
| 97 | + (vscode.workspace as any).workspaceFolders = [folder1, folder2]; |
| 98 | + |
| 99 | + const tryGetLogicAppProjectRootSpy = vi.fn(() => { |
| 100 | + return undefined; |
| 101 | + }); |
| 102 | + (tryGetLogicAppProjectRoot as Mock).mockImplementation(tryGetLogicAppProjectRootSpy); |
| 103 | + |
| 104 | + const result = await workspaceUtils.getWorkspaceFolder(mockContext); |
| 105 | + expect(tryGetLogicAppProjectRootSpy).toHaveBeenCalledTimes(2); |
| 106 | + expect(result).toBeUndefined(); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should return the only logic app project if there is only one', async () => { |
| 110 | + const folder1 = mockFolder('/logic/path'); |
| 111 | + const folder2 = mockFolder('/nonlogic/path'); |
| 112 | + (vscode.workspace as any).workspaceFolders = [folder1, folder2]; |
| 113 | + |
| 114 | + // For folder1 return a valid project root, for folder2 return undefined. |
| 115 | + const tryGetLogicAppProjectRootSpy = vi.fn(async (_context, folder) => { |
| 116 | + return folder.uri.fsPath === '/logic/path' ? folder.uri.fsPath : undefined; |
| 117 | + }); |
| 118 | + (tryGetLogicAppProjectRoot as Mock).mockImplementation(tryGetLogicAppProjectRootSpy); |
| 119 | + |
| 120 | + const result = await workspaceUtils.getWorkspaceFolder(mockContext); |
| 121 | + |
| 122 | + expect(result).toBe('/logic/path'); |
| 123 | + expect(tryGetLogicAppProjectRootSpy).toHaveBeenCalledTimes(2); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should return the first logic app project if skipPromptOnMultipleFolders is true', async () => { |
| 127 | + const folder1 = mockFolder('/logic/path1'); |
| 128 | + const folder2 = mockFolder('/logic/path2'); |
| 129 | + (vscode.workspace as any).workspaceFolders = [folder1, folder2]; |
| 130 | + |
| 131 | + // Both folders return a valid project root. |
| 132 | + const tryGetLogicAppProjectRootSpy = vi.fn(async (_context, folder) => { |
| 133 | + return folder.uri.fsPath; |
| 134 | + }); |
| 135 | + (tryGetLogicAppProjectRoot as Mock).mockImplementation(tryGetLogicAppProjectRootSpy); |
| 136 | + |
| 137 | + const result = await workspaceUtils.getWorkspaceFolder(mockContext, undefined, true); |
| 138 | + // Expect the first folder (or its project root) to be returned. |
| 139 | + expect(result).toBe('/logic/path1'); |
| 140 | + expect(tryGetLogicAppProjectRootSpy).toHaveBeenCalledTimes(2); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should prompt the user to select a logic app project if there are multiple', async () => { |
| 144 | + const folder1 = mockFolder('/logic/path1'); |
| 145 | + const folder2 = mockFolder('/logic/path2'); |
| 146 | + (vscode.workspace as any).workspaceFolders = [folder1, folder2]; |
| 147 | + // Both folders are logic app projects. |
| 148 | + const tryGetLogicAppProjectRootSpy = vi.fn(async (_context, folder) => { |
| 149 | + return folder.uri.fsPath; |
| 150 | + }); |
| 151 | + (tryGetLogicAppProjectRoot as Mock).mockImplementation(tryGetLogicAppProjectRootSpy); |
| 152 | + |
| 153 | + const quickPickSpy = vi.spyOn(mockContext.ui, 'showQuickPick').mockResolvedValue({ data: folder2 }); |
| 154 | + |
| 155 | + const result = await workspaceUtils.getWorkspaceFolder(mockContext); |
| 156 | + expect(quickPickSpy).toHaveBeenCalled(); |
| 157 | + expect(result).toBe(folder2); |
| 158 | + }); |
| 159 | + |
| 160 | + it('should throw UserCancelledError if user cancels the prompt', async () => { |
| 161 | + const folder1 = mockFolder('/logic/path1'); |
| 162 | + const folder2 = mockFolder('/logic/path2'); |
| 163 | + (vscode.workspace as any).workspaceFolders = [folder1, folder2]; |
| 164 | + |
| 165 | + const tryGetLogicAppProjectRootSpy = vi.fn(async (_context, folder) => { |
| 166 | + return folder.uri.fsPath; |
| 167 | + }); |
| 168 | + (tryGetLogicAppProjectRoot as Mock).mockImplementation(tryGetLogicAppProjectRootSpy); |
| 169 | + vi.spyOn(mockContext.ui, 'showQuickPick').mockResolvedValue(undefined); |
| 170 | + |
| 171 | + await expect(workspaceUtils.getWorkspaceFolder(mockContext)).rejects.toThrowError(); |
| 172 | + }); |
| 173 | +}); |
0 commit comments