All files / lib config.js

100% Statements 159/159
100% Branches 83/83
100% Functions 5/5
100% Lines 147/147

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                                                                                                                                                                    135x 135x   135x 135x 135x   135x 135x   135x   135x 135x   135x 135x 135x 1x   134x   135x 135x 135x 135x   135x 135x 135x 135x 135x 135x 135x 135x 135x 3x     135x 135x 3x     135x 135x 135x   134x 134x   134x 134x 134x 134x   134x   134x 2x 132x 1x   131x   134x     134x 134x 134x 134x 134x 134x 134x 134x 134x   134x 134x 134x 134x 134x 134x   134x 134x 134x 134x 2x   134x 84x   134x 134x 134x 134x 134x 134x   134x 7x 7x 3x 3x   6x       134x 134x 134x 134x 134x 134x 4x 7x 2x     134x 122x   73x     134x 134x 134x 134x   134x 134x 134x 134x   134x 134x 134x 134x 134x 134x 134x 134x 134x   134x 134x 134x   134x   134x 134x 134x 134x 134x 134x 134x 134x 134x   134x 1x 1x 1x 1x     134x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x     134x 134x 134x 134x   134x  
"use strict";
/**
 * Configures `GlaceJS` before tests run.
 *
 * @namespace GlaceConfig
 * @prop {object} cluster - Cluster namespace.
 * @prop {boolean} [cluster.isMaster=true] - Mark current process as master.
 * @prop {boolean} [cluster.isSlave=false] - Mark current process as slave.
 * @prop {number} [cluster.slavesNum=0] - Number of slaves to launch.
 * @prop {number} [cluster.slaveId=null] - ID of slave.
 * @prop {object} session - Session namespace.
 * @prop {string} session.name - Session name. By default contains timestamp.
 *  Can be overridden with CLI option `--session-name`.
 * @prop {string} session.id - Session ID. Default is timestamp.
 * @prop {boolean} [session.errors=[]] - List of session errors.
 * @prop {boolean} [session.isPassed=false] - Flag to define if tests session
 *  run is passed or no.
 * @prop {array<string>} [session.preloads=[]] - Array of paths to `js` modules
 *  which will be loaded before tests session run.
 * @prop {boolean} [session.interactive=false] - Flag to launch interactive
 *  session. Can be overridden with CLI option `-i / --interactive`.
 * @prop {boolean} [session.debugOnFail=false] - Flag to enter to interactive
 *  mode on step failure. Can be overridden with CLI option `--debug-on-fail`.
 * @prop {boolean} [session.exitOnFail=false] - Flag to break tests session on
 *  first test failure. Can be overridden with CLI option `--exit-on-fail`.
 * @prop {string} [session.uncaughtException=log] - Strategy to process uncaught
 *  exceptions. Available values are `log`, `fail`, `mocha`. Can be overridden
 *  with CLI option `--uncaught`.
 * @prop {string} session.rootConftest - Path to `js` module which will be
 *  loaded right after preloads. Can be overridden with CLI
 *  option `--root-conftest`.
 * @prop {array<string>} session.killProcs - Array of process names which will
 *  be killed before tests session run. Can be overridden with CLI
 *  option `--kill-procs`.
 * @prop {object} retry - Retry settings namespace.
 * @prop {integer} [retry.id=0] - Number of retried session.
 * @prop {object} [retry.chunkIds={}] - Chunk ids grouped by retry values. For
 * example: `{1: ['1_1', '1_2'], 3: ['2_1'], 2: ['3_1', '3_2']}`. Populated on
 * first session run.
 * @prop {?array<string>} [retry.curChunkIds=null] - Reference to currently
 * populated group in `retry.chunkIds`.
 * @prop {object} test - Test settings namespace.
 * @prop {integer} [test.id=0] - Number of currently created test. Each test has
 * unique incremental id. On first session run it's assigned, on retry is used
 * to find test by id, because test name can be non-unique.
 * @prop {?TestCase} [test.curCase=null] - Currently executed test case.
 * @prop {array<TestCase>} [test.cases=[]] - Array of session test cases.
 * @prop {array<string>} [test.languages=[]] - Array of language names. Can be
 *  overridden with CLI option `--languages`.
 * @prop {array<string>} [test.dirs=[]] - Array of test files or folders. Can
 *  be overridden with CLI arguments or option `--targets` separated with comma.
 * @prop {boolean} [test.checkNames=true] - Flag to check test names uniqueness.
 *  Can be overridden with CLI option `--dont-check-names`.
 * @prop {integer} [test.retries=0] - <a name="test-retry" href="#test-retry">#</a>
 * Number of test retries on failure. Overridden with CLI option `--retry`.
 * @prop {object} chunk - Chunk settings namespace.
 * @prop {integer} [chunk.id=0] - Number of chunk inside a test, incremental,
 * starts from `0` in each test. Fully unique chunk id is consist of `test.id` &
 * `chunk.id`, like `1_1`, `1_2`, `2_1`.
 * @prop {?string} [chunk.curId=null] - Currently executed fully unique chunk id.
 * @prop {array<string>} [chunk.passedIds=[]] - Fully unique ids of passed chunks.
 * Used to skip already passed chunks on retry.
 * @prop {integer} [chunk.retries=0] - <a name="test-chunk-retry" href="#test-chunk-retry">#</a>
 * Number of chunk retries on failure. Overridden with CLI option `--chunk-retry`.
 * @prop {integer} [chunk.timeout=180] - <a name="test-chunk-timeout" href="#test-chunk-timeout">#</a>
 * Timeout of chunk execution, **sec**. Overridden with CLI option `--chunk-timeout`.
 * @prop {object} report - Report namespace.
 * @prop {string} [report.dir=cwd/report] - Folder to save tests session report.
 * @prop {string} [report.testDir] - Folder to save test-specific artifacts.
 * @prop {boolean} [report.clear=true] - Flag to clear report before tests run.
 *  Can be overridden with CLI option `--dont-clear-report`.
 * @prop {boolean} [report.errorsNow=false] - Flag to print test error right
 *  after its capture. Can be overridden with CLI option `--errors-now`.
 * @prop {string} [report.failedTestsPath=cwd/failed-tests.json] - Path to file
 *  where info about failed tests will be saved to. Can be overridden with CLI
 *  option `--failed-tests-path`.
 * @prop {object} [xunit] - xUnit report namespace.
 * @prop {boolean} [xunit.use=false] - Flag to activate xUnit report. Can be
 *  overridden with CLI option `--xunit`.
 * @prop {string} [xunit.path=report.dir/xunit.xml] - Path to xUnit report. Can
 *  be overridden with CLI option `--xunit-path`.
 * @prop {string} [xunit.suiteName=session.name] - xUnit suite name. Can be
 *  overridden with CLI option `--xunit-suite-name`.
 * @prop {object} [allure] - Allure report namespace.
 * @prop {boolean} [allure.use=false] - Flag to activate allure report. Can be
 *  overridden with CLI option `--allure`.
 * @prop {string} [allure.dir=report.dir/allure] - Folder to save allure report.
 *  Can be overridden with CLI option `--allure-dir`.
 * @prop {string} [allure.suiteName=session.name] - Allure suite name. Can be
 *  overridden with CLI option `--allure-suite-name`.
 * @prop {object} testrail - Testrail report namespace.
 * @prop {boolean} [testrail.use=false] - Flag to activate testrail report. Can
 *  be overridden with CLI option `--testrail`.
 * @prop {string} testrail.host - Testrail host. Can be overridden with CLI
 *  option `--testrail-host`.
 * @prop {string} testrail.user - Testrail user name or email. Can be overridden
 *  with CLI option `--testrail-user`.
 * @prop {string} testrail.token - Testrail auth token. Can be overridden with
 *  CLI option `--testrail-token`.
 * @prop {string} testrail.projectId - Testrail project ID. Can be overridden
 *  with CLI option `--testrail-project-id`.
 * @prop {string} testrail.suiteId - Testrail suite ID. Can be overridden with
 *  CLI option `--testrail-suite-id`.
 * @prop {string} testrail.runName - Testrail run name. Can be overridden with
 *  CLI option `--testrail-run-name`.
 * @prop {string} testrail.runDescription - Testrail run description. Can be
 *  overridden with CLI option `--testrail-run-description`.
 * @prop {object} plugins - Plugins namespace.
 * @prop {string} plugins.dir - Folder with custom plugins. Can be overridden
 *  with CLI option `--plugins-dir`.
 * @prop {object} filter - Tests filter namespace.
 * @prop {string} filter.grep - Mocha grep option to filter tests, scopes and
 *  suites. Can be overridden with CLI option `-g / --grep`.
 * @prop {array<string>} filter.include - List of test names which should be
 *  included to tests session. Can be overridden with CLI option `--include`.
 * @prop {array<string>} filter.exclude - List of test names which should be
 *  excluded from tests session. Can be overridden with CLI option `--exclude`.
 * @prop {boolean} [filter.precise=false] - Flag for precise tests inclusion or
 *  exclusion (not substring pattern). Can be overridden with CLI option
 *  `--precise`.
 * @prop {object} tools - Tools namespace.
 * @prop {boolean} [tools.stepsList=false] - Flag to list available steps only.
 *  Can be overridden with CLI option `--list-steps`.
 * @prop {string} tools.stepsFilter - String to filter steps. Can be overridden
 *  with CLI option `--list-steps`.
 * @prop {boolean} [tools.testsList=false] - Flag to list implemented tests
 *  only. Can be overridden with CLI option `--list-tests`.
 * @prop {string} tools.testsFilter - String to filter tests. Can be overridden
 *  with CLI option `--list-tests`.
 * @prop {boolean} [tools.fixturesList=false] - Flag to list available fixtures
 *  only. Can be overridden with CLI option `--list-fixtures`.
 * @prop {string} tools.fixturesFilter - String to filter fixtures. Can be
 *  overridden with CLI option `--list-fixtures`.
 * @prop {boolean} [tools.checkTestrail=false] - Flag to check matching of
 *  testrail cases with implemented tests only. Can be overridden with CLI
 *  option `--testrail-check`.
 */
 
