Skip to content

Commit

Permalink
[Fix] error stack file path can contain parens/spaces
Browse files Browse the repository at this point in the history
 - [Tests] `stripFullStack`: fix regex when the dirname contains parens
  • Loading branch information
ljharb committed Dec 24, 2019
1 parent b765bba commit 9094271
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 24 deletions.
7 changes: 4 additions & 3 deletions lib/test.js
Expand Up @@ -267,15 +267,16 @@ 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 m = re.exec(err[i]);
var re = /^(?:[^\s]*\s*\bat\s+)(?:(.*)\s+\()?((?:\/|[a-zA-Z]:\\)[^:\)]+:(\d+)(?::(\d+))?)\)$/;
var lineWithTokens = err[i].replace(process.cwd(), '/\$CWD').replace(__dirname, '/\$TEST');
var m = re.exec(lineWithTokens);

if (!m) {
continue;
}

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

if (filePath.slice(0, dir.length) === dir) {
continue;
Expand Down
6 changes: 3 additions & 3 deletions test/anonymous-fn.js
Expand Up @@ -14,8 +14,8 @@ tap.test('inside anonymous functions', function (tt) {

// Handle stack trace variation in Node v0.8
body = body.replace(
'at Test.module.exports',
'at Test.<anonymous>'
/at(:?) Test\.module\.exports/g,
'at$1 Test.<anonymous>'
);

tt.same(body, [
Expand All @@ -24,7 +24,7 @@ tap.test('inside anonymous functions', function (tt) {
'not ok 1 fail',
' ---',
' operator: fail',
' at: <anonymous> ($TEST/anonymous-fn.js:$LINE:$COL)',
' at: Test.<anonymous> ($TEST/anonymous-fn/test-wrapper.js:$LINE:$COL)',
' stack: |-',
' Error: fail',
' [... stack stripped ...]',
Expand Down
28 changes: 13 additions & 15 deletions test/common.js
Expand Up @@ -31,27 +31,25 @@ module.exports.getDiag = function (body) {
// these changes are irrelevant to the tests themselves. To counter this, we
// strip out all stack frames that aren't directly under our test directory,
// and replace them with placeholders.

var stripChangingData = function (line) {
var withoutTestDir = line.replace(__dirname, '$TEST');
var withoutPackageDir = withoutTestDir.replace(path.dirname(__dirname), '$TAPE');
var withoutPathSep = withoutPackageDir.replace(new RegExp('\\' + path.sep, 'g'), '/');
var withoutLineNumbers = withoutPathSep.replace(/:\d+:\d+/g, ':$LINE:$COL');
var withoutNestedLineNumbers = withoutLineNumbers.replace(/, \<anonymous\>:\$LINE:\$COL\)$/, ')');
return withoutNestedLineNumbers;
};

module.exports.stripFullStack = function (output) {
var stripped = ' [... stack stripped ...]';
var withDuplicates = output.split('\n').map(function (line) {
var withDuplicates = output.split('\n').map(stripChangingData).map(function (line) {
var m = line.match(/[ ]{8}at .*\((.*)\)/);

var stripChangingData = function (line) {
var withoutTestDir = line.replace(__dirname, '$TEST');
var withoutPackageDir = withoutTestDir.replace(path.dirname(__dirname), '$TAPE');
var withoutPathSep = withoutPackageDir.replace(new RegExp('\\' + path.sep, 'g'), '/');
var withoutLineNumbers = withoutPathSep.replace(/:\d+:\d+/g, ':$LINE:$COL');
var withoutNestedLineNumbers = withoutLineNumbers.replace(/, \<anonymous\>:\$LINE:\$COL\)$/, ')');
return withoutNestedLineNumbers;
};

if (m) {
if (m[1].slice(0, __dirname.length) === __dirname) {
return stripChangingData(line);
}
if (m && m[1].slice(0, 5) !== '$TEST') {
return stripped;
}
return stripChangingData(line);
return line;
});

var deduped = withDuplicates.filter(function (line, ix) {
Expand Down
2 changes: 1 addition & 1 deletion test/exit.js
Expand Up @@ -52,7 +52,7 @@ tap.test('exit fail', function (t) {
' operator: deepEqual',
' expected: [ [ 1, 2, [ 3, 4444 ] ], [ 5, 6 ] ]',
' actual: [ [ 1, 2, [ 3, 4 ] ], [ 5, 6 ] ]',
' at: <anonymous> ($TEST/exit/fail.js:$LINE:$COL)',
' at: Test.<anonymous> ($TEST/exit/fail.js:$LINE:$COL)',
' stack: |-',
' Error: should be equivalent',
' [... stack stripped ...]',
Expand Down
2 changes: 1 addition & 1 deletion test/fail.js
Expand Up @@ -22,7 +22,7 @@ tap.test('array test', function (tt) {
' operator: deepEqual',
' expected: [ [ 1, 2, [ 3, 4444 ] ], [ 5, 6 ] ]',
' actual: [ [ 1, 2, [ 3, 4 ] ], [ 5, 6 ] ]',
' at: <anonymous> ($TEST/fail.js:$LINE:$COL)',
' at: Test.<anonymous> ($TEST/fail.js:$LINE:$COL)',
' stack: |-',
' Error: should be equivalent',
' [... stack stripped ...]',
Expand Down
2 changes: 1 addition & 1 deletion test/too_many.js
Expand Up @@ -22,7 +22,7 @@ tap.test('array test', function (tt) {
' operator: fail',
' expected: 3',
' actual: 4',
' at: <anonymous> ($TEST/too_many.js:$LINE:$COL)',
' at: Test.<anonymous> ($TEST/too_many.js:$LINE:$COL)',
' stack: |-',
' Error: plan != count',
' [... stack stripped ...]',
Expand Down

0 comments on commit 9094271

Please sign in to comment.