Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: make common.mustNotCall show file:linenumber #17257

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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` [<Function>]
* return [<String>]

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

### globalCheck
* [<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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better if we validated that if top is actually a function?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't really matter, in my opinion. The only place it is used is in Error.captureStackTrace() which does that checking for us. I.e. if it's not a function, the parameter is ignored and the top frame in the stack will be where Error.captureStackTrace() was called.

> const common = require('./test/common');
undefined
> let x = common.getCallSite()
undefined
> x
'/Users/lanceball/src/node/test/common/index.js:576'
> x = common.getCallSite('abc');
'/Users/lanceball/src/node/test/common/index.js:576'
> x = common.getCallSite(123)
'/Users/lanceball/src/node/test/common/index.js:576'
>

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);
}