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

assert: fix CallTracker calls wraps the function causes the original … #42909

Merged
merged 5 commits into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions lib/internal/assert/calltracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const {
ArrayPrototypePush,
Error,
FunctionPrototype,
Proxy,
ReflectApply,
SafeSet,
} = primordials;
Expand Down Expand Up @@ -46,20 +47,23 @@ class CallTracker {
const callChecks = this.#callChecks;
callChecks.add(context);

return function() {
context.actual++;
if (context.actual === context.exact) {
// Once function has reached its call count remove it from
// callChecks set to prevent memory leaks.
callChecks.delete(context);
}
// If function has been called more than expected times, add back into
// callchecks.
if (context.actual === context.exact + 1) {
callChecks.add(context);
}
return ReflectApply(fn, this, arguments);
};
return new Proxy(fn, {
__proto__: null,
apply() {
y1d7ng marked this conversation as resolved.
Show resolved Hide resolved
y1d7ng marked this conversation as resolved.
Show resolved Hide resolved
context.actual++;
if (context.actual === context.exact) {
// Once function has reached its call count remove it from
// callChecks set to prevent memory leaks.
callChecks.delete(context);
}
// If function has been called more than expected times, add back into
// callchecks.
if (context.actual === context.exact + 1) {
callChecks.add(context);
}
return ReflectApply(...arguments);
y1d7ng marked this conversation as resolved.
Show resolved Hide resolved
},
});
}

report() {
Expand Down
38 changes: 37 additions & 1 deletion test/parallel/test-assert-calltracker-calls.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');

// This test ensures that assert.CallTracker.calls() works as intended.
Expand Down Expand Up @@ -78,3 +78,39 @@ assert.throws(
callsNoop();
tracker.verify();
}

{
function func() {}
const tracker = new assert.CallTracker();
const callsfunc = tracker.calls(func);
assert.strictEqual(callsfunc.length, 0);
}

{
function func(a, b, c = 2) {}
const tracker = new assert.CallTracker();
const callsfunc = tracker.calls(func);
assert.strictEqual(callsfunc.length, 2);
}

{
function func(a, b, c = 2) {}
delete func.length;
const tracker = new assert.CallTracker();
const callsfunc = tracker.calls(func);
assert.strictEqual(Object.hasOwn(callsfunc, 'length'), false);
}

{
Object.prototype.get = common.mustNotCall('%Object.prototype%.get');
y1d7ng marked this conversation as resolved.
Show resolved Hide resolved
const customPropertyValue = Symbol();
function func(a, b, c = 2) {}
y1d7ng marked this conversation as resolved.
Show resolved Hide resolved
func.customProperty = customPropertyValue;
Object.defineProperty(func, 'length', { get: common.mustNotCall() });
const tracker = new assert.CallTracker();
const callsfunc = tracker.calls(func);
assert.strictEqual(Object.hasOwn(callsfunc, 'length'), true);
y1d7ng marked this conversation as resolved.
Show resolved Hide resolved
assert.strictEqual(callsfunc.customProperty, customPropertyValue);

delete Object.prototype.get;
y1d7ng marked this conversation as resolved.
Show resolved Hide resolved
}
y1d7ng marked this conversation as resolved.
Show resolved Hide resolved