|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the VS Code Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2021-2025 the VS Code Swift project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of VS Code Swift project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import * as assert from "assert"; |
| 16 | +import * as toolchain from "../../src/ui/ToolchainSelection"; |
| 17 | +import { afterEach } from "mocha"; |
| 18 | +import { stub, restore } from "sinon"; |
| 19 | +import { testAssetUri } from "../fixtures"; |
| 20 | +import { WorkspaceContext } from "../../src/WorkspaceContext"; |
| 21 | +import { FolderContext } from "../../src/FolderContext"; |
| 22 | +import { SwiftToolchain } from "../../src/toolchain/toolchain"; |
| 23 | +import { activateExtensionForSuite, getRootWorkspaceFolder } from "./utilities/testutilities"; |
| 24 | +import { MockedFunction, mockGlobalValue } from "../MockUtils"; |
| 25 | + |
| 26 | +suite("FolderContext Error Handling Test Suite", () => { |
| 27 | + let workspaceContext: WorkspaceContext; |
| 28 | + let swiftToolchainCreateStub: MockedFunction<typeof SwiftToolchain.create>; |
| 29 | + const showToolchainError = mockGlobalValue(toolchain, "showToolchainError"); |
| 30 | + |
| 31 | + activateExtensionForSuite({ |
| 32 | + async setup(ctx) { |
| 33 | + workspaceContext = ctx; |
| 34 | + this.timeout(60000); |
| 35 | + }, |
| 36 | + testAssets: ["defaultPackage"], |
| 37 | + }); |
| 38 | + |
| 39 | + afterEach(() => { |
| 40 | + restore(); |
| 41 | + }); |
| 42 | + |
| 43 | + test("handles SwiftToolchain.create failure gracefully with user dismissal", async () => { |
| 44 | + const mockError = new Error("Mock toolchain failure"); |
| 45 | + swiftToolchainCreateStub = stub(SwiftToolchain, "create").throws(mockError); |
| 46 | + |
| 47 | + // Mock showToolchainError to return false (user dismissed dialog) |
| 48 | + const showToolchainErrorStub = stub().resolves(false); |
| 49 | + showToolchainError.setValue(showToolchainErrorStub); |
| 50 | + |
| 51 | + const workspaceFolder = getRootWorkspaceFolder(); |
| 52 | + const testFolder = testAssetUri("package2"); |
| 53 | + |
| 54 | + const folderContext = await FolderContext.create( |
| 55 | + testFolder, |
| 56 | + workspaceFolder, |
| 57 | + workspaceContext |
| 58 | + ); |
| 59 | + |
| 60 | + assert.ok(folderContext, "FolderContext should be created despite toolchain failure"); |
| 61 | + assert.strictEqual( |
| 62 | + folderContext.toolchain, |
| 63 | + workspaceContext.globalToolchain, |
| 64 | + "Should fallback to global toolchain when user dismisses dialog" |
| 65 | + ); |
| 66 | + |
| 67 | + const errorLogs = workspaceContext.logger.logs.filter( |
| 68 | + log => |
| 69 | + log.includes("Failed to discover Swift toolchain") && |
| 70 | + log.includes("package2") && |
| 71 | + log.includes("Mock toolchain failure") |
| 72 | + ); |
| 73 | + assert.ok(errorLogs.length > 0, "Should log error message with folder context"); |
| 74 | + |
| 75 | + assert.ok( |
| 76 | + swiftToolchainCreateStub.calledWith(testFolder), |
| 77 | + "Should attempt to create toolchain for specific folder" |
| 78 | + ); |
| 79 | + assert.strictEqual( |
| 80 | + swiftToolchainCreateStub.callCount, |
| 81 | + 1, |
| 82 | + "Should only call SwiftToolchain.create once when user dismisses" |
| 83 | + ); |
| 84 | + }); |
| 85 | + |
| 86 | + test("retries toolchain creation when user makes selection and succeeds", async () => { |
| 87 | + const workspaceFolder = getRootWorkspaceFolder(); |
| 88 | + const testFolder = testAssetUri("package2"); |
| 89 | + |
| 90 | + // Arrange: Mock SwiftToolchain.create to fail first time, succeed second time |
| 91 | + swiftToolchainCreateStub = stub(SwiftToolchain, "create"); |
| 92 | + swiftToolchainCreateStub.onFirstCall().throws(new Error("Initial toolchain failure")); |
| 93 | + swiftToolchainCreateStub |
| 94 | + .onSecondCall() |
| 95 | + .returns(Promise.resolve(workspaceContext.globalToolchain)); |
| 96 | + |
| 97 | + // Mock showToolchainError to return true (user made selection) |
| 98 | + const showToolchainErrorStub = stub().resolves(true); |
| 99 | + showToolchainError.setValue(showToolchainErrorStub); |
| 100 | + |
| 101 | + const folderContext = await FolderContext.create( |
| 102 | + testFolder, |
| 103 | + workspaceFolder, |
| 104 | + workspaceContext |
| 105 | + ); |
| 106 | + |
| 107 | + // Assert: FolderContext should be created successfully |
| 108 | + assert.ok(folderContext, "FolderContext should be created after retry"); |
| 109 | + assert.strictEqual( |
| 110 | + folderContext.toolchain, |
| 111 | + workspaceContext.globalToolchain, |
| 112 | + "Should use successfully created toolchain after retry" |
| 113 | + ); |
| 114 | + |
| 115 | + // Assert: SwiftToolchain.create should be called twice (initial + retry) |
| 116 | + assert.strictEqual( |
| 117 | + swiftToolchainCreateStub.callCount, |
| 118 | + 2, |
| 119 | + "Should retry toolchain creation after user selection" |
| 120 | + ); |
| 121 | + |
| 122 | + // Assert: Should log both failure and success |
| 123 | + const failureLogs = workspaceContext.logger.logs.filter(log => |
| 124 | + log.includes("Failed to discover Swift toolchain for package2") |
| 125 | + ); |
| 126 | + const successLogs = workspaceContext.logger.logs.filter(log => |
| 127 | + log.includes("Successfully created toolchain for package2 after user selection") |
| 128 | + ); |
| 129 | + |
| 130 | + assert.ok(failureLogs.length > 0, "Should log initial failure"); |
| 131 | + assert.ok(successLogs.length > 0, "Should log success after retry"); |
| 132 | + }); |
| 133 | + |
| 134 | + test("retries toolchain creation when user makes selection but still fails", async () => { |
| 135 | + const workspaceFolder = getRootWorkspaceFolder(); |
| 136 | + const testFolder = testAssetUri("package2"); |
| 137 | + |
| 138 | + const initialError = new Error("Initial toolchain failure"); |
| 139 | + const retryError = new Error("Retry toolchain failure"); |
| 140 | + swiftToolchainCreateStub = stub(SwiftToolchain, "create"); |
| 141 | + swiftToolchainCreateStub.onFirstCall().throws(initialError); |
| 142 | + swiftToolchainCreateStub.onSecondCall().throws(retryError); |
| 143 | + |
| 144 | + // Mock showToolchainError to return true (user made selection) |
| 145 | + const showToolchainErrorStub = stub().resolves(true); |
| 146 | + showToolchainError.setValue(showToolchainErrorStub); |
| 147 | + |
| 148 | + const folderContext = await FolderContext.create( |
| 149 | + testFolder, |
| 150 | + workspaceFolder, |
| 151 | + workspaceContext |
| 152 | + ); |
| 153 | + |
| 154 | + assert.ok( |
| 155 | + folderContext, |
| 156 | + "FolderContext should be created with fallback after retry failure" |
| 157 | + ); |
| 158 | + assert.strictEqual( |
| 159 | + folderContext.toolchain, |
| 160 | + workspaceContext.globalToolchain, |
| 161 | + "Should fallback to global toolchain when retry also fails" |
| 162 | + ); |
| 163 | + |
| 164 | + assert.strictEqual( |
| 165 | + swiftToolchainCreateStub.callCount, |
| 166 | + 2, |
| 167 | + "Should retry toolchain creation after user selection" |
| 168 | + ); |
| 169 | + |
| 170 | + const initialFailureLogs = workspaceContext.logger.logs.filter(log => |
| 171 | + log.includes( |
| 172 | + "Failed to discover Swift toolchain for package2: Error: Initial toolchain failure" |
| 173 | + ) |
| 174 | + ); |
| 175 | + const retryFailureLogs = workspaceContext.logger.logs.filter(log => |
| 176 | + log.includes( |
| 177 | + "Failed to create toolchain for package2 even after user selection: Error: Retry toolchain failure" |
| 178 | + ) |
| 179 | + ); |
| 180 | + |
| 181 | + assert.ok(initialFailureLogs.length > 0, "Should log initial failure"); |
| 182 | + assert.ok(retryFailureLogs.length > 0, "Should log retry failure"); |
| 183 | + }); |
| 184 | +}); |
0 commit comments