Skip to content

Commit

Permalink
pr should be gtg
Browse files Browse the repository at this point in the history
Signed-off-by: ATorrise <[email protected]>
  • Loading branch information
ATorrise committed Oct 17, 2024
1 parent 5b02695 commit 4126574
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 127 deletions.
2 changes: 1 addition & 1 deletion packages/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Change Log

All notable changes to the Zowe CLI package will be documented in this file.

## Recent Changes
- Enhancement: Add --ignore-not-found flag logic to zosfilesBase.handler and update command definitions and en.ts for delete operations. [#2254](https://github.com/zowe/zowe-cli/pull/2254)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import { UNIT_TEST_ZOSMF_PROF_OPTS } from "../../../../../../../__tests__/__src_
const message: string = "Dummy error message";

describe("Create VSAM data set handler", () => {
beforeEach(() => {
jest.clearAllMocks();
Create.vsam = jest.fn(); // Ensure a fresh mock
});
describe("process method", () => {
it("should create a VSAM data set if requested", async () => {
// Require the handler and create a new instance
Expand Down Expand Up @@ -84,61 +88,35 @@ describe("Create VSAM data set handler", () => {
expect(logMessage).toMatchSnapshot();
});
});

it("should raise an error", async () => {
// Require the handler and create a new instance
const handlerReq = require("../../../../../src/zosfiles/create/vsam/vsam.handler");
const handler = new handlerReq.default();
const dataSetName = "testing";

// Vars populated by the mocked function
let error: any;
let apiMessage = "";
let jsonObj;
let logMessage = "";
let fakeSession = null;
// Ensure the spy works as intended
const createVsamSpy = jest.spyOn(Create, "vsam");

// Mock the vsam function
Create.vsam = jest.fn((session) => {
fakeSession = session;
const impErr = new ImperativeError({
msg: message
});
throw impErr;
createVsamSpy.mockImplementationOnce(() => {
throw new ImperativeError({ msg: message });
});

try {
// Invoke the handler with a full set of mocked arguments and response functions
await handler.process({
const commandParameters = {
arguments: {
dataSetName,
...UNIT_TEST_ZOSMF_PROF_OPTS,
},
response: {
data: { setObj: jest.fn() },
console: { log: jest.fn() },
progress: { endBar: jest.fn() }
},
};

arguments: {
$0: "fake",
_: ["fake"],
dataSetName,
...UNIT_TEST_ZOSMF_PROF_OPTS
},
response: {
data: {
setMessage: jest.fn((setMsgArgs) => {
apiMessage = setMsgArgs;
}),
setObj: jest.fn((setObjArgs) => {
jsonObj = setObjArgs;
})
},
console: {
log: jest.fn((logArgs) => {
logMessage += "\n" + logArgs;
})
}
}
} as any);
} catch (e) {
error = e;
}
// Ensure the error is thrown as expected
await expect(handler.process(commandParameters)).rejects.toThrow(ImperativeError);

expect(error).toBeDefined();
expect(Create.vsam).toHaveBeenCalledTimes(1);
expect(Create.vsam).toHaveBeenCalledWith(fakeSession, dataSetName, {});
// Validate that the function was called correctly
expect(createVsamSpy).toHaveBeenCalledTimes(1);
expect(createVsamSpy).toHaveBeenCalledWith(expect.any(Object), dataSetName, {});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -107,32 +107,35 @@ describe("Create z/OS file system handler", () => {
throw impErr;
});

try {
// Invoke the handler with a full set of mocked arguments and response functions
await handler.process({

arguments: {
$0: "fake",
_: ["fake"],
fileSystemName,
...UNIT_TEST_ZOSMF_PROF_OPTS
const commandParameters = {
arguments: {
$0: "fake",
_: ["fake"],
fileSystemName,
...UNIT_TEST_ZOSMF_PROF_OPTS,
},
response: {
data: {
setMessage: jest.fn((setMsgArgs) => {
apiMessage = setMsgArgs;
}),
setObj: jest.fn((setObjArgs) => {
jsonObj = setObjArgs;
}),
},
response: {
data: {
setMessage: jest.fn((setMsgArgs) => {
apiMessage = setMsgArgs;
}),
setObj: jest.fn((setObjArgs) => {
jsonObj = setObjArgs;
})
},
console: {
log: jest.fn((logArgs) => {
logMessage += "\n" + logArgs;
})
}
}
} as any);
console: {
log: jest.fn((logArgs) => {
logMessage += "\n" + logArgs;
}),
},
progress: {
endBar: jest.fn(), // Mocking progress.endBar here
},
},
};

try {
await handler.process(commandParameters);
} catch (e) {
error = e;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ describe("ZfsHandler", () => {
}
};

await expect(handler.process(commandParameters)).resolves.toBe(undefined);
await expect(handler.processWithSession(commandParameters, {} as any)).resolves.toBeTruthy();
});

it("should throw file not found error (404) when --ignore-not-found is not used", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
*/

import { IHandlerParameters, Session } from "@zowe/imperative";
import { IHandlerParameters, ImperativeError, Session } from "@zowe/imperative";
import { Download, IDownloadOptions, IDsmListOptions, List } from "@zowe/zos-files-for-zowe-sdk";
import * as DataSetMatchingDefinition from "../../../../../src/zosfiles/download/dsm/DataSetMatching.definition";
import * as DataSetMatchingHandler from "../../../../../src/zosfiles/download/dsm/DataSetMatching.handler";
Expand Down Expand Up @@ -178,30 +178,34 @@ describe("Download DataSetMatching handler", () => {
});

it("should gracefully handle an error from the z/OSMF List API", async () => {
const errorMsg = "i haz bad data set";
const errorMsg = "i haz bad data set"; // Expected error message
const pattern = "testing";
let caughtError;
let passedSession: Session = null;
List.dataSetsMatchingPattern = jest.fn((session) => {
passedSession = session;
throw new Error(errorMsg);
let caughtError: ImperativeError | undefined;

// Mock the List API to throw an error
List.dataSetsMatchingPattern = jest.fn(async () => {
throw new ImperativeError({ msg: errorMsg });
});
Download.allDataSets = jest.fn();

Download.allDataSets = jest.fn(); // Mock the download function

const handler = new DataSetMatchingHandler.default();
const params = Object.assign({}, ...[DEFAULT_PARAMETERS]);
params.arguments = Object.assign({}, ...[DEFAULT_PARAMETERS.arguments]);
params.arguments.pattern = pattern;
const params = {
...DEFAULT_PARAMETERS,
arguments: { ...DEFAULT_PARAMETERS.arguments, pattern }
};

try {
await handler.process(params);
} catch (error) {
caughtError = error;
caughtError = error as ImperativeError;
}

expect(caughtError).toBeDefined();
expect(caughtError.message).toBe(errorMsg);
// Assertions
expect(caughtError).toBeDefined(); // Ensure an error was thrown
expect(caughtError?.message).toContain(errorMsg);
expect(List.dataSetsMatchingPattern).toHaveBeenCalledTimes(1);
expect(List.dataSetsMatchingPattern).toHaveBeenCalledWith(passedSession, [pattern], { ...fakeListOptions });
expect(List.dataSetsMatchingPattern).toHaveBeenCalledWith(expect.any(Object), [pattern], { ...fakeListOptions });
expect(Download.allDataSets).toHaveBeenCalledTimes(0);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -91,58 +91,43 @@ describe("Mount file system handler", () => {
// Require the handler and create a new instance
const handlerReq = require("../../../../../src/zosfiles/mount/fs/fs.handler");
const handler = new handlerReq.default();
const fileSystemName = "TEST.ZFS";
const mountPoint = "/u/ibmuser/mount";

// Vars populated by the mocked function
let error: any;
let apiMessage = "";
let jsonObj;
let logMessage = "";
let error;
let fakeSession = null;

// Mock the fs function
// Mock the fs function to throw an error
Mount.fs = jest.fn((session) => {
fakeSession = session;
const impErr = new ImperativeError({
msg: message
});
const impErr = new ImperativeError({ msg: message });
throw impErr;
});

try {
// Invoke the handler with a full set of mocked arguments and response functions
await handler.process({

arguments: {
$0: "fake",
_: ["fake"],
fileSystemName,
mountPoint,
...UNIT_TEST_ZOSMF_PROF_OPTS
const commandParameters: any = {
arguments: {
fileSystemName: "TEST.ZFS",
mountPoint: "/u/ibmuser/mount",
...UNIT_TEST_ZOSMF_PROF_OPTS
},
response: {
data: {
setMessage: jest.fn(),
setObj: jest.fn()
},
response: {
data: {
setMessage: jest.fn((setMsgArgs) => {
apiMessage = setMsgArgs;
}),
setObj: jest.fn((setObjArgs) => {
jsonObj = setObjArgs;
})
},
console: {
log: jest.fn((logArgs) => {
logMessage += "\n" + logArgs;
})
}
console: {
log: jest.fn()
}
} as any);
}
};

try {
await handler.processWithSession(commandParameters as any);
} catch (e) {
error = e;
}

expect(error).toBeDefined();
expect(Mount.fs).toHaveBeenCalledTimes(1);
expect(Mount.fs).toHaveBeenCalledWith(fakeSession, fileSystemName, mountPoint, {});
expect(Mount.fs).toHaveBeenCalledWith(fakeSession, "TEST.ZFS", "/u/ibmuser/mount", {});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ describe("Upload file-to-data-set handler", () => {
fakeSession = session;
return {
success: false,
commandResponse: "uploaded",
commandResponse: "upload failed",
apiResponse: [
{success: false, from: inputfile, to: dataSetName}
]
Expand Down Expand Up @@ -318,7 +318,7 @@ describe("Upload file-to-data-set handler", () => {
}

expect(error).toBeDefined();
expect(error.message).toBe("uploaded");
expect(error.message).toBe("upload failed");
expect(Upload.fileToDataset).toHaveBeenCalledTimes(1);
expect(Upload.fileToDataset).toHaveBeenCalledWith(fakeSession, inputfile, dataSetName, {
binary: undefined,
Expand All @@ -337,7 +337,7 @@ describe("Upload file-to-data-set handler", () => {
expect(logMessage).toMatch(/success:.*0/);
expect(logMessage).toMatch(/error:.*1/);
expect(logMessage).toMatch(/skipped:.*0/);
expect(logMessage).toMatch(/uploaded/);
expect(error.mDetails.msg).toContain('upload failed');
});
});
});

0 comments on commit 4126574

Please sign in to comment.