UNPKG

tia

Version:

Time is All (logs driven test engine with ExtJs support)

282 lines 8.46 kB
"use strict"; /** * Inner utilities for logging. * **gIn.logger**. */ 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 }); /* globals gT: true */ /* globals gIn: true */ /* Inner utils for logging. */ let isVerbose = false; let logFile = ''; // TODO: bad design. let fd; function setLogFile(newLogFile) { logFile = newLogFile; } exports.setLogFile = setLogFile; function getLogFile() { return logFile; } exports.getLogFile = getLogFile; const fs = __importStar(require("fs")); const nodeUtils = __importStar(require("../../utils/nodejs-utils")); function logToFile(msg) { // TODO: check how diff work for unicode. fs.appendFileSync(logFile, msg, { encoding: gT.engineConsts.logEncoding }); } // function logToFileLn(msg) { // return logToFile(`${msg}\n`); // } /** * Logs to file and writes to console (if console output is enabled). * @param msg * @param noConsole */ function log(msg, dontWriteToFile) { // We use append here, to don't lost some strings if something will break the test engine. gIn.cLogger.logIfEnabled(msg); if (!dontWriteToFile) { logToFile(msg); } } exports.log = log; function logln(msg) { log(`${msg}\n`); } exports.logln = logln; function logResourcesUsage(prefix = '') { if (gT.config.resUsagePrintAtErrors) { logln(prefix + nodeUtils.getResourcesUsage(true)); } } exports.logResourcesUsage = logResourcesUsage; function logBold(msg) { gIn.cLogger.logBold(msg); logToFile(msg); } exports.logBold = logBold; function fail(msg) { gIn.cLogger.failIfEnabled(msg); logToFile(msg); } exports.fail = fail; function pass(msg) { gIn.cLogger.passIfEnabled(msg); logToFile(msg); } exports.pass = pass; /** * Report about some error. * @param msg */ function error(msg) { const msgNew = gIn.loggerCfg.errPrefix + msg; gIn.cLogger.errIfEnabled(msgNew); logToFile(msgNew); } exports.error = error; function errorln(msg) { return error(`${msg}\n`); } exports.errorln = errorln; /** * Report about some exception. * @param msg * @param e */ function exception(msg, e) { const msgNew = gIn.loggerCfg.excPrefix + msg; gIn.cLogger.errIfEnabled(`${msgNew} ${gIn.textUtils.excToStr(e)}\n`); logToFile(`${msgNew} ${gIn.textUtils.excToStr(e, !gT.cLParams.stackToLog)}\n`); } exports.exception = exception; /** * Logs a message. * @param msg - A message to be logged. * @param {Boolean}[enable]. If true log is enabled, othwerwise log is disabled. */ function logIfEnabled(msg, enable) { let dontWriteToFile = false; if (!enable && gT.cLParams.forceLogActions) { dontWriteToFile = true; } if (gT.cLParams.forceLogActions || enable) { log(msg, dontWriteToFile); } } exports.logIfEnabled = logIfEnabled; /** * Logs a message. * @param msg - A message to be logged. * @param {Boolean} [enable = gIn.loggerCfg.defLLLogAction] - If false - log is disabled, * otherwise - log is enabled. */ function logIfNotDisabled(msg, enable) { if (enable === undefined) { enable = gIn.loggerCfg.getDefLLLogAction(); } let dontWriteToFile = false; if (!enable && gT.cLParams.forceLogActions) { dontWriteToFile = true; } if (gT.cLParams.forceLogActions || enable) { log(msg, dontWriteToFile); } } exports.logIfNotDisabled = logIfNotDisabled; function writeStrToFile(str, fd /* , diffed, isDif*/) { fs.writeSync(fd, str, null, gT.engineConsts.logEncoding); } function writeStrToStdout(str, diffed, isDif) { // str = str.replace(/\s+$/g, ''); // str += '\n'; if (diffed) { gIn.cLogger.err(str); } else if (isDif) { gIn.cLogger.msgDifStr(str); } else { gIn.cLogger.msg(str); } } function writeToSuiteLog({ str, diffed, isDif, fd, }) { if (typeof fd !== 'undefined') { return writeStrToFile(str, fd); } writeStrToStdout(str, diffed, isDif); } function testSummary() { log('=================\n'); log(`Pass: ${gIn.tInfo.getPassed()}, Fail: ${gIn.tInfo.getFailed()}\n`); } exports.testSummary = testSummary; function saveDirInfoToSuiteLog({ dirInfo, indent, verbose, noTime, noTestDifs, fd, }) { gIn.tracer.msg3(`${dirInfo.path}, dirInfo.run: ${dirInfo.run}`); if (!dirInfo.run && !gT.suiteConfig.emptyDirToSuiteLog) { return; } writeToSuiteLog({ str: indent, fd }); writeToSuiteLog({ str: gIn.tInfo.testInfoToString({ curInfo: dirInfo, isDir: true, verbose, noTime, }), diffed: Boolean(dirInfo.diffed), fd, }); indent = gIn.loggerCfg.indentation + indent; // If directory is empty there will be empty array. // Absense of 'children' property says that it is test and not directory, // we should not allow to use this function for not directory. const len = dirInfo.children.length; for (let i = 0; i < len; i++) { const curInfo = dirInfo.children[i]; if (curInfo.diffed || verbose) { if (curInfo.children) { saveDirInfoToSuiteLog({ dirInfo: curInfo, indent, verbose, noTime, noTestDifs, fd, }); } else { writeToSuiteLog({ str: indent, fd }); writeToSuiteLog({ str: gIn.tInfo.testInfoToString({ curInfo, isDir: false, verbose, noTime, }), diffed: Boolean(curInfo.diffed), fd, }); if (curInfo.diffed && gT.cLParams.difsToSlog && !isVerbose && !noTestDifs) { const difPath = gIn.textUtils.jsToDif(curInfo.path); const dif = fs.readFileSync(difPath, gT.engineConsts.logEncoding); writeToSuiteLog({ str: `${indent}============== DIF ============== \n`, fd }); const diffStrs = dif.split('\n'); diffStrs.forEach(str => { writeToSuiteLog({ str: indent, fd }); writeToSuiteLog({ str: `${str}\n`, diffed: false, isDif: true, fd }); }); writeToSuiteLog({ str: `${indent}========== END OF DIF ========== \n`, fd }); } } } } } function saveSuiteLogPart({ verbose, dirInfo, noTime, noTestDifs, fd, }) { isVerbose = verbose; const title = verbose ? 'Verbose' : 'Short'; const decor = '===================='; writeToSuiteLog({ str: `${decor} ${title} Log BEGIN: ${decor}\n`, fd }); saveDirInfoToSuiteLog({ dirInfo, indent: '', verbose, noTime, noTestDifs, fd, }); writeToSuiteLog({ str: `${decor} ${title} Log END. ${decor}\n`, fd }); } /** * Saves suite log. * @param dirInfo * @param log * @parem noTime * @returns {string} - Verbose info for the root test directory. */ function saveSuiteLog({ dirInfo, log, noTime, noTestDifs, }) { fd = fs.openSync(log, 'w'); saveSuiteLogPart({ verbose: false, dirInfo, noTime, noTestDifs, fd, }); fs.writeSync(fd, '\n', null, gT.engineConsts.logEncoding); saveSuiteLogPart({ verbose: true, dirInfo, noTime, noTestDifs, fd, }); fs.closeSync(fd); return gIn.tInfo.testInfoToString({ curInfo: dirInfo, isDir: true, verbose: true, noTime, noTitle: true, noEol: true, }); } exports.saveSuiteLog = saveSuiteLog; /* Prints expected tests results to stdout and unexpected to stderr */ function printSuiteLog(dirInfo, noTestDifs) { saveSuiteLogPart({ verbose: false, dirInfo, noTime: false, noTestDifs }); writeToSuiteLog({ str: '\n' }); saveSuiteLogPart({ verbose: true, dirInfo, noTime: false, noTestDifs }); } exports.printSuiteLog = printSuiteLog; //# sourceMappingURL=logger.js.map