Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid throwing for flow control #1924

Merged
merged 8 commits into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/cli/assert_node_engine_version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ const readActualPackageJSON: () => PackageJSON = () =>
.toString()
)

export function assertNodeEngineVersion(
export function validateNodeEngineVersion(
currentVersion: string,
onError: (message: string) => void,
readPackageJSON: () => PackageJSON = readActualPackageJSON
): void {
const requiredVersion = readPackageJSON().engines.node
if (!semver.satisfies(currentVersion, requiredVersion)) {
const message = `Cucumber can only run on Node.js versions ${requiredVersion}. This Node.js version is ${currentVersion}`
throw new Error(message)
onError(
`Cucumber can only run on Node.js versions ${requiredVersion}. This Node.js version is ${currentVersion}`
)
}
}
43 changes: 31 additions & 12 deletions src/cli/assert_node_engine_version_spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
import assert from 'assert'
import { assertNodeEngineVersion } from './assert_node_engine_version'
import { expect } from 'chai'
import * as sinon from 'sinon'
import { validateNodeEngineVersion } from './assert_node_engine_version'

describe(assertNodeEngineVersion.name, () => {
it('fails when the version is lower than specified in package.json', () => {
assert.throws(() =>
assertNodeEngineVersion('v11.0.0', () => ({
engines: {
node: '>=12',
},
}))
describe(validateNodeEngineVersion.name, () => {
it('returns an error message when the version is lower than specified in package.json', () => {
// Arrange
const errorSpy = sinon.spy()

// Act
validateNodeEngineVersion('v11.0.0', errorSpy, () => ({
engines: {
node: '>=12',
},
}))

// Assert
expect(errorSpy).to.have.been.calledOnceWith(
'Cucumber can only run on Node.js versions >=12. This Node.js version is v11.0.0'
)
})

it('passes when the version is greater than specified in package.json', () => {
assertNodeEngineVersion('v17.0.0')
it('returns null when the version is greater than specified in package.json', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realised we should have changed these example descriptions too: we're not returning anything anymore.

// Arrange
const errorSpy = sinon.spy()

// Act
validateNodeEngineVersion('v17.0.0', errorSpy, () => ({
engines: {
node: '>=12',
},
}))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think its better to hardcode the engines here as well so this doesn't suddenly fail when we no longer support version 17 (probably a little ways off)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯, I missed that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh true, the list right now is 12, 14, 16, 17


// Assert
expect(errorSpy).not.to.have.been.called()
})
})
10 changes: 4 additions & 6 deletions src/cli/run.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Cli, { ICliRunResult } from './'
import VError from 'verror'
import publishBanner from './publish_banner'
import { assertNodeEngineVersion } from './assert_node_engine_version'
import { validateNodeEngineVersion } from './assert_node_engine_version'

function exitWithError(error: Error): void {
console.error(VError.fullStack(error)) // eslint-disable-line no-console
Expand All @@ -13,12 +13,10 @@ function displayPublishAdvertisementBanner(): void {
}

export default async function run(): Promise<void> {
try {
assertNodeEngineVersion(process.version)
} catch (error) {
console.error(error.message) // eslint-disable-line no-console
validateNodeEngineVersion(process.version, (error) => {
console.error(error) // eslint-disable-line no-console
process.exit(1)
}
})

const cli = new Cli({
argv: process.argv,
Expand Down