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 1 commit
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: 4 additions & 4 deletions src/cli/assert_node_engine_version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ const readActualPackageJSON: () => PackageJSON = () =>
.toString()
)

export function assertNodeEngineVersion(
export function validateNodeEngineVersion(
currentVersion: string,
readPackageJSON: () => PackageJSON = readActualPackageJSON
): void {
): string {
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)
return `Cucumber can only run on Node.js versions ${requiredVersion}. This Node.js version is ${currentVersion}`
}
return null
}
39 changes: 27 additions & 12 deletions src/cli/assert_node_engine_version_spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
import assert from 'assert'
import { assertNodeEngineVersion } from './assert_node_engine_version'
import { expect } from 'chai'
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

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

// Assert
expect(error).to.eql("Cucumber can only run on Node.js versions >=12. This Node.js version is 11.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

// Act
const result = validateNodeEngineVersion('v17.0.0', () => ({
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(result).to.eql(null)
})
})
9 changes: 4 additions & 5 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,10 +13,9 @@ 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
const error = validateNodeEngineVersion(process.version)
if (error != null) {
console.error(error) // eslint-disable-line no-console
process.exit(1)
}

Expand Down