diff --git a/src/cli.ts b/src/cli.ts index 4d339fb..bfecf48 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -25,7 +25,9 @@ const createCommandWithCommonOptions = (command: string): Command => { .option("-j, --json", "Output raw JSON response"); }; -export const run = (): void => { +const splitter = (value: string): string[] => value.split(/,| /); + +export const buildCmd = (): Command => { program .name("octomind-cli") .description( @@ -38,7 +40,7 @@ export const run = (): void => { .requiredOption("-u, --url ", "URL to test") .option("-e, --environment ", "Environment name", "default") .option("-d, --description ", "Test description") - .option("-g, --tags ", "comma separated list of tags") + .option("-g, --tags ", "comma separated list of tags", splitter) .action(executeTests); createCommandWithCommonOptions("report") @@ -109,6 +111,5 @@ export const run = (): void => { .requiredOption("-t, --test-target-id ", "Test target ID") .requiredOption("-e, --environment-id ", "Environment ID") .action(deleteEnvironment); - - program.parse(); + return program; }; diff --git a/src/index.ts b/src/index.ts index e013f8c..9d0edf9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,4 @@ #!/usr/bin/env node -import { run } from "./cli"; -run(); +import { buildCmd } from "./cli"; +const cmd = buildCmd(); +cmd.parse(); diff --git a/tests/cli.spec.ts b/tests/cli.spec.ts new file mode 100644 index 0000000..ee53a91 --- /dev/null +++ b/tests/cli.spec.ts @@ -0,0 +1,75 @@ +import { program } from "commander"; +import { buildCmd } from "../src/cli"; +import { executeTests } from "../src/api"; + +jest.mock("../src/api"); + +describe("CLI Commands parsing options", () => { + const stdArgs = [ + "node", + "cli.js", + "execute", + "--api-key", + "test-api-key", + "--test-target-id", + "test-target-id", + "--url", + "https://example.com", + ]; + + beforeAll(() => { + buildCmd(); + }); + + it("should parse executeTests tags option with comma", () => { + program.exitOverride((err) => { + throw err; + }); + program.parse([...stdArgs, "--tags", "tag1,tags2"]); + expect(executeTests).toHaveBeenCalledWith( + expect.objectContaining({ + tags: ["tag1", "tags2"], + }), + expect.anything(), + ); + }); + + it("should parse executeTests tags option with space", () => { + program.exitOverride((err) => { + throw err; + }); + program.parse([...stdArgs, "--tags", "tag1 tags2"]); + expect(executeTests).toHaveBeenCalledWith( + expect.objectContaining({ + tags: ["tag1", "tags2"], + }), + expect.anything(), + ); + }); + + it("should parse executeTests tags option with |", () => { + program.exitOverride((err) => { + throw err; + }); + program.parse([...stdArgs, "--tags", "tag1|tags2"]); + expect(executeTests).toHaveBeenCalledWith( + expect.objectContaining({ + tags: ["tag1", "tags2"], + }), + expect.anything(), + ); + }); + + it("should parse executeTests tags option always as array", () => { + program.exitOverride((err) => { + throw err; + }); + program.parse([...stdArgs, "--tags", "tag1"]); + expect(executeTests).toHaveBeenCalledWith( + expect.objectContaining({ + tags: ["tag1"], + }), + expect.anything(), + ); + }); +});