Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ZE activation and commands failing when team config contains JSON error #3075

Merged
merged 3 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/zowe-explorer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ All notable changes to the "vscode-extension-for-zowe" extension will be documen

- Fixed issue where creating a new team configuration file could cause Zowe Explorer to crash, resulting in all sessions disappearing from trees. [#2906](https://github.com/zowe/zowe-explorer-vscode/issues/2906)
- Fixed data set not opening when the token has expired. [#3001](https://github.com/zowe/zowe-explorer-vscode/issues/3001)
- Fixed JSON errors being ignored when `zowe.config.json` files change on disk and are reloaded. [#3066](https://github.com/zowe/zowe-explorer-vscode/issues/3066)
- Fixed JSON errors being ignored when `zowe.config.json` files change on disk and are reloaded. [#3066](https://github.com/zowe/zowe-explorer-vscode/issues/3066) [#3074](https://github.com/zowe/zowe-explorer-vscode/issues/3074)

## `2.17.0`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,6 @@ async function createGlobalMocks() {
Object.defineProperty(ZoweLogger, "trace", { value: jest.fn(), configurable: true });

newMocks.mockProfileInstance = new Profiles(newMocks.log);
Object.defineProperty(Profiles, "CreateInstance", {
value: () => newMocks.mockProfileInstance,
configurable: true,
});
Object.defineProperty(Profiles, "getInstance", {
value: () => newMocks.mockProfileInstance,
configurable: true,
Expand Down Expand Up @@ -250,6 +246,17 @@ describe("Profiles Unit Test - Function createInstance", () => {
expect(mockWorkspaceFolders).toHaveBeenCalledTimes(1);
expect(profilesInstance.cwd).toBe("fakePath");
});

it("Tests that createInstance catches error and logs it", async () => {
const globalMocks = await createGlobalMocks();
jest.spyOn(Profiles.prototype, "refresh").mockRejectedValueOnce(new Error("test error"));
jest.spyOn(Gui, "errorMessage").mockResolvedValueOnce("");
const errorSpy = jest.spyOn(ZoweLogger, "error");
await expect(Profiles.createInstance(globalMocks.log)).resolves.not.toThrow();
expect(errorSpy).toBeCalledTimes(1);
expect(errorSpy).toBeCalledWith(Error("test error"));
errorSpy.mockClear();
});
});

describe("Profiles Unit Tests - Function createNewConnection for v1 Profiles", () => {
Expand Down Expand Up @@ -583,6 +590,7 @@ describe("Profiles Unit Tests - Function createZoweSession", () => {
jest.spyOn(Gui, "resolveQuickPick").mockResolvedValueOnce(new utils.FilterDescriptor("Test1"));
jest.spyOn(Gui, "resolveQuickPick").mockResolvedValueOnce(new utils.FilterDescriptor("Test2"));
jest.spyOn(Profiles.getInstance(), "getProfileInfo").mockResolvedValue({
getAllProfiles: jest.fn().mockReturnValue([]),
usingTeamConfig: false,
} as any);
jest.spyOn(Gui, "showInputBox").mockResolvedValue("test");
Expand Down Expand Up @@ -617,7 +625,7 @@ describe("Profiles Unit Tests - Function createZoweSession", () => {
spyInfo.mockClear();
});

it("Tests that createZoweSession catches error and log warning", async () => {
it("Tests that createZoweSession catches error and logs it", async () => {
const globalMocks = await createGlobalMocks();
jest.spyOn(Gui, "createQuickPick").mockReturnValue({
show: jest.fn(),
Expand All @@ -626,11 +634,12 @@ describe("Profiles Unit Tests - Function createZoweSession", () => {
} 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");
jest.spyOn(Gui, "errorMessage").mockResolvedValueOnce("");
const errorSpy = jest.spyOn(ZoweLogger, "error");
await expect(Profiles.getInstance().createZoweSession(globalMocks.testUSSTree)).resolves.not.toThrow();
expect(warnSpy).toBeCalledTimes(1);
expect(warnSpy).toBeCalledWith(Error("test error"));
warnSpy.mockClear();
expect(errorSpy).toBeCalledTimes(1);
expect(errorSpy).toBeCalledWith(Error("test error"));
errorSpy.mockClear();
});
});

Expand Down Expand Up @@ -1099,6 +1108,17 @@ describe("Profiles Unit Tests - function promptCredentials", () => {
jest.spyOn(Profiles.getInstance(), "updateProfilesArrays").mockImplementation();
await expect(Profiles.getInstance().promptCredentials("secure_config_props")).resolves.toEqual(["test", "12345", "encodedAuth"]);
});

it("Tests that promptCredentials catches error and logs it", async () => {
const globalMocks = await createGlobalMocks();
jest.spyOn(Profiles.getInstance(), "getProfileInfo").mockRejectedValueOnce(new Error("test error"));
jest.spyOn(Gui, "errorMessage").mockResolvedValueOnce("");
const errorSpy = jest.spyOn(ZoweLogger, "error");
await expect(Profiles.getInstance().promptCredentials(globalMocks.testProfile)).resolves.not.toThrow();
expect(errorSpy).toBeCalledTimes(1);
expect(errorSpy).toBeCalledWith(Error("test error"));
errorSpy.mockClear();
});
});

describe("Profiles Unit Tests - function getDeleteProfile", () => {
Expand Down Expand Up @@ -1342,6 +1362,24 @@ describe("Profiles Unit Tests - function deleteProfile", () => {

await expect(Profiles.getInstance().deleteProfile(datasetTree, ussTree, jobsTree, testNode)).resolves.not.toThrow();
});

it("Tests that deleteProfile catches error and logs it", async () => {
const globalMocks = await createGlobalMocks();
const datasetSessionNode = createDatasetSessionNode(globalMocks.testSession, globalMocks.testProfile);
const datasetTree = createDatasetTree(datasetSessionNode, globalMocks.testProfile);
const ussSessionNode = [createUSSSessionNode(globalMocks.testSession, globalMocks.testProfile)];
const ussTree = createUSSTree([], ussSessionNode);
const jobsTree = createJobsTree(globalMocks.testSession, createIJobObject(), globalMocks.testProfile, createTreeView());

jest.spyOn(Profiles.getInstance(), "getProfileInfo").mockRejectedValueOnce(new Error("test error"));
jest.spyOn(Profiles.getInstance(), "getDeleteProfile").mockResolvedValue(globalMocks.testProfile);
jest.spyOn(Gui, "errorMessage").mockResolvedValueOnce("");
const errorSpy = jest.spyOn(ZoweLogger, "error");
await expect(Profiles.getInstance().deleteProfile(datasetTree, ussTree, jobsTree)).resolves.not.toThrow();
expect(errorSpy).toBeCalledTimes(1);
expect(errorSpy).toBeCalledWith(Error("test error"));
errorSpy.mockClear();
});
});

describe("Profiles Unit Tests - function checkCurrentProfile", () => {
Expand Down Expand Up @@ -1491,6 +1529,17 @@ describe("Profiles Unit Tests - function editSession", () => {
base64EncodedAuth: "base64Auth",
});
});

it("Tests that editSession catches error and logs it", async () => {
const globalMocks = await createGlobalMocks();
jest.spyOn(Profiles.getInstance(), "getProfileInfo").mockRejectedValueOnce(new Error("test error"));
jest.spyOn(Gui, "errorMessage").mockResolvedValueOnce("");
const errorSpy = jest.spyOn(ZoweLogger, "error");
await expect(Profiles.getInstance().editSession(globalMocks.testProfile, globalMocks.testProfile.name)).resolves.not.toThrow();
expect(errorSpy).toBeCalledTimes(1);
expect(errorSpy).toBeCalledWith(Error("test error"));
errorSpy.mockClear();
});
});

describe("Profiles Unit Tests - function getProfileSetting", () => {
Expand Down
52 changes: 42 additions & 10 deletions packages/zowe-explorer/src/Profiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@
// Processing stops if there are no profiles detected
public static async createInstance(log: zowe.imperative.Logger): Promise<Profiles> {
Profiles.loader = new Profiles(log, vscode.workspace.workspaceFolders?.[0]?.uri.fsPath);
await Profiles.loader.refresh(ZoweExplorerApiRegister.getInstance());
try {
await Profiles.loader.refresh(ZoweExplorerApiRegister.getInstance());
} catch (err) {
ZoweLogger.error(err);
ZoweExplorerExtender.showZoweConfigError(err.message);
}
return Profiles.loader;
}

Expand Down Expand Up @@ -337,13 +342,15 @@
let mProfileInfo: zowe.imperative.ProfileInfo;
try {
mProfileInfo = await this.getProfileInfo();
const profAllAttrs = mProfileInfo.getAllProfiles();
for (const pName of profileNamesList) {
const osLocInfo = mProfileInfo.getOsLocInfo(profAllAttrs.find((p) => p.profName === pName));
items.push(new FilterItem({ text: pName, icon: this.getProfileIcon(osLocInfo)[0] }));
}
} catch (err) {
ZoweLogger.warn(err);
ZoweLogger.error(err);
ZoweExplorerExtender.showZoweConfigError(err.message);
return;
}
const profAllAttrs = mProfileInfo.getAllProfiles();
for (const pName of profileNamesList) {
const osLocInfo = mProfileInfo.getOsLocInfo(profAllAttrs.find((p) => p.profName === pName));
items.push(new FilterItem({ text: pName, icon: this.getProfileIcon(osLocInfo)[0] }));

Check warning on line 353 in packages/zowe-explorer/src/Profiles.ts

View check run for this annotation

Codecov / codecov/patch

packages/zowe-explorer/src/Profiles.ts#L352-L353

Added lines #L352 - L353 were not covered by tests
}

const quickpick = Gui.createQuickPick();
Expand Down Expand Up @@ -406,6 +413,7 @@
} catch (error) {
ZoweLogger.error(error);
ZoweExplorerExtender.showZoweConfigError(error.message);
return;

Check warning on line 416 in packages/zowe-explorer/src/Profiles.ts

View check run for this annotation

Codecov / codecov/patch

packages/zowe-explorer/src/Profiles.ts#L416

Added line #L416 was not covered by tests
}
if (config.usingTeamConfig) {
const profiles = config.getAllProfiles();
Expand Down Expand Up @@ -468,7 +476,15 @@

public async editSession(profileLoaded: zowe.imperative.IProfileLoaded, profileName: string): Promise<any | undefined> {
ZoweLogger.trace("Profiles.editSession called.");
if ((await this.getProfileInfo()).usingTeamConfig) {
let usingTeamConfig: boolean;
try {
usingTeamConfig = (await this.getProfileInfo()).usingTeamConfig;
} catch (err) {
ZoweLogger.error(err);
ZoweExplorerExtender.showZoweConfigError(err.message);
return;
}
if (usingTeamConfig) {
const currentProfile = await this.getProfileFromConfig(profileLoaded.name);
const filePath = currentProfile.profLoc.osLoc[0];
await this.openConfigFile(filePath);
Expand Down Expand Up @@ -926,13 +942,21 @@
placeHolder: localize("promptCredentials.passwordInputBoxOptions.placeholder", "Password"),
prompt: localize("promptCredentials.passwordInputBoxOptions.prompt", "Enter the password for the connection. Leave blank to not store."),
};
let mProfileInfo: zowe.imperative.ProfileInfo;
try {
mProfileInfo = await this.getProfileInfo();
} catch (err) {
ZoweLogger.error(err);
ZoweExplorerExtender.showZoweConfigError(err.message);
return;
}

const promptInfo = await ZoweVsCodeExtension.updateCredentials(
{
profile: typeof profile === "string" ? undefined : profile,
sessionName: typeof profile === "string" ? profile : undefined,
rePrompt,
secure: (await this.getProfileInfo()).isSecured(),
secure: mProfileInfo.isSecured(),
userInputBoxOptions,
passwordInputBoxOptions,
},
Expand Down Expand Up @@ -993,8 +1017,16 @@
}

const deleteLabel = deletedProfile.name;
let usingTeamConfig: boolean;
try {
usingTeamConfig = (await this.getProfileInfo()).usingTeamConfig;
} catch (err) {
ZoweLogger.error(err);
ZoweExplorerExtender.showZoweConfigError(err.message);
return;
}

if ((await this.getProfileInfo()).usingTeamConfig) {
if (usingTeamConfig) {
const currentProfile = await this.getProfileFromConfig(deleteLabel);
const filePath = currentProfile.profLoc.osLoc[0];
await this.openConfigFile(filePath);
Expand Down
Loading