Skip to content
This repository has been archived by the owner on Nov 13, 2023. It is now read-only.

Commit

Permalink
Add tests that have V3 error format turned on
Browse files Browse the repository at this point in the history
Signed-off-by: Gene Johnston <[email protected]>
  • Loading branch information
gejohnston committed Jun 19, 2023
1 parent 6bda219 commit fd5cb0f
Showing 1 changed file with 103 additions and 1 deletion.
104 changes: 103 additions & 1 deletion packages/cmd/__tests__/CommandProcessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { CliUtils } from "../../utilities/src/CliUtils";
import { WebHelpManager } from "../src/help/WebHelpManager";
import { ImperativeConfig } from "../../utilities/src/ImperativeConfig";
import { setupConfigToLoad } from "../../../__tests__/src/TestUtil";
import { EnvFileUtils } from "../../utilities";
import { EnvFileUtils, NextVerFeatures } from "../../utilities";
import { join } from "path";

jest.mock("../src/syntax/SyntaxValidator");
Expand Down Expand Up @@ -1237,6 +1237,57 @@ describe("Command Processor", () => {
expect(commandResponse.error?.additionalDetails).toEqual("More details!");
});

it("should handle an imperative error thrown from the handler with a v3-format message", async () => {
jest.spyOn(NextVerFeatures, "useV3ErrFormat").mockReturnValue(true);

// Allocate the command processor
const processor: CommandProcessor = new CommandProcessor({
envVariablePrefix: ENV_VAR_PREFIX,
fullDefinition: SAMPLE_COMPLEX_COMMAND,
definition: SAMPLE_COMMAND_REAL_HANDLER,
helpGenerator: FAKE_HELP_GENERATOR,
profileManagerFactory: FAKE_PROFILE_MANAGER_FACTORY,
rootCommandName: SAMPLE_ROOT_COMMAND,
commandLine: "",
promptPhrase: "dummydummy"
});

// Mock read stdin
(SharedOptions.readStdinIfRequested as any) = jest.fn((args, response, type) => {
// Nothing to do
});

// Mock the profile loader
(CommandProfileLoader.loader as any) = jest.fn((args) => {
return {
loadProfiles: (profArgs: any) => {
// Nothing to do
}
};
});

const parms: any = {
arguments: {
_: ["check", "for", "banana"],
$0: "",
valid: true,
throwImperative: true
},
responseFormat: "json",
silent: true
};
const commandResponse: ICommandResponse = await processor.invoke(parms);
expect(commandResponse).toBeDefined();
const stderrText = (commandResponse.stderr as Buffer).toString();
expect(stderrText).toContain("Unable to perform this operation due to the following problem");
expect(stderrText).toContain("Handler threw an imperative error!");
expect(stderrText).toContain("Diagnostic Information");
expect(stderrText).toContain("More details!");
expect(commandResponse.message).toEqual("Handler threw an imperative error!");
expect(commandResponse.error?.msg).toEqual("Handler threw an imperative error!");
expect(commandResponse.error?.additionalDetails).toEqual("More details!");
});

it("should handle an error thrown from the handler", async () => {
// Allocate the command processor
const processor: CommandProcessor = new CommandProcessor({
Expand Down Expand Up @@ -1340,6 +1391,57 @@ describe("Command Processor", () => {
expect(commandResponse.error?.additionalDetails).not.toBeDefined();
});

it("should handle a handler-error with a v3-format message", async () => {
jest.spyOn(NextVerFeatures, "useV3ErrFormat").mockReturnValue(true);

// Allocate the command processor
const processor: CommandProcessor = new CommandProcessor({
envVariablePrefix: ENV_VAR_PREFIX,
fullDefinition: SAMPLE_COMPLEX_COMMAND,
definition: SAMPLE_COMMAND_REAL_HANDLER,
helpGenerator: FAKE_HELP_GENERATOR,
profileManagerFactory: FAKE_PROFILE_MANAGER_FACTORY,
rootCommandName: SAMPLE_ROOT_COMMAND,
commandLine: "",
promptPhrase: "dummydummy"
});

// Mock read stdin
(SharedOptions.readStdinIfRequested as any) = jest.fn((args, response, type) => {
// Nothing to do
});

// Mock the profile loader
(CommandProfileLoader.loader as any) = jest.fn((args) => {
return {
loadProfiles: (profArgs: any) => {
// Nothing to do
}
};
});

const parms: any = {
arguments: {
_: ["check", "for", "banana"],
$0: "",
valid: true,
rejectWithMessage: true
},
responseFormat: "json",
silent: true
};
const commandResponse: ICommandResponse = await processor.invoke(parms);
expect(commandResponse).toBeDefined();
const stderrText = (commandResponse.stderr as Buffer).toString();
expect(stderrText).toContain("Command Error:");
expect(stderrText).toContain("Rejected with a message");
expect(commandResponse.success).toEqual(false);
expect(commandResponse.exitCode).toEqual(1);
expect(commandResponse.message).toEqual("Rejected with a message");
expect(commandResponse.error?.msg).toEqual("Rejected with a message");
expect(commandResponse.error?.additionalDetails).not.toBeDefined();
});

it("should handle the handler rejecting with no messages", async () => {
// Allocate the command processor
const processor: CommandProcessor = new CommandProcessor({
Expand Down

0 comments on commit fd5cb0f

Please sign in to comment.