Skip to content

Commit a21ef64

Browse files
committed
tests: add initial set of tests
Will follow up with more end to end tests.
1 parent e4b8c1d commit a21ef64

File tree

4 files changed

+53
-77
lines changed

4 files changed

+53
-77
lines changed

packages/cli-repl/src/cli-repl.spec.ts

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,37 +1313,6 @@ describe('CliRepl', function () {
13131313
hasCollectionNames: true,
13141314
hasDatabaseNames: true,
13151315
});
1316-
1317-
context('logging configuration', function () {
1318-
afterEach(function () {
1319-
sinon.restore();
1320-
});
1321-
1322-
it('start logging when it is not disabled', async function () {
1323-
const emitSpy = sinon.spy(cliRepl.bus, 'emit');
1324-
1325-
await cliRepl.start(await testServer.connectionString(), {});
1326-
1327-
expect(cliRepl.getConfig('disableLogging')).is.false;
1328-
1329-
expect(emitSpy).calledWith('mongosh:logger-initialized');
1330-
expect(cliRepl.logWriter).is.instanceOf(MongoLogWriter);
1331-
});
1332-
1333-
it('does not start logging when it is disabled', async function () {
1334-
const emitSpy = sinon.spy(cliRepl.bus, 'emit');
1335-
cliRepl.config.disableLogging = true;
1336-
1337-
await cliRepl.start(await testServer.connectionString(), {});
1338-
1339-
expect(cliRepl.getConfig('disableLogging')).is.true;
1340-
1341-
expect(emitSpy).called;
1342-
expect(emitSpy).not.calledWith('mongosh:logger-initialized');
1343-
expect(cliRepl.logWriter).is.undefined;
1344-
});
1345-
});
1346-
13471316
context('analytics integration', function () {
13481317
context('with network connectivity', function () {
13491318
let srv: http.Server;
@@ -1394,6 +1363,33 @@ describe('CliRepl', function () {
13941363
setTelemetryDelay(0);
13951364
});
13961365

1366+
context('logging configuration', function () {
1367+
it('logging is enabled by default', async function () {
1368+
const onLoggerInitialized = sinon.stub();
1369+
cliRepl.bus.on('mongosh:logger-initialized', onLoggerInitialized);
1370+
1371+
await cliRepl.start(await testServer.connectionString(), {});
1372+
1373+
expect(await cliRepl.getConfig('disableLogging')).is.false;
1374+
1375+
expect(onLoggerInitialized).calledOnce;
1376+
expect(cliRepl.logWriter).is.instanceOf(MongoLogWriter);
1377+
});
1378+
1379+
it('does not start logging when it is disabled', async function () {
1380+
cliRepl.config.disableLogging = true;
1381+
const onLoggerInitialized = sinon.stub();
1382+
cliRepl.bus.on('mongosh:logger-initialized', onLoggerInitialized);
1383+
1384+
await cliRepl.start(await testServer.connectionString(), {});
1385+
1386+
expect(await cliRepl.getConfig('disableLogging')).is.true;
1387+
expect(onLoggerInitialized).not.called;
1388+
1389+
expect(cliRepl.logWriter).is.undefined;
1390+
});
1391+
});
1392+
13971393
it('times out fast', async function () {
13981394
const testStartMs = Date.now();
13991395
// The `httpRequestTimeout` of our Segment Analytics is set to

packages/cli-repl/src/cli-repl.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,6 @@ export class CliRepl implements MongoshIOProvider {
272272
loggingAndTelemetry.setupLogger(logger);
273273

274274
markTime(TimingCategories.Logging, 'instantiated log writer');
275-
this.bus.emit('mongosh:logger-initialized');
276275
logger.info('MONGOSH', mongoLogId(1_000_000_000), 'log', 'Starting log', {
277276
execPath: process.execPath,
278277
envInfo: redactSensitiveData(this.getLoggedEnvironmentVariables()),

packages/logging/src/logging-and-telemetry.spec.ts

Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ describe('MongoshLoggingAndTelemetry', function () {
3737
},
3838
};
3939

40+
let loggingAndTelemetry: MongoshLoggingAndTelemetry;
41+
4042
beforeEach(function () {
4143
logOutput = [];
4244
analyticsOutput = [];
4345
bus = new EventEmitter();
44-
});
4546

46-
it('throws when trying to setup writer prematurely', function () {
47-
const loggingAndTelemetry = new MongoshLoggingAndTelemetry(
47+
loggingAndTelemetry = new MongoshLoggingAndTelemetry(
4848
bus,
4949
analytics,
5050
{
@@ -53,23 +53,31 @@ describe('MongoshLoggingAndTelemetry', function () {
5353
},
5454
'1.0.0'
5555
);
56+
});
57+
58+
it('throws when running setup twice', function () {
59+
loggingAndTelemetry.setup();
60+
61+
expect(() => loggingAndTelemetry.setup()).throws(
62+
'Setup can only be called once.'
63+
);
64+
});
5665

66+
it('throws when trying to setup writer prematurely', function () {
5767
expect(() => loggingAndTelemetry.setupLogger(logger)).throws(
5868
'Run setup() before setting up the log writer.'
5969
);
6070
});
6171

62-
it('tracks new local connection events', function () {
63-
const loggingAndTelemetry = new MongoshLoggingAndTelemetry(
64-
bus,
65-
analytics,
66-
{
67-
platform: process.platform,
68-
arch: process.arch,
69-
},
70-
'1.0.0'
72+
it('throws when running setupLogger twice', function () {
73+
loggingAndTelemetry.setup();
74+
loggingAndTelemetry.setupLogger(logger);
75+
expect(() => loggingAndTelemetry.setupLogger(logger)).throws(
76+
'Logger has already been initialized.'
7177
);
78+
});
7279

80+
it('tracks new local connection events', function () {
7381
loggingAndTelemetry.setup();
7482
loggingAndTelemetry.setupLogger(logger);
7583

@@ -125,16 +133,6 @@ describe('MongoshLoggingAndTelemetry', function () {
125133
});
126134

127135
it('tracks new atlas connection events', function () {
128-
const loggingAndTelemetry = new MongoshLoggingAndTelemetry(
129-
bus,
130-
analytics,
131-
{
132-
platform: process.platform,
133-
arch: process.arch,
134-
},
135-
'1.0.0'
136-
);
137-
138136
loggingAndTelemetry.setup();
139137
loggingAndTelemetry.setupLogger(logger);
140138

@@ -663,16 +661,6 @@ describe('MongoshLoggingAndTelemetry', function () {
663661
});
664662

665663
it('buffers deprecated API calls', function () {
666-
const loggingAndTelemetry = new MongoshLoggingAndTelemetry(
667-
bus,
668-
analytics,
669-
{
670-
platform: process.platform,
671-
arch: process.arch,
672-
},
673-
'1.0.0'
674-
);
675-
676664
loggingAndTelemetry.setup();
677665
loggingAndTelemetry.setupLogger(logger);
678666

@@ -849,16 +837,6 @@ describe('MongoshLoggingAndTelemetry', function () {
849837
});
850838

851839
it('does not track database calls outside of evaluate-{started,finished}', function () {
852-
const loggingAndTelemetry = new MongoshLoggingAndTelemetry(
853-
bus,
854-
analytics,
855-
{
856-
platform: process.platform,
857-
arch: process.arch,
858-
},
859-
'1.0.0'
860-
);
861-
862840
loggingAndTelemetry.setup();
863841
loggingAndTelemetry.setupLogger(logger);
864842

packages/logging/src/logging-and-telemetry.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ import { MultiSet, toSnakeCase } from './helpers';
4646
import { Writable } from 'stream';
4747

4848
export class MongoshLoggingAndTelemetry {
49+
public log: MongoLogWriter;
50+
4951
private readonly trackingProperties: {
5052
mongosh_version: string;
5153
session_id: string;
@@ -54,14 +56,12 @@ export class MongoshLoggingAndTelemetry {
5456
[key: string]: unknown;
5557
};
5658

57-
private log: MongoLogWriter;
5859
private pendingLogEvents: CallableFunction[] = [];
5960

6061
private telemetryAnonymousId: string | undefined;
6162
private userId: string | undefined;
62-
63-
private isSetup = false;
6463
private hasMongoLogWriterInitialized = false;
64+
private isSetup = false;
6565

6666
constructor(
6767
public readonly bus: MongoshBus,
@@ -90,6 +90,9 @@ export class MongoshLoggingAndTelemetry {
9090
}
9191

9292
public setup(): void {
93+
if (this.isSetup) {
94+
throw new Error('Setup can only be called once.');
95+
}
9396
this._setupBusEventListeners();
9497
this.isSetup = true;
9598
}

0 commit comments

Comments
 (0)