Skip to content

Commit

Permalink
tests: resolve failing tests, add cases for promptedForUnsavedResource
Browse files Browse the repository at this point in the history
Signed-off-by: Trae Yelovich <[email protected]>
  • Loading branch information
traeok committed Nov 18, 2024
1 parent 117a2db commit 979e637
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ export function createUSSSessionNode(session: imperative.Session, profile: imper
collapsibleState: vscode.TreeItemCollapsibleState.Collapsed,
session,
profile,
parentPath: "/",
});
zoweUSSNode.fullPath = "/test";
zoweUSSNode.contextValue = Constants.USS_SESSION_CONTEXT;
Expand Down
3 changes: 1 addition & 2 deletions packages/zowe-explorer/__tests__/__mocks__/vscode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ export namespace l10n {
return options;
}
options.args?.forEach((arg: string, i: number) => {
options.message = options.message.replace(`{${i}}`, arg);
options.message = options.message.replaceAll(`{${i}}`, arg);
});
return options.message;
}
Expand Down Expand Up @@ -1312,7 +1312,6 @@ export enum FileSystemProviderErrorCode {
* a file or folder doesn't exist, use them like so: `throw vscode.FileSystemError.FileNotFound(someUri);`
*/
export const { FileSystemError } = require("jest-mock-vscode").createVSCodeMock(jest);

/**
* Namespace for dealing with the current workspace. A workspace is the representation
* of the folder that has been opened. There is no workspace when just a file but not a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ describe("stat", () => {
expect(lookupMock).toHaveBeenCalledWith(testUris.pdsMember, false);
expect(lookupParentDirMock).toHaveBeenCalledWith(testUris.pdsMember);
expect(allMembersMock).toHaveBeenCalledWith("USER.DATA.PDS", { attributes: true });
expect(res).toStrictEqual({ ...fakePdsMember, mtime: dayjs("2024-08-08 12:30").unix() });
expect(res).toStrictEqual({ ...fakePdsMember, mtime: dayjs("2024-08-08 12:30").valueOf() });
expect(fakePdsMember.wasAccessed).toBe(false);
lookupMock.mockRestore();
lookupParentDirMock.mockRestore();
Expand Down Expand Up @@ -1126,6 +1126,7 @@ describe("rename", () => {
.mockImplementation((uri): DirEntry | FileEntry => ((uri as Uri).path.includes("USER.DATA.PS2") ? (null as any) : oldPs));
const _lookupParentDirectoryMock = jest
.spyOn(DatasetFSProvider.instance as any, "_lookupParentDirectory")
.mockReturnValueOnce({ ...testEntries.session })
.mockReturnValueOnce({ ...testEntries.session });
await DatasetFSProvider.instance.rename(testUris.ps, testUris.ps.with({ path: "/USER.DATA.PS2" }), { overwrite: true });
expect(mockMvsApi.renameDataSet).toHaveBeenCalledWith("USER.DATA.PS", "USER.DATA.PS2");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import { FilterDescriptor } from "../../../../src/management/FilterManagement";
import { AuthUtils } from "../../../../src/utils/AuthUtils";
import { Icon } from "../../../../src/icons/Icon";
import { ZoweTreeProvider } from "../../../../src/trees/ZoweTreeProvider";
import { TreeViewUtils } from "../../../../src/utils/TreeViewUtils";

function createGlobalMocks() {
const globalMocks = {
Expand Down Expand Up @@ -934,6 +935,7 @@ describe("USSTree Unit Tests - Function rename", () => {
globalMocks.FileSystemProvider.rename.mockClear();

const newMocks = {
promptedForUnsavedResource: jest.spyOn(TreeViewUtils, "promptedForUnsavedResource").mockResolvedValue(false),
ussFavNode,
ussFavNodeParent,
setAttributes: jest.spyOn(ZoweUSSNode.prototype, "setAttributes").mockImplementation(),
Expand Down Expand Up @@ -1217,7 +1219,6 @@ describe("USSTree Unit Tests - Function getChildren", () => {
contextOverride: Constants.USS_SESSION_CONTEXT,
session: globalMocks.testSession,
profile: globalMocks.testProfile,
parentPath: "/",
}),
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@
*/

import * as vscode from "vscode";
import { PersistenceSchemaEnum } from "@zowe/zowe-explorer-api";
import { Gui, PersistenceSchemaEnum, ZoweScheme } from "@zowe/zowe-explorer-api";
import { createDatasetSessionNode, createDatasetTree } from "../../__mocks__/mockCreators/datasets";
import { createIProfile, createISession, createPersistentConfig, createTreeView } from "../../__mocks__/mockCreators/shared";
import { ZoweLocalStorage } from "../../../src/tools/ZoweLocalStorage";
import { TreeViewUtils } from "../../../src/utils/TreeViewUtils";
import { Constants } from "../../../src/configuration/Constants";
import { ZoweUSSNode } from "../../../src/trees/uss/ZoweUSSNode";
import { Profiles } from "../../../src/configuration/Profiles";
import { createUSSSessionNode } from "../../__mocks__/mockCreators/uss";
import { ZoweDatasetNode } from "../../../src/trees/dataset/ZoweDatasetNode";

describe("TreeViewUtils Unit Tests", () => {
function createBlockMocks(): { [key: string]: any } {
Expand Down Expand Up @@ -76,4 +80,195 @@ describe("TreeViewUtils Unit Tests", () => {
await TreeViewUtils.removeSession(blockMocks.testDatasetTree, "SESTEST");
expect(blockMocks.testDatasetTree.removeFileHistory).toHaveBeenCalledTimes(1);
});

describe("promptedForUnsavedResource", () => {
function getBlockMocks() {
return {
errorMessage: jest.spyOn(Gui, "errorMessage").mockClear(),
profilesInstance: jest.spyOn(Profiles, "getInstance").mockReturnValue({
checkCurrentProfile: jest.fn(),
} as any),
};
}
it("prompts for an unsaved USS file", async () => {
const ussNode = new ZoweUSSNode({
label: "testFile.txt",
collapsibleState: vscode.TreeItemCollapsibleState.None,
contextOverride: Constants.USS_TEXT_FILE_CONTEXT,
profile: createIProfile(),
parentNode: createUSSSessionNode(createISession(), createIProfile()),
});
ussNode.resourceUri = vscode.Uri.from({
path: "/sestest/testFile.txt",
scheme: ZoweScheme.USS,
});
(ussNode.resourceUri as any).fsPath = ussNode.resourceUri.path;
const blockMocks = getBlockMocks();

const textDocumentsMock = jest.replaceProperty(vscode.workspace, "textDocuments", [
{
fileName: ussNode.resourceUri?.fsPath as any,
uri: ussNode.resourceUri,
isDirty: true,
} as any,
]);

expect(await TreeViewUtils.promptedForUnsavedResource(ussNode)).toBe(true);
expect(blockMocks.errorMessage).toHaveBeenCalledWith(
"Unable to rename testFile.txt because you have unsaved changes in this file. " + "Please save your work before renaming the file.",
{ vsCodeOpts: { modal: true } }
);
textDocumentsMock.restore();
});

it("prompts for an unsaved file in a USS directory", async () => {
const ussFolder = new ZoweUSSNode({
label: "folder",
collapsibleState: vscode.TreeItemCollapsibleState.None,
contextOverride: Constants.USS_DIR_CONTEXT,
profile: createIProfile(),
parentNode: createUSSSessionNode(createISession(), createIProfile()),
});
ussFolder.resourceUri = vscode.Uri.from({
path: "/sestest/folder",
scheme: ZoweScheme.USS,
});
(ussFolder.resourceUri as any).fsPath = ussFolder.resourceUri.path;
const blockMocks = getBlockMocks();

const ussNode = new ZoweUSSNode({
label: "testFile.txt",
collapsibleState: vscode.TreeItemCollapsibleState.None,
contextOverride: Constants.USS_TEXT_FILE_CONTEXT,
profile: createIProfile(),
parentNode: ussFolder,
});
ussNode.resourceUri = vscode.Uri.from({
path: "/sestest/folder/testFile.txt",
scheme: ZoweScheme.USS,
});
(ussNode.resourceUri as any).fsPath = ussNode.resourceUri.path;

const textDocumentsMock = jest.replaceProperty(vscode.workspace, "textDocuments", [
{
fileName: ussNode.resourceUri?.fsPath as any,
uri: ussNode.resourceUri,
isDirty: true,
} as any,
]);

expect(await TreeViewUtils.promptedForUnsavedResource(ussFolder)).toBe(true);
expect(blockMocks.errorMessage).toHaveBeenCalledWith(
"Unable to rename folder because you have unsaved changes in this directory. " +
"Please save your work before renaming the directory.",
{ vsCodeOpts: { modal: true } }
);
textDocumentsMock.restore();
});

it("prompts for an unsaved data set", async () => {
const ps = new ZoweDatasetNode({
label: "TEST.PS",
collapsibleState: vscode.TreeItemCollapsibleState.None,
contextOverride: Constants.DS_DS_CONTEXT,
profile: createIProfile(),
parentNode: createDatasetSessionNode(createISession(), createIProfile()),
});
ps.resourceUri = vscode.Uri.from({
path: "/sestest/TEST.PS",
scheme: ZoweScheme.DS,
});
(ps.resourceUri as any).fsPath = ps.resourceUri.path;
const blockMocks = getBlockMocks();

const textDocumentsMock = jest.replaceProperty(vscode.workspace, "textDocuments", [
{
fileName: ps.resourceUri?.fsPath as any,
uri: ps.resourceUri,
isDirty: true,
} as any,
]);

expect(await TreeViewUtils.promptedForUnsavedResource(ps)).toBe(true);
expect(blockMocks.errorMessage).toHaveBeenCalledWith(
"Unable to rename TEST.PS because you have unsaved changes in this data set. " +
"Please save your work before renaming the data set.",
{ vsCodeOpts: { modal: true } }
);
textDocumentsMock.restore();
});

it("doesn't prompt if no resources are unsaved in editor", async () => {
const ps = new ZoweDatasetNode({
label: "TEST.PS",
collapsibleState: vscode.TreeItemCollapsibleState.None,
contextOverride: Constants.DS_DS_CONTEXT,
profile: createIProfile(),
parentNode: createDatasetSessionNode(createISession(), createIProfile()),
});
ps.resourceUri = vscode.Uri.from({
path: "/sestest/TEST.PS",
scheme: ZoweScheme.DS,
});
(ps.resourceUri as any).fsPath = ps.resourceUri.path;
const blockMocks = getBlockMocks();

const textDocumentsMock = jest.replaceProperty(vscode.workspace, "textDocuments", [
{
fileName: ps.resourceUri?.fsPath as any,
uri: ps.resourceUri,
isDirty: false,
} as any,
]);

expect(await TreeViewUtils.promptedForUnsavedResource(ps)).toBe(false);
expect(blockMocks.errorMessage).not.toHaveBeenCalled();
textDocumentsMock.restore();
});

it("prompts for an unsaved PDS member", async () => {
const pds = new ZoweDatasetNode({
label: "TEST.PDS",
collapsibleState: vscode.TreeItemCollapsibleState.None,
contextOverride: Constants.DS_PDS_CONTEXT,
profile: createIProfile(),
parentNode: createDatasetSessionNode(createISession(), createIProfile()),
});
pds.resourceUri = vscode.Uri.from({
path: "/sestest/TEST.PDS",
scheme: ZoweScheme.DS,
});
(pds.resourceUri as any).fsPath = pds.resourceUri.path;

const pdsMember = new ZoweDatasetNode({
label: "MEMB",
collapsibleState: vscode.TreeItemCollapsibleState.None,
contextOverride: Constants.DS_MEMBER_CONTEXT,
profile: createIProfile(),
parentNode: createDatasetSessionNode(createISession(), createIProfile()),
});
pdsMember.resourceUri = vscode.Uri.from({
path: "/sestest/TEST.PDS/MEMB",
scheme: ZoweScheme.DS,
});
(pdsMember.resourceUri as any).fsPath = pdsMember.resourceUri.path;
const blockMocks = getBlockMocks();

const textDocumentsMock = jest.replaceProperty(vscode.workspace, "textDocuments", [
{
fileName: pdsMember.resourceUri?.fsPath as any,
uri: pdsMember.resourceUri,
isDirty: true,
} as any,
]);

expect(await TreeViewUtils.promptedForUnsavedResource(pds)).toBe(true);
expect(blockMocks.errorMessage).toHaveBeenCalledWith(
"Unable to rename TEST.PDS because you have unsaved changes in this data set. " +
"Please save your work before renaming the data set.",
{ vsCodeOpts: { modal: true } }
);
textDocumentsMock.restore();
});
});
});
7 changes: 1 addition & 6 deletions packages/zowe-explorer/src/trees/uss/UssFSProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,10 @@ export class UssFSProvider extends BaseProvider implements vscode.FileSystemProv
const newTime = (fileResp.apiResponse?.items ?? [])?.[0]?.mtime ?? entry.mtime;

if (entry.mtime != newTime) {
entry.mtime = newTime;

Check warning on line 94 in packages/zowe-explorer/src/trees/uss/UssFSProvider.ts

View check run for this annotation

Codecov / codecov/patch

packages/zowe-explorer/src/trees/uss/UssFSProvider.ts#L94

Added line #L94 was not covered by tests
// if the modification time has changed, invalidate the previous contents to signal to `readFile` that data needs to be fetched
entry.wasAccessed = false;
}
return {
...entry,
// If there isn't a valid mtime on the API response, we cannot determine whether the resource has been updated.
// Use the last-known modification time to prevent superfluous updates.
mtime: newTime,
};
}

return entry;
Expand Down

0 comments on commit 979e637

Please sign in to comment.