Skip to content

Commit

Permalink
add unit testing
Browse files Browse the repository at this point in the history
Signed-off-by: Billie Simmons <[email protected]>
  • Loading branch information
JillieBeanSim committed Oct 9, 2023
1 parent 68f7f5a commit b54abe3
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,15 @@
*
*/

import * as vscode from "vscode";
import * as globals from "../../../src/globals";
import * as os from "os";
import * as vscode from "vscode";
import * as sharedMock from "../../../__mocks__/mockCreators/shared";
import { ZoweDatasetNode } from "../../../src/dataset/ZoweDatasetNode";
import { ZoweUSSNode } from "../../../src/uss/ZoweUSSNode";
import * as dsMock from "../../../__mocks__/mockCreators/datasets";
import * as unixMock from "../../../__mocks__/mockCreators/uss";
import * as unixActions from "../../../src/uss/actions";
import * as dsActions from "../../../src/dataset/actions";
import * as utils from "../../../src/shared/utils";
import { LocalFileManagement } from "../../../src/utils/LocalFileManagement";
import * as utils from "../../../src/shared/utils";

jest.mock("fs");
jest.mock("vscode");
Expand All @@ -36,32 +33,49 @@ describe("LocalFileManagement unit tests", () => {
mockSession: sharedMock.createISession(),
mockProfile: sharedMock.createValidIProfile(),
mockDsFileNode: ZoweDatasetNode,
mockUnixFileNode: ZoweUSSNode,
mockFilesToCompare: null as any,
mockDlUnixSpy: null as any,
mockDlDsSpy: null as any,
mockIsDsNode: true,
mockIsUnixNode: false,
mockPath: "/u/fake/path/file.txt",
};
newMocks.mockDsFileNode = dsMock.createDatasetSessionNode(newMocks.mockSession, newMocks.mockProfile) as any;
newMocks.mockUnixFileNode = unixMock.createUSSNode(newMocks.mockSession, newMocks.mockProfile) as any;
// newMocks.mockFilesToCompare = [newMocks.mockDsFileNode, newMocks.mockUnixFileNode];
Object.defineProperty(globals, "filesToCompare", { value: newMocks.mockFilesToCompare, configurable: true });
Object.defineProperty(dsActions, "downloadPs", { value: jest.fn(), configurable: true });
Object.defineProperty(dsActions, "downloadPs", { value: jest.fn().mockResolvedValue({ path: newMocks.mockPath }), configurable: true });
newMocks.mockDlDsSpy = jest.spyOn(dsActions, "downloadPs");
Object.defineProperty(unixActions, "downloadUnixFile", { value: jest.fn(), configurable: true });
Object.defineProperty(unixActions, "downloadUnixFile", {
value: jest.fn().mockResolvedValue({ path: newMocks.mockPath }),
configurable: true,
});
newMocks.mockDlUnixSpy = jest.spyOn(unixActions, "downloadUnixFile");
Object.defineProperty(utils, "isZoweDatasetTreeNode", { value: jest.fn().mockReturnValue(newMocks.mockIsDsNode), configurable: true });
Object.defineProperty(utils, "isZoweUSSTreeNode", { value: jest.fn(), configurable: true });
Object.defineProperty(vscode.Uri, "file", { value: jest.fn().mockReturnValue({ path: newMocks.mockPath }), configurable: true });
Object.defineProperty(vscode.commands, "executeCommand", { value: jest.fn(), configurable: true });
Object.defineProperty(globals, "resetCompareChoices", { value: jest.fn(), configurable: true });
return newMocks;
}

