From 4458e7ec71f74137b5c31fec033eb4c3236e5288 Mon Sep 17 00:00:00 2001 From: Brian Stack Date: Tue, 26 Nov 2024 10:41:41 -0800 Subject: [PATCH] feat: allow configuring the CLI to error on no matching tests --- packages/cli/e2e/__tests__/trigger.spec.ts | 16 ++++++++++++++++ packages/cli/src/commands/trigger.ts | 12 ++++++++++++ .../cli/src/services/checkly-config-loader.ts | 1 + 3 files changed, 29 insertions(+) diff --git a/packages/cli/e2e/__tests__/trigger.spec.ts b/packages/cli/e2e/__tests__/trigger.spec.ts index 780755d4..293bf67c 100644 --- a/packages/cli/e2e/__tests__/trigger.spec.ts +++ b/packages/cli/e2e/__tests__/trigger.spec.ts @@ -65,4 +65,20 @@ describe('trigger', () => { expect(result.stdout).toContain('No matching checks were found.') expect(result.status).toBe(0) }) + + test('Should return code 1 when no checks match and the fail-on-no-match flag is set', async () => { + const result = await runChecklyCli({ + args: [ + 'trigger', + '--tags', + 'no-checks-match-this-tag', + '--fail-on-no-matching', + ], + apiKey: config.get('apiKey'), + accountId: config.get('accountId'), + }) + + expect(result.stdout).toContain('No matching checks were found.') + expect(result.status).toBe(1) + }) }) diff --git a/packages/cli/src/commands/trigger.ts b/packages/cli/src/commands/trigger.ts index 0315e7e1..95e16cd1 100644 --- a/packages/cli/src/commands/trigger.ts +++ b/packages/cli/src/commands/trigger.ts @@ -58,6 +58,9 @@ export default class Trigger extends AuthCommand { description: 'Always show the full logs of the checks.', allowNo: true, }), + 'fail-on-no-matching': Flags.boolean({ + description: 'Exit with a failing status code when there are no matching tests.', + }), reporter: Flags.string({ char: 'r', description: 'A list of custom reporters for the test output.', @@ -96,6 +99,7 @@ export default class Trigger extends AuthCommand { tags: targetTags, timeout, verbose: verboseFlag, + 'fail-on-no-matching': failOnNoMatchingFlag, record: shouldRecord, reporter: reporterFlag, env, @@ -116,6 +120,7 @@ export default class Trigger extends AuthCommand { privateRunLocation, }) const verbose = this.prepareVerboseFlag(verboseFlag, checklyConfig?.cli?.verbose) + const failOnNoMatching = this.prepareFailOnNoMatching(failOnNoMatchingFlag, checklyConfig?.cli?.failOnNoMatching) const reporterTypes = this.prepareReportersTypes(reporterFlag as ReporterType, checklyConfig?.cli?.reporters) const reporters = createReporters(reporterTypes, location, verbose) const testRetryStrategy = this.prepareTestRetryStrategy(retries, checklyConfig?.cli?.retries) @@ -167,6 +172,9 @@ export default class Trigger extends AuthCommand { if (err instanceof NoMatchingChecksError) { // For consistency with `checkly test`, we log a message and exit with code 0. this.log('No matching checks were found.') + if (failOnNoMatching) { + process.exitCode = 1 + } return } reporters.forEach(r => r.onError(err)) @@ -221,6 +229,10 @@ export default class Trigger extends AuthCommand { return verboseFlag ?? cliVerboseFlag ?? false } + prepareFailOnNoMatching (failOnNoMatchingFlag?: boolean, cliFailOnNoMatchingFlag?: boolean) { + return failOnNoMatchingFlag ?? cliFailOnNoMatchingFlag ?? false + } + prepareReportersTypes (reporterFlag: ReporterType, cliReporters: ReporterType[] = []): ReporterType[] { if (!reporterFlag && !cliReporters.length) { return [isCI ? 'ci' : 'list'] diff --git a/packages/cli/src/services/checkly-config-loader.ts b/packages/cli/src/services/checkly-config-loader.ts index eef8fbb2..250a7598 100644 --- a/packages/cli/src/services/checkly-config-loader.ts +++ b/packages/cli/src/services/checkly-config-loader.ts @@ -67,6 +67,7 @@ export type ChecklyConfig = { runLocation?: keyof Region, privateRunLocation?: string, verbose?: boolean, + failOnNoMatching?: boolean, reporters?: ReporterType[], retries?: number, }