Skip to content

Commit

Permalink
Merge pull request #13418 from Automattic/IslandRhythms/gh-13364
Browse files Browse the repository at this point in the history
fix: custom debug function not processing all args
  • Loading branch information
vkarpov15 committed May 18, 2023
2 parents 02fd721 + 05eca74 commit 722ab43
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
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

0 comments on commit 722ab43

Please sign in to comment.