1 |
|
2 |
|
3 | const { CLI_MODE } = require('./constants');
|
4 |
|
5 |
|
6 | const Promise = require('bluebird');
|
7 | const os = require('os');
|
8 | const localRunnerCache = require('./runnerFileCache');
|
9 | const utils = require('../utils');
|
10 | const Ajv = require('ajv');
|
11 | const prepareRunnerAndTestimStartUtils = require('./prepareRunnerAndTestimStartUtils');
|
12 | const mockNetworkRuleFileSchema = require('./mockNetworkRuleFileSchema.json');
|
13 |
|
14 | const MAX_RULE_FILE_SIZE_IN_MB = 1;
|
15 | const PREPARE_MOCK_NETWORK_ERROR_PREFIX = 'JSON file supplied to --mock-network-pattern';
|
16 | const MSEC_IN_HALF_DAY = 1000 * 60 * 60 * 12;
|
17 |
|
18 | const logger = require('./logger').getLogger('prepare runner');
|
19 |
|
20 | Promise.resolve().then(() => {
|
21 |
|
22 | global.xhr2 = require('./xhr2');
|
23 | });
|
24 |
|
25 | async function prepare(options) {
|
26 | |
27 |
|
28 |
|
29 | let chromedriverPromise = Promise.resolve();
|
30 |
|
31 | const hasNoGrid = !options.host && !options.gridId && !options.grid && (!options.testPlan || options.testPlan.length === 0);
|
32 | const isTdkRun = options.files.length !== 0;
|
33 | if ((hasNoGrid && isTdkRun) || options.useLocalChromeDriver) {
|
34 | chromedriverPromise = prepareRunnerAndTestimStartUtils.prepareChromeDriver();
|
35 | options.useLocalChromeDriver = true;
|
36 | }
|
37 |
|
38 | if (!options.playerRequirePath && options.mode !== CLI_MODE.EXTENSION) {
|
39 | await prepareRunnerAndTestimStartUtils.preparePlayer(options.playerLocation, options.canary);
|
40 | }
|
41 | if (options.mode === CLI_MODE.EXTENSION && !options.ext) {
|
42 | await localRunnerCache.memoize(() => prepareRunnerAndTestimStartUtils.prepareExtension(options.extensionLocation), 'prepareExtension', MSEC_IN_HALF_DAY)();
|
43 | }
|
44 |
|
45 | let customExtensionLocation;
|
46 |
|
47 | if (options.installCustomExtension) {
|
48 | customExtensionLocation = await prepareRunnerAndTestimStartUtils.prepareCustomExtension(options.installCustomExtension);
|
49 | }
|
50 |
|
51 | await chromedriverPromise;
|
52 |
|
53 | return customExtensionLocation;
|
54 | }
|
55 |
|
56 | async function prepareMockNetwork(location) {
|
57 | logger.info('prepare MockNetwork', { location });
|
58 | const rulesJsonBuf = await utils.getSourceAsBuffer(location);
|
59 | if (rulesJsonBuf.byteLength > MAX_RULE_FILE_SIZE_IN_MB * 1000000) {
|
60 | throw new Error(`${PREPARE_MOCK_NETWORK_ERROR_PREFIX} exceeded ${MAX_RULE_FILE_SIZE_IN_MB}MB`);
|
61 | }
|
62 |
|
63 | let rulesJson;
|
64 | try {
|
65 | rulesJson = JSON.parse(rulesJsonBuf);
|
66 | } catch (error) {
|
67 | throw new Error(`${PREPARE_MOCK_NETWORK_ERROR_PREFIX} cannot be parsed.${os.EOL}${error}`);
|
68 | }
|
69 |
|
70 | const ajv = new Ajv();
|
71 | const valid = ajv.validate(mockNetworkRuleFileSchema, rulesJson);
|
72 | if (!valid) {
|
73 | const errors = ajv.errors.map((e, i) => `${++i}) ${e.dataPath} ${e.message}`).join(os.EOL);
|
74 | throw new Error(`${PREPARE_MOCK_NETWORK_ERROR_PREFIX} is malformed.${os.EOL}${errors}`);
|
75 | }
|
76 |
|
77 | return rulesJson.entries;
|
78 | }
|
79 |
|
80 | module.exports = {
|
81 | prepare,
|
82 | prepareMockNetwork,
|
83 | };
|