Skip to content

Commit

Permalink
test: make common.mustNotCall show file:linenumber
Browse files Browse the repository at this point in the history
When a test fails via `common.mustNotCall` it is sometimes hard to
determine exactly what was called. This modification stores the
caller's file and line number by using the V8 Error API to capture
a stack at the time `common.mustNotCall()` is called. In the event
of failure, this information is printed.

This change also exposes a new function in test/common, `getCallSite()`
which accepts a `function` and returns a `String` with the file name and
line number for the function.

PR-URL: #17257
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Gibson Fahnestock <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Sakthipriyan Vairamani <[email protected]>
Reviewed-By: Khaidi Chu <[email protected]>
  • Loading branch information
lance committed Dec 4, 2017
1 parent cf4448c commit a322b8e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
6 changes: 6 additions & 0 deletions test/common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ a reason otherwise.

Returns an instance of all possible `ArrayBufferView`s of the provided Buffer.

### getCallSite(func)
* `func` [&lt;Function>]
* return [&lt;String>]

Returns the file name and line number for the provided Function.

### globalCheck
* [&lt;Boolean>]

Expand Down
16 changes: 15 additions & 1 deletion test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -566,9 +566,23 @@ exports.canCreateSymLink = function() {
return true;
};

exports.getCallSite = function getCallSite(top) {
const originalStackFormatter = Error.prepareStackTrace;
Error.prepareStackTrace = (err, stack) =>
`${stack[0].getFileName()}:${stack[0].getLineNumber()}`;
const err = new Error();
Error.captureStackTrace(err, top);
// with the V8 Error API, the stack is not formatted until it is accessed
err.stack;
Error.prepareStackTrace = originalStackFormatter;
return err.stack;
};

exports.mustNotCall = function(msg) {
const callSite = exports.getCallSite(exports.mustNotCall);
return function mustNotCall() {
assert.fail(msg || 'function should not have been called');
assert.fail(
`${msg || 'function should not have been called'} at ${callSite}`);
};
};

Expand Down
26 changes: 26 additions & 0 deletions test/parallel/test-common-must-not-call.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const path = require('path');

const message = 'message';
const testFunction = common.mustNotCall(message);

const validateError = common.mustCall((e) => {
const prefix = `${message} at `;
assert.ok(e.message.startsWith(prefix));
if (process.platform === 'win32') {
e.message = e.message.substring(2); // remove 'C:'
}
const [ fileName, lineNumber ] = e.message
.substring(prefix.length).split(':');
assert.strictEqual(path.basename(fileName), 'test-common-must-not-call.js');
assert.strictEqual(lineNumber, '8');
});

try {
testFunction();
} catch (e) {
validateError(e);
}

0 comments on commit a322b8e

Please sign in to comment.