From fc643ba190249fb4b14b70cbe69ccf40e62bdc9a Mon Sep 17 00:00:00 2001 From: Arvind Date: Fri, 22 Dec 2023 05:19:39 +0530 Subject: [PATCH 1/2] fix(ProfileUtils.ts): remove redundant isTheia() function Signed-off-by: Arvind --- packages/zowe-explorer/src/utils/ProfilesUtils.ts | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/packages/zowe-explorer/src/utils/ProfilesUtils.ts b/packages/zowe-explorer/src/utils/ProfilesUtils.ts index eaa23c1148..7a321641e7 100644 --- a/packages/zowe-explorer/src/utils/ProfilesUtils.ts +++ b/packages/zowe-explorer/src/utils/ProfilesUtils.ts @@ -78,7 +78,7 @@ export async function errorHandling(errorDetails: Error | string, label?: string const isTokenAuth = await ProfilesUtils.isUsingTokenAuth(label); if (tokenError.includes("Token is not valid or expired.") || isTokenAuth) { - if (isTheia()) { + if (globals.ISTHEIA) { Gui.errorMessage(errToken); await Profiles.getInstance().ssoLogin(null, label); return; @@ -93,7 +93,7 @@ export async function errorHandling(errorDetails: Error | string, label?: string } } - if (isTheia()) { + if (globals.ISTHEIA) { Gui.errorMessage(errMsg); return; } @@ -121,17 +121,6 @@ export async function errorHandling(errorDetails: Error | string, label?: string Gui.errorMessage(moreInfo + errorDetails.toString().replace(/\n/g, " | ")); } -// TODO: remove this second occurence -export function isTheia(): boolean { - ZoweLogger.trace("ProfileUtils.isTheia called."); - const VSCODE_APPNAME: string[] = ["Visual Studio Code", "VSCodium"]; - const appName = vscode.env.appName; - if (appName && !VSCODE_APPNAME.includes(appName)) { - return true; - } - return false; -} - /** * Function to update session and profile information in provided node * @param profiles is data source to find profiles From a57856c7a653a2e22513c91bc4215abda7874634 Mon Sep 17 00:00:00 2001 From: Trae Yelovich Date: Wed, 10 Jan 2024 15:01:43 -0500 Subject: [PATCH 2/2] fix: broken unit tests, update existing theia tests Signed-off-by: Trae Yelovich --- .../__tests__/__unit__/utils.unit.test.ts | 16 ++++++---------- .../__unit__/utils/ProfilesUtils.unit.test.ts | 17 +++++++---------- 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/packages/zowe-explorer/__tests__/__unit__/utils.unit.test.ts b/packages/zowe-explorer/__tests__/__unit__/utils.unit.test.ts index ca4a59e7f4..5043a60e6c 100644 --- a/packages/zowe-explorer/__tests__/__unit__/utils.unit.test.ts +++ b/packages/zowe-explorer/__tests__/__unit__/utils.unit.test.ts @@ -20,7 +20,7 @@ import { ZoweLogger } from "../../src/utils/LoggerUtils"; function createGlobalMocks() { const globalMocks = { - isTheia: jest.fn(), + isTheia: jest.fn().mockReturnValue(false), testProfileLoaded: createValidIProfile(), mockProfileInstance: null, mockProfileInfo: createInstanceOfProfileInfo(), @@ -28,7 +28,6 @@ function createGlobalMocks() { }; globalMocks.mockProfileInstance = createInstanceOfProfile(globalMocks.testProfileLoaded); - const isTheia = jest.fn(); Object.defineProperty(vscode.window, "showQuickPick", { value: jest.fn(), configurable: true }); Object.defineProperty(vscode.window, "createQuickPick", { value: jest.fn(), configurable: true }); @@ -42,10 +41,9 @@ function createGlobalMocks() { .mockReturnValue(globalMocks.mockProfileInstance), configurable: true, }); - Object.defineProperty(globals, "ISTHEIA", { get: isTheia, configurable: true }); + Object.defineProperty(globals, "ISTHEIA", { get: globalMocks.isTheia, configurable: true }); Object.defineProperty(globals, "LOG", { value: jest.fn(), configurable: true }); Object.defineProperty(globals.LOG, "error", { value: jest.fn(), configurable: true }); - Object.defineProperty(utils, "isTheia", { value: jest.fn(), configurable: true }); Object.defineProperty(globalMocks.mockProfilesCache, "getProfileInfo", { value: jest.fn(() => { @@ -55,9 +53,7 @@ function createGlobalMocks() { Object.defineProperty(ZoweLogger, "error", { value: jest.fn(), configurable: true }); Object.defineProperty(ZoweLogger, "trace", { value: jest.fn(), configurable: true }); - return { - isTheia, - }; + return globalMocks; } // Idea is borrowed from: https://github.com/kulshekhar/ts-jest/blob/master/src/util/testing.ts @@ -112,10 +108,11 @@ describe("Utils Unit Tests - Function errorHandling", () => { }); it("Checking common error handling - Theia", async () => { const blockMocks = createBlockMocks(); + const globalMocks = createGlobalMocks(); + globalMocks.isTheia.mockReturnValue(true); mocked(Profiles.getInstance).mockReturnValue(blockMocks.profile); mocked(vscode.window.showErrorMessage).mockResolvedValueOnce({ title: "Update Credentials" }); - jest.spyOn(utils, "isTheia").mockReturnValue(true); const errorDetails = new imperative.ImperativeError({ msg: "Invalid credentials", errorCode: 401 as unknown as string, @@ -127,8 +124,7 @@ describe("Utils Unit Tests - Function errorHandling", () => { // TODO: check why this return two messages? expect(vscode.window.showErrorMessage).toHaveBeenCalledWith( `Invalid Credentials for profile '${label}'. Please ensure the username and password are valid or this may lead to a lock-out.`, - { modal: true }, - "Update Credentials" + undefined // covers undefined label in function ); }); }); diff --git a/packages/zowe-explorer/__tests__/__unit__/utils/ProfilesUtils.unit.test.ts b/packages/zowe-explorer/__tests__/__unit__/utils/ProfilesUtils.unit.test.ts index 6bd720ab07..23776fffb7 100644 --- a/packages/zowe-explorer/__tests__/__unit__/utils/ProfilesUtils.unit.test.ts +++ b/packages/zowe-explorer/__tests__/__unit__/utils/ProfilesUtils.unit.test.ts @@ -146,7 +146,6 @@ describe("ProfilesUtils unit tests", () => { }); const label = "test"; const moreInfo = "Task failed successfully"; - jest.spyOn(profUtils, "isTheia").mockReturnValue(false); const showMessageSpy = jest.spyOn(Gui, "errorMessage").mockImplementation(() => Promise.resolve("Update Credentials")); const promptCredsSpy = jest.fn(); jest.spyOn(Profiles, "getInstance").mockReturnValue({ @@ -162,7 +161,7 @@ describe("ProfilesUtils unit tests", () => { showMessageSpy.mockClear(); promptCredsSpy.mockClear(); }); - it("should handle token error and procede to login", async () => { + it("should handle token error and proceed to login", async () => { const errorDetails = new zowe.imperative.ImperativeError({ msg: "Invalid credentials", errorCode: 401 as unknown as string, @@ -170,7 +169,6 @@ describe("ProfilesUtils unit tests", () => { }); const label = "test"; const moreInfo = "Task failed successfully"; - jest.spyOn(profUtils, "isTheia").mockReturnValue(false); const showErrorSpy = jest.spyOn(Gui, "errorMessage"); const showMessageSpy = jest.spyOn(Gui, "showMessage").mockImplementation(() => Promise.resolve("selection")); const ssoLoginSpy = jest.fn(); @@ -189,7 +187,7 @@ describe("ProfilesUtils unit tests", () => { showMessageSpy.mockClear(); ssoLoginSpy.mockClear(); }); - it("should handle token error and procede to login - Theia", async () => { + it("should handle token error and proceed to login - Theia", async () => { const errorDetails = new zowe.imperative.ImperativeError({ msg: "Invalid credentials", errorCode: String(401), @@ -197,11 +195,8 @@ describe("ProfilesUtils unit tests", () => { }); const label = "test"; const moreInfo = "Task failed successfully"; - Object.defineProperty(vscode, "env", { - value: { - appName: "Theia", - }, - configurable: true, + Object.defineProperty(globals, "ISTHEIA", { + value: true, }); const showErrorSpy = jest.spyOn(Gui, "errorMessage").mockImplementation(() => Promise.resolve(undefined)); const showMessageSpy = jest.spyOn(Gui, "showMessage"); @@ -220,6 +215,9 @@ describe("ProfilesUtils unit tests", () => { showErrorSpy.mockClear(); showMessageSpy.mockClear(); ssoLoginSpy.mockClear(); + Object.defineProperty(globals, "ISTHEIA", { + value: false, + }); }); it("should handle credential error and no selection made for update", async () => { const errorDetails = new zowe.imperative.ImperativeError({ @@ -261,7 +259,6 @@ describe("ProfilesUtils unit tests", () => { }); const label = "test"; const moreInfo = "Task failed successfully"; - jest.spyOn(profUtils, "isTheia").mockReturnValue(true); const showErrorSpy = jest.spyOn(Gui, "errorMessage"); const promptCredentialsSpy = jest.fn(); jest.spyOn(Profiles, "getInstance").mockReturnValue({