const fs = require("fs");
const path = require("path");
 
require("colors");
const _ = require("lodash");
const expect = require("chai").expect;
 
const U = require("glace-utils");
U.docString();
 
const plugins = require("./plugins");
 
let config = U.config;
const args = config.args;
 
config.cluster = U.defVal(config.cluster, {});
config.cluster.slavesNum = U.defVal(args.slaves, 0);
if (config.cluster.slavesNum === "auto") {
    config.cluster.slavesNum = require("os").cpus().length;
} else {
    config.cluster.slavesNum = +config.cluster.slavesNum;
}
config.cluster.slaveId = parseInt(process.env.GLACE_SLAVE_ID) || null;
config.cluster.isSlave = !!process.env.GLACE_SLAVE_ID;
config.cluster.isMaster = !!config.cluster.slavesNum && !config.cluster.isSlave;
config.cluster.artifactsDir = path.resolve(U.cwd, U.defVal(args.reportDir, "report"));
 
config.session = U.defVal(config.session, {});
const date = new Date();
config.session.name = U.defVal(args.sessionName, `Session ${date.toLocaleString()}`);
config.session.id = date.getTime();
config.session.errors = [];
config.session.isPassed = false;
config.session.preloads = [];
config.session.interactive = args.i || args.interactive || false;
if (config.cluster.slavesNum) {
    expect(config.session.interactive,
        "Interactive mode is incompatible with `--slaves`").to.be.false;
}
config.session.debugOnFail = args.debugOnFail || false;
if (config.cluster.slavesNum) {
    expect(config.session.debugOnFail,
        "`--debug-on-fail` is incompatible with `--slaves`").to.be.false;
}
config.session.exitOnFail = args.exitOnFail || false;
config.session.uncaughtException = (args.uncaught || "log").toLowerCase();
expect([ "log", "fail", "mocha" ],
    "Invalid `--uncaught` option").include(config.session.uncaughtException);
