Skip to content

Commit c021660

Browse files
sinon tests working
1 parent 1d026d4 commit c021660

File tree

5 files changed

+110
-11
lines changed

5 files changed

+110
-11
lines changed

src/connection_string.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ export function parseOptions(
529529
...mongoOptions[Symbol.for('@@mdb.internalLoggerConfig')]
530530
};
531531
loggerClientOptions = {
532-
mongodbLogPath: mongoOptions.mongoLoggerClientOptions.mongodbLogPath
532+
mongodbLogPath: mongoOptions.mongodbLogPath
533533
};
534534
}
535535
mongoOptions.mongoLoggerOptions = MongoLogger.resolveOptions(
@@ -1228,7 +1228,7 @@ export const OPTIONS = {
12281228
* @internal
12291229
* TODO: NODE-5671 - remove internal flag
12301230
*/
1231-
mongoLoggerClientOptions: { type: 'any' }
1231+
mongodbLogPath: { type: 'any' }
12321232
} as Record<keyof MongoClientOptions, OptionDescriptor>;
12331233

12341234
export const DEFAULT_OPTIONS = new CaseInsensitiveMap(

src/mongo_client.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@ import { MONGO_CLIENT_EVENTS } from './constants';
2121
import { Db, type DbOptions } from './db';
2222
import type { Encrypter } from './encrypter';
2323
import { MongoInvalidArgumentError } from './error';
24-
import {
25-
MongoLogger,
26-
type MongoLoggerMongoClientOptions,
27-
type MongoLoggerOptions
28-
} from './mongo_logger';
24+
import { type MongoDBLogWritable, MongoLogger, type MongoLoggerOptions } from './mongo_logger';
2925
import { TypedEventEmitter } from './mongo_types';
3026
import { executeOperation } from './operations/execute_operation';
3127
import { RunAdminCommandOperation } from './operations/run_command';
@@ -260,7 +256,7 @@ export interface MongoClientOptions extends BSONSerializeOptions, SupportedNodeC
260256
* @internal
261257
* TODO: NODE-5671 - remove internal flag
262258
*/
263-
mongoLoggerClientOptions?: MongoLoggerMongoClientOptions;
259+
mongodbLogPath?: 'stderr' | 'stdout' | MongoDBLogWritable;
264260

265261
/** @internal */
266262
[featureFlag: symbol]: any;
@@ -843,5 +839,5 @@ export interface MongoOptions
843839
* @internal
844840
* TODO: NODE-5671 - remove internal flag
845841
*/
846-
mongoLoggerClientOptions?: MongoLoggerMongoClientOptions;
842+
mongodbLogPath?: 'stderr' | 'stdout' | MongoDBLogWritable;
847843
}

test/tools/unified-spec-runner/entities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ export class UnifiedMongoClient extends MongoClient {
191191
[Symbol.for('@@mdb.internalLoggerConfig')]: componentSeverities,
192192
...getEnvironmentalOptions(),
193193
...(description.serverApi ? { serverApi: description.serverApi } : {}),
194-
mongoLoggerClientOptions: { mongodbLogPath: logCollector }
194+
mongodbLogPath: logCollector
195195
} as any);
196196
this.logCollector = logCollector;
197197

test/unit/connection_string.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import * as process from 'node:process';
44
import { expect } from 'chai';
55
import * as dns from 'dns';
66
import * as sinon from 'sinon';
7+
import { inspect } from 'util';
78

