Skip to content

Commit

Permalink
Merge pull request #1582 from tiantn/unittest-uss
Browse files Browse the repository at this point in the history
unit test for FtpUssApi
  • Loading branch information
JillieBeanSim committed Nov 29, 2021
2 parents 0381f21 + 983821b commit fd7efaa
Show file tree
Hide file tree
Showing 7 changed files with 268 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* This program and the accompanying materials are made available under the terms of the *
* Eclipse Public License v2.0 which accompanies this distribution, and is available at *
* https://www.eclipse.org/legal/epl-v20.html *
* *
* SPDX-License-Identifier: EPL-2.0 *
* *
* Copyright Contributors to the Zowe Project. *
* *
*/

export class IZoweLogger {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* This program and the accompanying materials are made available under the terms of the *
* Eclipse Public License v2.0 which accompanies this distribution, and is available at *
* https://www.eclipse.org/legal/epl-v20.html *
* *
* SPDX-License-Identifier: EPL-2.0 *
* *
* Copyright Contributors to the Zowe Project. *
* *
*/

import * as Stream from "stream";
const Readable = Stream.Readable;

export default class TestUtils {
public static getSingleLineStream(): any {
const stream = new Readable();
stream.push("Hello world");
stream.push(null);
return stream;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
/*
* This program and the accompanying materials are made available under the terms of the *
* Eclipse Public License v2.0 which accompanies this distribution, and is available at *
* https://www.eclipse.org/legal/epl-v20.html *
* *
* SPDX-License-Identifier: EPL-2.0 *
* *
* Copyright Contributors to the Zowe Project. *
* *
*/

/* eslint-disable @typescript-eslint/unbound-method */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-var-requires */

import { FtpUssApi } from "../../../../zowe-explorer-ftp-extension/src/ZoweExplorerFtpUssApi";
import { UssUtils } from "@zowe/zos-ftp-for-zowe-cli";
import TestUtils from "./TestUtils";
import * as zowe from "@zowe/cli";

// two methods to mock modules: create a __mocks__ file for zowe-explorer-api.ts and direct mock for extension.ts
jest.mock("../../../__mocks__/@zowe/zowe-explorer-api.ts");
jest.mock("../../../src/extension.ts");

const steam = require("stream");
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-call
const readbaleStream = steam.Readable.from([]);
const fs = require("fs");
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
fs.createReadStream = jest.fn().mockReturnValue(readbaleStream);
const UssApi = new FtpUssApi();

describe("FtpUssApi", () => {
beforeAll(() => {
UssApi.checkedProfile = jest.fn().mockReturnValue({ message: "success", type: "zftp", failNotFound: false });
UssApi.ftpClient = jest.fn().mockReturnValue({ host: "", user: "", password: "", port: "" });
UssApi.releaseConnection = jest.fn();
});

it("should list uss files.", async () => {
const response = [
{ name: "file1", size: "123" },
{ name: "dir1", size: "456" },
];
UssUtils.listFiles = jest.fn().mockReturnValue(response);
const mockParams = {
ussFilePath: "/a/b/c",
};
const result = await UssApi.fileList(mockParams.ussFilePath);
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
expect(result.apiResponse.items[0].name).toContain("file1");
expect(UssUtils.listFiles).toBeCalledTimes(1);
expect(UssApi.releaseConnection).toBeCalled();
});

it("should view uss files.", async () => {
const localFile = "/tmp/testfile1.txt";
const response = TestUtils.getSingleLineStream();
UssUtils.downloadFile = jest.fn().mockReturnValue(response);

const mockParams = {
ussFilePath: "/a/b/c.txt",
options: {
file: localFile,
},
};
const result = await UssApi.getContents(mockParams.ussFilePath, mockParams.options);
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
expect(result.apiResponse.etag).toHaveLength(40);
expect(UssUtils.downloadFile).toBeCalledTimes(1);
expect(UssApi.releaseConnection).toBeCalled();
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-call
expect(response._readableState.buffer.head.data.toString()).toContain("Hello world");
});

it("should upload uss files.", async () => {
const localFile = "/tmp/testfile1.txt";
const response = TestUtils.getSingleLineStream();
UssUtils.uploadFile = jest.fn().mockReturnValue(response);
UssApi.getContents = jest.fn().mockReturnValue({ apiResponse: { etag: "123" } });
const mockParams = {
inputFilePath: localFile,
ussFilePath: "/a/b/c.txt",
etag: "123",
returnEtag: true,
options: {
file: localFile,
},
};
const result = await UssApi.putContents(mockParams.inputFilePath, mockParams.ussFilePath);
expect(result.commandResponse).toContain("File updated.");
expect(UssUtils.downloadFile).toBeCalledTimes(1);
expect(UssUtils.uploadFile).toBeCalledTimes(1);
expect(UssApi.releaseConnection).toBeCalled();
});

it("should upload uss directory.", async () => {
const localpath = "/tmp";
const files = ["file1", "file2"];
zowe.ZosFilesUtils.getFileListFromPath = jest.fn().mockReturnValue(files);
const mockParams = {
inputDirectoryPath: localpath,
ussDirectoryPath: "/a/b/c",
options: {},
};
const response = {};
UssApi.putContents = jest.fn().mockReturnValue(response);
await UssApi.uploadDirectory(mockParams.inputDirectoryPath, mockParams.ussDirectoryPath, mockParams.options);
expect(UssApi.putContents).toBeCalledTimes(2);
});

it("should create uss directory.", async () => {
UssUtils.makeDirectory = jest.fn();
UssUtils.uploadFile = jest.fn();
const mockParams = {
ussPath: "/a/b/c",
type: "directory",
};
const result = await UssApi.create(mockParams.ussPath, mockParams.type);
expect(result.commandResponse).toContain("Directory or file created.");
expect(UssUtils.makeDirectory).toBeCalledTimes(1);
expect(UssUtils.uploadFile).not.toBeCalled;
expect(UssApi.releaseConnection).toBeCalled();
});

it("should create uss file.", async () => {
UssUtils.makeDirectory = jest.fn();
UssUtils.uploadFile = jest.fn();
const mockParams = {
ussPath: "/a/b/c",
type: "file",
};
const result = await UssApi.create(mockParams.ussPath, mockParams.type);
expect(result.commandResponse).toContain("Directory or file created.");
expect(UssUtils.uploadFile).toBeCalledTimes(1);
expect(UssUtils.makeDirectory).not.toBeCalled;
expect(UssApi.releaseConnection).toBeCalled();
});

it("should delete uss directory with recursive.", async () => {
UssUtils.deleteDirectory = jest.fn();
UssUtils.deleteFile = jest.fn();
const mockParams = {
ussPath: "/a/b/c",
recursive: true,
};
const result = await UssApi.delete(mockParams.ussPath, mockParams.recursive);
expect(result.commandResponse).toContain("Delete completed.");
expect(UssUtils.deleteDirectory).toBeCalledTimes(1);
expect(UssUtils.deleteFile).not.toBeCalled;
expect(UssApi.releaseConnection).toBeCalled();
});

it("should delete uss file.", async () => {
UssUtils.deleteDirectory = jest.fn();
UssUtils.deleteFile = jest.fn();
const mockParams = {
ussPath: "/a/b/c",
recursive: false,
};
const result = await UssApi.delete(mockParams.ussPath, mockParams.recursive);
expect(result.commandResponse).toContain("Delete completed.");
expect(UssUtils.deleteFile).toBeCalledTimes(1);
expect(UssUtils.deleteDirectory).not.toBeCalled;
expect(UssApi.releaseConnection).toBeCalled();
});

it("should rename uss file or directory.", async () => {
UssUtils.renameFile = jest.fn();
const mockParams = {
currentUssPath: "/a/b/c",
newUssPath: "/d/e/f",
};
const result = await UssApi.rename(mockParams.currentUssPath, mockParams.newUssPath);
expect(result.commandResponse).toContain("Rename completed.");
expect(UssUtils.renameFile).toBeCalledTimes(1);
expect(UssApi.releaseConnection).toBeCalled();
});
});
32 changes: 31 additions & 1 deletion packages/zowe-explorer-ftp-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"scripts": {
"prepare": "yarn build",
"build": "yarn clean && webpack --mode development",
"test": "echo \"zowe-explorer-ftp-extension: Tests coming soon.\"",
"test:unit": "jest \".*__tests__.*\\.unit\\.test\\.ts\" --coverage",
"test": "yarn test:unit",
"lint": "concurrently -n \"_eslint_,prettier\" \"eslint . --ext .ts\" \"prettier --check .\"",
"pretty": "prettier --write .",
"watch": "webpack --mode development --watch --info-verbosity verbose",
Expand All @@ -54,5 +55,34 @@
"concurrently": "^5.2.0",
"webpack": "^4.42.1",
"webpack-cli": "^3.3.11"
},
"jest": {
"moduleFileExtensions": [
"ts",
"tsx",
"js"
],
"testPathIgnorePatterns": [
"<rootDir>/src/decorators"
],
"watchPathIgnorePatterns": [
"<rootDir>/results/unit"
],
"transform": {
"^.+\\.(ts|tsx)$": "ts-jest"
},
"globals": {
"ts-jest": {
"tsConfig": "tsconfig.json",
"diagnostics": false
}
},
"testRegex": "__tests__.*\\.(spec|test)\\.ts$",
"modulePathIgnorePatterns": [
"out/"
],
"reporters": [
"default"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export class FtpJesApi extends AbstractFtpApi implements ZoweExplorerApi.IJes {
parms.outDir
);
imperative.IO.createDirsSyncFromFilePath(destinationFile);
imperative.IO.writeFile(destinationFile, spoolFileToDownload.contents as Buffer);
imperative.IO.writeFile(destinationFile, spoolFileToDownload.contents);
}
}
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export class FtpUssApi extends AbstractFtpApi implements ZoweExplorerApi.IUss {
let connection: any;
try {
connection = await this.ftpClient(this.checkedProfile());
if (connection && connection.client) {
if (connection) {
if (type === "directory") {
await UssUtils.makeDirectory(connection, ussPath);
} else if (type === "File" || type === "file") {
Expand Down
27 changes: 22 additions & 5 deletions packages/zowe-explorer-ftp-extension/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,29 @@
"module": "commonjs",
"target": "es6",
"outDir": "out",
"lib": ["es6"],
"jsx": "react",
"lib": ["es6", "dom"],
"skipLibCheck": true,
"sourceMap": true,
"rootDir": "src",
"strict": true,
"noImplicitReturns": true
/* Strict Type-Checking Option */
"strict": false /* enable all strict type-checking options */,
/* Additional Checks */
"noUnusedLocals": false /* Report errors on unused locals. */,
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
"experimentalDecorators": true,
"removeComments": true,
"resolveJsonModule": true,
"rootDir": "."
},
"exclude": ["node_modules"]
"exclude": ["node_modules", ".vscode-test"],
"include": [
"src/**/*.ts",
// Needed in production for vscode-nls localization to work:
"./node_modules/vscode/vscode.d.ts",
"./node_modules/vscode/lib/*",
"__tests__",
"__mocks__"
]
}

0 comments on commit fd7efaa

Please sign in to comment.