if (args.rootConftest) config.session.rootConftest = path.resolve(U.cwd, args.rootConftest);
if (args.killProcs && !config.cluster.isSlave) config.session.killProcs = U.splitBy(args.killProcs, ",");
 
config.retry = U.defVal(config.retry, {});
config.retry.id = 0;
config.retry.chunkIds = {};
config.retry.curChunkIds = null;
 
const get_targets = () => {
    let tt;
    if (args._ && args._.length) {
        tt = args._;
    } else if (args.targets && args.targets.length) {
        tt = U.splitBy(args.targets, ",");
    } else {
        tt = ["tests"];
    };
    return tt.map(t => path.resolve(U.cwd, t));
};
 
config.test = U.defVal(config.test, {});
config.test.id = 0;
config.test.curCase = null;
config.test.cases = [];
config.test.languages = [];
if (args.languages) config.test.languages = U.splitBy(args.languages, ",");
config.test.dirs = get_targets();
config.test.checkNames = !args.dontCheckNames;
config.test.retries = Math.max(0, U.defVal(args.retry, 0));
 
config.chunk = U.defVal(config.chunk, {});
config.chunk.id = 0;
config.chunk.curId = null;
config.chunk.passedIds = [];
config.chunk.retries = Math.max(0, U.defVal(args.chunkRetry, 0));
config.chunk.timeout = (args.chunkTimeout || 180) * 1000 || Infinity;
 
