/
config-specs.js
348 lines (330 loc) · 11.1 KB
/
config-specs.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// transpile:mocha
import _ from 'lodash';
import chai from 'chai';
import sinon from 'sinon';
import chaiAsPromised from 'chai-as-promised';
import { getGitRev, getAppiumConfig, checkNodeOk, warnNodeDeprecations,
getNonDefaultArgs, getDeprecatedArgs, validateServerArgs,
validateTmpDir, showConfig, checkValidPort } from '../lib/config';
import getParser from '../lib/parser';
import logger from '../lib/logger';
let should = chai.should();
chai.use(chaiAsPromised);
describe('Config', () => {
describe('getGitRev', () => {
it('should get a reasonable git revision', async () => {
let rev = await getGitRev();
rev.should.be.a('string');
rev.length.should.be.equal(40);
rev.match(/[0-9a-f]+/i)[0].should.eql(rev);
});
});
describe('Appium config', () => {
describe('getAppiumConfig', () => {
it('should get a configuration object', async () => {
let config = await getAppiumConfig();
config.should.be.an('object');
should.exist(config['git-sha']);
should.exist(config.built);
should.exist(config.version);
});
});
describe('showConfig', () => {
before(() => {
sinon.spy(console, "log");
});
it('should log the config to console', async () => {
let config = await getAppiumConfig();
await showConfig();
console.log.calledOnce.should.be.true; // eslint-disable-line no-console
console.log.getCall(0).args[0].should.contain(JSON.stringify(config)); // eslint-disable-line no-console
});
});
});
describe('node.js config', () => {
let _process = process;
before(() => {
// need to be able to write to process.version
// but also to have access to process methods
// so copy them over to a writable object
let tempProcess = {};
for (let [prop, value] of _.toPairs(process)) {
tempProcess[prop] = value;
}
process = tempProcess;
});
after(() => {
process = _process;
});
describe('checkNodeOk', () => {
it('should fail if node is below 4', () => {
process.version = 'v0.9.12';
checkNodeOk.should.throw();
process.version = 'v0.1';
checkNodeOk.should.throw();
process.version = 'v0.10.36';
checkNodeOk.should.throw();
process.version = 'v0.12.14';
checkNodeOk.should.throw();
});
it('should succeed if node is 4+', () => {
process.version = 'v4.4.7';
checkNodeOk.should.not.throw();
});
it('should succeed if node is 5+', () => {
process.version = 'v5.7.0';
checkNodeOk.should.not.throw();
});
it('should succeed if node is 6+', () => {
process.version = 'v6.3.1';
checkNodeOk.should.not.throw();
});
});
describe('warnNodeDeprecations', () => {
let spy;
before(() => {
spy = sinon.spy(logger, "warn");
});
beforeEach(() => {
spy.reset();
});
it('should log a warning if node is below 4', () => {
process.version = 'v0.9.12';
warnNodeDeprecations();
logger.warn.callCount.should.equal(1);
});
it('should log a warning if node is 0.12', () => {
process.version = 'v0.12.0';
warnNodeDeprecations();
logger.warn.callCount.should.equal(1);
});
it('should not log a warning if node is 4+', () => {
process.version = 'v4.4.7';
warnNodeDeprecations();
logger.warn.callCount.should.equal(0);
});
it('should not log a warning if node is 5+', () => {
process.version = 'v5.7.0';
warnNodeDeprecations();
logger.warn.callCount.should.equal(0);
});
it('should not log a warning if node is 6+', () => {
process.version = 'v6.3.1';
warnNodeDeprecations();
logger.warn.callCount.should.equal(0);
});
});
});
describe('server arguments', () => {
let parser = getParser();
parser.debug = true; // throw instead of exit on error; pass as option instead?
let args = {};
beforeEach(() => {
// give all the defaults
for (let rawArg of parser.rawArgs) {
args[rawArg[1].dest] = rawArg[1].defaultValue;
}
});
describe('getNonDefaultArgs', () => {
it('should show none if we have all the defaults', () => {
let nonDefaultArgs = getNonDefaultArgs(parser, args);
_.keys(nonDefaultArgs).length.should.equal(0);
});
it('should catch a non-default argument', () => {
args.isolateSimDevice = true;
let nonDefaultArgs = getNonDefaultArgs(parser, args);
_.keys(nonDefaultArgs).length.should.equal(1);
should.exist(nonDefaultArgs.isolateSimDevice);
});
});
describe('getDeprecatedArgs', () => {
it('should show none if we have no deprecated arguments', () => {
let deprecatedArgs = getDeprecatedArgs(parser, args);
_.keys(deprecatedArgs).length.should.equal(0);
});
it('should catch a deprecated argument', () => {
args.showIOSLog = true;
let deprecatedArgs = getDeprecatedArgs(parser, args);
_.keys(deprecatedArgs).length.should.equal(1);
should.exist(deprecatedArgs['--show-ios-log']);
});
it('should catch a non-boolean deprecated argument', () => {
args.calendarFormat = 'orwellian';
let deprecatedArgs = getDeprecatedArgs(parser, args);
_.keys(deprecatedArgs).length.should.equal(1);
should.exist(deprecatedArgs['--calendar-format']);
});
});
});
describe('checkValidPort', () => {
it('should be false for port too high', () => {
checkValidPort(65536).should.be.false;
});
it('should be false for port too low', () => {
checkValidPort(0).should.be.false;
});
it('should be true for port 1', () => {
checkValidPort(1).should.be.true;
});
it('should be true for port 65535', () => {
checkValidPort(65535).should.be.true;
});
});
describe('validateTmpDir', () => {
it('should fail to use a tmp dir with incorrect permissions', async () => {
validateTmpDir('/private/if_you_run_with_sudo_this_wont_fail').should.be.rejectedWith(/could not ensure/);
});
it('should fail to use an undefined tmp dir', async () => {
validateTmpDir().should.be.rejectedWith(/could not ensure/);
});
it('should be able to use a tmp dir with correct permissions', async () => {
validateTmpDir('/tmp/test_tmp_dir/with/any/number/of/levels').should.not.be.rejected;
});
});
describe('parsing args with empty argv[1]', () => {
let argv1;
before(() => {
argv1 = process.argv[1];
});
after(() => {
process.argv[1] = argv1;
});
it('should not fail if process.argv[1] is undefined', () => {
process.argv[1] = undefined;
let args = getParser();
args.prog.should.be.equal('Appium');
});
it('should set "prog" to process.argv[1]', () => {
process.argv[1] = 'Hello World';
let args = getParser();
args.prog.should.be.equal('Hello World');
});
});
describe('validateServerArgs', () => {
let parser = getParser();
parser.debug = true; // throw instead of exit on error; pass as option instead?
const defaultArgs = {};
// give all the defaults
for (let rawArg of parser.rawArgs) {
defaultArgs[rawArg[1].dest] = rawArg[1].defaultValue;
}
let args = {};
beforeEach(() => {
args = _.clone(defaultArgs);
});
describe('mutually exclusive server arguments', () => {
describe('noReset and fullReset', () => {
it('should not allow both', () => {
(() => {
args.noReset = args.fullReset = true;
validateServerArgs(parser, args);
}).should.throw();
});
it('should allow noReset', () => {
(() => {
args.noReset = true;
validateServerArgs(parser, args);
}).should.not.throw();
});
it('should allow fullReset', () => {
(() => {
args.fullReset = true;
validateServerArgs(parser, args);
}).should.not.throw();
});
});
describe('ipa and safari', () => {
it('should not allow both', () => {
(() => {
args.ipa = args.safari = true;
validateServerArgs(parser, args);
}).should.throw();
});
it('should allow ipa', () => {
(() => {
args.ipa = true;
validateServerArgs(parser, args);
}).should.not.throw();
});
it('should allow safari', () => {
(() => {
args.safari = true;
validateServerArgs(parser, args);
}).should.not.throw();
});
});
describe('app and safari', () => {
it('should not allow both', () => {
(() => {
args.app = args.safari = true;
validateServerArgs(parser, args);
}).should.throw();
});
it('should allow app', () => {
(() => {
args.app = true;
validateServerArgs(parser, args);
}).should.not.throw();
});
});
describe('forceIphone and forceIpad', () => {
it('should not allow both', () => {
(() => {
args.forceIphone = args.forceIpad = true;
validateServerArgs(parser, args);
}).should.throw();
});
it('should allow forceIphone', () => {
(() => {
args.forceIphone = true;
validateServerArgs(parser, args);
}).should.not.throw();
});
it('should allow forceIpad', () => {
(() => {
args.forceIpad = true;
validateServerArgs(parser, args);
}).should.not.throw();
});
});
describe('deviceName and defaultDevice', () => {
it('should not allow both', () => {
(() => {
args.deviceName = args.defaultDevice = true;
validateServerArgs(parser, args);
}).should.throw();
});
it('should allow deviceName', () => {
(() => {
args.deviceName = true;
validateServerArgs(parser, args);
}).should.not.throw();
});
it('should allow defaultDevice', () => {
(() => {
args.defaultDevice = true;
validateServerArgs(parser, args);
}).should.not.throw();
});
});
});
describe('validated arguments', () => {
// checking ports is already done.
// the only argument left is `backendRetries`
describe('backendRetries', () => {
it('should fail with value less than 0', () => {
args.backendRetries = -1;
(() => {validateServerArgs(parser, args);}).should.throw();
});
it('should succeed with value of 0', () => {
args.backendRetries = 0;
(() => {validateServerArgs(parser, args);}).should.not.throw();
});
it('should succeed with value above 0', () => {
args.backendRetries = 100;
(() => {validateServerArgs(parser, args);}).should.not.throw();
});
});
});
});
});