Skip to content

Commit 3d5cf50

Browse files
bahmutovgrabartley
authored andcommitted
Catch env variable with reserved name CYPRESS_ENV 1621 (cypress-io#1626)
* server: check CYPRESS_ENV variable when merging configs * catch invalid CYPRESS_ENV value in CLI, close cypress-io#1621 * linting * sanitize platform in test snapshot * linting * update error message text * add missing comma * fix finally merge in JS code * pass CLI linter * fix log reference, should be debug * use correct sinon reference * update message, show first part in red * update error message text
1 parent 3fe7fb1 commit 3d5cf50

File tree

11 files changed

+293
-51
lines changed

11 files changed

+293
-51
lines changed

cli/__snapshots__/cli_spec.js

+29
Original file line numberDiff line numberDiff line change
@@ -354,3 +354,32 @@ exports['shows help for run --foo 1'] = `
354354
-------
355355
356356
`
357+
358+
exports['cli CYPRESS_ENV allows staging environment 1'] = `
359+
code: 0
360+
stderr:
361+
-------
362+
363+
-------
364+
365+
`
366+
367+
exports['cli CYPRESS_ENV catches environment "foo" 1'] = `
368+
code: 11
369+
stderr:
370+
-------
371+
The environment variable with the reserved name "CYPRESS_ENV" is set.
372+
373+
Unset the "CYPRESS_ENV" environment variable and run Cypress again.
374+
375+
----------
376+
377+
CYPRESS_ENV=foo
378+
379+
----------
380+
381+
Platform: xxx
382+
Cypress Version: 1.2.3
383+
-------
384+
385+
`

cli/__snapshots__/errors_spec.js

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ exports['errors individual has the following errors 1'] = [
3232
"failedDownload",
3333
"failedUnzip",
3434
"invalidCacheDirectory",
35+
"invalidCypressEnv",
3536
"invalidSmokeTestDisplayError",
3637
"missingApp",
3738
"missingDependency",

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

+86-21
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const logSymbols = require('log-symbols')
55
const debug = require('debug')('cypress:cli')
66
const util = require('./util')
77
const logger = require('./logger')
8+
const errors = require('./errors')
89
const cache = require('./tasks/cache')
910

1011
// patch "commander" method called when a user passed an unknown option
@@ -27,7 +28,9 @@ const coerceFalse = (arg) => {
2728
const spaceDelimitedSpecsMsg = (files) => {
2829
logger.log()
2930
logger.warn(stripIndent`
30-
${logSymbols.warning} Warning: It looks like you're passing --spec a space-separated list of files:
31+
${
32+
logSymbols.warning
33+
} Warning: It looks like you're passing --spec a space-separated list of files:
3134
3235
"${files.join(' ')}"
3336
@@ -54,7 +57,8 @@ const parseVariableOpts = (fnArgs, args) => {
5457
const nextOptOffset = _.findIndex(_.slice(args, argIndex), (arg) => {
5558
return _.startsWith(arg, '--')
5659
})
57-
const endIndex = nextOptOffset !== -1 ? argIndex + nextOptOffset : args.length
60+
const endIndex =
61+
nextOptOffset !== -1 ? argIndex + nextOptOffset : args.length
5862

5963
const maybeSpecs = _.slice(args, argIndex, endIndex)
6064
const extraSpecs = _.intersection(maybeSpecs, fnArgs)
@@ -70,11 +74,34 @@ const parseVariableOpts = (fnArgs, args) => {
7074
}
7175

7276
const parseOpts = (opts) => {
73-
opts = _.pick(opts,
74-
'project', 'spec', 'reporter', 'reporterOptions', 'path', 'destination',
75-
'port', 'env', 'cypressVersion', 'config', 'record', 'key',
76-
'browser', 'detached', 'headed', 'global', 'dev', 'force', 'exit',
77-
'cachePath', 'cacheList', 'cacheClear', 'parallel', 'group', 'ciBuildId')
77+
opts = _.pick(
78+
opts,
79+
'project',
80+
'spec',
81+
'reporter',
82+
'reporterOptions',
83+
'path',
84+
'destination',
85+
'port',
86+
'env',
87+
'cypressVersion',
88+
'config',
89+
'record',
90+
'key',
91+
'browser',
92+
'detached',
93+
'headed',
94+
'global',
95+
'dev',
96+
'force',
97+
'exit',
98+
'cachePath',
99+
'cacheList',
100+
'cacheClear',
101+
'parallel',
102+
'group',
103+
'ciBuildId'
104+
)
78105

79106
if (opts.exit) {
80107
opts = _.omit(opts, 'exit')
@@ -86,16 +113,23 @@ const parseOpts = (opts) => {
86113
}
87114

88115
const descriptions = {
89-
record: 'records the run. sends test results, screenshots and videos to your Cypress Dashboard.',
90-
key: 'your secret Record Key. you can omit this if you set a CYPRESS_RECORD_KEY environment variable.',
116+
record:
117+
'records the run. sends test results, screenshots and videos to your Cypress Dashboard.',
118+
key:
119+
'your secret Record Key. you can omit this if you set a CYPRESS_RECORD_KEY environment variable.',
91120
spec: 'runs a specific spec file. defaults to "all"',
92-
reporter: 'runs a specific mocha reporter. pass a path to use a custom reporter. defaults to "spec"',
121+
reporter:
122+
'runs a specific mocha reporter. pass a path to use a custom reporter. defaults to "spec"',
93123
reporterOptions: 'options for the mocha reporter. defaults to "null"',
94124
port: 'runs Cypress on a specific port. overrides any value in cypress.json.',
95-
env: 'sets environment variables. separate multiple values with a comma. overrides any value in cypress.json or cypress.env.json',
96-
config: 'sets configuration values. separate multiple values with a comma. overrides any value in cypress.json.',
97-
browserRunMode: 'runs Cypress in the browser with the given name. if a filesystem path is supplied, Cypress will attempt to use the browser at that path.',
98-
browserOpenMode: 'path to a custom browser to be added to the list of available browsers in Cypress',
125+
env:
126+
'sets environment variables. separate multiple values with a comma. overrides any value in cypress.json or cypress.env.json',
127+
config:
128+
'sets configuration values. separate multiple values with a comma. overrides any value in cypress.json.',
129+
browserRunMode:
130+
'runs Cypress in the browser with the given name. if a filesystem path is supplied, Cypress will attempt to use the browser at that path.',
131+
browserOpenMode:
132+
'path to a custom browser to be added to the list of available browsers in Cypress',
99133
detached: 'runs Cypress application in detached mode',
100134
project: 'path to the project',
101135
global: 'force Cypress into global mode as if its globally installed',
@@ -108,11 +142,25 @@ const descriptions = {
108142
cacheList: 'list cached binary versions',
109143
cacheClear: 'delete all cached binaries',
110144
group: 'a named group for recorded runs in the Cypress dashboard',
111-
parallel: 'enables concurrent runs and automatic load balancing of specs across multiple machines or processes',
112-
ciBuildId: 'the unique identifier for a run on your CI provider. typically a "BUILD_ID" env var. this value is automatically detected for most CI providers',
145+
parallel:
146+
'enables concurrent runs and automatic load balancing of specs across multiple machines or processes',
147+
ciBuildId:
148+
'the unique identifier for a run on your CI provider. typically a "BUILD_ID" env var. this value is automatically detected for most CI providers',
113149
}
114150

115-
const knownCommands = ['version', 'run', 'open', 'install', 'verify', '-v', '--version', 'help', '-h', '--help', 'cache']
151+
const knownCommands = [
152+
'version',
153+
'run',
154+
'open',
155+
'install',
156+
'verify',
157+
'-v',
158+
'--version',
159+
'help',
160+
'-h',
161+
'--help',
162+
'cache',
163+
]
116164

117165
const text = (description) => {
118166
if (!descriptions[description]) {
@@ -123,9 +171,11 @@ const text = (description) => {
123171
}
124172

125173
function includesVersion (args) {
126-
return _.includes(args, 'version') ||
174+
return (
175+
_.includes(args, 'version') ||
127176
_.includes(args, '--version') ||
128177
_.includes(args, '-v')
178+
)
129179
}
130180

131181
function showVersions () {
@@ -147,6 +197,14 @@ module.exports = {
147197
args = process.argv
148198
}
149199

200+
if (!util.isValidCypressEnvValue(process.env.CYPRESS_ENV)) {
201+
debug('invalid CYPRESS_ENV value', process.env.CYPRESS_ENV)
202+
203+
return errors.exitWithError(errors.errors.invalidCypressEnv)(
204+
`CYPRESS_ENV=${process.env.CYPRESS_ENV}`
205+
)
206+
}
207+
150208
const program = new commander.Command()
151209

152210
// bug in commaner not printing name
@@ -177,7 +235,10 @@ module.exports = {
177235
.option('-k, --key <record-key>', text('key'))
178236
.option('-s, --spec <spec>', text('spec'))
179237
.option('-r, --reporter <reporter>', text('reporter'))
180-
.option('-o, --reporter-options <reporter-options>', text('reporterOptions'))
238+
.option(
239+
'-o, --reporter-options <reporter-options>',
240+
text('reporterOptions')
241+
)
181242
.option('-p, --port <port>', text('port'))
182243
.option('-e, --env <env>', text('env'))
183244
.option('-c, --config <config>', text('config'))
@@ -218,7 +279,9 @@ module.exports = {
218279
program
219280
.command('install')
220281
.usage('[options]')
221-
.description('Installs the Cypress executable matching this package\'s version')
282+
.description(
283+
'Installs the Cypress executable matching this package\'s version'
284+
)
222285
.option('-f, --force', text('forceInstall'))
223286
.action((opts) => {
224287
require('./tasks/install')
@@ -229,7 +292,9 @@ module.exports = {
229292
program
230293
.command('verify')
231294
.usage('[options]')
232-
.description('Verifies that Cypress is installed correctly and executable')
295+
.description(
296+
'Verifies that Cypress is installed correctly and executable'
297+
)
233298
.option('--dev', text('dev'), coerceFalse)
234299
.action((opts) => {
235300
const defaultOpts = { force: true, welcomeMessage: false }

0 commit comments

Comments
 (0)