Skip to content

Commit aa54750

Browse files
committed
catch invalid CYPRESS_ENV value in CLI, close #1621
1 parent 2eb472e commit aa54750

File tree

8 files changed

+103
-3
lines changed

8 files changed

+103
-3
lines changed

cli/__snapshots__/cli_spec.js

+27
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,30 @@ exports['cli -v no binary version 1'] = `
168168
Cypress package version: 1.2.3
169169
Cypress binary version: not installed
170170
`
171+
172+
exports['cli CYPRESS_ENV allows staging environment 1'] = `
173+
code: 0
174+
stderr:
175+
-------
176+
177+
-------
178+
179+
`
180+
181+
exports['cli CYPRESS_ENV catches environment "foo" 1'] = `
182+
code: 11
183+
stderr:
184+
-------
185+
We have detected unknown or unsupported CYPRESS_ENV value
186+
187+
Please unset CYPRESS_ENV variable and run Cypress again
188+
----------
189+
190+
foo
191+
----------
192+
193+
Platform: darwin (16.7.0)
194+
Cypress Version: 0.0.0
195+
-------
196+
197+
`

cli/__snapshots__/errors_spec.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ exports['errors individual has the following errors 1'] = [
66
"versionMismatch",
77
"unexpected",
88
"failedDownload",
9-
"failedUnzip"
9+
"failedUnzip",
10+
"invalidCypressEnv"
1011
]
1112

1213
exports['errors .errors.formErrorText returns fully formed text message 1'] = `

cli/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ switch (args.exec) {
2323

2424
break
2525
default:
26-
// export our node module interface
26+
debug('exporting Cypress module interface')
2727
module.exports = require('./lib/cypress')
2828
}

cli/lib/cli.js

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { oneLine } = require('common-tags')
44
const debug = require('debug')('cypress:cli')
55
const util = require('./util')
66
const logger = require('./logger')
7+
const errors = require('./errors')
78

89
const coerceFalse = (arg) => {
910
return arg !== 'false'
@@ -77,6 +78,11 @@ module.exports = {
7778
args = process.argv
7879
}
7980

81+
if (!util.isValidCypressEnvValue(process.env.CYPRESS_ENV)) {
82+
debug('invalid CYPRESS_ENV value', process.env.CYPRESS_ENV)
83+
return errors.exitWithError(errors.errors.invalidCypressEnv)(process.env.CYPRESS_ENV)
84+
}
85+
8086
const program = new commander.Command()
8187

8288
// bug in commaner not printing name

cli/lib/errors.js

+22
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ const unexpected = {
8989
`,
9090
}
9191

92+
const invalidCypressEnv = {
93+
description: 'We have detected unknown or unsupported CYPRESS_ENV value',
94+
solution: 'Please unset CYPRESS_ENV variable and run Cypress again',
95+
exitCode: 11,
96+
}
97+
9298
const getOsVersion = () => {
9399
if (os.platform() === 'linux') {
94100
return getos()
@@ -171,8 +177,23 @@ const throwFormErrorText = (info) => (msg) => {
171177
.then(raise)
172178
}
173179

180+
/**
181+
* Forms full error message with error and OS details, prints to the error output
182+
* and then exits the process.
183+
* @param {ErrorInformation} info Error information {description, solution}
184+
* @example return exitWithError(errors.invalidCypressEnv)('foo')
185+
*/
186+
const exitWithError = (info) => (msg) => {
187+
return formErrorText(info, msg).then((text) => {
188+
// eslint-disable-next-line no-console
189+
console.error(text)
190+
process.exit(info.exitCode || 1)
191+
})
192+
}
193+
174194
module.exports = {
175195
raise,
196+
exitWithError,
176197
// formError,
177198
formErrorText,
178199
throwFormErrorText,
@@ -185,5 +206,6 @@ module.exports = {
185206
unexpected,
186207
failedDownload,
187208
failedUnzip,
209+
invalidCypressEnv,
188210
},
189211
}

cli/lib/util.js

+20
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,29 @@ function stdoutLineMatches (expectedLine, stdout) {
3636
return lines.some(lineMatches)
3737
}
3838

39+
/**
40+
* Confirms if given value is a valid CYPRESS_ENV value. Undefined values
41+
* are valid, because the system can set the default one.
42+
*
43+
* @param {string} value
44+
* @example util.isValidCypressEnvValue(process.env.CYPRESS_ENV)
45+
*/
46+
function isValidCypressEnvValue (value) {
47+
if (_.isUndefined(value)) {
48+
// will get default value
49+
return true
50+
}
51+
52+
// names of config environments, see "packages/server/config/app.yml"
53+
const names = ['development', 'test', 'staging', 'production']
54+
return _.includes(names, value)
55+
}
56+
3957
const util = {
4058
normalizeModuleOptions,
4159

60+
isValidCypressEnvValue,
61+
4262
isCi () {
4363
return isCi
4464
},

cli/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
"clear-module": "^2.1.0",
7575
"dependency-check": "^2.8.0",
7676
"dtslint": "0.2.0",
77-
"execa-wrap": "1.1.0",
77+
"execa-wrap": "1.4.0",
7878
"nock": "^9.0.9",
7979
"shelljs": "0.7.8",
8080
"sinon": "3.2.1",

cli/test/lib/cli_spec.js

+24
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,30 @@ describe('cli', function () {
4242
)
4343
})
4444

45+
context('CYPRESS_ENV', () => {
46+
it('allows staging environment', () => {
47+
const options = {
48+
env: {
49+
CYPRESS_ENV: 'staging'
50+
},
51+
// we are only interested in the exit code
52+
filter: ['code', 'stderr']
53+
}
54+
return execa('bin/cypress', ['help'], options).then(snapshot)
55+
})
56+
57+
it('catches environment "foo"', () => {
58+
const options = {
59+
env: {
60+
CYPRESS_ENV: 'foo'
61+
},
62+
// we are only interested in the exit code
63+
filter: ['code', 'stderr']
64+
}
65+
return execa('bin/cypress', ['help'], options).then(snapshot)
66+
})
67+
})
68+
4569
context('cypress version', function () {
4670
it('reports package version', function (done) {
4771
this.sandbox.stub(util, 'pkgVersion').returns('1.2.3')

0 commit comments

Comments
 (0)