-
-
Notifications
You must be signed in to change notification settings - Fork 222
/
cli-parameters.js
179 lines (152 loc) · 5.7 KB
/
cli-parameters.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
var path = require('path');
var chai = require('chai');
var expect = chai.expect;
var parseCommandAndOptions;
var processNodeEnv;
/**
* Builds a valid ARGV array with passed arguments.
*/
function buildArgv(args) {
var argv = ['/somewhere/node', '/somewhere/selenium-standalone'];
if (args) {
argv = argv.concat(args);
}
return argv;
}
/**
* Tests for `selenium-standalone` command parameters parsing.
*/
describe('`selenium-standalone` command parameters', function() {
// Allow tests to mock `process.platform`
before(function() {
this.originalArgv = Object.getOwnPropertyDescriptor(process, 'argv');
});
after(function() {
Object.defineProperty(process, 'argv', this.originalArgv);
});
// Ensure that any internal state of the module is clean for each test
beforeEach(function() {
if (process.env) {
processNodeEnv = process.env.NODE_ENV;
process.env.NODE_ENV = 'test-cli-parameters';
} else {
process.env = { NODE_ENV: 'test-cli-parameters' };
}
parseCommandAndOptions = require('../bin/selenium-standalone').bind(null, '/path/to/java');
});
afterEach(function() {
delete require.cache[require.resolve('../bin/selenium-standalone')];
if (processNodeEnv) {
process.env.NODE_ENV = processNodeEnv;
} else {
delete process.env.NODE_ENV;
}
});
describe('action', function() {
it('is required', function() {
process.argv = buildArgv();
expect(parseCommandAndOptions)
.to.throw(/^No action provided$/);
});
it('only accepts valid values', function() {
process.argv = buildArgv(['test']);
expect(parseCommandAndOptions).to.throw(/^Invalid action /);
process.argv = buildArgv(['install']);
expect(parseCommandAndOptions).to.not.throw();
process.argv = buildArgv(['start']);
expect(parseCommandAndOptions).to.not.throw();
});
});
describe('arguments', function() {
it('are correctly parsed', function() {
process.argv = buildArgv([
'install',
'--test=1',
'-a', 'ok',
'-h'
]);
var parsed = parseCommandAndOptions();
expect(parsed[1].test).to.be.equal(1);
expect(parsed[1].a).to.be.equal('ok');
expect(parsed[1].h).to.be.true;
});
it('are correctly passed unparsed to selenium when after --', function() {
process.argv = buildArgv([
'install',
'--test=1',
'--',
'--test=1',
'-a', 'ok',
'-h',
]);
var parsed = parseCommandAndOptions();
expect(parsed[1].test).to.be.equal(1);
expect(parsed[1].a).to.be.undefined;
expect(parsed[1].seleniumArgs).to.deep.equal(['--test=1', '-a', 'ok', '-h']);
});
it('takes default values when not specified', function() {
process.argv = buildArgv(['install']);
var parsed1 = parseCommandAndOptions('/somewhere');
var defaultValues = require('../lib/default-config');
Object.keys(defaultValues).forEach(function(key) {
expect(parsed1[1][key]).to.deep.equal(defaultValues[key]);
});
process.argv = buildArgv(['install', '--drivers.chrome.version=42']);
var parsed2 = parseCommandAndOptions('/somewhere');
expect(parsed2[1].drivers.chrome.version).to.be.equal('42');
expect(parsed2[1].drivers.chrome.baseURL)
.to.be.equal(defaultValues.drivers.chrome.baseURL);
});
it('are correctly parsed from a JSON config file', function() {
process.argv = buildArgv([
'install',
'--config=' + path.join(__dirname, 'fixtures', 'config.valid.json'),
]);
var parsed = parseCommandAndOptions('/somewhere');
expect(parsed[1].version).to.be.equal('42');
expect(parsed[1].drivers.ie.version).to.be.equal(42);
expect(parsed[1].drivers.ie.baseURL).to.be.equal('http://www.google.fr');
expect(parsed[1].seleniumArgs).to.be.deep.equal([]);
});
it('are correctly parsed from a JS module config file', function() {
process.argv = buildArgv([
'install',
'--config=' + path.join(__dirname, 'fixtures', 'config.valid.js'),
]);
var parsed = parseCommandAndOptions('/somewhere');
expect(parsed[1].version).to.be.equal('42');
expect(parsed[1].drivers.ie.version).to.be.equal(42);
expect(parsed[1].drivers.ie.baseURL).to.be.equal('http://www.google.fr');
expect(parsed[1].seleniumArgs).to.be.deep.equal(['--test=1', '-flag']);
});
it('throws if config file is invalid', function() {
process.argv = buildArgv([
'install',
'--config=' + path.join(__dirname, 'fixtures', 'config.invalid.json'),
]);
expect(parseCommandAndOptions).to.throw(/^Error parsing config file :/);
});
it('throws if config file is not an object', function() {
process.argv = buildArgv([
'install',
'--config=' + path.join(__dirname, 'fixtures', 'config.invalid.js'),
]);
expect(parseCommandAndOptions).to.throw(/^Error parsing config file : Config file does not exports an object$/);
});
it('respects the precedence order : command line > config file > default', function() {
var defaultValues = require('../lib/default-config');
process.argv = buildArgv([
'install',
'--config=' + path.join(__dirname, 'fixtures', 'config.valid.js'),
'--drivers.ie.version=43',
'--',
'--some=seleniumArgs'
]);
var parsed = parseCommandAndOptions('/somewhere');
expect(parsed[1].version).to.be.equal('42');
expect(parsed[1].drivers.ie.arch).to.be.equal(defaultValues.drivers.ie.arch);
expect(parsed[1].drivers.ie.version).to.be.equal('43');
expect(parsed[1].seleniumArgs).to.be.deep.equal(['--some=seleniumArgs']);
});
});
});