Skip to content

Commit 30d2244

Browse files
committed
chore(cli): throw errors on unknown flags
clsoes angular#3216
1 parent c194af8 commit 30d2244

File tree

2 files changed

+76
-40
lines changed

2 files changed

+76
-40
lines changed

lib/cli.ts

Lines changed: 70 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -31,49 +31,79 @@ process.argv.slice(2).forEach(function(arg: string) {
3131
}
3232
});
3333

34-
optimist
35-
.usage(
36-
'Usage: protractor [configFile] [options]\n' +
37-
'configFile defaults to protractor.conf.js\n' +
38-
'The [options] object will override values from the config file.\n' +
39-
'See the reference config for a full list of options.')
40-
.describe('help', 'Print Protractor help menu')
41-
.describe('version', 'Print Protractor version')
42-
.describe('browser', 'Browsername, e.g. chrome or firefox')
43-
.describe('seleniumAddress', 'A running selenium address to use')
44-
.describe('seleniumSessionId', 'Attaching an existing session id')
45-
.describe('seleniumServerJar', 'Location of the standalone selenium jar file')
46-
.describe('seleniumPort', 'Optional port for the selenium standalone server')
47-
.describe('baseUrl', 'URL to prepend to all relative paths')
48-
.describe('rootElement', 'Element housing ng-app, if not html or body')
49-
.describe('specs', 'Comma-separated list of files to test')
50-
.describe('exclude', 'Comma-separated list of files to exclude')
51-
.describe('verbose', 'Print full spec names')
52-
.describe('stackTrace', 'Print stack trace on error')
53-
.describe('params', 'Param object to be passed to the tests')
54-
.describe('framework', 'Test framework to use: jasmine, mocha, or custom')
55-
.describe('resultJsonOutputFile', 'Path to save JSON test result')
56-
.describe('troubleshoot', 'Turn on troubleshooting output')
57-
.describe('elementExplorer', 'Interactively test Protractor commands')
58-
.describe('debuggerServerPort', 'Start a debugger server at specified port instead of repl')
59-
.alias('browser', 'capabilities.browserName')
60-
.alias('name', 'capabilities.name')
61-
.alias('platform', 'capabilities.platform')
62-
.alias('platform-version', 'capabilities.version')
63-
.alias('tags', 'capabilities.tags')
64-
.alias('build', 'capabilities.build')
65-
.alias('grep', 'jasmineNodeOpts.grep')
66-
.alias('invert-grep', 'jasmineNodeOpts.invertGrep')
67-
.alias('explorer', 'elementExplorer')
68-
.string('capabilities.tunnel-identifier')
69-
.check(function(arg: any) {
70-
if (arg._.length > 1) {
71-
throw new Error('Error: more than one config file specified');
72-
}
73-
});
34+
let optimistOptions: any = {
35+
describes: {
36+
help: 'Print Protractor help menu',
37+
version: 'Print Protractor version',
38+
browser: 'Browsername, e.g. chrome or firefox',
39+
seleniumAddress: 'A running selenium address to use',
40+
seleniumSessionId: 'Attaching an existing session id',
41+
seleniumServerJar: 'Location of the standalone selenium jar file',
42+
seleniumPort: 'Optional port for the selenium standalone server',
43+
baseUrl: 'URL to prepend to all relative paths',
44+
rootElement: 'Element housing ng-app, if not html or body',
45+
specs: 'Comma-separated list of files to test',
46+
exclude: 'Comma-separated list of files to exclude',
47+
verbose: 'Print full spec names',
48+
stackTrace: 'Print stack trace on error',
49+
params: 'Param object to be passed to the tests',
50+
framework: 'Test framework to use: jasmine, mocha, or custom',
51+
resultJsonOutputFile: 'Path to save JSON test result',
52+
troubleshoot: 'Turn on troubleshooting output',
53+
elementExplorer: 'Interactively test Protractor commands',
54+
debuggerServerPort: 'Start a debugger server at specified port instead of repl'
55+
},
56+
aliases: {
57+
browser: 'capabilities.browserName',
58+
name: 'capabilities.name',
59+
platform: 'capabilities.platform',
60+
'platform-version': 'capabilities.version',
61+
tags: 'capabilities.tags',
62+
build: 'capabilities.build',
63+
grep: 'jasmineNodeOpts.grep',
64+
'invert-grep': 'jasmineNodeOpts.invertGrep',
65+
explorer: 'elementExplorer'
66+
},
67+
strings: {'capabilities.tunnel-identifier': ''}
68+
};
69+
70+
optimist.usage(
71+
'Usage: protractor [configFile] [options]\n' +
72+
'configFile defaults to protractor.conf.js\n' +
73+
'The [options] object will override values from the config file.\n' +
74+
'See the reference config for a full list of options.');
75+
for (let key of Object.keys(optimistOptions.describes)) {
76+
optimist.describe(key, optimistOptions.describes[key]);
77+
}
78+
for (let key of Object.keys(optimistOptions.aliases)) {
79+
optimist.alias(key, optimistOptions.aliases[key]);
80+
}
81+
for (let key of Object.keys(optimistOptions.strings)) {
82+
optimist.string(key);
83+
}
84+
optimist.check(function(arg: any) {
85+
if (arg._.length > 1) {
86+
throw new Error('Error: more than one config file specified');
87+
}
88+
});
7489

7590
let argv: any = optimist.parse(args);
7691

92+
// Check to see if additional flags were used.
93+
let unknownKeys: string[] = Object.keys(argv).slice(2).filter((element: string) => {
94+
let found = element !== '$0';
95+
for (let optimistKey of Object.keys(optimistOptions)) {
96+
found = found || (optimistOptions[optimistKey][element] !== undefined);
97+
}
98+
if (!found) {
99+
return element;
100+
}
101+
});
102+
103+
if (unknownKeys.length > 0) {
104+
throw new Error('Found extra flags: ' + unknownKeys.join(', '));
105+
}
106+
77107
if (argv.help) {
78108
optimist.showHelp();
79109
process.exit(0);

scripts/errorTest.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ var checkLogs = function(output, messages) {
1919
*Below are exit failure tests*
2020
******************************/
2121

22+
runProtractor = spawn('node',
23+
['bin/protractor', '--help', '--foobar', 'foobar']);
24+
output = runProtractor.stderr.toString();
25+
messages = ['Error: Found extra flags: foobar'];
26+
checkLogs(output, messages);
27+
2228
// assert authentication error for sauce labs
2329
runProtractor = spawn('node',
2430
['bin/protractor', 'spec/errorTest/sauceLabsAuthentication.js']);

0 commit comments

Comments
 (0)