diff --git a/utils/testrunner/utils.js b/utils/testrunner/utils.js new file mode 100644 index 0000000000..43e655646f --- /dev/null +++ b/utils/testrunner/utils.js @@ -0,0 +1,32 @@ +const path = require('path'); + +module.exports = { + getLocation: function(filename) { + const error = new Error(); + const stackFrames = error.stack.split('\n').slice(1); + // Find first stackframe that doesn't point to this file. + for (let frame of stackFrames) { + frame = frame.trim(); + if (!frame.startsWith('at ')) + return null; + if (frame.endsWith(')')) { + const from = frame.indexOf('('); + frame = frame.substring(from + 1, frame.length - 1); + } else { + frame = frame.substring('at '.length); + } + + const match = frame.match(/^(.*):(\d+):(\d+)$/); + if (!match) + return null; + const filePath = match[1]; + const lineNumber = parseInt(match[2], 10); + const columnNumber = parseInt(match[3], 10); + if (filePath === __filename || filePath === filename) + continue; + const fileName = filePath.split(path.sep).pop(); + return { fileName, filePath, lineNumber, columnNumber }; + } + return null; + }, +};