Skip to content

Commit

Permalink
Merge pull request #1317 from zowe/next-daemon-stdin
Browse files Browse the repository at this point in the history
[next] Add stdin to handler parameters for daemon mode
  • Loading branch information
t1m0thyj authored Feb 24, 2022
2 parents 6e53ecd + f44e5b6 commit ba15774
Show file tree
Hide file tree
Showing 101 changed files with 683 additions and 1,176 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ In addition to external documentation, please thoroughly comment your code for f
### JS Documentation

- Use jsdoc annotations - [document this](https://marketplace.visualstudio.com/items?itemName=joelday.docthis) makes extensive use of jsdoc tags.
- Common tags to use, `@static`, `@memberOf`, `@returns`, `@params`, `@class`, `@exports`, `@interface`, `@types`, `@throws`, `@link`
- Common tags to use, `@static`, `@memberof`, `@returns`, `@params`, `@class`, `@exports`, `@interface`, `@types`, `@throws`, `@link`
- CLI auto-generated documentation is created via command definitions
- [tsdoc](https://typedoc.org/) is used to generate html documentation

Expand Down
2 changes: 1 addition & 1 deletion __tests__/__packages__/cli-test-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
},
"devDependencies": {
"@types/node": "^14.14.37",
"@zowe/imperative": "5.0.0-next.202202111730",
"@zowe/imperative": "5.0.0-next.202202232039",
"eslint": "^7.32.0",
"rimraf": "^3.0.2",
"typescript": "^4.2.3"
Expand Down
79 changes: 78 additions & 1 deletion __tests__/__packages__/cli-test-utils/src/TestUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import * as fs from "fs";
import { spawnSync, SpawnSyncReturns } from "child_process";
import { ITestEnvironment } from "./environment/doc/response/ITestEnvironment";
import { CommandProfiles, ICommandDefinition, IHandlerParameters } from "@zowe/imperative";

/**
* Execute a CLI script
Expand Down Expand Up @@ -41,7 +42,7 @@ export function runCliScript(scriptPath: string, testEnvironment: ITestEnvironme
encoding: "buffer"
});
} else {
throw new Error(`The script file ${scriptPath} doesn't exist`);
throw new Error(`The script file ${scriptPath} doesn't exist`);

}
}
Expand All @@ -57,3 +58,79 @@ export function isStderrEmptyForProfilesCommand(output: Buffer): boolean {
.replace(/Recommended replacement: Edit your Zowe V2 configuration\s+zowe\.config\.json/g, "")
.trim().length === 0;
}

/**
* Type for handler data used to build mock IHandlerParameters object.
* The type inherits from IHandlerParameters but is different:
* - `arguments` omits the required properties `$0` and `_`
* - All properties are optional except for `definition`
*/
type PartialHandlerParameters = Partial<Omit<IHandlerParameters, "arguments">> & {
arguments?: Record<string, any>;
definition: ICommandDefinition;
};

/**
* Build a mocked IHandlerParameters object. Includes the following properties:
* - `response` - Mocked IHandlerResponseApi
* - `arguments`
* - `$0` - hardcoded to "zowe"
* - `_` = `params.positionals`
* - `...params.arguments`
* - `positionals` = `params.positionals`
* - `profiles` = `params.profiles`
* - `definition` = `params.definition`
* - `fullDefinition` = `params.definition`
* - `stdin` - hardcoded to `process.stdin`
* @param params Partial handler parameters object (see above for usage)
* @returns Mocked handler parameters object. Most mocks do nothing, but the
* following methods call `expect().toMatchSnapshot`:
* - `response.data`: `setMessage`, `setObj`
* - `response.console`: `log`, `error`
* - `response.format`: `output`
*/
export function mockHandlerParameters(params: PartialHandlerParameters): IHandlerParameters {
return {
response: {
data: {
setMessage: jest.fn((setMsgArgs) => {
expect(setMsgArgs).toMatchSnapshot();
}),
setObj: jest.fn((setObjArgs) => {
expect(Buffer.isBuffer(setObjArgs) ? setObjArgs.toString() : setObjArgs).toMatchSnapshot();
}),
setExitCode: jest.fn()
},
console: {
log: jest.fn((logs) => {
expect(logs.toString()).toMatchSnapshot();
}),
error: jest.fn((errors) => {
expect(errors.toString()).toMatchSnapshot();
}),
errorHeader: jest.fn(() => undefined),
prompt: jest.fn(() => null)
},
progress: {
startBar: jest.fn((parms) => undefined),
endBar: jest.fn(() => undefined)
},
format: {
output: jest.fn((parms) => {
expect(parms).toMatchSnapshot();
})
}
},
arguments: {
$0: "zowe",
_: params.positionals || [],
...(params.arguments || {})
},
positionals: params.positionals || [],
profiles: params.profiles || new CommandProfiles(new Map()),
definition: params.definition,
fullDefinition: params.definition,
stdin: process.stdin,
isChained: params.isChained
};
}
2 changes: 1 addition & 1 deletion __tests__/__resources__/application_instances/zowe.cmd
Original file line number Diff line number Diff line change
@@ -1 +1 @@
node "%~dp0\..\..\..\packages\cli\lib\main.js" %*
@node "%~dp0\..\..\..\packages\cli\lib\main.js" %*
39 changes: 1 addition & 38 deletions __tests__/__src__/mocks/ZosmfProfileMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
*/

import { IProfile, CommandProfiles, IHandlerResponseApi } from "@zowe/imperative";
import { IProfile, CommandProfiles } from "@zowe/imperative";
// Some test constants that are needed by multiple packages for unit tests

// Mocked profile options to be added to args
Expand Down Expand Up @@ -53,40 +53,3 @@ UNIT_TEST_PROFILE_MAP_ZOSMF_TSO.set(
}]
);
export const UNIT_TEST_PROFILES_ZOSMF_TSO: CommandProfiles = new CommandProfiles(UNIT_TEST_PROFILE_MAP_ZOSMF_TSO);

/**
* Returns a jest.fn mocked version of the handler arguments
* @returns {IHandlerResponseApi}
*/
export function getMockedResponse(): IHandlerResponseApi {
return {
data: {
setMessage: jest.fn((setMsgArgs) => {
expect(setMsgArgs).toMatchSnapshot();
}),
setObj: jest.fn((setObjArgs) => {
expect(setObjArgs).toMatchSnapshot();
}),
setExitCode: jest.fn()
},
console: {
log: jest.fn((logs) => {
expect(logs.toString()).toMatchSnapshot();
}),
error: jest.fn((errors) => {
expect(errors.toString()).toMatchSnapshot();
}),
errorHeader: jest.fn(() => undefined),
prompt: jest.fn(() => null)
},
progress: {
startBar: jest.fn((parms) => undefined),
endBar: jest.fn(() => undefined)
},
format: {
output: jest.fn((parms) => {
expect(parms).toMatchSnapshot();
})
}
};
}
Loading

0 comments on commit ba15774

Please sign in to comment.