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

cli: warn when untested node version #1959

Merged
merged 6 commits into from Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/build.yaml
Expand Up @@ -19,6 +19,7 @@ jobs:
os:
- ubuntu-latest
- windows-latest
# these versions must be kept in sync with enginesTested.node in package.json
node-version: [12.x, 14.x, 16.x, 17.x]
fail-fast: false

Expand Down
3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -188,6 +188,9 @@
"engines": {
"node": "12 || 14 || >=16"
},
"enginesTested": {
davidjgoss marked this conversation as resolved.
Show resolved Hide resolved
"node": "12 || 14 || 16 || 17"
},
"dependencies": {
"@cspotcode/source-map-support": "^0.7.0",
"@cucumber/ci-environment": "9.0.0",
Expand Down
12 changes: 8 additions & 4 deletions src/cli/run.ts
Expand Up @@ -16,10 +16,14 @@ function displayPublishAdvertisementBanner(): void {
}

export default async function run(): Promise<void> {
validateNodeEngineVersion(process.version, (error) => {
console.error(error)
process.exit(1)
})
validateNodeEngineVersion(
process.version,
(error) => {
console.error(error)
process.exit(1)
},
console.warn
)

const cli = new Cli({
argv: process.argv,
Expand Down
13 changes: 10 additions & 3 deletions src/cli/validate_node_engine_version.ts
Expand Up @@ -4,6 +4,7 @@ import semver from 'semver'

type PackageJSON = {
engines: { node: string }
enginesTested: { node: string }
}

const readActualPackageJSON: () => PackageJSON = () =>
Expand All @@ -16,12 +17,18 @@ const readActualPackageJSON: () => PackageJSON = () =>
export function validateNodeEngineVersion(
currentVersion: string,
onError: (message: string) => void,
onWarning: (message: string) => void,
readPackageJSON: () => PackageJSON = readActualPackageJSON
): void {
const requiredVersion = readPackageJSON().engines.node
if (!semver.satisfies(currentVersion, requiredVersion)) {
const requiredVersions = readPackageJSON().engines.node
const testedVersions = readPackageJSON().enginesTested.node
if (!semver.satisfies(currentVersion, requiredVersions)) {
onError(
`Cucumber can only run on Node.js versions ${requiredVersion}. This Node.js version is ${currentVersion}`
`Cucumber can only run on Node.js versions ${requiredVersions}. This Node.js version is ${currentVersion}`
)
} else if (!semver.satisfies(currentVersion, testedVersions)) {
onWarning(
`This Node.js version (${currentVersion}) has not been tested with this version of Cucumber; it should work normally, but please raise an issue if you see anything unexpected.`
)
}
}
45 changes: 24 additions & 21 deletions src/cli/validate_node_engine_version_spec.ts
Expand Up @@ -3,66 +3,69 @@ import * as sinon from 'sinon'
import { validateNodeEngineVersion } from './validate_node_engine_version'

describe(validateNodeEngineVersion.name, () => {
const readPackageJSON = () => ({
davidjgoss marked this conversation as resolved.
Show resolved Hide resolved
engines: {
node: '12 || 14 || >=16',
},
enginesTested: {
node: '12 || 14 || 16 || 17',
},
})

it('calls the onError callback when the version is lower than any of our supported versions', () => {
// Arrange
const errorSpy = sinon.spy()
const warningSpy = sinon.spy()

// Act
validateNodeEngineVersion('v11.1.2', errorSpy, () => ({
engines: {
node: '12 || 14 || >=16',
},
}))
validateNodeEngineVersion('v11.1.2', errorSpy, warningSpy, readPackageJSON)

// Assert
expect(errorSpy).to.have.been.calledOnceWith(
'Cucumber can only run on Node.js versions 12 || 14 || >=16. This Node.js version is v11.1.2'
)
expect(warningSpy).not.to.have.been.called()
})

it('calls the onError callback when the version is between our supported versions', () => {
// Arrange
const errorSpy = sinon.spy()
const warningSpy = sinon.spy()

validateNodeEngineVersion('v13.1.2', errorSpy, () => ({
engines: {
node: '12 || 14 || >=16',
},
}))
validateNodeEngineVersion('v13.1.2', errorSpy, warningSpy, readPackageJSON)

// Assert
expect(errorSpy).to.have.been.calledOnceWith(
'Cucumber can only run on Node.js versions 12 || 14 || >=16. This Node.js version is v13.1.2'
)
expect(warningSpy).not.to.have.been.called()
})

it('does not call the onError when the version is one of our supported versions', () => {
it('does not call onError or onWarning when the version is one of our supported versions', () => {
// Arrange
const errorSpy = sinon.spy()
const warningSpy = sinon.spy()

// Act
validateNodeEngineVersion('v17.1.2', errorSpy, () => ({
engines: {
node: '12 || 14 || >=16',
},
}))
validateNodeEngineVersion('v17.1.2', errorSpy, warningSpy, readPackageJSON)

// Assert
expect(errorSpy).not.to.have.been.called()
expect(warningSpy).not.to.have.been.called()
})

it('does not call onError when the version is a version that isnt out yet at time of release', () => {
// Arrange
const errorSpy = sinon.spy()
const warningSpy = sinon.spy()

// Act
validateNodeEngineVersion('v18.0.0', errorSpy, () => ({
engines: {
node: '12 || 14 || >=16',
},
}))
validateNodeEngineVersion('v18.0.0', errorSpy, warningSpy, readPackageJSON)

// Assert
expect(errorSpy).not.to.have.been.called()
expect(warningSpy).to.have.been.calledOnceWith(
`This Node.js version (v18.0.0) has not been tested with this version of Cucumber; it should work normally, but please raise an issue if you see anything unexpected.`
)
})
})