UNPKG

alm

Version:

The best IDE for TypeScript

109 lines (108 loc) 4.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var json_1 = require("../../../../common/json"); var json_2 = require("../../../../common/json"); exports.stringify = json_2.stringify; var fsu_1 = require("../../../utils/fsu"); /** Error: Fail at Context.<anonymous> (D:\REPOS\alm\src\tests\testedTest.ts:21:15) at callFn (D:\REPOS\alm\node_modules\mocha\lib\runnable.js:334:21) at Test.Runnable.run (D:\REPOS\alm\node_modules\mocha\lib\runnable.js:327:7) at Runner.runTest (D:\REPOS\alm\node_modules\mocha\lib\runner.js:429:10) at D:\REPOS\alm\node_modules\mocha\lib\runner.js:535:12 at next (D:\REPOS\alm\node_modules\mocha\lib\runner.js:349:14) at D:\REPOS\alm\node_modules\mocha\lib\runner.js:359:7 at next (D:\REPOS\alm\node_modules\mocha\lib\runner.js:285:14) at Immediate._onImmediate (D:\REPOS\alm\node_modules\mocha\lib\runner.js:327:5) */ exports.makeStack = function (raw) { var lines = raw.split(/\r\n?|\n/); /** First line is just the error message. Don't need it */ lines = lines.slice(1); /** Remove each leading `at ` */ lines = lines.map(function (l) { return l.trim().substr(3); }); /** For lines that have function name, they end with `)`. So detect and remove leading `(` for them */ lines = lines.map(function (l) { if (l.endsWith(')')) { var withStartRemoved = l.substr(l.indexOf('(') + 1); var withEndRemoved = withStartRemoved.substr(0, withStartRemoved.length - 1); return withEndRemoved; } else { return l; } }); var stack = lines.map(function (l) { var parts = l.split(':'); var chStr = parts[parts.length - 1]; var lineStr = parts[parts.length - 2]; /** NOTE: file path on windows will contain `:`. Hence the join */ var filePath = fsu_1.consistentPath(parts.slice(0, parts.length - 2).join(':')); /** * The chrome ones are 1 based. We want 0 based */ var line = parseInt(lineStr) - 1; var ch = parseInt(chStr) - 1; return { filePath: filePath, position: { line: line, ch: ch } }; }); return stack; }; /** * When we care about the last log point in the file. */ exports.makeTestLogPosition = function (filePath, stack) { var tipOfTheStack = stack[0]; var result = { isActualLastInFile: tipOfTheStack.filePath === filePath, lastPositionInFile: stack.find(function (s) { return s.filePath === filePath; }) ? stack.find(function (s) { return s.filePath === filePath; }).position : { line: 0, ch: 0 }, stack: stack }; return result; }; /** * When we care about the last log point in the file. */ exports.makeTestLogPositionFromMochaError = function (filePath, stack, /** * The stack might not actually contain any reference to file path * This happens e.g. when one throws a `string` instead of `error` in mocha. * So this is the position we use in such cases */ positionIfFilePathNotFound) { var tipOfTheStack = stack[0]; var lastPositionInFile = stack.find(function (s) { return s.filePath === filePath; }) ? stack.find(function (s) { return s.filePath === filePath; }).position : positionIfFilePathNotFound; var result = { isActualLastInFile: !!tipOfTheStack && tipOfTheStack.filePath === filePath, lastPositionInFile: lastPositionInFile, stack: stack }; return result; }; /** Utility to get stack */ exports.stackFromCaller = function () { return exports.makeStack(new Error().stack) .slice(2); }; /** * We use the file to pass information from * - the instrumenter * - to the runner */ var getDataFilePath = function (filePath) { return filePath + '_almTestData.json'; }; exports.writeDataFile = function (filePath, contents) { var dataFilePath = getDataFilePath(filePath); var contentsStr = json_1.stringify(contents); fsu_1.writeFile(dataFilePath, contentsStr); }; exports.readAndDeleteDataFile = function (filePath) { var dataFilePath = getDataFilePath(filePath); var result = json_1.parse(fsu_1.readFile(dataFilePath)).data || { logs: [], suites: [], its: [] }; fsu_1.deleteFile(dataFilePath); return result; };