Skip to content

Commit

Permalink
catch invalid CYPRESS_ENV value in CLI, close #1621
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Apr 20, 2018
1 parent 2eb472e commit aa54750
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 3 deletions.
27 changes: 27 additions & 0 deletions cli/__snapshots__/cli_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,30 @@ exports['cli -v no binary version 1'] = `
Cypress package version: 1.2.3
Cypress binary version: not installed
`

exports['cli CYPRESS_ENV allows staging environment 1'] = `
code: 0
stderr:
-------
-------
`

exports['cli CYPRESS_ENV catches environment "foo" 1'] = `
code: 11
stderr:
-------
We have detected unknown or unsupported CYPRESS_ENV value
Please unset CYPRESS_ENV variable and run Cypress again
----------
foo
----------
Platform: darwin (16.7.0)
Cypress Version: 0.0.0
-------
`
3 changes: 2 additions & 1 deletion cli/__snapshots__/errors_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ exports['errors individual has the following errors 1'] = [
"versionMismatch",
"unexpected",
"failedDownload",
"failedUnzip"
"failedUnzip",
"invalidCypressEnv"
]

exports['errors .errors.formErrorText returns fully formed text message 1'] = `
Expand Down
2 changes: 1 addition & 1 deletion cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ switch (args.exec) {

break
default:
// export our node module interface
debug('exporting Cypress module interface')
module.exports = require('./lib/cypress')
}
6 changes: 6 additions & 0 deletions cli/lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { oneLine } = require('common-tags')
const debug = require('debug')('cypress:cli')
const util = require('./util')
const logger = require('./logger')
const errors = require('./errors')

const coerceFalse = (arg) => {
return arg !== 'false'
Expand Down Expand Up @@ -77,6 +78,11 @@ module.exports = {
args = process.argv
}

if (!util.isValidCypressEnvValue(process.env.CYPRESS_ENV)) {
debug('invalid CYPRESS_ENV value', process.env.CYPRESS_ENV)
return errors.exitWithError(errors.errors.invalidCypressEnv)(process.env.CYPRESS_ENV)
}

const program = new commander.Command()

// bug in commaner not printing name
Expand Down
22 changes: 22 additions & 0 deletions cli/lib/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ const unexpected = {
`,
}

const invalidCypressEnv = {
description: 'We have detected unknown or unsupported CYPRESS_ENV value',
solution: 'Please unset CYPRESS_ENV variable and run Cypress again',
exitCode: 11,
}

const getOsVersion = () => {
if (os.platform() === 'linux') {
return getos()
Expand Down Expand Up @@ -171,8 +177,23 @@ const throwFormErrorText = (info) => (msg) => {
.then(raise)
}

/**
* Forms full error message with error and OS details, prints to the error output
* and then exits the process.
* @param {ErrorInformation} info Error information {description, solution}
* @example return exitWithError(errors.invalidCypressEnv)('foo')
*/
const exitWithError = (info) => (msg) => {
return formErrorText(info, msg).then((text) => {
// eslint-disable-next-line no-console
console.error(text)
process.exit(info.exitCode || 1)
})
}

module.exports = {
raise,
exitWithError,
// formError,
formErrorText,
throwFormErrorText,
Expand All @@ -185,5 +206,6 @@ module.exports = {
unexpected,
failedDownload,
failedUnzip,
invalidCypressEnv,
},
}
20 changes: 20 additions & 0 deletions cli/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,29 @@ function stdoutLineMatches (expectedLine, stdout) {
return lines.some(lineMatches)
}

/**
* Confirms if given value is a valid CYPRESS_ENV value. Undefined values
* are valid, because the system can set the default one.
*
* @param {string} value
* @example util.isValidCypressEnvValue(process.env.CYPRESS_ENV)
*/
function isValidCypressEnvValue (value) {
if (_.isUndefined(value)) {
// will get default value
return true
}

// names of config environments, see "packages/server/config/app.yml"
const names = ['development', 'test', 'staging', 'production']
return _.includes(names, value)
}

const util = {
normalizeModuleOptions,

isValidCypressEnvValue,

isCi () {
return isCi
},
Expand Down
2 changes: 1 addition & 1 deletion cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"clear-module": "^2.1.0",
"dependency-check": "^2.8.0",
"dtslint": "0.2.0",
"execa-wrap": "1.1.0",
"execa-wrap": "1.4.0",
"nock": "^9.0.9",
"shelljs": "0.7.8",
"sinon": "3.2.1",
Expand Down
24 changes: 24 additions & 0 deletions cli/test/lib/cli_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,30 @@ describe('cli', function () {
)
})

context('CYPRESS_ENV', () => {
it('allows staging environment', () => {
const options = {
env: {
CYPRESS_ENV: 'staging'
},
// we are only interested in the exit code
filter: ['code', 'stderr']
}
return execa('bin/cypress', ['help'], options).then(snapshot)
})

it('catches environment "foo"', () => {
const options = {
env: {
CYPRESS_ENV: 'foo'
},
// we are only interested in the exit code
filter: ['code', 'stderr']
}
return execa('bin/cypress', ['help'], options).then(snapshot)
})
})

context('cypress version', function () {
it('reports package version', function (done) {
this.sandbox.stub(util, 'pkgVersion').returns('1.2.3')
Expand Down

0 comments on commit aa54750

Please sign in to comment.