Skip to content

Commit

Permalink
tests: FileManagement functions
Browse files Browse the repository at this point in the history
Signed-off-by: Trae Yelovich <[email protected]>
  • Loading branch information
traeok committed Dec 23, 2024
1 parent 00fe312 commit 6dc0e01
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 29 deletions.
3 changes: 3 additions & 0 deletions packages/zowe-explorer-api/__mocks__/vscode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,8 @@ export namespace window {
close: jest.fn(),
};

export let activeTextEditor: TextDocument | undefined = { fileName: "placeholderFile.txt" } as any;

/**
* Show an information message to users. Optionally provide an array of items which will be presented as
* clickable buttons.
Expand Down Expand Up @@ -1096,6 +1098,7 @@ export class FileSystemError extends Error {
*/
export namespace workspace {
export const textDocuments: TextDocument[] = [];
export const workspaceFolders: readonly WorkspaceFolder[] | undefined = [];
export function getConfiguration(_configuration: string) {
return {
update: () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ describe("AuthHandler.isProfileLocked", () => {
describe("AuthHandler.lockProfile", () => {
it("assigns and acquires a Mutex to the profile in the profile map", async () => {
await AuthHandler.lockProfile(TEST_PROFILE_NAME);
expect((AuthHandler as any).lockedProfiles.has(TEST_PROFILE_NAME)).toBe(true);
expect((AuthHandler as any).lockedProfiles.get(TEST_PROFILE_NAME)).toBeInstanceOf(Mutex);
expect((AuthHandler as any).profileLocks.has(TEST_PROFILE_NAME)).toBe(true);
expect((AuthHandler as any).profileLocks.get(TEST_PROFILE_NAME)).toBeInstanceOf(Mutex);
AuthHandler.unlockProfile(TEST_PROFILE_NAME);
});

Expand All @@ -58,15 +58,15 @@ describe("AuthHandler.lockProfile", () => {

it("reuses the same Mutex for the profile if it already exists", async () => {
await AuthHandler.lockProfile(TEST_PROFILE_NAME);
expect((AuthHandler as any).lockedProfiles.has(TEST_PROFILE_NAME)).toBe(true);
expect((AuthHandler as any).profileLocks.has(TEST_PROFILE_NAME)).toBe(true);
// cache initial mutex for comparison
const mutex = (AuthHandler as any).lockedProfiles.get(TEST_PROFILE_NAME);
const mutex = (AuthHandler as any).profileLocks.get(TEST_PROFILE_NAME);
expect(mutex).toBeInstanceOf(Mutex);
AuthHandler.unlockProfile(TEST_PROFILE_NAME);

// same mutex is still present in map since lock/unlock sequence was used
await AuthHandler.lockProfile(TEST_PROFILE_NAME);
expect(mutex).toBe((AuthHandler as any).lockedProfiles.get(TEST_PROFILE_NAME));
expect(mutex).toBe((AuthHandler as any).profileLocks.get(TEST_PROFILE_NAME));
AuthHandler.unlockProfile(TEST_PROFILE_NAME);
});
});
Expand All @@ -75,7 +75,7 @@ describe("AuthHandler.unlockProfile", () => {
it("releases the Mutex for the profile in the profile map", async () => {
await AuthHandler.lockProfile(TEST_PROFILE_NAME);
AuthHandler.unlockProfile(TEST_PROFILE_NAME);
expect((AuthHandler as any).lockedProfiles.get(TEST_PROFILE_NAME)!.isLocked()).toBe(false);
expect((AuthHandler as any).profileLocks.get(TEST_PROFILE_NAME)!.isLocked()).toBe(false);
});

it("does nothing if there is no mutex in the profile map", async () => {
Expand All @@ -96,14 +96,14 @@ describe("AuthHandler.unlockProfile", () => {
it("reuses the same Mutex for the profile if it already exists", async () => {
await AuthHandler.lockProfile(TEST_PROFILE_NAME);
AuthHandler.unlockProfile(TEST_PROFILE_NAME);
expect((AuthHandler as any).lockedProfiles.has(TEST_PROFILE_NAME)).toBe(true);
expect((AuthHandler as any).profileLocks.has(TEST_PROFILE_NAME)).toBe(true);
// cache initial mutex for comparison
const mutex = (AuthHandler as any).lockedProfiles.get(TEST_PROFILE_NAME);
const mutex = (AuthHandler as any).profileLocks.get(TEST_PROFILE_NAME);

// same mutex is still present in map since lock/unlock sequence was used
await AuthHandler.lockProfile(TEST_PROFILE_NAME);
AuthHandler.unlockProfile(TEST_PROFILE_NAME);
expect(mutex).toBe((AuthHandler as any).lockedProfiles.get(TEST_PROFILE_NAME));
expect(mutex).toBe((AuthHandler as any).profileLocks.get(TEST_PROFILE_NAME));
});

it("refreshes resources if refreshResources parameter is true", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,103 @@
*
*/

import { FileSystemError, FileType, Uri, window, workspace } from "vscode";
import { FileManagement } from "../../../src/utils/FileManagement";
import { IFileSystemEntry, ZoweScheme } from "../../../src";

describe("utils/file.ts", () => {
describe("permStringToOctal", () => {
it("converts drwxrwxrwx to 777", () => {
expect(FileManagement.permStringToOctal("drwxrwxrwx")).toBe(777);
});
describe("permStringToOctal", () => {
it("converts drwxrwxrwx to 777", () => {
expect(FileManagement.permStringToOctal("drwxrwxrwx")).toBe(777);
});

it("converts d--------- to 0", () => {
expect(FileManagement.permStringToOctal("d---------")).toBe(0);
});
it("converts d--------- to 0", () => {
expect(FileManagement.permStringToOctal("d---------")).toBe(0);
});

it("converts drwxr-xr-x to 755", () => {
expect(FileManagement.permStringToOctal("drwxr-xr-x")).toBe(755);
});
it("converts drwxr-xr-x to 755", () => {
expect(FileManagement.permStringToOctal("drwxr-xr-x")).toBe(755);
});

it("converts -rwxrwxrwx to 777", () => {
expect(FileManagement.permStringToOctal("-rwxrwxrwx")).toBe(777);
it("converts -rwxrwxrwx to 777", () => {
expect(FileManagement.permStringToOctal("-rwxrwxrwx")).toBe(777);
});
});

describe("reloadActiveEditorForProfile", () => {
it("calls workspace.fs.{readFile,stat} to reload contents of editor", async () => {
const fakeFsEntry: IFileSystemEntry = {
name: "exampleFile.txt",
wasAccessed: true,
type: FileType.Directory,
metadata: {
path: "/sestest/exampleFolder/exampleFile.txt",
profile: {
name: "sestest",
message: "",
type: "zosmf",
failNotFound: true,
},
},
ctime: Date.now() - 10,
mtime: Date.now(),
size: 123,
};
const fileUri = Uri.from({ scheme: ZoweScheme.USS, path: "/sestest/exampleFolder/exampleFile.txt" });
const activeTextEditorMock = jest.replaceProperty(window, "activeTextEditor", {
document: {
fileName: "exampleFile.txt",
uri: fileUri,
} as any,
} as any);
const statMock = jest.spyOn(workspace.fs, "stat").mockResolvedValueOnce(fakeFsEntry);
const readFileMock = jest.spyOn(workspace.fs, "readFile").mockImplementationOnce(async (uri): Promise<Uint8Array> => {
// wasAccessed flag should be false after reassigning in reloadActiveEditorForProfile
expect(fakeFsEntry.wasAccessed).toBe(false);
return new Uint8Array([1, 2, 3]);
});
await FileManagement.reloadActiveEditorForProfile("sestest");
expect(statMock).toHaveBeenCalledTimes(1);
expect(statMock).toHaveBeenCalledWith(fileUri);
expect(readFileMock).toHaveBeenCalledTimes(1);
expect(readFileMock).toHaveBeenCalledWith(fileUri);
activeTextEditorMock.restore();
});
});

describe("reloadWorkspacesForProfile", () => {
it("calls workspace.fs.stat with fetch=true for each workspace folder", async () => {
const folderUri = Uri.from({ scheme: ZoweScheme.USS, path: "/sestest/exampleFolder" });
const workspaceFoldersMock = jest.replaceProperty(workspace, "workspaceFolders", [
{
uri: folderUri,
name: "exampleFolder",
index: 0,
},
]);
const statMock = jest
.spyOn(workspace.fs, "stat")
.mockClear()
.mockResolvedValueOnce(undefined as any);
await FileManagement.reloadWorkspacesForProfile("sestest");
expect(statMock).toHaveBeenCalledTimes(1);
expect(statMock).toHaveBeenCalledWith(folderUri.with({ query: "fetch=true" }));
workspaceFoldersMock.restore();
});
it("calls console.error in event of an error", async () => {
const folderUri = Uri.from({ scheme: ZoweScheme.USS, path: "/sestest/exampleFolder" });
const workspaceFoldersMock = jest.replaceProperty(workspace, "workspaceFolders", [
{
uri: folderUri,
name: "exampleFolder",
index: 0,
},
]);
const statMock = jest.spyOn(workspace.fs, "stat").mockClear().mockRejectedValueOnce(FileSystemError.FileNotFound(folderUri));
const consoleErrorMock = jest.spyOn(console, "error").mockImplementationOnce(() => {});
await FileManagement.reloadWorkspacesForProfile("sestest");
expect(statMock).toHaveBeenCalledTimes(1);
expect(statMock).toHaveBeenCalledWith(folderUri.with({ query: "fetch=true" }));
expect(consoleErrorMock).toHaveBeenCalledWith("reloadWorkspacesForProfile:", "file not found");
workspaceFoldersMock.restore();
});
});
12 changes: 7 additions & 5 deletions packages/zowe-explorer-api/src/utils/FileManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,16 @@ export class FileManagement {
}

public static async reloadActiveEditorForProfile(profileName: string): Promise<void> {
const document = window.activeTextEditor?.document;
if (
(Object.values(ZoweScheme) as string[]).includes(window.activeTextEditor.document.uri.scheme) &&
window.activeTextEditor.document.uri.path.startsWith(`/${profileName}/`) &&
!window.activeTextEditor.document.isDirty
document != null &&
(Object.values(ZoweScheme) as string[]).includes(document.uri.scheme) &&
document.uri.path.startsWith(`/${profileName}/`) &&
!document.isDirty
) {
const fsEntry = (await workspace.fs.stat(window.activeTextEditor.document.uri)) as IFileSystemEntry;
const fsEntry = (await workspace.fs.stat(document.uri)) as IFileSystemEntry;
fsEntry.wasAccessed = false;
await workspace.fs.readFile(window.activeTextEditor.document.uri);
await workspace.fs.readFile(document.uri);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,9 +390,7 @@ describe("ProfilesUtils unit tests", () => {
const promptCredentialsProfilesMock = jest.spyOn(mockProfileInstance, "promptCredentials").mockResolvedValueOnce(["someusername", "pw"]);
const updateCachedProfileMock = jest.spyOn(mockProfileInstance, "updateCachedProfile").mockResolvedValueOnce(undefined);
const profile = createIProfile();
jest.spyOn(mockProfileInstance, "getLoadedProfConfig").mockResolvedValue(profile);
Object.defineProperty(Constants, "PROFILES_CACHE", { value: mockProfileInstance, configurable: true });
jest.spyOn(ZoweVsCodeExtension as any, "promptUserPass").mockResolvedValue([]);
const unlockProfileSpy = jest.spyOn(AuthHandler, "unlockProfile");
const mockNode = createDatasetSessionNode(createISession(), profile);
await ProfilesUtils.promptCredentials(mockNode);
Expand Down

0 comments on commit 6dc0e01

Please sign in to comment.