describe("CompareChosenFileContent method unit tests", () => {
it("should pass with mockDlDsSpy spy called", async () => {
const mocks = createGlobalMocks();
const dsNode2 = mocks.mockDsFileNode;
dsNode2.label = "newFile.txt";
mocks.mockFilesToCompare = [mocks.mockDsFileNode, dsNode2];

mocks.mockFilesToCompare = [mocks.mockDsFileNode, mocks.mockDsFileNode];
Object.defineProperty(globals, "filesToCompare", { value: mocks.mockFilesToCompare, configurable: true });
await LocalFileManagement.compareChosenFileContent();
expect(mocks.mockDlDsSpy).toBeCalled();
expect(mocks.mockDlDsSpy).toBeCalledTimes(2);
expect(mocks.mockDlUnixSpy).not.toBeCalled();
});
it("should pass with mockDlUnixSpy spy called", async () => {
const mocks = createGlobalMocks();
mocks.mockFilesToCompare = [mocks.mockDsFileNode, mocks.mockDsFileNode];
Object.defineProperty(globals, "filesToCompare", { value: mocks.mockFilesToCompare, configurable: true });
mocks.mockIsDsNode = false;
Object.defineProperty(utils, "isZoweDatasetTreeNode", { value: jest.fn().mockReturnValue(mocks.mockIsDsNode), configurable: true });
mocks.mockIsUnixNode = true;
Object.defineProperty(utils, "isZoweUSSTreeNode", { value: jest.fn().mockReturnValue(mocks.mockIsUnixNode), configurable: true });
await LocalFileManagement.compareChosenFileContent();
expect(mocks.mockDlUnixSpy).toBeCalledTimes(2);
expect(mocks.mockDlDsSpy).not.toBeCalled();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
"deleteUssPrompt.deleteCancelled": "Delete action was cancelled.",
"deleteUSSNode.error.node": "Unable to delete node: ",
"deleteUssNode.itemDeleted": "The item {0} has been deleted.",
"openUSS.error.invalidNode": "open() called from invalid node.",
"openUSS.name.exists": "There is already a file with the same name. Please change your OS file system settings if you want to give case sensitive file names",
"ussFile.opening": "$(sync~spin) Opening USS file...",
"refreshUSS.error.invalidNode": "refreshUSS() called from invalid node.",
"refreshUSS.error.notFound": "not found",
"refreshUSS.file1": "Unable to find file: ",
Expand Down
5 changes: 4 additions & 1 deletion packages/zowe-explorer/i18n/sample/src/uss/actions.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@
"deleteUssPrompt.confirmation.message": "Are you sure you want to delete the following item?\nThis will permanently remove the following file or folder from your system.\n\n{0}",
"deleteUssPrompt.confirmation.cancel.log.debug": "Delete action was canceled.",
"ZoweUssNode.copyDownload.progress": "Copying file structure...",
"ZoweUssNode.copyUpload.progress": "Pasting files..."
"ZoweUssNode.copyUpload.progress": "Pasting files...",
"downloadUnixFile.invalidNode.error": "open() called from invalid node.",
"downloadUnixFile.name.exists": "There is already a file with the same name. Please change your OS file system settings if you want to give case sensitive file names",
"downloadUnixFile.downloading": "$(sync~spin) Downloading USS file..."
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"saveFile.etagMismatch.log.warning": "Remote file has changed. Presenting with way to resolve file."
"saveFile.etagMismatch.log.warning": "Remote file has changed. Presenting with way to resolve file.",
"getCompareFilePaths.treeNodeCheck.fail": "Something went wrong with compare of files."
}
3 changes: 1 addition & 2 deletions packages/zowe-explorer/src/shared/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ import * as globals from "../globals";
import { openPS } from "../dataset/actions";
import { Gui, IZoweDatasetTreeNode, IZoweUSSTreeNode, IZoweNodeType, IZoweTree } from "@zowe/zowe-explorer-api";
import { Profiles } from "../Profiles";
import { filterTreeByString, isZoweDatasetTreeNode, isZoweUSSTreeNode } from "../shared/utils";
import { filterTreeByString } from "../shared/utils";
import { FilterItem, FilterDescriptor } from "../utils/ProfilesUtils";
import * as contextually from "../shared/context";
import * as nls from "vscode-nls";
import { getIconById, IconId } from "../generators/icons";
import { ZoweLogger } from "../utils/LoggerUtils";
import { IZoweTreeNode } from "@zowe/zowe-explorer-api";

// Set up localization
nls.config({
Expand Down
33 changes: 23 additions & 10 deletions packages/zowe-explorer/src/utils/LocalFileManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,22 +85,35 @@ export class LocalFileManagement {
public static async compareChosenFileContent(): Promise<void> {
const docUriArray: vscode.Uri[] = [];
for (const node of globals.filesToCompare) {
const filepath = await this.getCompareFilePaths(node);
docUriArray.push(vscode.Uri.file(filepath));
const fileInfo = await this.getCompareFilePaths(node);
if (fileInfo.path) {
docUriArray.push(vscode.Uri.file(fileInfo.path));
} else {
return;

Check warning on line 92 in packages/zowe-explorer/src/utils/LocalFileManagement.ts

View check run for this annotation

Codecov / codecov/patch

packages/zowe-explorer/src/utils/LocalFileManagement.ts#L91-L92

Added lines #L91 - L92 were not covered by tests
}
}
vscode.commands.executeCommand("vscode.diff", docUriArray[0], docUriArray[1]);
globals.resetCompareChoices();
if (docUriArray.length === 2) {
vscode.commands.executeCommand("vscode.diff", docUriArray[0], docUriArray[1]);
}
}

private static async getCompareFilePaths(node: IZoweTreeNode): Promise<string> {
private static async getCompareFilePaths(node: IZoweTreeNode): Promise<localFileInfo> {
ZoweLogger.info(`Getting files ${String(globals.filesToCompare[0].label)} and ${String(globals.filesToCompare[1].label)} for comparison.`);
let fileInfo = {} as localFileInfo;
if (isZoweDatasetTreeNode(node)) {
fileInfo = await downloadPs(node);
}
if (isZoweUSSTreeNode(node)) {
fileInfo = await downloadUnixFile(node, true);
switch (true) {
case isZoweDatasetTreeNode(node): {
fileInfo = await downloadPs(node);
break;
}
case isZoweUSSTreeNode(node): {
fileInfo = await downloadUnixFile(node, true);
break;
}
default: {
ZoweLogger.warn(localize("getCompareFilePaths.treeNodeCheck.fail", "Something went wrong with compare of files."));

Check warning on line 114 in packages/zowe-explorer/src/utils/LocalFileManagement.ts

View check run for this annotation

Codecov / codecov/patch

packages/zowe-explorer/src/utils/LocalFileManagement.ts#L113-L114

Added lines #L113 - L114 were not covered by tests
}
}
return fileInfo.path;
return fileInfo;
}
}

0 comments on commit b54abe3

Please sign in to comment.