From d28384c7f5229ec17c8b381127efa40e12359c01 Mon Sep 17 00:00:00 2001 From: Nick Ayres <40183141+nayres@users.noreply.github.com> Date: Tue, 25 Apr 2023 09:44:50 -0700 Subject: [PATCH] Add exitCode to CLI test (#205) * Add exitCode to CLI test * Narrow down exitCode assignment * Add changeset --- .changeset/late-hounds-refuse.md | 5 ++++ package-lock.json | 6 ++--- .../src/__tests__/cli.test.ts | 25 +++++++++++++++++++ packages/content-conformance/src/cli.ts | 6 ++++- 4 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 .changeset/late-hounds-refuse.md diff --git a/.changeset/late-hounds-refuse.md b/.changeset/late-hounds-refuse.md new file mode 100644 index 00000000..34a01b9d --- /dev/null +++ b/.changeset/late-hounds-refuse.md @@ -0,0 +1,5 @@ +--- +'@hashicorp/platform-content-conformance': patch +--- + +Added non-zero exit code for Content Conformance CLI diff --git a/package-lock.json b/package-lock.json index 826a3840..12e853cb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -38667,7 +38667,7 @@ }, "packages/cli": { "name": "@hashicorp/platform-cli", - "version": "2.6.0", + "version": "2.7.0", "license": "MPL-2.0", "dependencies": { "@hashicorp/platform-cms": "0.3.0", @@ -42060,7 +42060,7 @@ }, "packages/tools": { "name": "@hashicorp/platform-tools", - "version": "0.8.0", + "version": "0.10.0", "license": "MPL-2.0", "dependencies": { "@actions/core": "^1.10.0", @@ -44203,7 +44203,7 @@ "inquirer": "7.3.3", "lint-staged": "11.1.2", "open": "7.3.0", - "prettier": "2.8.7", + "prettier": "^2.8.7", "readdirp": "3.5.0", "signale": "1.4.0", "slugify": "1.4.6", diff --git a/packages/content-conformance/src/__tests__/cli.test.ts b/packages/content-conformance/src/__tests__/cli.test.ts index d392e2c7..bf7f55f6 100644 --- a/packages/content-conformance/src/__tests__/cli.test.ts +++ b/packages/content-conformance/src/__tests__/cli.test.ts @@ -5,6 +5,19 @@ import { currentFilePath, getFixturePath } from '../test/utils' const fixturePath = getFixturePath('basic-with-content-files') function execCli(...args: string[]) { + try { + return String( + execFileSync('node', [ + path.join(currentFilePath, '../../..', '/dist/cli.js'), + ...args, + ]) + ) + } catch (error) { + return String(error.stdout) + } +} + +function execCliExitCode(...args: string[]) { return String( execFileSync('node', [ path.join(currentFilePath, '../../..', '/dist/cli.js'), @@ -116,4 +129,16 @@ describe('Content-conformance CLI', () => { " `) }) + + test('Returns non-zero exit code on failure', () => { + expect(() => + execCliExitCode( + '--cwd', + `${fixturePath}`, + `./content/index.mdx`, + '--config', + './content-conformance.config.mjs' + ) + ).toThrow() + }) }) diff --git a/packages/content-conformance/src/cli.ts b/packages/content-conformance/src/cli.ts index 8cb29c87..71eb14e7 100755 --- a/packages/content-conformance/src/cli.ts +++ b/packages/content-conformance/src/cli.ts @@ -2,7 +2,7 @@ import yargs from 'yargs' import { hideBin } from 'yargs/helpers' import chalk from 'chalk' -import { ContentConformanceRunner } from './runner.js' +import { ContentConformanceRunner, RunnerStatus } from './runner.js' // Disable colored output when running in a test environment so we can snapshot the CLI output without the color codes. if (process.env.NODE_ENV === 'test') { @@ -94,5 +94,9 @@ yargs(hideBin(process.argv)).command( console.log('') console.log(chalk.redBright(stack)) } + + if (runner.status !== RunnerStatus.SUCCESS) { + process.exitCode = 1 + } } ).argv