Skip to content

Commit

Permalink
add checkAbsoluteFilePath in UssUtils
Browse files Browse the repository at this point in the history
Signed-off-by: Tian Na <[email protected]>
  • Loading branch information
tiantn committed Nov 10, 2023
1 parent e271d16 commit 10bf0ac
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 78 deletions.
9 changes: 9 additions & 0 deletions __tests__/__unit__/api/UssUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*
*/

import { ImperativeError } from "@zowe/imperative";
import { UssUtils } from "../../../src/api/UssUtils";

describe("UssUtils", () => {
Expand All @@ -28,4 +29,12 @@ describe("UssUtils", () => {
path = UssUtils.normalizeUnixPath("//home/user1/hello.text");
expect(path).toBe("/home/user1/hello.text");
});

it("should throw error when the uss file path is not absolute file Path", () => {
const path = "test.txt";
const result = () => {
UssUtils.checkAbsoluteFilePath(path);
};
expect(result).toThrow(ImperativeError);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,4 @@ describe("Upload file to data set handler", () => {
expect(mockResponse.console.log.mock.calls[0][1]).toBe("local file '/u/user/file1'");
expect(mockResponse.console.log.mock.calls[0][2]).toBe("/u/user/ussfile1");
});

it("should return error if the uss file path is not a full path.", async () => {
const handler = new UploadFileToUssFileHandler();

IO.readFileSync = jest.fn().mockReturnValue(Promise.resolve(Buffer.from("sss")));
// UssUtils.normalizeUnixPath =
CoreUtils.addCarriageReturns = jest.fn().mockReturnValue(("sss"));
const mockResponse = TestUtils.getMockResponse();
const mockParams: any = {
arguments: {
file: "/u/user/file1",
ussFile: "ussfile1"
},
connection: {
uploadDataset: jest.fn().mockReturnValue(Promise.resolve(""))
},
response: mockResponse
};
await handler.processFTP(mockParams);
expect(mockResponse.console.log.mock.calls[0][0]).toBe("Please check the uss file path. The full file path is required.");
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,4 @@ describe("Upload file to data set handler", () => {
expect(mockResponse.console.log.mock.calls[0][1]).toBe("stdin");
expect(mockResponse.console.log.mock.calls[0][2]).toBe("/u/user/ussfile1");
});

it("should return error if the uss file path is not a full path.", async () => {
const handler = new UploadStdinToUssFileHandler();

CoreUtils.readStdin = jest.fn().mockReturnValue(Promise.resolve(Buffer.from("sss")));
CoreUtils.addCarriageReturns = jest.fn().mockReturnValue(("sss"));
const mockResponse = TestUtils.getMockResponse();
const mockParams: any = {
arguments: {
file: "/u/user/file1",
ussFile: "ussfile1"
},
connection: {
uploadDataset: jest.fn().mockReturnValue(Promise.resolve(""))
},
response: mockResponse
};
await handler.processFTP(mockParams);
expect(mockResponse.console.log.mock.calls[0][0]).toBe("Please check the uss file path. The full file path is required.");
});

});
10 changes: 9 additions & 1 deletion src/api/UssUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import * as PATH from "path";
import * as fs from "fs";

import { IHandlerResponseConsoleApi, IO, Logger } from "@zowe/imperative";
import { IHandlerResponseConsoleApi, IO, ImperativeError, Logger } from "@zowe/imperative";
import { CoreUtils, TRANSFER_TYPE_ASCII } from "./CoreUtils";
import { StreamUtils } from "./StreamUtils";
import { IDeleteFileOption, IDownloadFileOption, IUploadFileOption } from "./UssInterface";
Expand Down Expand Up @@ -201,6 +201,14 @@ export class UssUtils {
}
}

public static checkAbsoluteFilePath(filePath: string) {
if (!filePath.startsWith('/')) {
throw new ImperativeError({ msg: "Please check the uss file path. The full file path is required."});
} else {
return filePath;
}
}

private static get log(): Logger {
return Logger.getAppLogger();
}
Expand Down
25 changes: 10 additions & 15 deletions src/cli/upload/file-to-uss-file/FileToUssFile.Handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,17 @@ export default class UploadFileToUssFileHandler extends FTPBaseHandler {

public async processFTP(params: IFTPHandlerParams): Promise<void> {
const ussFile = UssUtils.normalizeUnixPath(params.arguments.ussFile);
if (!ussFile.startsWith('/')) {
const errMsg = params.response.console.log("Please check the uss file path. The full file path is required.");
params.response.data.setMessage(errMsg);
this.log.info(errMsg);
} else {
const options = {
localFile: params.arguments.file,
transferType: params.arguments.binary ? TRANSFER_TYPE_BINARY : TRANSFER_TYPE_ASCII,
};
await UssUtils.uploadFile(params.connection, ussFile, options);
UssUtils.checkAbsoluteFilePath(ussFile);
const options = {
localFile: params.arguments.file,
transferType: params.arguments.binary ? TRANSFER_TYPE_BINARY : TRANSFER_TYPE_ASCII,
};
await UssUtils.uploadFile(params.connection, ussFile, options);

const uploadSource: string = "local file '" + params.arguments.file + "'";
const successMsg = params.response.console.log("Uploaded from %s to %s ", uploadSource, ussFile);
params.response.data.setMessage(successMsg);
this.log.info(successMsg);
}
const uploadSource: string = "local file '" + params.arguments.file + "'";
const successMsg = params.response.console.log("Uploaded from %s to %s ", uploadSource, ussFile);
params.response.data.setMessage(successMsg);
this.log.info(successMsg);
}
}

33 changes: 14 additions & 19 deletions src/cli/upload/stdin-to-uss-file/StdinToUssFile.Handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,20 @@ export default class UploadStdinToUssFileHandler extends FTPBaseHandler {
public async processFTP(params: IFTPHandlerParams): Promise<void> {

const ussFile = UssUtils.normalizeUnixPath(params.arguments.ussFile);
if (!ussFile.startsWith('/')) {
const errMsg = params.response.console.log("Please check the uss file path. The full file path is required.");
params.response.data.setMessage(errMsg);
this.log.info(errMsg);
} else {
const content: Buffer | string = await CoreUtils.readStdin(params.stdin);

const options = {
content,
transferType: params.arguments.binary ? TRANSFER_TYPE_BINARY : TRANSFER_TYPE_ASCII,
};
await UssUtils.uploadFile(params.connection, ussFile, options);

const uploadSource: string = "stdin";
const successMsg = params.response.console.log("Uploaded from %s to %s ", uploadSource, ussFile);
params.response.data.setMessage(successMsg);
this.log.info(successMsg);

}
UssUtils.checkAbsoluteFilePath(ussFile);
const content: Buffer | string = await CoreUtils.readStdin(params.stdin);

const options = {
content,
transferType: params.arguments.binary ? TRANSFER_TYPE_BINARY : TRANSFER_TYPE_ASCII,
};
await UssUtils.uploadFile(params.connection, ussFile, options);

const uploadSource: string = "stdin";
const successMsg = params.response.console.log("Uploaded from %s to %s ", uploadSource, ussFile);
params.response.data.setMessage(successMsg);
this.log.info(successMsg);
}

}

0 comments on commit 10bf0ac

Please sign in to comment.