|
| 1 | +// test/extension/commands/installPods.test.ts |
| 2 | + |
| 3 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 4 | +// Licensed under the MIT license. See LICENSE file in the project root for details. |
| 5 | + |
| 6 | +import * as assert from "assert"; |
| 7 | +import * as path from "path"; |
| 8 | +import * as fs from "fs"; |
| 9 | +import * as os from "os"; |
| 10 | +import Sinon = require("sinon"); |
| 11 | +import * as vscode from "vscode"; |
| 12 | +import { InstallPods } from "../../../src/extension/commands/installPods"; |
| 13 | +import { AppLauncher } from "../../../src/extension/appLauncher"; |
| 14 | +import { OutputChannelLogger } from "../../../src/extension/log/OutputChannelLogger"; |
| 15 | + |
| 16 | +suite("installPodsCommand", function () { |
| 17 | + let showWarningMessageStub: Sinon.SinonStub; |
| 18 | + let showErrorMessageStub: Sinon.SinonStub; |
| 19 | + let showInformationMessageStub: Sinon.SinonStub; |
| 20 | + let loggerErrorStub: Sinon.SinonStub; |
| 21 | + let getMainChannelStub: Sinon.SinonStub; |
| 22 | + |
| 23 | + let tempDir: string; |
| 24 | + let installPodsCommand: InstallPods; |
| 25 | + |
| 26 | + const isMac = process.platform === "darwin"; |
| 27 | + |
| 28 | + /** |
| 29 | + * Helper function to remove directory recursively |
| 30 | + */ |
| 31 | + function removeDirRecursive(dirPath: string): void { |
| 32 | + if (fs.existsSync(dirPath)) { |
| 33 | + fs.readdirSync(dirPath).forEach(file => { |
| 34 | + const curPath = path.join(dirPath, file); |
| 35 | + if (fs.lstatSync(curPath).isDirectory()) { |
| 36 | + removeDirRecursive(curPath); |
| 37 | + } else { |
| 38 | + fs.unlinkSync(curPath); |
| 39 | + } |
| 40 | + }); |
| 41 | + fs.rmdirSync(dirPath); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Helper function to create a mock AppLauncher project |
| 47 | + */ |
| 48 | + function createMockProject(projectPath: string, nodeModulesRoot: string): AppLauncher { |
| 49 | + return { |
| 50 | + getPackager: () => ({ |
| 51 | + getProjectPath: () => projectPath, |
| 52 | + }), |
| 53 | + getOrUpdateNodeModulesRoot: () => nodeModulesRoot, |
| 54 | + getWorkspaceFolder: () => ({ |
| 55 | + uri: { |
| 56 | + fsPath: projectPath, |
| 57 | + }, |
| 58 | + }), |
| 59 | + } as unknown as AppLauncher; |
| 60 | + } |
| 61 | + |
| 62 | + setup(function () { |
| 63 | + // Create temp directory for tests |
| 64 | + tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "installPods-test-")); |
| 65 | + |
| 66 | + // Stub vscode.window methods |
| 67 | + showWarningMessageStub = Sinon.stub(vscode.window, "showWarningMessage"); |
| 68 | + showErrorMessageStub = Sinon.stub(vscode.window, "showErrorMessage"); |
| 69 | + showInformationMessageStub = Sinon.stub(vscode.window, "showInformationMessage"); |
| 70 | + |
| 71 | + // Stub OutputChannelLogger |
| 72 | + const mockLogger = { |
| 73 | + info: Sinon.stub(), |
| 74 | + error: Sinon.stub(), |
| 75 | + warning: Sinon.stub(), |
| 76 | + debug: Sinon.stub(), |
| 77 | + }; |
| 78 | + loggerErrorStub = mockLogger.error; |
| 79 | + getMainChannelStub = Sinon.stub(OutputChannelLogger, "getMainChannel").returns( |
| 80 | + mockLogger as unknown as OutputChannelLogger, |
| 81 | + ); |
| 82 | + |
| 83 | + // Create InstallPods instance |
| 84 | + installPodsCommand = InstallPods.formInstance(); |
| 85 | + }); |
| 86 | + |
| 87 | + teardown(function () { |
| 88 | + // Restore all stubs |
| 89 | + showWarningMessageStub.restore(); |
| 90 | + showErrorMessageStub.restore(); |
| 91 | + showInformationMessageStub.restore(); |
| 92 | + getMainChannelStub.restore(); |
| 93 | + |
| 94 | + // Clean up temp directory |
| 95 | + removeDirRecursive(tempDir); |
| 96 | + }); |
| 97 | + |
| 98 | + suite("Platform checks", function () { |
| 99 | + test("should show warning on non-macOS platform", async function () { |
| 100 | + // Skip this test on macOS since we want to test non-macOS behavior |
| 101 | + if (isMac) { |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + // Mock project |
| 106 | + const mockProject = createMockProject(tempDir, path.join(tempDir, "node_modules")); |
| 107 | + (installPodsCommand as any).project = mockProject; |
| 108 | + |
| 109 | + await installPodsCommand.baseFn(); |
| 110 | + |
| 111 | + assert.ok( |
| 112 | + showWarningMessageStub.calledOnce, |
| 113 | + "Should show warning message on non-macOS", |
| 114 | + ); |
| 115 | + assert.ok( |
| 116 | + showWarningMessageStub.firstCall.args[0].includes("macOS"), |
| 117 | + "Warning message should mention macOS", |
| 118 | + ); |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + suite("Directory validation", function () { |
| 123 | + test("should show error when ios directory does not exist", async function () { |
| 124 | + // Skip on non-macOS |
| 125 | + if (!isMac) { |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + // Use temp directory without ios folder |
| 130 | + const mockProject = createMockProject(tempDir, path.join(tempDir, "node_modules")); |
| 131 | + (installPodsCommand as any).project = mockProject; |
| 132 | + |
| 133 | + await installPodsCommand.baseFn(); |
| 134 | + |
| 135 | + assert.ok( |
| 136 | + loggerErrorStub.calledOnce, |
| 137 | + "Should log error message when ios directory is missing", |
| 138 | + ); |
| 139 | + assert.ok( |
| 140 | + loggerErrorStub.firstCall.args[0].includes("iOS directory") || |
| 141 | + loggerErrorStub.firstCall.args[0].includes("iOS"), |
| 142 | + "Error message should mention iOS directory", |
| 143 | + ); |
| 144 | + }); |
| 145 | + |
| 146 | + test("should show error when Podfile does not exist", async function () { |
| 147 | + // Skip on non-macOS |
| 148 | + if (!isMac) { |
| 149 | + return; |
| 150 | + } |
| 151 | + |
| 152 | + // Create ios directory without Podfile |
| 153 | + const iosPath = path.join(tempDir, "ios"); |
| 154 | + fs.mkdirSync(iosPath, { recursive: true }); |
| 155 | + |
| 156 | + const mockProject = createMockProject(tempDir, path.join(tempDir, "node_modules")); |
| 157 | + (installPodsCommand as any).project = mockProject; |
| 158 | + |
| 159 | + await installPodsCommand.baseFn(); |
| 160 | + |
| 161 | + assert.ok( |
| 162 | + loggerErrorStub.calledOnce, |
| 163 | + "Should log error message when Podfile is missing", |
| 164 | + ); |
| 165 | + assert.ok( |
| 166 | + loggerErrorStub.firstCall.args[0].includes("Podfile"), |
| 167 | + "Error message should mention Podfile", |
| 168 | + ); |
| 169 | + }); |
| 170 | + }); |
| 171 | +}); |
0 commit comments