Skip to content

Commit

Permalink
Catch env variable with reserved name CYPRESS_ENV 1621 (#1626)
Browse files Browse the repository at this point in the history
* server: check CYPRESS_ENV variable when merging configs

* catch invalid CYPRESS_ENV value in CLI, close #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
  • Loading branch information
bahmutov authored Sep 23, 2019
1 parent b5b79fc commit 9f082d9
Show file tree
Hide file tree
Showing 11 changed files with 293 additions and 51 deletions.
29 changes: 29 additions & 0 deletions cli/__snapshots__/cli_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,32 @@ exports['shows help for run --foo 1'] = `
-------
`

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

exports['cli CYPRESS_ENV catches environment "foo" 1'] = `
code: 11
stderr:
-------
The environment variable with the reserved name "CYPRESS_ENV" is set.
Unset the "CYPRESS_ENV" environment variable and run Cypress again.
----------
CYPRESS_ENV=foo
----------
Platform: xxx
Cypress Version: 1.2.3
-------
`
1 change: 1 addition & 0 deletions cli/__snapshots__/errors_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ exports['errors individual has the following errors 1'] = [
"failedDownload",
"failedUnzip",
"invalidCacheDirectory",
"invalidCypressEnv",
"invalidSmokeTestDisplayError",
"missingApp",
"missingDependency",
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')
}
107 changes: 86 additions & 21 deletions cli/lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const logSymbols = require('log-symbols')
const debug = require('debug')('cypress:cli')
const util = require('./util')
const logger = require('./logger')
const errors = require('./errors')
const cache = require('./tasks/cache')

// patch "commander" method called when a user passed an unknown option
Expand All @@ -27,7 +28,9 @@ const coerceFalse = (arg) => {
const spaceDelimitedSpecsMsg = (files) => {
logger.log()
logger.warn(stripIndent`
${logSymbols.warning} Warning: It looks like you're passing --spec a space-separated list of files:
${
logSymbols.warning
} Warning: It looks like you're passing --spec a space-separated list of files:
"${files.join(' ')}"
Expand All @@ -54,7 +57,8 @@ const parseVariableOpts = (fnArgs, args) => {
const nextOptOffset = _.findIndex(_.slice(args, argIndex), (arg) => {
return _.startsWith(arg, '--')
})
const endIndex = nextOptOffset !== -1 ? argIndex + nextOptOffset : args.length
const endIndex =
nextOptOffset !== -1 ? argIndex + nextOptOffset : args.length

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

const parseOpts = (opts) => {
opts = _.pick(opts,
'project', 'spec', 'reporter', 'reporterOptions', 'path', 'destination',
'port', 'env', 'cypressVersion', 'config', 'record', 'key',
'browser', 'detached', 'headed', 'global', 'dev', 'force', 'exit',
'cachePath', 'cacheList', 'cacheClear', 'parallel', 'group', 'ciBuildId')
opts = _.pick(
opts,
'project',
'spec',
'reporter',
'reporterOptions',
'path',
'destination',
'port',
'env',
'cypressVersion',
'config',
'record',
'key',
'browser',
'detached',
'headed',
'global',
'dev',
'force',
'exit',
'cachePath',
'cacheList',
'cacheClear',
'parallel',
'group',
'ciBuildId'
)

if (opts.exit) {
opts = _.omit(opts, 'exit')
Expand All @@ -86,16 +113,23 @@ const parseOpts = (opts) => {
}

const descriptions = {
record: 'records the run. sends test results, screenshots and videos to your Cypress Dashboard.',
key: 'your secret Record Key. you can omit this if you set a CYPRESS_RECORD_KEY environment variable.',
record:
'records the run. sends test results, screenshots and videos to your Cypress Dashboard.',
key:
'your secret Record Key. you can omit this if you set a CYPRESS_RECORD_KEY environment variable.',
spec: 'runs a specific spec file. defaults to "all"',
reporter: 'runs a specific mocha reporter. pass a path to use a custom reporter. defaults to "spec"',
reporter:
'runs a specific mocha reporter. pass a path to use a custom reporter. defaults to "spec"',
reporterOptions: 'options for the mocha reporter. defaults to "null"',
port: 'runs Cypress on a specific port. overrides any value in cypress.json.',
env: 'sets environment variables. separate multiple values with a comma. overrides any value in cypress.json or cypress.env.json',
config: 'sets configuration values. separate multiple values with a comma. overrides any value in cypress.json.',
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.',
browserOpenMode: 'path to a custom browser to be added to the list of available browsers in Cypress',
env:
'sets environment variables. separate multiple values with a comma. overrides any value in cypress.json or cypress.env.json',
config:
'sets configuration values. separate multiple values with a comma. overrides any value in cypress.json.',
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.',
browserOpenMode:
'path to a custom browser to be added to the list of available browsers in Cypress',
detached: 'runs Cypress application in detached mode',
project: 'path to the project',
global: 'force Cypress into global mode as if its globally installed',
Expand All @@ -108,11 +142,25 @@ const descriptions = {
cacheList: 'list cached binary versions',
cacheClear: 'delete all cached binaries',
group: 'a named group for recorded runs in the Cypress dashboard',
parallel: 'enables concurrent runs and automatic load balancing of specs across multiple machines or processes',
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',
parallel:
'enables concurrent runs and automatic load balancing of specs across multiple machines or processes',
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',
}

const knownCommands = ['version', 'run', 'open', 'install', 'verify', '-v', '--version', 'help', '-h', '--help', 'cache']
const knownCommands = [
'version',
'run',
'open',
'install',
'verify',
'-v',
'--version',
'help',
'-h',
'--help',
'cache',
]

const text = (description) => {
if (!descriptions[description]) {
Expand All @@ -123,9 +171,11 @@ const text = (description) => {
}

function includesVersion (args) {
return _.includes(args, 'version') ||
return (
_.includes(args, 'version') ||
_.includes(args, '--version') ||
_.includes(args, '-v')
)
}

function showVersions () {
Expand All @@ -147,6 +197,14 @@ 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)(
`CYPRESS_ENV=${process.env.CYPRESS_ENV}`
)
}

const program = new commander.Command()

// bug in commaner not printing name
Expand Down Expand Up @@ -177,7 +235,10 @@ module.exports = {
.option('-k, --key <record-key>', text('key'))
.option('-s, --spec <spec>', text('spec'))
.option('-r, --reporter <reporter>', text('reporter'))
.option('-o, --reporter-options <reporter-options>', text('reporterOptions'))
.option(
'-o, --reporter-options <reporter-options>',
text('reporterOptions')
)
.option('-p, --port <port>', text('port'))
.option('-e, --env <env>', text('env'))
.option('-c, --config <config>', text('config'))
Expand Down Expand Up @@ -218,7 +279,9 @@ module.exports = {
program
.command('install')
.usage('[options]')
.description('Installs the Cypress executable matching this package\'s version')
.description(
'Installs the Cypress executable matching this package\'s version'
)
.option('-f, --force', text('forceInstall'))
.action((opts) => {
require('./tasks/install')
Expand All @@ -229,7 +292,9 @@ module.exports = {
program
.command('verify')
.usage('[options]')
.description('Verifies that Cypress is installed correctly and executable')
.description(
'Verifies that Cypress is installed correctly and executable'
)
.option('--dev', text('dev'), coerceFalse)
.action((opts) => {
const defaultOpts = { force: true, welcomeMessage: false }
Expand Down
Loading

6 comments on commit 9f082d9

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 9f082d9 Sep 23, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

You can install this pre-release platform-specific build using instructions at https://on.cypress.io/installing-cypress#Install-pre-release-version.

You will need to use custom CYPRESS_INSTALL_BINARY url and install Cypress using an url instead of the version.

export CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/3.5.0/linux-x64/circle-develop-9f082d97ca45d4512478f8b212a2abe89bb30fb9-153647/cypress.zip
npm install https://cdn.cypress.io/beta/npm/3.5.0/circle-develop-9f082d97ca45d4512478f8b212a2abe89bb30fb9-153648/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 9f082d9 Sep 23, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AppVeyor has built the win32 x64 version of the Test Runner.

You can install this pre-release platform-specific build using instructions at https://on.cypress.io/installing-cypress#Install-pre-release-version.

You will need to use custom CYPRESS_INSTALL_BINARY url and install Cypress using an url instead of the version.

set CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/3.5.0/win32-x64/appveyor-develop-9f082d97ca45d4512478f8b212a2abe89bb30fb9-27603654/cypress.zip
npm install https://cdn.cypress.io/beta/binary/3.5.0/win32-x64/appveyor-develop-9f082d97ca45d4512478f8b212a2abe89bb30fb9-27603654/cypress.zip

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 9f082d9 Sep 23, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AppVeyor has built the win32 ia32 version of the Test Runner.

You can install this pre-release platform-specific build using instructions at https://on.cypress.io/installing-cypress#Install-pre-release-version.

You will need to use custom CYPRESS_INSTALL_BINARY url and install Cypress using an url instead of the version.

set CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/3.5.0/win32-ia32/appveyor-develop-9f082d97ca45d4512478f8b212a2abe89bb30fb9-27603654/cypress.zip
npm install https://cdn.cypress.io/beta/binary/3.5.0/win32-ia32/appveyor-develop-9f082d97ca45d4512478f8b212a2abe89bb30fb9-27603654/cypress.zip

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 9f082d9 Sep 23, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin x64 version of the Test Runner.

You can install this pre-release platform-specific build using instructions at https://on.cypress.io/installing-cypress#Install-pre-release-version.

You will need to use custom CYPRESS_INSTALL_BINARY url and install Cypress using an url instead of the version.

export CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/3.5.0/darwin-x64/circle-develop-9f082d97ca45d4512478f8b212a2abe89bb30fb9-153661/cypress.zip
npm install https://cdn.cypress.io/beta/npm/3.5.0/circle-develop-9f082d97ca45d4512478f8b212a2abe89bb30fb9-153655/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 9f082d9 Sep 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

You can install this pre-release platform-specific build using instructions at https://on.cypress.io/installing-cypress#Install-pre-release-version.

You will need to use custom CYPRESS_INSTALL_BINARY url and install Cypress using an url instead of the version.

export CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/3.5.0/linux-x64/circle-develop-9f082d97ca45d4512478f8b212a2abe89bb30fb9-154032/cypress.zip
npm install https://cdn.cypress.io/beta/npm/3.5.0/circle-develop-9f082d97ca45d4512478f8b212a2abe89bb30fb9-154030/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 9f082d9 Sep 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

You can install this pre-release platform-specific build using instructions at https://on.cypress.io/installing-cypress#Install-pre-release-version.

You will need to use custom CYPRESS_INSTALL_BINARY url and install Cypress using an url instead of the version.

export CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/3.5.0/linux-x64/circle-develop-9f082d97ca45d4512478f8b212a2abe89bb30fb9-154032/cypress.zip
npm install https://cdn.cypress.io/beta/npm/3.5.0/circle-develop-9f082d97ca45d4512478f8b212a2abe89bb30fb9-154030/cypress.tgz

Please sign in to comment.