Skip to content

Commit

Permalink
Fix extra tree refresh on credential update
Browse files Browse the repository at this point in the history
Signed-off-by: Timothy Johnson <timothy.johnson@broadcom.com>
  • Loading branch information
t1m0thyj committed Oct 7, 2024
1 parent a8ef66f commit 23c7fa1
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3314,7 +3314,6 @@ describe("Dataset Tree Unit Tests - Function applyPatternsToChildren", () => {
const withProfileMock = jest.spyOn(SharedContext, "withProfile").mockImplementation((child) => String(child.contextValue));
testTree.applyPatternsToChildren(fakeChildren as any[], [{ dsn: "HLQ.PROD.PDS", member: "A*" }], fakeSessionNode as any);
expect(SharedContext.isFilterFolder(fakeChildren[0])).toBe(true);
expect(fakeSessionNode.dirty).toBe(true);
withProfileMock.mockRestore();
});
it("applies a closed filter folder icon to the PDS if collapsed", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,8 @@ describe("Test src/shared/extension", () => {
describe("watchConfigProfile", () => {
let context: any;
let watcherPromise: any;
const spyReadFile = jest.fn().mockReturnValue("test");
const fakeUri = { fsPath: "fsPath" };
const spyReadFile = jest.fn().mockReturnValue(Buffer.from("test"));
const mockEmitter = jest.fn();
const watcher: any = {
onDidCreate: jest.fn(),
Expand All @@ -333,9 +334,12 @@ describe("Test src/shared/extension", () => {
beforeEach(() => {
context = { subscriptions: [] };
jest.clearAllMocks();
Object.defineProperty(vscode.workspace, "workspaceFolders", { value: [{ uri: { fsPath: "fsPath" } }], configurable: true });
Object.defineProperty(vscode.workspace, "workspaceFolders", { value: [{ uri: fakeUri }], configurable: true });
Object.defineProperty(vscode.workspace, "fs", { value: { readFile: spyReadFile }, configurable: true });
Object.defineProperty(Constants, "SAVED_PROFILE_CONTENTS", { value: "test", configurable: true });
Object.defineProperty(Constants, "SAVED_PROFILE_CONTENTS", {
value: new Map(Object.entries({ [fakeUri.fsPath]: Buffer.from("test") })),
configurable: true,
});
jest.spyOn(vscode.workspace, "createFileSystemWatcher").mockReturnValue(watcher);
jest.spyOn(ZoweExplorerApiRegister.getInstance().onProfilesUpdateEmitter, "fire").mockImplementation(mockEmitter);
});
Expand Down Expand Up @@ -366,23 +370,23 @@ describe("Test src/shared/extension", () => {

it("should be able to trigger onDidChange listener", async () => {
const spyRefreshAll = jest.spyOn(SharedActions, "refreshAll").mockImplementation();
watcher.onDidChange.mockImplementationOnce((fun) => (watcherPromise = fun("uri")));
watcher.onDidChange.mockImplementationOnce((fun) => (watcherPromise = fun(fakeUri)));
SharedInit.watchConfigProfile(context);
await watcherPromise;
expect(context.subscriptions).toContain(watcher);
expect(spyReadFile).toHaveBeenCalledWith("uri");
expect(spyReadFile).toHaveBeenCalledWith(fakeUri);
expect(spyRefreshAll).not.toHaveBeenCalled();
expect(mockEmitter).not.toHaveBeenCalled();
});

it("should be able to trigger onDidChange listener with changes", async () => {
const spyRefreshAll = jest.spyOn(SharedActions, "refreshAll").mockImplementation();
spyReadFile.mockReturnValueOnce("other");
watcher.onDidChange.mockImplementationOnce((fun) => (watcherPromise = fun("uri")));
spyReadFile.mockReturnValueOnce(Buffer.from("other"));
watcher.onDidChange.mockImplementationOnce((fun) => (watcherPromise = fun(fakeUri)));
SharedInit.watchConfigProfile(context);
await watcherPromise;
expect(context.subscriptions).toContain(watcher);
expect(spyReadFile).toHaveBeenCalledWith("uri");
expect(spyReadFile).toHaveBeenCalledWith(fakeUri);
expect(spyRefreshAll).toHaveBeenCalledTimes(1);
expect(mockEmitter).toHaveBeenCalledTimes(1);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ describe("ProfilesUtils unit tests", () => {
const mockReadProfilesFromDisk = jest.fn();
const profInfoSpy = jest.spyOn(ProfilesUtils, "getProfileInfo").mockReturnValue({
readProfilesFromDisk: mockReadProfilesFromDisk,
getTeamConfig: () => ({ exists: true }),
getTeamConfig: () => ({ exists: true, layers: [] }),
} as never);
await expect(ProfilesUtils.readConfigFromDisk()).resolves.not.toThrow();
expect(mockReadProfilesFromDisk).toHaveBeenCalledTimes(1);
Expand Down
3 changes: 1 addition & 2 deletions packages/zowe-explorer/src/configuration/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { imperative, PersistenceSchemaEnum } from "@zowe/zowe-explorer-api";
import type { Profiles } from "./Profiles";

export class Constants {
public static CONFIG_PATH: string;
public static readonly COMMAND_COUNT = 99;
public static readonly MAX_SEARCH_HISTORY = 5;
public static readonly MAX_FILE_HISTORY = 10;
Expand Down Expand Up @@ -82,7 +81,7 @@ export class Constants {
public static DS_NAME_REGEX_CHECK = /^[a-zA-Z#@$][a-zA-Z0-9#@$-]{0,7}(\.[a-zA-Z#@$][a-zA-Z0-9#@$-]{0,7})*$/;
public static MEMBER_NAME_REGEX_CHECK = /^[a-zA-Z#@$][a-zA-Z0-9#@$]{0,7}$/;
public static ACTIVATED = false;
public static SAVED_PROFILE_CONTENTS = new Uint8Array();
public static SAVED_PROFILE_CONTENTS = new Map<string, Buffer>();
public static IGNORE_VAULT_CHANGE = false;
public static readonly JOBS_MAX_PREFIX = 8;
public static PROFILES_CACHE: Profiles;
Expand Down
24 changes: 11 additions & 13 deletions packages/zowe-explorer/src/trees/dataset/DatasetTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,13 +391,13 @@ export class DatasetTree extends ZoweTreeProvider<IZoweDatasetTreeNode> implemen
for (const favorite of favsForProfile) {
// If profile and session already exists for favorite node, add to updatedFavsForProfile and go to next array item
if (favorite.getProfile() && favorite.getSession()) {
updatedFavsForProfile.push(favorite as IZoweDatasetTreeNode);
updatedFavsForProfile.push(favorite);
continue;
}
// If no profile/session for favorite node yet, then add session and profile to favorite node:
favorite.setProfileToChoice(profile);
favorite.setSessionToChoice(session);
updatedFavsForProfile.push(favorite as IZoweDatasetTreeNode);
updatedFavsForProfile.push(favorite);
}
// This updates the profile node's children in the this.mFavorites array, as well.
return updatedFavsForProfile;
Expand Down Expand Up @@ -559,7 +559,7 @@ export class DatasetTree extends ZoweTreeProvider<IZoweDatasetTreeNode> implemen
if (matchingNode) {
matchingNode.label = afterLabel;
matchingNode.tooltip = afterLabel;
this.refreshElement(matchingNode as IZoweDatasetTreeNode);
this.refreshElement(matchingNode);
}
}
}
Expand Down Expand Up @@ -880,10 +880,10 @@ export class DatasetTree extends ZoweTreeProvider<IZoweDatasetTreeNode> implemen
if (session.children) {
for (const node of session.children) {
if (node.contextValue !== Constants.INFORMATION_CONTEXT) {
loadedItems.push(node as IZoweDatasetTreeNode);
loadedItems.push(node);
for (const member of node.children) {
if (member.contextValue !== Constants.INFORMATION_CONTEXT) {
loadedItems.push(member as IZoweDatasetTreeNode);
loadedItems.push(member);
}
}
}
Expand Down Expand Up @@ -971,16 +971,14 @@ export class DatasetTree extends ZoweTreeProvider<IZoweDatasetTreeNode> implemen
let setIcon: IconUtils.IIconItem;
if (child.collapsibleState === vscode.TreeItemCollapsibleState.Collapsed) {
setIcon = IconGenerator.getIconById(IconUtils.IconId.filterFolder);
}
if (child.collapsibleState === vscode.TreeItemCollapsibleState.Expanded) {
} else if (child.collapsibleState === vscode.TreeItemCollapsibleState.Expanded) {
setIcon = IconGenerator.getIconById(IconUtils.IconId.filterFolderOpen);
}
if (setIcon) {
child.iconPath = setIcon.path;
}
}
}
sessionNode.dirty = true;
const icon = IconGenerator.getIconByNode(sessionNode);
if (icon) {
sessionNode.iconPath = icon.path;
Expand Down Expand Up @@ -1264,7 +1262,7 @@ export class DatasetTree extends ZoweTreeProvider<IZoweDatasetTreeNode> implemen
}

c.children.sort(ZoweDatasetNode.sortBy(node.sort));
this.nodeDataChanged(c as IZoweDatasetTreeNode);
this.nodeDataChanged(c);
}
}
}
Expand Down Expand Up @@ -1377,7 +1375,7 @@ export class DatasetTree extends ZoweTreeProvider<IZoweDatasetTreeNode> implemen
if (node.children?.length > 0) {
// children nodes already exist, sort and repaint to avoid extra refresh
for (const c of node.children) {
const asDs = c as IZoweDatasetTreeNode;
const asDs = c;

// PDS-level filters should have precedence over a session-level filter
if (asDs.filter != null) {
Expand All @@ -1388,15 +1386,15 @@ export class DatasetTree extends ZoweTreeProvider<IZoweDatasetTreeNode> implemen
// If there was an old session-wide filter set: refresh to get any
// missing nodes - new filter will be applied
if (oldFilter != null) {
this.refreshElement(c as IZoweDatasetTreeNode);
this.refreshElement(c);
continue;
}

if (newFilter != null && c.children?.length > 0) {
c.children = c.children.filter(ZoweDatasetNode.filterBy(newFilter));
this.nodeDataChanged(c as IZoweDatasetTreeNode);
this.nodeDataChanged(c);
} else {
this.refreshElement(c as IZoweDatasetTreeNode);
this.refreshElement(c);
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions packages/zowe-explorer/src/trees/shared/SharedInit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import { SharedContext } from "./SharedContext";
import { TreeViewUtils } from "../../utils/TreeViewUtils";
import { CertificateWizard } from "../../utils/CertificateWizard";
import { ZosConsoleViewProvider } from "../../zosconsole/ZosConsolePanel";
import * as path from "path";

export class SharedInit {
private static originalEmitZoweEvent: typeof imperative.EventProcessor.prototype.emitEvent;
Expand Down Expand Up @@ -331,11 +332,12 @@ export class SharedInit {
});
watcher.onDidChange(async (uri: vscode.Uri) => {
ZoweLogger.info(vscode.l10n.t("Team config file updated."));
const newProfileContents = await vscode.workspace.fs.readFile(uri);
if (newProfileContents.toString() === Constants.SAVED_PROFILE_CONTENTS.toString()) {
const newProfileContents = Buffer.from(await vscode.workspace.fs.readFile(uri));
const normalizedPath = path.normalize(uri.fsPath);
if (Constants.SAVED_PROFILE_CONTENTS.get(normalizedPath)?.equals(newProfileContents)) {
return;
}
Constants.SAVED_PROFILE_CONTENTS = newProfileContents;
Constants.SAVED_PROFILE_CONTENTS.set(normalizedPath, newProfileContents);
void SharedActions.refreshAll();
ZoweExplorerApiRegister.getInstance().onProfilesUpdateEmitter.fire(Validation.EventType.UPDATE);
});
Expand Down
10 changes: 5 additions & 5 deletions packages/zowe-explorer/src/trees/uss/USSTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ export class USSTree extends ZoweTreeProvider<IZoweUSSTreeNode> implements Types
const children = nodeToCheck.children;
if (children.length !== 0) {
for (const child of children) {
await checkForChildren(child as IZoweUSSTreeNode);
await checkForChildren(child);
}
}
loadedNodes.push(nodeToCheck);
Expand Down Expand Up @@ -1011,13 +1011,13 @@ export class USSTree extends ZoweTreeProvider<IZoweUSSTreeNode> implements Types
for (const favorite of favsForProfile) {
// If profile and session already exists for favorite node, add to updatedFavsForProfile and go to next array item
if (favorite.getProfile() && favorite.getSession()) {
updatedFavsForProfile.push(favorite as IZoweUSSTreeNode);
updatedFavsForProfile.push(favorite);
continue;
}
// If no profile/session for favorite node yet, then add session and profile to favorite node:
favorite.setProfileToChoice(profile);
favorite.setSessionToChoice(session);
updatedFavsForProfile.push(favorite as IZoweUSSTreeNode);
updatedFavsForProfile.push(favorite);
}
// This updates the profile node's children in the this.mFavorites array, as well.
return updatedFavsForProfile;
Expand Down Expand Up @@ -1118,11 +1118,11 @@ export class USSTree extends ZoweTreeProvider<IZoweUSSTreeNode> implements Types
}
const isFullPathChild: boolean = SharedUtils.checkIfChildPath(node.fullPath, fullPath);
if (isFullPathChild) {
return this.findMatchInLoadedChildren(node as IZoweUSSTreeNode, fullPath);
return this.findMatchInLoadedChildren(node, fullPath);
}
}
}
return match as IZoweUSSTreeNode;
return match;
}

public async openWithEncoding(node: IZoweUSSTreeNode, encoding?: ZosEncoding): Promise<void> {
Expand Down
7 changes: 6 additions & 1 deletion packages/zowe-explorer/src/utils/ProfilesUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,12 @@ export class ProfilesUtils {
Gui.warningMessage(schemaWarning);
ZoweLogger.warn(schemaWarning);
}
Constants.CONFIG_PATH = rootPath ? rootPath : FileManagement.getZoweDir();
Constants.SAVED_PROFILE_CONTENTS.clear();
for (const layer of mProfileInfo.getTeamConfig().layers) {
if (layer.exists) {
Constants.SAVED_PROFILE_CONTENTS.set(layer.path, fs.readFileSync(layer.path));
}
}
ZoweLogger.info(`Zowe Explorer is using the team configuration file "${mProfileInfo.getTeamConfig().configName}"`);
const layers = mProfileInfo.getTeamConfig().layers || [];
const layerSummary = layers.map(
Expand Down

0 comments on commit 23c7fa1

Please sign in to comment.