Skip to content

Commit

Permalink
Merge pull request #388 from fongandrew/fix-dirname
Browse files Browse the repository at this point in the history
Fix dirname in stack traces
  • Loading branch information
ljharb committed Sep 11, 2017
2 parents de7f4fa + 1eba217 commit 0bb42ee
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/test.js
Expand Up @@ -216,7 +216,7 @@ Test.prototype._assert = function assert (ok, opts) {
if (!ok) {
var e = new Error('exception');
var err = (e.stack || '').split('\n');
var dir = path.dirname(__dirname) + path.sep;
var dir = __dirname + path.sep;

for (var i = 0; i < err.length; i++) {
var m = /^[^\s]*\s*\bat\s+(.+)/.exec(err[i]);
Expand Down
1 change: 1 addition & 0 deletions test/circular-things.js
Expand Up @@ -20,6 +20,7 @@ tap.test('circular test', function (assert) {
+ ' {}\n'
+ ' actual: |-\n'
+ ' { circular: [Circular] }\n'
+ ' at: Test.<anonymous> ($TEST/circular-things.js:$LINE:$COL)\n'
+ ' stack: |-\n'
+ ' Error: should be equal\n'
+ ' [... stack stripped ...]\n'
Expand Down
14 changes: 9 additions & 5 deletions test/common.js
@@ -1,3 +1,4 @@
var path = require('path');
var yaml = require('js-yaml');

module.exports.getDiag = function (body) {
Expand All @@ -7,10 +8,11 @@ module.exports.getDiag = function (body) {
return line.slice(2);
}).join('\n');

// The stack trace will vary depending on where the code is run, so just
// strip it out.
// The stack trace and at variable will vary depending on where the code
// is run, so just strip it out.
var withStack = yaml.safeLoad(diag);
delete withStack.stack;
delete withStack.at;
return withStack;
}

Expand All @@ -19,7 +21,8 @@ module.exports.getDiag = function (body) {
// 1) The base checkout directory of tape might change. Because stack traces
// include absolute paths, the stack traces will change depending on the
// checkout path. We handle this by replacing the base test directory with a
// placeholder $TEST variable.
// placeholder $TEST variable and the package root with a placehodler
// $TAPE variable.
// 2) Line positions within the file might change. We handle this by replacing
// line and column markers with placeholder $LINE and $COL "variables"
// a) node 0.8 does not provide nested eval line numbers, so we remove them
Expand All @@ -34,8 +37,9 @@ module.exports.stripFullStack = function (output) {
var m = line.match(/[ ]{8}at .*\((.*)\)/);

var stripChangingData = function (line) {
var withoutDirectory = line.replace(__dirname, '$TEST');
var withoutLineNumbers = withoutDirectory.replace(/:\d+:\d+/g, ':$LINE:$COL');
var withoutTestDir = line.replace(__dirname, '$TEST');
var withoutPackageDir = withoutTestDir.replace(path.dirname(__dirname), '$TAPE');
var withoutLineNumbers = withoutPackageDir.replace(/:\d+:\d+/g, ':$LINE:$COL');
var withoutNestedLineNumbers = withoutLineNumbers.replace(/, \<anonymous\>:\$LINE:\$COL\)$/, ')');
return withoutNestedLineNumbers;
}
Expand Down
6 changes: 6 additions & 0 deletions test/deep-equal-failure.js
Expand Up @@ -26,6 +26,7 @@ tap.test('deep equal failure', function (assert) {
+ ' { b: 2 }\n'
+ ' actual: |-\n'
+ ' { a: 1 }\n'
+ ' at: Test.<anonymous> ($TEST/deep-equal-failure.js:$LINE:$COL)\n'
+ ' stack: |-\n'
+ ' Error: should be equal\n'
+ ' [... stack stripped ...]\n'
Expand All @@ -48,6 +49,7 @@ tap.test('deep equal failure', function (assert) {

parser.once('assert', function (data) {
delete data.diag.stack;
delete data.diag.at;
assert.deepEqual(data, {
ok: false,
id: 1,
Expand Down Expand Up @@ -85,6 +87,7 @@ tap.test('deep equal failure, depth 6, with option', function (assert) {
+ ' { a: { a1: { a2: { a3: { a4: { a5: 2 } } } } } }\n'
+ ' actual: |-\n'
+ ' { a: { a1: { a2: { a3: { a4: { a5: 1 } } } } } }\n'
+ ' at: Test.<anonymous> ($TEST/deep-equal-failure.js:$LINE:$COL)\n'
+ ' stack: |-\n'
+ ' Error: should be equal\n'
+ ' [... stack stripped ...]\n'
Expand All @@ -107,6 +110,7 @@ tap.test('deep equal failure, depth 6, with option', function (assert) {

parser.once('assert', function (data) {
delete data.diag.stack;
delete data.diag.at;
assert.deepEqual(data, {
ok: false,
id: 1,
Expand Down Expand Up @@ -144,6 +148,7 @@ tap.test('deep equal failure, depth 6, without option', function (assert) {
+ ' { a: { a1: { a2: { a3: { a4: [Object] } } } } }\n'
+ ' actual: |-\n'
+ ' { a: { a1: { a2: { a3: { a4: [Object] } } } } }\n'
+ ' at: Test.<anonymous> ($TEST/deep-equal-failure.js:$LINE:$COL)\n'
+ ' stack: |-\n'
+ ' Error: should be equal\n'
+ ' [... stack stripped ...]\n'
Expand All @@ -166,6 +171,7 @@ tap.test('deep equal failure, depth 6, without option', function (assert) {

parser.once('assert', function (data) {
delete data.diag.stack;
delete data.diag.at;
assert.deepEqual(data, {
ok: false,
id: 1,
Expand Down
3 changes: 3 additions & 0 deletions test/double_end.js
Expand Up @@ -24,13 +24,15 @@ test(function (t) {
to._onTimeout = doEnd;

var stackExpected;
var atExpected;
try {
to._onTimeout();
}
catch (e) {
stackExpected = stripFullStack(e.stack).split('\n')[1];
stackExpected = stackExpected.replace('double_end.js', 'double_end/double.js');
stackExpected = stackExpected.trim();
atExpected = stackExpected.replace(/^at\s+/, 'at: ');
}

var stripped = stripFullStack(body.toString('utf8'));
Expand All @@ -41,6 +43,7 @@ test(function (t) {
'not ok 2 .end() called twice',
' ---',
' operator: fail',
' ' + atExpected,
' stack: |-',
' Error: .end() called twice',
' [... stack stripped ...]',
Expand Down
3 changes: 3 additions & 0 deletions test/exit.js
Expand Up @@ -52,6 +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: Test.<anonymous> ($TEST/exit/fail.js:$LINE:$COL)',
' stack: |-',
' Error: should be equivalent',
' [... stack stripped ...]',
Expand Down Expand Up @@ -93,6 +94,7 @@ tap.test('too few exit', function (t) {
' operator: fail',
' expected: 6',
' actual: 5',
' at: process.<anonymous> ($TAPE/index.js:$LINE:$COL)',
' stack: |-',
' Error: plan != count',
' [... stack stripped ...]',
Expand Down Expand Up @@ -127,6 +129,7 @@ tap.test('more planned in a second test', function (t) {
' operator: fail',
' expected: 2',
' actual: 1',
' at: process.<anonymous> ($TAPE/index.js:$LINE:$COL)',
' stack: |-',
' Error: plan != count',
' [... stack stripped ...]',
Expand Down
1 change: 1 addition & 0 deletions test/fail.js
Expand Up @@ -22,6 +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: Test.<anonymous> ($TEST/fail.js:$LINE:$COL)',
' stack: |-',
' Error: should be equivalent',
' [... stack stripped ...]',
Expand Down
26 changes: 20 additions & 6 deletions test/stackTrace.js
Expand Up @@ -13,6 +13,7 @@ tap.test('preserves stack trace with newlines', function (tt) {
var stackTrace = 'foo\n bar';

parser.once('assert', function (data) {
delete data.diag.at;
tt.deepEqual(data, {
ok: false,
id: 1,
Expand All @@ -27,7 +28,8 @@ tap.test('preserves stack trace with newlines', function (tt) {
});

stream.pipe(concat(function (body) {
var body = body.toString('utf8')
var body = body.toString('utf8');
body = stripAt(body);
tt.equal(
body,
'TAP version 13\n'
Expand Down Expand Up @@ -67,22 +69,25 @@ tap.test('preserves stack trace with newlines', function (tt) {
});

tap.test('preserves stack trace for failed assertions', function (tt) {
tt.plan(5);
tt.plan(6);

var test = tape.createHarness();
var stream = test.createStream();
var parser = stream.pipe(tapParser());

var stack = ''
parser.once('assert', function (data) {
tt.equal(typeof data.diag.stack, 'string')
stack = data.diag.stack || ''
tt.equal(typeof data.diag.at, 'string');
tt.equal(typeof data.diag.stack, 'string');
at = data.diag.at || '';
stack = data.diag.stack || '';
tt.ok(/^Error: false should be true(\n at .+)+/.exec(stack), 'stack should be a stack')
tt.deepEqual(data, {
ok: false,
id: 1,
name: "false should be true",
diag: {
at: at,
stack: stack,
operator: 'equal',
expected: false,
Expand All @@ -92,7 +97,8 @@ tap.test('preserves stack trace for failed assertions', function (tt) {
});

stream.pipe(concat(function (body) {
var body = body.toString('utf8')
var body = body.toString('utf8');
body = stripAt(body);
tt.equal(
body,
'TAP version 13\n'
Expand Down Expand Up @@ -134,5 +140,13 @@ function getDiag (body) {
return line.slice(2);
}).join('\n');

return yaml.safeLoad(diag);
// Get rid of 'at' variable (which has a line number / path of its own that's
// difficult to check).
var withStack = yaml.safeLoad(diag);
delete withStack.at;
return withStack;
}

function stripAt (body) {
return body.replace(/^\s*at:\s+Test.*$\n/m, '');
}
9 changes: 9 additions & 0 deletions test/throws.js
Expand Up @@ -40,6 +40,7 @@ tap.test('failures', function (tt) {
+ ' undefined\n'
+ ' actual: |-\n'
+ " { [TypeError: " + getNonFunctionMessage() + "] message: '" + getNonFunctionMessage() + "' }\n"
+ ' at: Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
+ ' stack: |-\n'
+ ' TypeError: ' + getNonFunctionMessage(undefined) + '\n'
+ ' [... stack stripped ...]\n'
Expand All @@ -53,6 +54,7 @@ tap.test('failures', function (tt) {
+ ' undefined\n'
+ ' actual: |-\n'
+ " { [TypeError: " + getNonFunctionMessage(null) + "] message: '" + getNonFunctionMessage(null) + "' }\n"
+ ' at: Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
+ ' stack: |-\n'
+ ' TypeError: ' + getNonFunctionMessage(null) + '\n'
+ ' [... stack stripped ...]\n'
Expand All @@ -66,6 +68,7 @@ tap.test('failures', function (tt) {
+ ' undefined\n'
+ ' actual: |-\n'
+ " { [TypeError: " + getNonFunctionMessage(true) + "] message: '" + getNonFunctionMessage(true) + "' }\n"
+ ' at: Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
+ ' stack: |-\n'
+ ' TypeError: ' + getNonFunctionMessage(true) + '\n'
+ ' [... stack stripped ...]\n'
Expand All @@ -79,6 +82,7 @@ tap.test('failures', function (tt) {
+ ' undefined\n'
+ ' actual: |-\n'
+ " { [TypeError: " + getNonFunctionMessage(false) + "] message: '" + getNonFunctionMessage(false) + "' }\n"
+ ' at: Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
+ ' stack: |-\n'
+ ' TypeError: ' + getNonFunctionMessage(false) + '\n'
+ ' [... stack stripped ...]\n'
Expand All @@ -92,6 +96,7 @@ tap.test('failures', function (tt) {
+ ' undefined\n'
+ ' actual: |-\n'
+ " { [TypeError: " + getNonFunctionMessage('abc') + "] message: '" + getNonFunctionMessage('abc') + "' }\n"
+ ' at: Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
+ ' stack: |-\n'
+ ' TypeError: ' + getNonFunctionMessage('abc') + '\n'
+ ' [... stack stripped ...]\n'
Expand All @@ -105,6 +110,7 @@ tap.test('failures', function (tt) {
+ ' undefined\n'
+ ' actual: |-\n'
+ " { [TypeError: " + getNonFunctionMessage(/a/g) + "] message: '" + getNonFunctionMessage(/a/g) + "' }\n"
+ ' at: Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
+ ' stack: |-\n'
+ ' TypeError: ' + getNonFunctionMessage(/a/g) + '\n'
+ ' [... stack stripped ...]\n'
Expand All @@ -118,6 +124,7 @@ tap.test('failures', function (tt) {
+ ' undefined\n'
+ ' actual: |-\n'
+ " { [TypeError: " + getNonFunctionMessage([]) + "] message: '" + getNonFunctionMessage([]) + "' }\n"
+ ' at: Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
+ ' stack: |-\n'
+ ' TypeError: ' + getNonFunctionMessage([]) + '\n'
+ ' [... stack stripped ...]\n'
Expand All @@ -131,6 +138,7 @@ tap.test('failures', function (tt) {
+ ' undefined\n'
+ ' actual: |-\n'
+ " { [TypeError: " + getNonFunctionMessage({}) + "] message: '" + getNonFunctionMessage({}) + "' }\n"
+ ' at: Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
+ ' stack: |-\n'
+ ' TypeError: ' + getNonFunctionMessage({}) + '\n'
+ ' [... stack stripped ...]\n'
Expand All @@ -143,6 +151,7 @@ tap.test('failures', function (tt) {
+ ' operator: throws\n'
+ ' expected: undefined\n'
+ ' actual: undefined\n'
+ ' at: Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
+ ' stack: |-\n'
+ ' Error: should throw\n'
+ ' [... stack stripped ...]\n'
Expand Down
1 change: 1 addition & 0 deletions test/too_many.js
Expand Up @@ -22,6 +22,7 @@ tap.test('array test', function (tt) {
' operator: fail',
' expected: 3',
' actual: 4',
' at: Test.<anonymous> ($TEST/too_many.js:$LINE:$COL)',
' stack: |-',
' Error: plan != count',
' [... stack stripped ...]',
Expand Down
1 change: 1 addition & 0 deletions test/undef.js
Expand Up @@ -20,6 +20,7 @@ tap.test('array test', function (tt) {
+ ' { beep: undefined }\n'
+ ' actual: |-\n'
+ ' {}\n'
+ ' at: Test.<anonymous> ($TEST/undef.js:$LINE:$COL)\n'
+ ' stack: |-\n'
+ ' Error: should be equivalent\n'
+ ' [... stack stripped ...]\n'
Expand Down

0 comments on commit 0bb42ee

Please sign in to comment.