2015-05-23 18:11:42 +03:00
|
|
|
// 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';
|
2015-09-30 19:34:29 +03:00
|
|
|
|
2015-05-23 18:11:42 +03:00
|
|
|
|
|
|
|
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);
|
2015-12-02 22:37:57 +03:00
|
|
|
should.exist(config.version);
|
2015-05-23 18:11:42 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('showConfig', () => {
|
|
|
|
before(() => {
|
2015-12-02 22:33:36 +03:00
|
|
|
sinon.spy(console, "log");
|
2015-05-23 18:11:42 +03:00
|
|
|
});
|
2015-12-02 22:33:36 +03:00
|
|
|
it('should log the config to console', async () => {
|
2015-05-23 18:11:42 +03:00
|
|
|
let config = await getAppiumConfig();
|
|
|
|
await showConfig();
|
2016-08-03 22:14:29 +03:00
|
|
|
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
|
2015-05-23 18:11:42 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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 = {};
|
2016-03-17 04:23:05 +03:00
|
|
|
for (let [prop, value] of _.toPairs(process)) {
|
2015-05-23 18:11:42 +03:00
|
|
|
tempProcess[prop] = value;
|
|
|
|
}
|
|
|
|
process = tempProcess;
|
|
|
|
});
|
|
|
|
after(() => {
|
|
|
|
process = _process;
|
|
|
|
});
|
|
|
|
describe('checkNodeOk', () => {
|
2016-05-01 17:31:59 +03:00
|
|
|
it('should fail if node is below 0.12', () => {
|
2015-05-23 18:11:42 +03:00
|
|
|
process.version = 'v0.9.12';
|
2016-05-01 17:31:59 +03:00
|
|
|
checkNodeOk.should.throw();
|
|
|
|
process.version = 'v0.1';
|
|
|
|
checkNodeOk.should.throw();
|
|
|
|
process.version = 'v0.10.36';
|
|
|
|
checkNodeOk.should.throw();
|
2015-05-23 18:11:42 +03:00
|
|
|
});
|
2016-05-01 17:31:59 +03:00
|
|
|
it('should succeed if node is 0.12+', () => {
|
|
|
|
process.version = 'v0.12.0';
|
|
|
|
checkNodeOk.should.not.throw();
|
2015-05-23 18:11:42 +03:00
|
|
|
});
|
|
|
|
it('should succeed if node is 1.x', () => {
|
|
|
|
process.version = 'v1.0.0';
|
2016-05-01 17:31:59 +03:00
|
|
|
checkNodeOk.should.not.throw();
|
2015-05-23 18:11:42 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('warnNodeDeprecations', () => {
|
|
|
|
let spy;
|
|
|
|
before(() => {
|
|
|
|
spy = sinon.spy(logger, "warn");
|
|
|
|
});
|
|
|
|
beforeEach(() => {
|
|
|
|
spy.reset();
|
|
|
|
});
|
|
|
|
it('should log a warning if node is below 0.12', () => {
|
|
|
|
process.version = 'v0.9.12';
|
|
|
|
warnNodeDeprecations();
|
|
|
|
logger.warn.callCount.should.equal(1);
|
|
|
|
});
|
|
|
|
it('should not log a warning if node is 0.12+', () => {
|
|
|
|
process.version = 'v0.12.0';
|
|
|
|
warnNodeDeprecations();
|
|
|
|
logger.warn.callCount.should.equal(0);
|
|
|
|
});
|
|
|
|
it('should not log a warning if node is 1.x', () => {
|
|
|
|
process.version = 'v1.0.0';
|
|
|
|
warnNodeDeprecations();
|
|
|
|
logger.warn.callCount.should.equal(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('server arguments', () => {
|
|
|
|
let parser = getParser();
|
2016-05-01 17:31:59 +03:00
|
|
|
parser.debug = true; // throw instead of exit on error; pass as option instead?
|
2015-05-28 18:39:10 +03:00
|
|
|
let args = {};
|
|
|
|
beforeEach(() => {
|
|
|
|
// give all the defaults
|
|
|
|
for (let rawArg of parser.rawArgs) {
|
|
|
|
args[rawArg[1].dest] = rawArg[1].defaultValue;
|
|
|
|
}
|
|
|
|
});
|
2015-05-23 18:11:42 +03:00
|
|
|
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', () => {
|
2015-06-03 20:41:21 +03:00
|
|
|
args.showIOSLog = true;
|
2015-05-23 18:11:42 +03:00
|
|
|
let deprecatedArgs = getDeprecatedArgs(parser, args);
|
|
|
|
_.keys(deprecatedArgs).length.should.equal(1);
|
2015-06-03 20:41:21 +03:00
|
|
|
should.exist(deprecatedArgs['--show-ios-log']);
|
2015-05-23 18:11:42 +03:00
|
|
|
});
|
2015-06-03 20:41:21 +03:00
|
|
|
it('should catch a non-boolean deprecated argument', () => {
|
2015-05-28 18:39:10 +03:00
|
|
|
args.calendarFormat = 'orwellian';
|
|
|
|
let deprecatedArgs = getDeprecatedArgs(parser, args);
|
|
|
|
_.keys(deprecatedArgs).length.should.equal(1);
|
|
|
|
should.exist(deprecatedArgs['--calendar-format']);
|
|
|
|
});
|
2015-05-23 18:11:42 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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('validateServerArgs', () => {
|
|
|
|
let parser = getParser();
|
2016-05-01 17:31:59 +03:00
|
|
|
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);
|
|
|
|
});
|
2015-05-23 18:11:42 +03:00
|
|
|
describe('mutually exclusive server arguments', () => {
|
|
|
|
describe('noReset and fullReset', () => {
|
|
|
|
it('should not allow both', () => {
|
|
|
|
(() => {
|
2016-05-01 17:31:59 +03:00
|
|
|
args.noReset = args.fullReset = true;
|
|
|
|
validateServerArgs(parser, args);
|
|
|
|
}).should.throw();
|
2015-05-23 18:11:42 +03:00
|
|
|
});
|
|
|
|
it('should allow noReset', () => {
|
|
|
|
(() => {
|
2016-05-01 17:31:59 +03:00
|
|
|
args.noReset = true;
|
|
|
|
validateServerArgs(parser, args);
|
|
|
|
}).should.not.throw();
|
2015-05-23 18:11:42 +03:00
|
|
|
});
|
|
|
|
it('should allow fullReset', () => {
|
|
|
|
(() => {
|
2016-05-01 17:31:59 +03:00
|
|
|
args.fullReset = true;
|
|
|
|
validateServerArgs(parser, args);
|
|
|
|
}).should.not.throw();
|
2015-05-23 18:11:42 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('ipa and safari', () => {
|
|
|
|
it('should not allow both', () => {
|
|
|
|
(() => {
|
2016-05-01 17:31:59 +03:00
|
|
|
args.ipa = args.safari = true;
|
|
|
|
validateServerArgs(parser, args);
|
|
|
|
}).should.throw();
|
2015-05-23 18:11:42 +03:00
|
|
|
});
|
|
|
|
it('should allow ipa', () => {
|
|
|
|
(() => {
|
2016-05-01 17:31:59 +03:00
|
|
|
args.ipa = true;
|
|
|
|
validateServerArgs(parser, args);
|
|
|
|
}).should.not.throw();
|
2015-05-23 18:11:42 +03:00
|
|
|
});
|
|
|
|
it('should allow safari', () => {
|
|
|
|
(() => {
|
2016-05-01 17:31:59 +03:00
|
|
|
args.safari = true;
|
|
|
|
validateServerArgs(parser, args);
|
|
|
|
}).should.not.throw();
|
2015-05-23 18:11:42 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('app and safari', () => {
|
|
|
|
it('should not allow both', () => {
|
|
|
|
(() => {
|
2016-05-01 17:31:59 +03:00
|
|
|
args.app = args.safari = true;
|
|
|
|
validateServerArgs(parser, args);
|
|
|
|
}).should.throw();
|
2015-05-23 18:11:42 +03:00
|
|
|
});
|
|
|
|
it('should allow app', () => {
|
|
|
|
(() => {
|
2016-05-01 17:31:59 +03:00
|
|
|
args.app = true;
|
|
|
|
validateServerArgs(parser, args);
|
|
|
|
}).should.not.throw();
|
2015-05-23 18:11:42 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('forceIphone and forceIpad', () => {
|
|
|
|
it('should not allow both', () => {
|
|
|
|
(() => {
|
2016-05-01 17:31:59 +03:00
|
|
|
args.forceIphone = args.forceIpad = true;
|
|
|
|
validateServerArgs(parser, args);
|
|
|
|
}).should.throw();
|
2015-05-23 18:11:42 +03:00
|
|
|
});
|
|
|
|
it('should allow forceIphone', () => {
|
|
|
|
(() => {
|
2016-05-01 17:31:59 +03:00
|
|
|
args.forceIphone = true;
|
|
|
|
validateServerArgs(parser, args);
|
|
|
|
}).should.not.throw();
|
2015-05-23 18:11:42 +03:00
|
|
|
});
|
|
|
|
it('should allow forceIpad', () => {
|
|
|
|
(() => {
|
2016-05-01 17:31:59 +03:00
|
|
|
args.forceIpad = true;
|
|
|
|
validateServerArgs(parser, args);
|
|
|
|
}).should.not.throw();
|
2015-05-23 18:11:42 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('deviceName and defaultDevice', () => {
|
|
|
|
it('should not allow both', () => {
|
|
|
|
(() => {
|
2016-05-01 17:31:59 +03:00
|
|
|
args.deviceName = args.defaultDevice = true;
|
|
|
|
validateServerArgs(parser, args);
|
|
|
|
}).should.throw();
|
2015-05-23 18:11:42 +03:00
|
|
|
});
|
|
|
|
it('should allow deviceName', () => {
|
|
|
|
(() => {
|
2016-05-01 17:31:59 +03:00
|
|
|
args.deviceName = true;
|
|
|
|
validateServerArgs(parser, args);
|
|
|
|
}).should.not.throw();
|
2015-05-23 18:11:42 +03:00
|
|
|
});
|
|
|
|
it('should allow defaultDevice', () => {
|
|
|
|
(() => {
|
2016-05-01 17:31:59 +03:00
|
|
|
args.defaultDevice = true;
|
|
|
|
validateServerArgs(parser, args);
|
|
|
|
}).should.not.throw();
|
2015-05-23 18:11:42 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('validated arguments', () => {
|
|
|
|
// checking ports is already done.
|
|
|
|
// the only argument left is `backendRetries`
|
|
|
|
describe('backendRetries', () => {
|
|
|
|
it('should fail with value less than 0', () => {
|
2016-05-01 17:31:59 +03:00
|
|
|
args.backendRetries = -1;
|
|
|
|
(() => {validateServerArgs(parser, args);}).should.throw();
|
2015-05-23 18:11:42 +03:00
|
|
|
});
|
|
|
|
it('should succeed with value of 0', () => {
|
2016-05-01 17:31:59 +03:00
|
|
|
args.backendRetries = 0;
|
|
|
|
(() => {validateServerArgs(parser, args);}).should.not.throw();
|
2015-05-23 18:11:42 +03:00
|
|
|
});
|
|
|
|
it('should succeed with value above 0', () => {
|
2016-05-01 17:31:59 +03:00
|
|
|
args.backendRetries = 100;
|
|
|
|
(() => {validateServerArgs(parser, args);}).should.not.throw();
|
2015-05-23 18:11:42 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|