tia
Version:
Time is All (logs driven test engine with ExtJs support)
206 lines • 8.05 kB
JavaScript
;
/* global gIn */
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const path = __importStar(require("path"));
const fs = __importStar(require("fs"));
const fileUtils = __importStar(require("./file-utils"));
function containsSuite(dir) {
const suiteDir = path.join(dir, gT.engineConsts.suiteDirName);
return fileUtils.isDirectory(suiteDir);
}
exports.containsSuite = containsSuite;
function isRootDirInited(rootDir) {
const rootResultsDir = path.join(rootDir, gT.engineConsts.suiteDirName, gT.engineConsts.rootResDirName);
return fileUtils.isDirectory(rootResultsDir);
}
exports.isRootDirInited = isRootDirInited;
function isSuiteDirInited(dir) {
const suiteResultsDir = path.join(dir, gT.engineConsts.suiteDirName, gT.engineConsts.suiteResDirName);
return fileUtils.isDirectory(suiteResultsDir);
}
exports.isSuiteDirInited = isSuiteDirInited;
function resolveRootDirFromArgsAndEnv(argsTiaRootDir) {
let tiaRootDir = argsTiaRootDir || process.env[gT.engineConsts.rootDirEnvVarName];
if (tiaRootDir && !path.isAbsolute(tiaRootDir)) {
gIn.tracer.msg0(`Relative root dir "${tiaRootDir}" will be resolved to absolute one.`);
tiaRootDir = path.resolve(process.cwd(), tiaRootDir);
tiaRootDir = fileUtils.rmLastDirSep(tiaRootDir);
}
if (tiaRootDir) {
gIn.tracer.msg0(`Root dir is: ${tiaRootDir}`);
return tiaRootDir;
}
return null;
}
exports.resolveRootDirFromArgsAndEnv = resolveRootDirFromArgsAndEnv;
function findTiaRootInParents(dir) {
const systemRoot = path.parse(dir).root;
let notFound = false;
try {
while (!isRootDirInited(dir)) {
if (dir === systemRoot) {
notFound = true;
break;
}
// eslint-disable-next-line no-param-reassign
dir = path.dirname(dir);
}
}
catch (err) {
gIn.tracer.exc(err);
notFound = true;
}
if (notFound)
return null;
return dir;
}
exports.findTiaRootInParents = findTiaRootInParents;
// export function findTiaSuiteResInParents(dir) {
//
// const systemRoot = path.parse(dir).root;
// let notFound = false;
//
// try {
// while (!isSuiteDirInited(dir)) {
// if (dir === systemRoot || isRootDirInited(dir)) {
// notFound = true;
// break;
// }
//
// // eslint-disable-next-line no-param-reassign
// dir = path.dirname(dir);
// }
// } catch (err) {
// gIn.tracer.exc(err);
// notFound = true;
// }
//
// if (notFound) return null;
// return dir;
// };
function getTiaSuiteFromParents(dir) {
const startIndex = dir.indexOf(gT.engineConsts.suiteDirName);
if (startIndex === -1) {
throw new Error(`No suite found for directory: ${dir}`);
}
const result = dir.slice(0, startIndex + gT.engineConsts.suiteDirName.length);
return result;
}
exports.getTiaSuiteFromParents = getTiaSuiteFromParents;
function isTiaSuiteInParents(dir) {
const dirArr = dir.split(path.sep);
return dirArr.includes(gT.engineConsts.suiteDirName);
}
exports.isTiaSuiteInParents = isTiaSuiteInParents;
function findTiaRootInChildren(dir) {
return fileUtils.whichDirContain(dir, [gT.engineConsts.rootResDirName], path.join(dir, gT.engineConsts.suiteDirName));
}
exports.findTiaRootInChildren = findTiaRootInChildren;
/**
* Resolves path specified by cmd line option or environment variable.
* Non mandatory path resolved to CWD.
* Relative paths resolved relative to CWD.
* @return {String} - resolved path.
*/
function resolveRootDirEx(argsTiaRootDir) {
let tiaRootDir = resolveRootDirFromArgsAndEnv(argsTiaRootDir);
if (tiaRootDir) {
return tiaRootDir;
}
gIn.tracer.msg0('Root dir is not specified by cmd line or env var.');
tiaRootDir = findTiaRootInParents(process.cwd());
if (!tiaRootDir) {
gIn.tracer.err('You have not initialized any directories. See tia -h for init command');
process.exit(1);
}
return tiaRootDir || '';
}
exports.resolveRootDirEx = resolveRootDirEx;
function initTiaSuite() {
// eslint-disable-next-line no-param-reassign
const dir = process.cwd();
if (isTiaSuiteInParents(dir)) {
gIn.tracer.err(`TIA suite is existed in parent dirs here: ${dir}`);
gIn.tracer.err('You can not create suite inside suite');
process.exit(1);
}
const existedTiaRootInChildren = findTiaRootInChildren(dir);
if (existedTiaRootInChildren) {
gIn.tracer.err(`TIA root is existed in children dirs here: ${existedTiaRootInChildren}`);
gIn.tracer.err('You can not have TIA root inside TIA suite');
process.exit(1);
}
const tiaRootInParents = findTiaRootInParents(dir);
if (!tiaRootInParents) {
gIn.tracer.err(`TIA root is not found for : ${dir}`);
gIn.tracer.err('You must have TIA root, before suite creation, see tia -h for initRoot mode');
process.exit(1);
}
gIn.cLogger.msgln(`Root dir "${tiaRootInParents}" is found.`);
gIn.cLogger.msgln(`Suite dir "${dir}" is created successfully.`);
fileUtils.mkDirRecursive(dir, [gT.engineConsts.suiteDirName, gT.engineConsts.suiteResDirName]);
fs.writeFileSync(path.join(dir, gT.engineConsts.suiteDirName, gT.engineConsts.suiteResDirName, '.gitkeep'), 'Just dummy file to commit _tia-suite to VCS', 'utf8');
process.exit(0);
}
exports.initTiaSuite = initTiaSuite;
function initTiaRoot(argsTiaRootDir) {
const tiaRootCandidate = resolveRootDirFromArgsAndEnv(argsTiaRootDir) || process.cwd();
if (isTiaSuiteInParents(tiaRootCandidate)) {
gIn.tracer.err(`TIA suite is existed in parent dirs here: ${tiaRootCandidate}`);
gIn.tracer.err('You can not create root inside suite');
process.exit(1);
}
const existedTiaRootInChildren = findTiaRootInChildren(tiaRootCandidate);
if (existedTiaRootInChildren) {
gIn.tracer.err(`TIA root is existed in children dirs here: ${existedTiaRootInChildren}`);
gIn.tracer.err('You can not have TIA root inside TIA root');
process.exit(1);
}
const tiaRootInParents = findTiaRootInParents(tiaRootCandidate);
if (tiaRootInParents) {
gIn.tracer.err(`TIA root is found here : ${tiaRootInParents}`);
gIn.tracer.err('You can not have TIA root inside TIA root');
process.exit(1);
}
fileUtils.mkDirRecursive(tiaRootCandidate, [
gT.engineConsts.suiteDirName,
gT.engineConsts.rootResDirName,
]);
gIn.cLogger.msgln(`Root results is created successfully in "${tiaRootCandidate}"`);
process.exit(0);
}
exports.initTiaRoot = initTiaRoot;
/**
* Resolves path specified by cmd line option or environment variable.
* Relative paths resolved relative to gT.cLParams.rootDir.
* @param {Object} argsObj
* @return {String} - resolved path or empty string.
*/
function resolvePathOptionRelativeToRootDir({ cmdLineArgsPath, envVarName, description, cutLastDirSep, mandatory, }) {
let myPath = cmdLineArgsPath || process.env[envVarName];
if (!myPath) {
if (mandatory) {
gIn.cLogger.errln(`${description} is not specified`);
process.exit(1); // TODO: change to debug-assert ??
}
gIn.tracer.msg3(`${description} is not specified`);
return '';
}
if (!path.isAbsolute(myPath)) {
myPath = path.resolve(gT.cLParams.rootDir, myPath);
if (cutLastDirSep && myPath.endsWith(path.sep)) {
myPath = myPath.slice(0, -1);
}
}
gIn.tracer.msg3(`${description}: ${myPath}`);
return myPath;
}
exports.resolvePathOptionRelativeToRootDir = resolvePathOptionRelativeToRootDir;
//# sourceMappingURL=tia-arguments-utils.js.map