89
import {
910
AUTH_MECHS_AUTH_SRC_EXTERNAL,
1011
AuthMechanism,
1112
DEFAULT_ALLOWED_HOSTS,
1213
FEATURE_FLAGS,
14+
type Log,
1315
MongoAPIError,
1416
MongoClient,
1517
MongoCredentials,
@@ -832,4 +834,47 @@ describe('Connection String', function () {
832834
.that.matches(/useUnifiedTopology has no effect/);
833835
});
834836
});
837+
838+
describe('when mongodbLogPath is in options', function () {
839+
const loggerFeatureFlag = Symbol.for('@@mdb.enableMongoLogger');
840+
841+
let stderrStub;
842+
let stdoutStub;
843+
844+
before(() => {
845+
stdoutStub = sinon.stub(process.stdout);
846+
stderrStub = sinon.stub(process.stderr);
847+
});
848+
849+
after(() => {
850+
sinon.restore();
851+
});
852+
853+
it('when option is `stderr`, it is accessible through mongoLogger.logDestination', function () {
854+
const client = new MongoClient('mongodb://a/?mongodbLogPath=stderr', {
855+
[loggerFeatureFlag]: true
856+
});
857+
const log: Log = { t: new Date(), c: 'ConnectionStringStdErr', s: 'error' };
858+
client.options.mongoLoggerOptions.logDestination.write(log);
859+
expect(stderrStub.write).calledWith(inspect(log, { breakLength: Infinity, compact: true }));
860+
});
861+
862+
it('when option is `stdout`, it is accessible through mongoLogger.logDestination', function () {
863+
const client = new MongoClient('mongodb://a/?mongodbLogPath=stdout', {
864+
[loggerFeatureFlag]: true
865+
});
866+
const log: Log = { t: new Date(), c: 'ConnectionStringStdOut', s: 'error' };
867+
client.options.mongoLoggerOptions.logDestination.write(log);
868+
expect(stdoutStub.write).calledWith(inspect(log, { breakLength: Infinity, compact: true }));
869+
});
870+
871+
it('when option is invalid, it defaults to stderr', function () {
872+
const client = new MongoClient('mongodb://a/?mongodbLogPath=stdnothing', {
873+
[loggerFeatureFlag]: true
874+
});
875+
const log: Log = { t: new Date(), c: 'ConnectionStringInvalidOption', s: 'error' };
876+
client.options.mongoLoggerOptions.logDestination.write(log);
877+
expect(stderrStub.write).calledWith(inspect(log, { breakLength: Infinity, compact: true }));
878+
});
879+
});
835880
});

test/unit/mongo_client.test.js

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
'use strict';
1+
import { inspect } from 'util';
2+
3+
('use strict');
24
const os = require('os');
35
const fs = require('fs');
46
const { expect } = require('chai');
@@ -813,4 +815,60 @@ describe('MongoOptions', function () {
813815
expect(client.options).to.have.property('mongoLoggerOptions').to.equal(expectedLoggingObject);
814816
});
815817
});
818+
819+
context('when mongodbLogPath is in options', function () {
820+
const loggerFeatureFlag = Symbol.for('@@mdb.enableMongoLogger');
821+
822+
let stderrStub;
823+
let stdoutStub;
824+
825+
before(() => {
826+
stdoutStub = sinon.stub(process.stdout);
827+
stderrStub = sinon.stub(process.stderr);
828+
});
829+
830+
after(() => {
831+
sinon.restore();
832+
});
833+
834+
it('when option is `stderr`, it is accessible through mongoLogger.logDestination', function () {
835+
const client = new MongoClient('mongodb://a/', {
836+
[loggerFeatureFlag]: true,
837+
mongodbLogPath: 'stderr'
838+
});
839+
const log = { t: new Date(), c: 'constructorStdErr', s: 'error' };
840+
client.options.mongoLoggerOptions.logDestination.write(log);
841+
expect(stderrStub.write).calledWith(inspect(log, { breakLength: Infinity, compact: true }));
842+
});
843+
844+
it('when option is a Writable stream, it is accessible through mongoLogger.logDestination', function () {
845+
const writable = new Writable();
846+
const client = new MongoClient('mongodb://a/', {
847+
[loggerFeatureFlag]: true,
848+
mongodbLogPath: writable
849+
});
850+
expect(client.options.mongoLoggerOptions.logDestination).to.deep.equal(writable);
851+
});
852+
853+
it('when option is `stdout`, it is accessible through mongoLogger.logDestination', function () {
854+
const client = new MongoClient('mongodb://a/', {
855+
[loggerFeatureFlag]: true,
856+
mongodbLogPath: 'stdout'
857+
});
858+
const log = { t: new Date(), c: 'constructorStdOut', s: 'error' };
859+
client.options.mongoLoggerOptions.logDestination.write(log);
860+
expect(stdoutStub.write).calledWith(inspect(log, { breakLength: Infinity, compact: true }));
861+
});
862+
863+
it('when option is invalid, it defaults to stderr', function () {
864+
const invalidOption = 'stdnothing';
865+
const client = new MongoClient('mongodb://a/', {
866+
[loggerFeatureFlag]: true,
867+
mongodbLogPath: invalidOption
868+
});
869+
const log = { t: new Date(), c: 'constructorInvalidOption', s: 'error' };
870+
client.options.mongoLoggerOptions.logDestination.write(log);
871+
expect(stderrStub.write).calledWith(inspect(log, { breakLength: Infinity, compact: true }));
872+
});
873+
});
816874
});

0 commit comments

Comments
 (0)