Skip to content

Commit 9cbb0da

Browse files
lanceMylesBorins
authored andcommitted
test: make common.mustNotCall show file:linenumber
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]>
1 parent 40acda2 commit 9cbb0da

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

test/common/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ a reason otherwise.
133133

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

136+
### getCallSite(func)
137+
* `func` [&lt;Function>]
138+
* return [&lt;String>]
139+
140+
Returns the file name and line number for the provided Function.
141+
136142
### globalCheck
137143
* [&lt;Boolean>]
138144

test/common/index.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -566,9 +566,23 @@ exports.canCreateSymLink = function() {
566566
return true;
567567
};
568568

569+
exports.getCallSite = function getCallSite(top) {
570+
const originalStackFormatter = Error.prepareStackTrace;
571+
Error.prepareStackTrace = (err, stack) =>
572+
`${stack[0].getFileName()}:${stack[0].getLineNumber()}`;
573+
const err = new Error();
574+
Error.captureStackTrace(err, top);
575+
// with the V8 Error API, the stack is not formatted until it is accessed
576+
err.stack;
577+
Error.prepareStackTrace = originalStackFormatter;
578+
return err.stack;
579+
};
580+
569581
exports.mustNotCall = function(msg) {
582+
const callSite = exports.getCallSite(exports.mustNotCall);
570583
return function mustNotCall() {
571-
assert.fail(msg || 'function should not have been called');
584+
assert.fail(
585+
`${msg || 'function should not have been called'} at ${callSite}`);
572586
};
573587
};
574588

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const path = require('path');
6+
7+
const message = 'message';
8+
const testFunction = common.mustNotCall(message);
9+
10+
const validateError = common.mustCall((e) => {
11+
const prefix = `${message} at `;
12+
assert.ok(e.message.startsWith(prefix));
13+
if (process.platform === 'win32') {
14+
e.message = e.message.substring(2); // remove 'C:'
15+
}
16+
const [ fileName, lineNumber ] = e.message
17+
.substring(prefix.length).split(':');
18+
assert.strictEqual(path.basename(fileName), 'test-common-must-not-call.js');
19+
assert.strictEqual(lineNumber, '8');
20+
});
21+
22+
try {
23+
testFunction();
24+
} catch (e) {
25+
validateError(e);
26+
}

0 commit comments

Comments
 (0)