cli: pass group and groupId flags, close #455 (#836)

* cli: pass group and groupId flags, close #455

* upgrade commander to latest

* latest command moved options in help below command

* update tests, make sure -v, --version and version work

* run test one more time
This commit is contained in:
Gleb Bahmutov
2017-10-27 13:18:21 -04:00
committed by Brian Mann
parent bac9d70a90
commit d217d0e9d2
4 changed files with 111 additions and 34 deletions
+34 -20
View File
@@ -12,6 +12,12 @@ exports['cli help command shows help 1'] = `
Usage: cypress [options] [command]
Options:
-v, --version Prints Cypress version
-h, --help output usage information
Commands:
help Shows CLI help and exits
@@ -20,11 +26,6 @@ exports['cli help command shows help 1'] = `
open [options] Opens Cypress in the interactive GUI.
install Installs the Cypress executable matching this package's version
verify Verifies that Cypress is installed correctly and executable
Options:
-h, --help output usage information
-v, --version Prints Cypress version
-------
stderr:
-------
@@ -47,6 +48,12 @@ exports['cli help command shows help for -h 1'] = `
Usage: cypress [options] [command]
Options:
-v, --version Prints Cypress version
-h, --help output usage information
Commands:
help Shows CLI help and exits
@@ -55,11 +62,6 @@ exports['cli help command shows help for -h 1'] = `
open [options] Opens Cypress in the interactive GUI.
install Installs the Cypress executable matching this package's version
verify Verifies that Cypress is installed correctly and executable
Options:
-h, --help output usage information
-v, --version Prints Cypress version
-------
stderr:
-------
@@ -82,6 +84,12 @@ exports['cli help command shows help for --help 1'] = `
Usage: cypress [options] [command]
Options:
-v, --version Prints Cypress version
-h, --help output usage information
Commands:
help Shows CLI help and exits
@@ -90,11 +98,6 @@ exports['cli help command shows help for --help 1'] = `
open [options] Opens Cypress in the interactive GUI.
install Installs the Cypress executable matching this package's version
verify Verifies that Cypress is installed correctly and executable
Options:
-h, --help output usage information
-v, --version Prints Cypress version
-------
stderr:
-------
@@ -119,6 +122,12 @@ exports['cli unknown command shows usage and exits 1'] = `
Usage: cypress [options] [command]
Options:
-v, --version Prints Cypress version
-h, --help output usage information
Commands:
help Shows CLI help and exits
@@ -127,11 +136,6 @@ exports['cli unknown command shows usage and exits 1'] = `
open [options] Opens Cypress in the interactive GUI.
install Installs the Cypress executable matching this package's version
verify Verifies that Cypress is installed correctly and executable
Options:
-h, --help output usage information
-v, --version Prints Cypress version
-------
stderr:
-------
@@ -154,3 +158,13 @@ exports['cli version no binary version 1'] = `
Cypress package version: 1.2.3
Cypress binary version: not installed
`
exports['cli --version no binary version 1'] = `
Cypress package version: 1.2.3
Cypress binary version: not installed
`
exports['cli -v no binary version 1'] = `
Cypress package version: 1.2.3
Cypress binary version: not installed
`
+37 -11
View File
@@ -13,7 +13,9 @@ const coerceFalse = (arg) => {
const parseOpts = (opts) => {
opts = _.pick(opts,
'project', 'spec', 'reporter', 'reporterOptions', 'path', 'destination',
'port', 'env', 'cypressVersion', 'config', 'record', 'key', 'browser', 'detached', 'headed')
'port', 'env', 'cypressVersion', 'config', 'record', 'key',
'browser', 'detached', 'headed',
'group', 'groupId')
if (opts.project) {
opts.project = path.resolve(opts.project)
@@ -41,6 +43,8 @@ const descriptions = {
project: 'path to the project',
version: 'Prints Cypress version',
headed: 'displays the Electron browser instead of running headlessly',
group: 'flag to group individual runs by using common --group-id',
groupId: 'optional common id to group runs by, extracted from CI environment variables by default',
}
const knownCommands = ['version', 'run', 'open', 'install', 'verify', '-v', '--version', 'help', '-h', '--help']
@@ -53,6 +57,23 @@ const text = (description) => {
return descriptions[description]
}
function includesVersion (args) {
return _.includes(args, 'version') ||
_.includes(args, '--version') ||
_.includes(args, '-v')
}
function showVersions () {
debug('printing Cypress version')
return require('./exec/versions')
.getVersions()
.then((versions = {}) => {
logger.log('Cypress package version:', versions.package)
logger.log('Cypress binary version:', versions.binary)
process.exit(0)
})
}
module.exports = {
init (args) {
if (!args) {
@@ -76,15 +97,7 @@ module.exports = {
.option('-v, --version', text('version'))
.command('version')
.description(text('version'))
.action(() => {
return require('./exec/versions')
.getVersions()
.then((versions = {}) => {
logger.log('Cypress package version:', versions.package)
logger.log('Cypress binary version:', versions.binary)
process.exit(0)
})
})
.action(showVersions)
program
.command('run')
@@ -101,7 +114,10 @@ module.exports = {
.option('-c, --config <config>', text('config'))
.option('-b, --browser <browser-name>', text('browser'))
.option('-P, --project <project-path>', text('project'))
.option('--group', text('group'), coerceFalse)
.option('--group-id <group-id>', text('groupId'))
.action((opts) => {
debug('running Cypress')
require('./exec/run')
.start(parseOpts(opts))
.then(util.exit)
@@ -118,6 +134,7 @@ module.exports = {
.option('-d, --detached [bool]', text('detached'), coerceFalse)
.option('-P, --project <project path>', text('project'))
.action((opts) => {
debug('opening Cypress')
require('./exec/open')
.start(parseOpts(opts))
.catch(util.logErrorExit1)
@@ -145,18 +162,27 @@ module.exports = {
// if there are no arguments
if (args.length <= 2) {
// then display the help
debug('printing help')
program.help()
// exits
}
const firstCommand = args[2]
if (!_.includes(knownCommands, firstCommand)) {
debug('unknwon command %s', firstCommand)
logger.error('Unknown command', `"${firstCommand}"`)
program.outputHelp()
return util.exit(1)
}
if (includesVersion(args)) {
// commander 2.11.0 changes behavior
// and now does not understand top level options
// .option('-v, --version').command('version')
// so we have to manually catch '-v, --version'
return showVersions()
}
debug('program parsing arguments')
return program.parse(args)
},
}
+1 -1
View File
@@ -28,7 +28,7 @@
"bluebird": "3.5.0",
"chalk": "2.1.0",
"check-more-types": "2.24.0",
"commander": "2.9.0",
"commander": "2.11.0",
"common-tags": "1.4.0",
"debug": "2.6.8",
"dev-null": "0.1.1",
+39 -2
View File
@@ -64,13 +64,35 @@ describe('cli', function () {
})
})
it('handles non-existent binary', function (done) {
it('handles non-existent binary version', function (done) {
this.sandbox.stub(util, 'pkgVersion').returns('1.2.3')
this.sandbox.stub(info, 'getInstalledVersion').resolves(null)
this.exec('version')
process.exit.callsFake(() => {
snapshot('cli version no binary version', logger.print())
done()
})
})
it('handles non-existent binary --version', function (done) {
this.sandbox.stub(util, 'pkgVersion').returns('1.2.3')
this.sandbox.stub(info, 'getInstalledVersion').resolves(null)
this.exec('--version')
process.exit.callsFake(() => {
snapshot('cli version no binary version', logger.print())
snapshot('cli --version no binary version', logger.print())
done()
})
})
it('handles non-existent binary -v', function (done) {
this.sandbox.stub(util, 'pkgVersion').returns('1.2.3')
this.sandbox.stub(info, 'getInstalledVersion').resolves(null)
this.exec('-v')
process.exit.callsFake(() => {
snapshot('cli -v no binary version', logger.print())
done()
})
})
@@ -102,6 +124,21 @@ describe('cli', function () {
})
})
it('calls run without group flag', function () {
this.exec('run')
expect(run.start).to.be.calledWith({})
})
it('calls run with group flag', function () {
this.exec('run --group')
expect(run.start).to.be.calledWith({ group: true })
})
it('calls run with groupId', function () {
this.exec('run --group-id foo')
expect(run.start).to.be.calledWith({ groupId: 'foo' })
})
it('calls run with port', function () {
this.exec('run --port 7878')
expect(run.start).to.be.calledWith({ port: '7878' })