Skip to content
This repository was archived by the owner on Jul 29, 2024. It is now read-only.

Commit 6373e9e

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

File tree

2 files changed

+86
-40
lines changed

2 files changed

+86
-40
lines changed

lib/cli.ts

+80-40
Original file line numberDiff line numberDiff line change
@@ -31,49 +31,89 @@ 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: {
68+
'capabilities.tunnel-identifier': ''
69+
}
70+
};
71+
72+
optimist.usage(
73+
'Usage: protractor [configFile] [options]\n' +
74+
'configFile defaults to protractor.conf.js\n' +
75+
'The [options] object will override values from the config file.\n' +
76+
'See the reference config for a full list of options.');
77+
for (let key of Object.keys(optimistOptions.describes)) {
78+
optimist.describe(key, optimistOptions.describes[key]);
79+
}
80+
for (let key of Object.keys(optimistOptions.aliases)) {
81+
optimist.alias(key, optimistOptions.aliases[key]);
82+
}
83+
for (let key of Object.keys(optimistOptions.strings)) {
84+
optimist.string(key);
85+
}
86+
optimist.check(function(arg: any) {
87+
if (arg._.length > 1) {
88+
throw new Error('Error: more than one config file specified');
89+
}
90+
});
7491

7592
let argv: any = optimist.parse(args);
7693

94+
// Check to see if additional flags were used.
95+
let unknownKeys: string[] = [];
96+
for (let key of Object.keys(argv).slice(2)) {
97+
if (key === '$0') {
98+
continue;
99+
}
100+
let found = false;
101+
for (let optimistKey of Object.keys(optimistOptions)) {
102+
for (let optionKey of Object.keys(optimistOptions[optimistKey])) {
103+
if (optionKey === key) {
104+
found = true;
105+
break;
106+
}
107+
}
108+
}
109+
if (!found) {
110+
unknownKeys.push(key);
111+
}
112+
}
113+
if (unknownKeys.length > 0) {
114+
throw new Error('Found extra flags: ' + unknownKeys.join(', '));
115+
}
116+
77117
if (argv.help) {
78118
optimist.showHelp();
79119
process.exit(0);

scripts/errorTest.js

+6
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)