Skip to content

Commit

Permalink
[Refactor] Test: cleaner at logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Nov 29, 2023
1 parent d1987c0 commit af4d109
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ Test.prototype._assert = function assert(ok, opts) {
if (!ok) {
var e = new Error('exception');
var err = $split(e.stack || '', '\n');
var dir = __dirname + path.sep;
var tapeDir = __dirname + path.sep;

for (var i = 0; i < err.length; i++) {
/*
Expand Down Expand Up @@ -521,17 +521,35 @@ Test.prototype._assert = function assert(ok, opts) {
/((?:\/|[a-zA-Z]:\\)[^:\)]+:(\d+)(?::(\d+))?)\)?/
*/
var re = /^(?:[^\s]*\s*\bat\s+)(?:(.*)\s+\()?((?:\/|[a-zA-Z]:\\)[^:)]+:(\d+)(?::(\d+))?)\)?$/;
var lineWithTokens = $replace($replace(err[i], process.cwd(), '/$CWD'), __dirname, '/$TEST');
// first tokenize the PWD, then tokenize tape
var lineWithTokens = $replace(
$replace(
err[i],
process.cwd(),
path.sep + '$CWD'
),
tapeDir,
path.sep + '$TEST' + path.sep
);
var m = re.exec(lineWithTokens);

if (!m) {
continue;
}

var callDescription = m[1] || '<anonymous>';
var filePath = $replace($replace(m[2], '/$CWD', process.cwd()), '/$TEST', __dirname);

if ($strSlice(filePath, 0, dir.length) === dir) {
// first untokenize tape, and then untokenize the PWD, then strip the line/column
var filePath = $replace(
$replace(
$replace(m[2], path.sep + '$TEST' + path.sep, tapeDir),
path.sep + '$CWD',
process.cwd()
),
/:\d+:\d+$/,
''
);

if ($strSlice(filePath, 0, tapeDir.length) === tapeDir) {
continue;
}

Expand All @@ -542,7 +560,7 @@ Test.prototype._assert = function assert(ok, opts) {
res.line = Number(m[3]);
if (m[4]) { res.column = Number(m[4]); }

res.at = callDescription + ' (' + filePath + ')';
res.at = callDescription + ' (' + filePath + ':' + res.line + (res.column ? ':' + res.column : '') + ')';
break;
}
}
Expand Down

0 comments on commit af4d109

Please sign in to comment.