Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9cbb0da

Browse filesBrowse 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 <jasnell@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Khaidi Chu <i@2333.moe>
1 parent 40acda2 commit 9cbb0da
Copy full SHA for 9cbb0da

File tree

Expand file treeCollapse file tree

3 files changed

+47
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+47
-1
lines changed
Open diff view settings
Collapse file

‎test/common/README.md‎

Copy file name to clipboardExpand all lines: test/common/README.md
+6Lines changed: 6 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
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

Collapse file

‎test/common/index.js‎

Copy file name to clipboardExpand all lines: test/common/index.js
+15-1Lines changed: 15 additions & 1 deletion
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

Collapse file
+26Lines changed: 26 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.