config.report = U.defVal(config.report, {});
config.report.dots = args.dots || false;
config.report.dir = config.cluster.artifactsDir;
if (config.cluster.isMaster) {
    config.report.dir = path.resolve(config.report.dir, "master");
}
if (config.cluster.isSlave) {
    config.report.dir = path.resolve(config.report.dir, "slave-" + config.cluster.slaveId);
}
config.report.testDir = null;
config.report.clear = !args.dontClearReport;
config.report.errorsNow = args.errorsNow || false;
config.report.deepErrors = args.deepErrors || false;
config.report.failedTestsPath = path.resolve(config.report.dir, U.defVal(args.failedTestsPath, "failed-tests.json"));
if (!config.report.failedTestsPath.endsWith(".json")) config.report.failedTestsPath += ".json";
 
const tests_filter = filter => {
    const filePath = path.resolve(U.cwd, filter);
    if (fs.existsSync(filePath)) {
        config.filter.precise = true;
        return U.loadJson(filePath);
    } else {
        return U.splitBy(filter, "|").map(e => ({ id: e }));
    }
};
 
config.filter = U.defVal(config.filter, {});
config.filter.grep = args.g || args.grep;
config.filter.precise = args.preciseMatch || false;
if (args.include) config.filter.include = tests_filter(args.include);
if (args.exclude) config.filter.exclude = tests_filter(args.exclude);
if (config.filter.include) {
    for (const include of config.filter.include) {
        if (!include.passed_chunk_ids) continue;
        config.chunk.passedIds = config.chunk.passedIds.concat(include.passed_chunk_ids);
    }
}
if (process.env.GLACE_TEST_IDS) {
    config.filter.testIds = U.splitBy(process.env.GLACE_TEST_IDS, ",").map(i => +i);
} else {
    config.filter.testIds = null;
}
 
config.xunit = U.defVal(config.xunit, {});
config.xunit.use = U.defVal(args.xunit, false);
config.xunit.path = path.resolve(config.report.dir, U.defVal(args.xunitPath, "xunit.xml"));
config.xunit.suiteName = U.defVal(args.xunitSuiteName, config.session.name);
 
config.allure = U.defVal(config.allure, {});
config.allure.use = U.defVal(args.allure, false);
config.allure.dir = path.resolve(config.report.dir, U.defVal(args.allureDir, "allure"));
config.allure.suiteName = U.defVal(args.allureSuiteName, config.session.name);
 
config.testrail = U.defVal(config.testrail, {});
config.testrail.use = U.defVal(args.testrail, false);
config.testrail.host = U.defVal(args.testrailHost);
config.testrail.user = U.defVal(args.testrailUser);
config.testrail.token = U.defVal(args.testrailToken);
config.testrail.projectId = U.defVal(args.testrailProjectId);
config.testrail.suiteId = U.defVal(args.testrailSuiteId);
config.testrail.runName = U.defVal(args.testrailRunName);
config.testrail.runDescription = U.defVal(args.testrailRunDesc);
 
config.plugins = U.defVal(config.plugins, {});
if (args.pluginsDir) config.plugins.dir = path.join(U.cwd, args.pluginsDir);
config.plugins.disableDefault = U.defVal(args.disableDefaultPlugins, false);
 
plugins.getModules("config");
 
config.tools = U.defVal(config.tools, {});
config.tools.stepsList = !!args.listSteps;
config.tools.stepsFilter = typeof(args.listSteps) === "string" ? args.listSteps : null;
config.tools.testsList = !!args.listTests;
config.tools.testsFilter = typeof(args.listTests) === "string" ? args.listTests : null;
config.tools.fixturesList = !!args.listFixtures;
config.tools.fixturesFilter = typeof(args.listFixtures) === "string" ? args.listFixtures : null;
config.tools.pluginsList = !!args.listPlugins;
config.tools.checkTestrail = U.defVal(args.testrailCheck, false);
 
if (config.session.debugOnFail) {
    config.chunk.timeout = Infinity;
    config.xunit.use = false;
    config.allure.use = false;
    config.testrail.use = false;
}
 
if (config.session.interactive) {
    const temp = require("temp").track();
    const tempPath = temp.path({ prefix: "test", suffix: ".js" });
    const tempData = "test('interactive', () => chunk(async () => await $.debug()));";
    fs.writeFileSync(tempPath, tempData);
    config.test.dirs = [tempPath];
    config.chunk.timeout = Infinity;
    config.filter.grep = null;
    config.filter.include = null;
    config.filter.exclude = null;
    config.xunit.use = false;
    config.allure.use = false;
    config.testrail.use = false;
}
 
let userConfig = {};
const userConfigPath = path.resolve(U.cwd, (args.userConfig || "config.js"));
if (fs.existsSync(userConfigPath)) userConfig = require(userConfigPath);
_.assign(config, userConfig);
 
module.exports = config;