From 8e50e1050c76988a6ac7ae79d1572ee476350a05 Mon Sep 17 00:00:00 2001 From: Stefan Rinke Date: Mon, 3 Mar 2025 21:59:20 +0100 Subject: [PATCH 1/3] fix tags --- src/cli.ts | 9 +++++---- src/index.ts | 5 +++-- tests/cli.spec.ts | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 tests/cli.spec.ts 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..06619b1 --- /dev/null +++ b/tests/cli.spec.ts @@ -0,0 +1,49 @@ +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(), + ); + }); +}); From 555c15b527f9dafda9f37d03b1d5107072e810b0 Mon Sep 17 00:00:00 2001 From: Stefan Rinke Date: Tue, 4 Mar 2025 11:41:36 +0100 Subject: [PATCH 2/3] more tests --- tests/cli.spec.ts | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/cli.spec.ts b/tests/cli.spec.ts index 06619b1..760867d 100644 --- a/tests/cli.spec.ts +++ b/tests/cli.spec.ts @@ -46,4 +46,31 @@ describe("CLI Commands parsing options", () => { 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(), + ); + }); + }); From 4609cbe015980504fc9849e76d15dccd3c283fa1 Mon Sep 17 00:00:00 2001 From: Stefan Rinke Date: Tue, 4 Mar 2025 11:42:48 +0100 Subject: [PATCH 3/3] lint --- tests/cli.spec.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/cli.spec.ts b/tests/cli.spec.ts index 760867d..ee53a91 100644 --- a/tests/cli.spec.ts +++ b/tests/cli.spec.ts @@ -72,5 +72,4 @@ describe("CLI Commands parsing options", () => { expect.anything(), ); }); - });