Skip to content

Commit

Permalink
Add exitCode to CLI test (#205)
Browse files Browse the repository at this point in the history
* Add exitCode to CLI test

* Narrow down exitCode assignment

* Add changeset
  • Loading branch information
nayres authored Apr 25, 2023
1 parent 5baa5d2 commit d28384c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/late-hounds-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hashicorp/platform-content-conformance': patch
---

Added non-zero exit code for Content Conformance CLI
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions packages/content-conformance/src/__tests__/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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()
})
})
6 changes: 5 additions & 1 deletion packages/content-conformance/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -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

0 comments on commit d28384c

Please sign in to comment.