|
| 1 | +import { Command, OptionValues } from "commander"; |
| 2 | +import { runCli } from "../utls/cli-runner"; |
| 3 | +import { mockAxiosGet, mockAxiosGetError } from "../utls/http-requests-mock"; |
| 4 | +import { |
| 5 | + ASSET_REGISTRY_DISABLED_ERROR, |
| 6 | + ASSET_REGISTRY_DISABLED_USER_MESSAGE, |
| 7 | +} from "../../src/commands/asset-registry/asset-registry-error"; |
| 8 | +import { AssetRegistryMetadata } from "../../src/commands/asset-registry/asset-registry.interfaces"; |
| 9 | +import { Configurator, IModule } from "../../src/core/command/module-handler"; |
| 10 | +import { Context } from "../../src/core/command/cli-context"; |
| 11 | +import { FatalError, GracefulError } from "../../src/core/utils/logger"; |
| 12 | + |
| 13 | +import AssetRegistryModule = require("../../src/commands/asset-registry/module"); |
| 14 | + |
| 15 | +const GRACEFUL_MESSAGE = "graceful failure - should not fail the process"; |
| 16 | + |
| 17 | +class DiagnosticsModule extends IModule { |
| 18 | + public register(context: Context, configurator: Configurator): void { |
| 19 | + const diag = configurator.command("diag").description("Diagnostics test commands"); |
| 20 | + |
| 21 | + diag.command("graceful") |
| 22 | + .description("Throws a GracefulError") |
| 23 | + .action(async (_ctx: Context, _cmd: Command, _opts: OptionValues): Promise<void> => { |
| 24 | + throw new GracefulError(GRACEFUL_MESSAGE); |
| 25 | + }); |
| 26 | + |
| 27 | + diag.command("fatal") |
| 28 | + .description("Throws a FatalError") |
| 29 | + .action(async (_ctx: Context, _cmd: Command, _opts: OptionValues): Promise<void> => { |
| 30 | + throw new FatalError("boom"); |
| 31 | + }); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +describe("CLI process output and exit codes", () => { |
| 36 | + const TYPES_URL = "https://myTeam.celonis.cloud/pacman/api/core/asset-registry/types"; |
| 37 | + |
| 38 | + const metadata: AssetRegistryMetadata = { |
| 39 | + types: { |
| 40 | + BOARD_V2: { |
| 41 | + assetType: "BOARD_V2", |
| 42 | + displayName: "View", |
| 43 | + description: null, |
| 44 | + group: "DASHBOARDS", |
| 45 | + assetSchema: { version: "2.1.0" }, |
| 46 | + service: { basePath: "/blueprint/api" }, |
| 47 | + endpoints: { |
| 48 | + schema: "/schema/board_v2", |
| 49 | + validate: "/validate/board_v2", |
| 50 | + examples: "/examples/board_v2", |
| 51 | + }, |
| 52 | + contributions: { pigEntityTypes: [], dataPipelineEntityTypes: [], actionTypes: [] }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + }; |
| 56 | + |
| 57 | + describe("successful commands", () => { |
| 58 | + it("Should print the version and exit with code 0", async () => { |
| 59 | + const result = await runCli(["-V"], [AssetRegistryModule]); |
| 60 | + |
| 61 | + expect(result.exitCode).toBe(0); |
| 62 | + expect(result.stdout).toContain("test-version"); |
| 63 | + }); |
| 64 | + |
| 65 | + it("Should print asset types to stdout and exit with code 0", async () => { |
| 66 | + mockAxiosGet(TYPES_URL, metadata); |
| 67 | + |
| 68 | + const result = await runCli(["asset-registry", "list"], [AssetRegistryModule]); |
| 69 | + |
| 70 | + expect(result.exitCode).toBe(0); |
| 71 | + expect(result.output).toContain("BOARD_V2 - View [DASHBOARDS]"); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + describe("failing commands produce a non-zero exit code", () => { |
| 76 | + it("Should exit non-zero and report the friendly message when the feature flag is disabled", async () => { |
| 77 | + mockAxiosGetError(TYPES_URL, 403, { error: ASSET_REGISTRY_DISABLED_ERROR }); |
| 78 | + |
| 79 | + const result = await runCli(["asset-registry", "list"], [AssetRegistryModule]); |
| 80 | + |
| 81 | + expect(result.exitCode).toBe(1); |
| 82 | + expect(result.output).toContain(ASSET_REGISTRY_DISABLED_USER_MESSAGE); |
| 83 | + }); |
| 84 | + |
| 85 | + it("Should exit non-zero for an unknown command", async () => { |
| 86 | + const result = await runCli(["this-command-does-not-exist"], [AssetRegistryModule]); |
| 87 | + |
| 88 | + expect(result.exitCode).toBe(1); |
| 89 | + }); |
| 90 | + |
| 91 | + it("Should exit non-zero when a required option is missing", async () => { |
| 92 | + const result = await runCli(["asset-registry", "get"], [AssetRegistryModule]); |
| 93 | + |
| 94 | + expect(result.exitCode).toBe(1); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe("action-wrapper error semantics", () => { |
| 99 | + it("Should report a GracefulError without forcing a non-zero exit code", async () => { |
| 100 | + const result = await runCli(["diag", "graceful"], [DiagnosticsModule]); |
| 101 | + |
| 102 | + expect(result.exitCode).toBe(0); |
| 103 | + expect(result.output).toContain(GRACEFUL_MESSAGE); |
| 104 | + }); |
| 105 | + |
| 106 | + it("Should exit non-zero for a regular error thrown by a command", async () => { |
| 107 | + const result = await runCli(["diag", "fatal"], [DiagnosticsModule]); |
| 108 | + |
| 109 | + expect(result.exitCode).toBe(1); |
| 110 | + }); |
| 111 | + }); |
| 112 | +}); |
0 commit comments