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

fix: custom debug function not processing all args #13418

Merged
merged 6 commits into from
May 18, 2023
Merged
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
8 changes: 7 additions & 1 deletion lib/drivers/node-mongodb-native/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,14 @@ function iter(i) {

if (debug) {
if (typeof debug === 'function') {
let argsToAdd = null;
if (typeof args[args.length - 1] == 'function') {
argsToAdd = args.slice(0, args.length - 1);
} else {
argsToAdd = args;
}
debug.apply(_this,
[_this.name, i].concat(args.slice(0, args.length - 1)));
[_this.name, i].concat(argsToAdd));
} else if (debug instanceof stream.Writable) {
this.$printToStream(_this.name, i, args, debug);
} else {
Expand Down
2 changes: 1 addition & 1 deletion test/docs/debug.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ describe('debug: shell', function() {
await Test.create({ name: 'foo' });
assert.equal(args.length, 1);
assert.equal(args[0][1], 'insertOne');
assert.strictEqual(args[0][3], undefined);
assert.strictEqual(args[0][4], undefined);

await m.disconnect();
});
Expand Down
13 changes: 13 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@ describe('mongoose module:', function() {
await mongoose.disconnect();
});

it('should collect the args correctly gh-13364', async function() {
const util = require('util');
const mongoose = new Mongoose();
const conn = await mongoose.connect(start.uri);
let actual = '';
mongoose.set('debug', (collectionName, methodName, ...methodArgs) => {
actual = `${collectionName}.${methodName}(${util.inspect(methodArgs).slice(2, -2)})`;
});
const user = conn.connection.collection('User');
await user.findOne({ key: 'value' });
assert.equal('User.findOne({ key: \'value\' })', actual);
});

it('{g,s}etting options', function() {
const mongoose = new Mongoose();

Expand Down
2 changes: 1 addition & 1 deletion test/query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3984,7 +3984,7 @@ describe('Query', function() {

let lastOptions = {};
m.set('debug', function(_coll, _method, ...args) {
lastOptions = args[args.length - 1];
lastOptions = args[args.length - 2];
});

const connDebug = m.createConnection(start.uri);
Expand Down