appium.old/test/config-specs.js

425 строки
15 KiB
JavaScript
Исходник Постоянная ссылка Обычный вид История

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, getBuildInfo, checkNodeOk, warnNodeDeprecations,
2015-05-23 18:11:42 +03:00
getNonDefaultArgs, getDeprecatedArgs, validateServerArgs,
validateTmpDir, showConfig, checkValidPort, updateBuildInfo,
APPIUM_VER } from '../lib/config';
2015-05-23 18:11:42 +03:00
import getParser from '../lib/parser';
import logger from '../lib/logger';
import { fs } from 'appium-support';
import request from 'request-promise';
2015-05-23 18:11:42 +03:00
let should = chai.should();
chai.use(chaiAsPromised);
describe('Config', function () {
describe('getGitRev', function () {
it('should get a reasonable git revision', async function () {
2015-05-23 18:11:42 +03:00
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', function () {
describe('getBuildInfo', function () {
async function verifyBuildInfoUpdate (useLocalGit) {
2018-10-03 16:39:25 +03:00
const buildInfo = getBuildInfo();
mockFs.expects('exists').atLeast(1).returns(useLocalGit);
buildInfo['git-sha'] = undefined;
buildInfo.built = undefined;
await updateBuildInfo(true);
buildInfo.should.be.an('object');
should.exist(buildInfo['git-sha']);
should.exist(buildInfo.built);
should.exist(buildInfo.version);
}
let mockFs;
let getStub;
beforeEach(function () {
mockFs = sinon.mock(fs);
getStub = sinon.stub(request, 'get');
});
afterEach(function () {
getStub.restore();
mockFs.restore();
});
it('should get a configuration object if the local git metadata is present', async function () {
await verifyBuildInfoUpdate(true);
});
it('should get a configuration object if the local git metadata is not present', async function () {
getStub.onCall(0).returns([
{
"name": `v${APPIUM_VER}`,
"zipball_url": "https://api.github.com/repos/appium/appium/zipball/v1.9.0-beta.1",
"tarball_url": "https://api.github.com/repos/appium/appium/tarball/v1.9.0-beta.1",
"commit": {
"sha": "3c2752f9f9c56000705a4ae15b3ba68a5d2e644c",
"url": "https://api.github.com/repos/appium/appium/commits/3c2752f9f9c56000705a4ae15b3ba68a5d2e644c"
},
"node_id": "MDM6UmVmNzUzMDU3MDp2MS45LjAtYmV0YS4x"
},
{
"name": "v1.8.2-beta",
"zipball_url": "https://api.github.com/repos/appium/appium/zipball/v1.8.2-beta",
"tarball_url": "https://api.github.com/repos/appium/appium/tarball/v1.8.2-beta",
"commit": {
"sha": "5b98b9197e75aa85e7507d21d3126c1a63d1ce8f",
"url": "https://api.github.com/repos/appium/appium/commits/5b98b9197e75aa85e7507d21d3126c1a63d1ce8f"
},
"node_id": "MDM6UmVmNzUzMDU3MDp2MS44LjItYmV0YQ=="
}
]);
getStub.onCall(1).returns({
"sha": "3c2752f9f9c56000705a4ae15b3ba68a5d2e644c",
"node_id": "MDY6Q29tbWl0NzUzMDU3MDozYzI3NTJmOWY5YzU2MDAwNzA1YTRhZTE1YjNiYTY4YTVkMmU2NDRj",
"commit": {
"author": {
"name": "Isaac Murchie",
"email": "isaac@saucelabs.com",
"date": "2018-08-17T19:48:00Z"
},
"committer": {
"name": "Isaac Murchie",
"email": "isaac@saucelabs.com",
"date": "2018-08-17T19:48:00Z"
},
"message": "v1.9.0-beta.1",
"tree": {
"sha": "2c0974727470eba419ea0b9951c52f72f8036b18",
"url": "https://api.github.com/repos/appium/appium/git/trees/2c0974727470eba419ea0b9951c52f72f8036b18"
},
"url": "https://api.github.com/repos/appium/appium/git/commits/3c2752f9f9c56000705a4ae15b3ba68a5d2e644c",
"comment_count": 0,
"verification": {
"verified": false,
"reason": "unsigned",
"signature": null,
"payload": null
}
},
"url": "https://api.github.com/repos/appium/appium/commits/3c2752f9f9c56000705a4ae15b3ba68a5d2e644c",
"html_url": "https://github.com/appium/appium/commit/3c2752f9f9c56000705a4ae15b3ba68a5d2e644c",
"comments_url": "https://api.github.com/repos/appium/appium/commits/3c2752f9f9c56000705a4ae15b3ba68a5d2e644c/comments",
});
await verifyBuildInfoUpdate(false);
2015-05-23 18:11:42 +03:00
});
});
describe('showConfig', function () {
before(function () {
sinon.spy(console, "log");
2015-05-23 18:11:42 +03:00
});
it('should log the config to console', async function () {
2018-10-03 16:39:25 +03:00
const config = getBuildInfo();
2015-05-23 18:11:42 +03:00
await showConfig();
2018-10-03 16:39:25 +03:00
console.log.calledOnce.should.be.true; // eslint-disable-line no-console
2016-08-03 22:14:29 +03:00
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', function () {
2015-05-23 18:11:42 +03:00
let _process = process;
before(function () {
2015-05-23 18:11:42 +03:00
// 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)) {
2015-05-23 18:11:42 +03:00
tempProcess[prop] = value;
}
2018-10-03 16:39:25 +03:00
process = tempProcess; // eslint-disable-line no-global-assign
2015-05-23 18:11:42 +03:00
});
after(function () {
2018-10-03 16:39:25 +03:00
process = _process; // eslint-disable-line no-global-assign
2015-05-23 18:11:42 +03:00
});
describe('checkNodeOk', function () {
2018-02-12 23:03:11 +03:00
it('should fail if node is below 6', function () {
process.version = 'v4.4.7';
checkNodeOk.should.throw();
2015-05-23 18:11:42 +03:00
process.version = 'v0.9.12';
checkNodeOk.should.throw();
process.version = 'v0.1';
checkNodeOk.should.throw();
process.version = 'v0.10.36';
checkNodeOk.should.throw();
2016-08-03 21:38:35 +03:00
process.version = 'v0.12.14';
checkNodeOk.should.throw();
process.version = 'v5.7.0';
2018-02-12 23:03:11 +03:00
checkNodeOk.should.throw();
2016-08-03 21:38:35 +03:00
});
it('should succeed if node is 6+', function () {
2016-08-03 21:38:35 +03:00
process.version = 'v6.3.1';
checkNodeOk.should.not.throw();
2015-05-23 18:11:42 +03:00
});
it('should succeed if node is 7+', function () {
process.version = 'v7.1.1';
checkNodeOk.should.not.throw();
});
it('should succeed if node is 8+', function () {
process.version = 'v8.1.2';
checkNodeOk.should.not.throw();
});
2018-02-12 23:03:11 +03:00
it('should succeed if node is 9+', function () {
process.version = 'v9.1.2';
checkNodeOk.should.not.throw();
});
2015-05-23 18:11:42 +03:00
});
describe('warnNodeDeprecations', function () {
2015-05-23 18:11:42 +03:00
let spy;
before(function () {
2015-05-23 18:11:42 +03:00
spy = sinon.spy(logger, "warn");
});
beforeEach(function () {
2018-05-08 18:19:41 +03:00
spy.resetHistory();
2015-05-23 18:11:42 +03:00
});
2018-02-12 23:03:11 +03:00
it('should log a warning if node is below 8', function () {
process.version = 'v7.10.1';
2015-05-23 18:11:42 +03:00
warnNodeDeprecations();
logger.warn.callCount.should.equal(1);
});
2018-02-12 23:03:11 +03:00
it('should not log a warning if node is 8+', function () {
process.version = 'v8.0.0';
2016-08-03 21:38:35 +03:00
warnNodeDeprecations();
logger.warn.callCount.should.equal(0);
});
2018-02-12 23:03:11 +03:00
it('should not log a warning if node is 9+', function () {
process.version = 'v9.0.0';
2015-05-23 18:11:42 +03:00
warnNodeDeprecations();
logger.warn.callCount.should.equal(0);
});
});
});
describe('server arguments', function () {
2015-05-23 18:11:42 +03:00
let parser = getParser();
parser.debug = true; // throw instead of exit on error; pass as option instead?
let args = {};
beforeEach(function () {
// give all the defaults
for (let rawArg of parser.rawArgs) {
args[rawArg[1].dest] = rawArg[1].defaultValue;
}
});
describe('getNonDefaultArgs', function () {
it('should show none if we have all the defaults', function () {
2015-05-23 18:11:42 +03:00
let nonDefaultArgs = getNonDefaultArgs(parser, args);
_.keys(nonDefaultArgs).length.should.equal(0);
});
it('should catch a non-default argument', function () {
2015-05-23 18:11:42 +03:00
args.isolateSimDevice = true;
let nonDefaultArgs = getNonDefaultArgs(parser, args);
_.keys(nonDefaultArgs).length.should.equal(1);
should.exist(nonDefaultArgs.isolateSimDevice);
});
});
describe('getDeprecatedArgs', function () {
it('should show none if we have no deprecated arguments', function () {
2015-05-23 18:11:42 +03:00
let deprecatedArgs = getDeprecatedArgs(parser, args);
_.keys(deprecatedArgs).length.should.equal(0);
});
it('should catch a deprecated argument', function () {
args.showIOSLog = true;
2015-05-23 18:11:42 +03:00
let deprecatedArgs = getDeprecatedArgs(parser, args);
_.keys(deprecatedArgs).length.should.equal(1);
should.exist(deprecatedArgs['--show-ios-log']);
2015-05-23 18:11:42 +03:00
});
it('should catch a non-boolean deprecated argument', function () {
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', function () {
it('should be false for port too high', function () {
2015-05-23 18:11:42 +03:00
checkValidPort(65536).should.be.false;
});
it('should be false for port too low', function () {
2015-05-23 18:11:42 +03:00
checkValidPort(0).should.be.false;
});
it('should be true for port 1', function () {
2015-05-23 18:11:42 +03:00
checkValidPort(1).should.be.true;
});
it('should be true for port 65535', function () {
2015-05-23 18:11:42 +03:00
checkValidPort(65535).should.be.true;
});
});
describe('validateTmpDir', function () {
2018-10-03 16:39:25 +03:00
it('should fail to use a tmp dir with incorrect permissions', function () {
2015-05-23 18:11:42 +03:00
validateTmpDir('/private/if_you_run_with_sudo_this_wont_fail').should.be.rejectedWith(/could not ensure/);
});
2018-10-03 16:39:25 +03:00
it('should fail to use an undefined tmp dir', function () {
2015-05-23 18:11:42 +03:00
validateTmpDir().should.be.rejectedWith(/could not ensure/);
});
2018-10-03 16:39:25 +03:00
it('should be able to use a tmp dir with correct permissions', function () {
2015-05-23 18:11:42 +03:00
validateTmpDir('/tmp/test_tmp_dir/with/any/number/of/levels').should.not.be.rejected;
});
});
describe('parsing args with empty argv[1]', function () {
let argv1;
before(function () {
argv1 = process.argv[1];
});
after(function () {
process.argv[1] = argv1;
});
it('should not fail if process.argv[1] is undefined', function () {
process.argv[1] = undefined;
let args = getParser();
args.prog.should.be.equal('Appium');
});
it('should set "prog" to process.argv[1]', function () {
process.argv[1] = 'Hello World';
let args = getParser();
args.prog.should.be.equal('Hello World');
});
});
describe('validateServerArgs', function () {
2015-05-23 18:11:42 +03:00
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(function () {
args = _.clone(defaultArgs);
});
describe('mutually exclusive server arguments', function () {
describe('noReset and fullReset', function () {
it('should not allow both', function () {
2015-05-23 18:11:42 +03:00
(() => {
args.noReset = args.fullReset = true;
validateServerArgs(parser, args);
}).should.throw();
2015-05-23 18:11:42 +03:00
});
it('should allow noReset', function () {
2015-05-23 18:11:42 +03:00
(() => {
args.noReset = true;
validateServerArgs(parser, args);
}).should.not.throw();
2015-05-23 18:11:42 +03:00
});
it('should allow fullReset', function () {
2015-05-23 18:11:42 +03:00
(() => {
args.fullReset = true;
validateServerArgs(parser, args);
}).should.not.throw();
2015-05-23 18:11:42 +03:00
});
});
describe('ipa and safari', function () {
it('should not allow both', function () {
2015-05-23 18:11:42 +03:00
(() => {
args.ipa = args.safari = true;
validateServerArgs(parser, args);
}).should.throw();
2015-05-23 18:11:42 +03:00
});
it('should allow ipa', function () {
2015-05-23 18:11:42 +03:00
(() => {
args.ipa = true;
validateServerArgs(parser, args);
}).should.not.throw();
2015-05-23 18:11:42 +03:00
});
it('should allow safari', function () {
2015-05-23 18:11:42 +03:00
(() => {
args.safari = true;
validateServerArgs(parser, args);
}).should.not.throw();
2015-05-23 18:11:42 +03:00
});
});
describe('app and safari', function () {
it('should not allow both', function () {
2015-05-23 18:11:42 +03:00
(() => {
args.app = args.safari = true;
validateServerArgs(parser, args);
}).should.throw();
2015-05-23 18:11:42 +03:00
});
it('should allow app', function () {
2015-05-23 18:11:42 +03:00
(() => {
args.app = true;
validateServerArgs(parser, args);
}).should.not.throw();
2015-05-23 18:11:42 +03:00
});
});
describe('forceIphone and forceIpad', function () {
it('should not allow both', function () {
2015-05-23 18:11:42 +03:00
(() => {
args.forceIphone = args.forceIpad = true;
validateServerArgs(parser, args);
}).should.throw();
2015-05-23 18:11:42 +03:00
});
it('should allow forceIphone', function () {
2015-05-23 18:11:42 +03:00
(() => {
args.forceIphone = true;
validateServerArgs(parser, args);
}).should.not.throw();
2015-05-23 18:11:42 +03:00
});
it('should allow forceIpad', function () {
2015-05-23 18:11:42 +03:00
(() => {
args.forceIpad = true;
validateServerArgs(parser, args);
}).should.not.throw();
2015-05-23 18:11:42 +03:00
});
});
describe('deviceName and defaultDevice', function () {
it('should not allow both', function () {
2015-05-23 18:11:42 +03:00
(() => {
args.deviceName = args.defaultDevice = true;
validateServerArgs(parser, args);
}).should.throw();
2015-05-23 18:11:42 +03:00
});
it('should allow deviceName', function () {
2015-05-23 18:11:42 +03:00
(() => {
args.deviceName = true;
validateServerArgs(parser, args);
}).should.not.throw();
2015-05-23 18:11:42 +03:00
});
it('should allow defaultDevice', function () {
2015-05-23 18:11:42 +03:00
(() => {
args.defaultDevice = true;
validateServerArgs(parser, args);
}).should.not.throw();
2015-05-23 18:11:42 +03:00
});
});
});
describe('validated arguments', function () {
2015-05-23 18:11:42 +03:00
// checking ports is already done.
// the only argument left is `backendRetries`
describe('backendRetries', function () {
it('should fail with value less than 0', function () {
args.backendRetries = -1;
(() => {validateServerArgs(parser, args);}).should.throw();
2015-05-23 18:11:42 +03:00
});
it('should succeed with value of 0', function () {
args.backendRetries = 0;
(() => {validateServerArgs(parser, args);}).should.not.throw();
2015-05-23 18:11:42 +03:00
});
it('should succeed with value above 0', function () {
args.backendRetries = 100;
(() => {validateServerArgs(parser, args);}).should.not.throw();
2015-05-23 18:11:42 +03:00
});
});
});
});
});