Skip to content

Commit

Permalink
Merge pull request #2192 from zowe/output-logger
Browse files Browse the repository at this point in the history
Zowe Explorer Output Logger
  • Loading branch information
JillieBeanSim authored Apr 4, 2023
2 parents a8e9479 + 2c04788 commit 0fd2473
Show file tree
Hide file tree
Showing 88 changed files with 1,573 additions and 576 deletions.
2 changes: 2 additions & 0 deletions packages/zowe-explorer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ All notable changes to the "vscode-extension-for-zowe" extension will be documen

### New features and enhancements

- Added a new Zowe Explorer setting, `zowe.logger`, with a default setting of `INFO`.
- Added an output channel, `Zowe Explorer`, for logging within VS Code's Output view. The log level is set by the new Zowe Explorer setting, `zowe.logger`.
- Opening a dialog for Upload or Download of files will now open at the project level directory or the user's home directory if no project is opened. [#2203](https://github.com/zowe/vscode-extension-for-zowe/issues/2203)
- Updated linter rules and addressed linter errors throughout the codebase. [#2184](https://github.com/zowe/vscode-extension-for-zowe/issues/2184)

Expand Down
13 changes: 13 additions & 0 deletions packages/zowe-explorer/__mocks__/mockCreators/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -501,3 +501,16 @@ const originalGetDirectValue = SettingsConfig.getDirectValue;
export function createGetConfigMock(settings: { [key: string]: any }) {
return jest.fn((key: string) => settings[key] ?? originalGetDirectValue(key));
}

export function createOutputChannel() {
return {
append: jest.fn(),
name: "Zowe Explorer",
appendLine: jest.fn(),
clear: jest.fn(),
show: jest.fn(),
hide: jest.fn(),
dispose: jest.fn(),
replace: jest.fn(),
} as vscode.OutputChannel;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
*/

import { PersistentFilters } from "../../src/PersistentFilters";
import { ZoweLogger } from "../../src/utils/LoggerUtils";

describe("PersistentFilters Unit Test", () => {
Object.defineProperty(ZoweLogger, "trace", { value: jest.fn(), configurable: true });
describe("addSearchHistory()", () => {
it("should pop search history if history length is larger than max length", () => {
const pf: PersistentFilters = new PersistentFilters("", 1, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { createUSSNode, createUSSSessionNode, createUSSTree } from "../../__mock
import { createIJobObject, createJobsTree } from "../../__mocks__/mockCreators/jobs";
import * as path from "path";
import { SettingsConfig } from "../../src/utils/SettingsConfig";
import { ZoweLogger } from "../../src/utils/LoggerUtils";

jest.mock("child_process");
jest.mock("fs");
Expand Down Expand Up @@ -106,12 +107,10 @@ async function createGlobalMocks() {
});
Object.defineProperty(globals, "LOG", { value: newMocks.mockLog, configurable: true });
Object.defineProperty(vscode.window, "createInputBox", { value: newMocks.mockCreateInputBox, configurable: true });
Object.defineProperty(globals.LOG, "debug", { value: newMocks.mockDebug, configurable: true });
Object.defineProperty(zowe.ZosmfSession, "createSessCfgFromArgs", {
value: newMocks.mockCreateSessCfgFromArgs,
configurable: true,
});
Object.defineProperty(globals.LOG, "error", { value: newMocks.mockError, configurable: true });
Object.defineProperty(globals, "ISTHEIA", { get: () => false, configurable: true });
Object.defineProperty(vscode.window, "createTreeView", { value: jest.fn(), configurable: true });
Object.defineProperty(vscode.workspace, "getConfiguration", {
Expand All @@ -124,6 +123,11 @@ async function createGlobalMocks() {
});
Object.defineProperty(vscode, "ProgressLocation", { value: newMocks.ProgressLocation, configurable: true });
Object.defineProperty(vscode.window, "withProgress", { value: newMocks.withProgress, configurable: true });
Object.defineProperty(ZoweLogger, "error", { value: jest.fn(), configurable: true });
Object.defineProperty(ZoweLogger, "debug", { value: jest.fn(), configurable: true });
Object.defineProperty(ZoweLogger, "warn", { value: jest.fn(), configurable: true });
Object.defineProperty(ZoweLogger, "info", { value: jest.fn(), configurable: true });
Object.defineProperty(ZoweLogger, "trace", { value: jest.fn(), configurable: true });

newMocks.mockProfileInstance = new Profiles(newMocks.log);
Object.defineProperty(Profiles, "CreateInstance", {
Expand Down Expand Up @@ -251,7 +255,7 @@ describe("Profiles Unit Tests - Function createNewConnection for v1 Profiles", (

await Profiles.getInstance().createNewConnection("");
expect(globalMocks.mockShowInformationMessage.mock.calls.length).toBe(1);
expect(globalMocks.mockShowInformationMessage.mock.calls[0][0]).toBe("Profile name was not supplied. Operation Cancelled");
expect(globalMocks.mockShowInformationMessage.mock.calls[0][0]).toBe("Operation Cancelled");
});

it("Tests that createNewConnection fails if profileType is missing", async () => {
Expand All @@ -260,7 +264,7 @@ describe("Profiles Unit Tests - Function createNewConnection for v1 Profiles", (

await Profiles.getInstance().createNewConnection(globalMocks.testProfile.name, undefined);
expect(globalMocks.mockShowInformationMessage.mock.calls.length).toBe(1);
expect(globalMocks.mockShowInformationMessage.mock.calls[0][0]).toBe("No profile type was chosen. Operation Cancelled");
expect(globalMocks.mockShowInformationMessage.mock.calls[0][0]).toBe("Operation Cancelled");
});

it("Tests that createNewConnection fails if zOSMF URL is missing", async () => {
Expand All @@ -272,7 +276,7 @@ describe("Profiles Unit Tests - Function createNewConnection for v1 Profiles", (

await Profiles.getInstance().createNewConnection(globalMocks.testProfile.name, "zosmf");
expect(globalMocks.mockShowInformationMessage.mock.calls.length).toBe(1);
expect(globalMocks.mockShowInformationMessage.mock.calls[0][0]).toBe("No valid value for z/OS URL. Operation Cancelled");
expect(globalMocks.mockShowInformationMessage.mock.calls[0][0]).toBe("Operation Cancelled");
});

it("Tests that createNewConnection fails if user escapes create at username", async () => {
Expand Down Expand Up @@ -535,15 +539,19 @@ describe("Profiles Unit Tests - Function createZoweSession", () => {
const globalMocks = await createGlobalMocks();
const blockMocks = await createBlockMocks(globalMocks);
const spy = jest.spyOn(Gui, "createQuickPick");
const spyDebug = jest.spyOn(ZoweLogger, "debug");
jest.spyOn(Gui, "resolveQuickPick").mockResolvedValueOnce(undefined);
await Profiles.getInstance().createZoweSession(blockMocks.testDatasetTree);
expect(spy).toBeCalled();
expect(globalMocks.mockShowInformationMessage.mock.calls[0][0]).toBe("No selection made. Operation cancelled.");
expect(globalMocks.mockShowInformationMessage.mock.calls[0][0]).toBe("Profile selection has been cancelled.");
expect(spyDebug).toBeCalledWith("Profile selection has been cancelled.");
spy.mockClear();
spyDebug.mockClear();
});

it("Tests that createZoweSession runs successfully", async () => {
const globalMocks = await createGlobalMocks();
const spyInfo = jest.spyOn(ZoweLogger, "info");
jest.spyOn(Gui, "createQuickPick").mockReturnValue({
show: jest.fn(),
hide: jest.fn(),
Expand All @@ -558,6 +566,25 @@ describe("Profiles Unit Tests - Function createZoweSession", () => {
const refreshSpy = jest.spyOn(Profiles.getInstance(), "refresh").mockImplementation();
await expect(Profiles.getInstance().createZoweSession(globalMocks.testUSSTree)).resolves.not.toThrow();
expect(refreshSpy).toBeCalledTimes(1);
expect(spyInfo).toBeCalledWith("New profile created, test.");
refreshSpy.mockClear();
spyInfo.mockClear();
});

it("Tests that createZoweSession catches error and log warning", async () => {
const globalMocks = await createGlobalMocks();
jest.spyOn(Gui, "createQuickPick").mockReturnValue({
show: jest.fn(),
hide: jest.fn(),
value: "test",
} as any);
jest.spyOn(Gui, "resolveQuickPick").mockResolvedValueOnce(new utils.FilterDescriptor("Test"));
jest.spyOn(Profiles.getInstance(), "getProfileInfo").mockRejectedValueOnce(new Error("test error"));
const warnSpy = jest.spyOn(ZoweLogger, "warn");
await expect(Profiles.getInstance().createZoweSession(globalMocks.testUSSTree)).resolves.not.toThrow();
expect(warnSpy).toBeCalledTimes(1);
expect(warnSpy).toBeCalledWith(Error("test error"));
warnSpy.mockClear();
});
});

Expand Down Expand Up @@ -1431,6 +1458,7 @@ describe("Profiles Unit Tests - function getProfileSetting", () => {
describe("Profiles Unit Tests - function disableValidationContext", () => {
it("should disable validation context and return updated node", async () => {
const globalMocks = await createGlobalMocks();
const spy = jest.spyOn(ZoweLogger, "trace");
const testNode = new (ZoweTreeNode as any)(
"test",
vscode.TreeItemCollapsibleState.None,
Expand All @@ -1442,12 +1470,15 @@ describe("Profiles Unit Tests - function disableValidationContext", () => {
const expectNode = testNode;
expectNode.contextValue = globals.VALIDATE_SUFFIX + "false";
await expect(Profiles.getInstance().disableValidationContext(testNode)).toEqual(expectNode);
expect(spy).toBeCalled();
spy.mockClear();
});
});

describe("Profiles Unit Tests - function enableValidationContext", () => {
it("should enable validation context and return updated node", async () => {
const globalMocks = await createGlobalMocks();
const spy = jest.spyOn(ZoweLogger, "trace");
const testNode = new (ZoweTreeNode as any)(
"test",
vscode.TreeItemCollapsibleState.None,
Expand All @@ -1459,6 +1490,8 @@ describe("Profiles Unit Tests - function enableValidationContext", () => {
const expectedNode = testNode;
expectedNode.contextValue = globals.VALIDATE_SUFFIX + "true";
await expect(Profiles.getInstance().enableValidationContext(testNode)).toEqual(expectedNode);
expect(spy).toBeCalled();
spy.mockClear();
});
});

Expand Down Expand Up @@ -1486,7 +1519,6 @@ describe("Profiles Unit Tests - function ssoLogin", () => {
});
jest.spyOn(Gui, "showMessage").mockImplementation();
});

it("should perform an SSOLogin successfully while fetching the base profile", async () => {
jest.spyOn(ZoweExplorerApiRegister.getInstance(), "getCommonApi").mockReturnValue({
getTokenTypeName: () => zowe.imperative.SessConstants.TOKEN_TYPE_APIML,
Expand All @@ -1496,7 +1528,6 @@ describe("Profiles Unit Tests - function ssoLogin", () => {
jest.spyOn(Profiles.getInstance() as any, "updateBaseProfileFileLogin").mockImplementation();
await expect(Profiles.getInstance().ssoLogin(testNode, "fake")).resolves.not.toThrow();
});

it("should perform an SSOLogin successfully while fetching from session", async () => {
jest.spyOn(ZoweExplorerApiRegister.getInstance(), "getCommonApi").mockReturnValue({
getTokenTypeName: () => "tokenType",
Expand All @@ -1506,6 +1537,31 @@ describe("Profiles Unit Tests - function ssoLogin", () => {
jest.spyOn(Profiles.getInstance() as any, "loginCredentialPrompt").mockReturnValue(["fake", "12345"]);
await expect(Profiles.getInstance().ssoLogin(testNode, "fake")).resolves.not.toThrow();
});
it("should catch error getting token type and log warning", async () => {
jest.spyOn(ZoweExplorerApiRegister.getInstance(), "getCommonApi").mockReturnValueOnce({
getTokenTypeName: () => {
throw new Error("test error.");
},
login: () => "ajshdlfkjshdalfjhas",
} as never);
const warnSpy = jest.spyOn(ZoweLogger, "warn");
await expect(Profiles.getInstance().ssoLogin(testNode, "fake")).resolves.not.toThrow();
expect(warnSpy).toBeCalledWith(Error("test error."));
warnSpy.mockClear();
});
it("should catch error during login and log error", async () => {
jest.spyOn(ZoweExplorerApiRegister.getInstance(), "getCommonApi").mockReturnValueOnce({
getTokenTypeName: () => zowe.imperative.SessConstants.TOKEN_TYPE_APIML,
login: () => {
throw new Error("test error.");
},
} as never);
jest.spyOn(Profiles.getInstance() as any, "loginCredentialPrompt").mockReturnValue(["fake", "12345"]);
const errorSpy = jest.spyOn(ZoweLogger, "error");
await expect(Profiles.getInstance().ssoLogin(testNode, "fake")).resolves.not.toThrow();
expect(errorSpy).toBeCalled();
errorSpy.mockClear();
});
});

describe("Profiles Unit Tests - function updateBaseProfileFileLogin", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import * as spoolprovider from "../../src/SpoolProvider";
import * as zowe from "@zowe/cli";
import * as vscode from "vscode";
import { Profiles } from "../../src/Profiles";
import { ZoweLogger } from "../../src/utils/LoggerUtils";

describe("SpoolProvider Unit Tests", () => {
const iJobFile: zowe.IJobFile = {
Expand Down Expand Up @@ -78,6 +79,7 @@ describe("SpoolProvider Unit Tests", () => {
};
}),
});
Object.defineProperty(ZoweLogger, "trace", { value: jest.fn(), configurable: true });

afterEach(() => {
jest.resetAllMocks();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import * as path from "path";
import * as fs from "fs";
import { getZoweDir, Gui } from "@zowe/zowe-explorer-api";
import * as profilesUtils from "../../src/utils/ProfilesUtils";
import * as globals from "../../src/globals";
import { ZoweLogger } from "../../src/utils/LoggerUtils";
jest.mock("fs");

describe("ZoweExplorerExtender unit tests", () => {
Expand Down Expand Up @@ -66,8 +66,18 @@ describe("ZoweExplorerExtender unit tests", () => {
value: newMocks.mockGetConfiguration,
configurable: true,
});
Object.defineProperty(globals, "LOG", { value: jest.fn(), configurable: true });
Object.defineProperty(globals.LOG, "error", { value: jest.fn(), configurable: true });
Object.defineProperty(ZoweLogger, "warn", {
value: jest.fn(),
configurable: true,
});
Object.defineProperty(ZoweLogger, "error", {
value: jest.fn(),
configurable: true,
});
Object.defineProperty(ZoweLogger, "trace", {
value: jest.fn(),
configurable: true,
});

return newMocks;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { ZoweDatasetNode } from "../../src/dataset/ZoweDatasetNode";
import { List, imperative } from "@zowe/cli";
import { Profiles } from "../../src/Profiles";
import * as globals from "../../src/globals";
import { ZoweLogger } from "../../src/utils/LoggerUtils";

describe("Unit Tests (Jest)", () => {
// Globals
Expand Down Expand Up @@ -48,6 +49,8 @@ describe("Unit Tests (Jest)", () => {
Object.defineProperty(globals.LOG, "error", { value: jest.fn(), configurable: true });
Object.defineProperty(vscode, "ProgressLocation", { value: ProgressLocation });
Object.defineProperty(vscode.window, "withProgress", { value: withProgress });
Object.defineProperty(ZoweLogger, "error", { value: jest.fn(), configurable: true });
Object.defineProperty(ZoweLogger, "trace", { value: jest.fn(), configurable: true });

beforeEach(() => {
withProgress.mockImplementation((progLocation, callback) => {
Expand Down
Loading

0 comments on commit 0fd2473

Please sign in to comment.