Skip to content

Commit 0858257

Browse files
committed
tests: add e2e tests for global config and midway disabling and enabling of the logs
1 parent da2b5b3 commit 0858257

File tree

4 files changed

+106
-18
lines changed

4 files changed

+106
-18
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1385,7 +1385,7 @@ describe('CliRepl', function () {
13851385
});
13861386

13871387
context('logging configuration', function () {
1388-
it('logging is enabled by default', async function () {
1388+
it('logging is enabled by default and event is called', async function () {
13891389
const onLogInitialized = sinon.stub();
13901390
cliRepl.bus.on('mongosh:log-initialized', onLogInitialized);
13911391

@@ -1396,6 +1396,19 @@ describe('CliRepl', function () {
13961396
expect(onLogInitialized).calledOnce;
13971397
expect(cliRepl.logWriter).is.instanceOf(MongoLogWriter);
13981398
});
1399+
1400+
it('does not initialize logging when it is disabled', async function () {
1401+
cliRepl.config.disableLogging = true;
1402+
const onLogInitialized = sinon.stub();
1403+
cliRepl.bus.on('mongosh:log-initialized', onLogInitialized);
1404+
1405+
await cliRepl.start(await testServer.connectionString(), {});
1406+
1407+
expect(await cliRepl.getConfig('disableLogging')).is.true;
1408+
expect(onLogInitialized).not.called;
1409+
1410+
expect(cliRepl.logWriter).is.undefined;
1411+
});
13991412
});
14001413

14011414
it('times out fast', async function () {

packages/e2e-tests/test/e2e.spec.ts

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,7 +1356,7 @@ describe('e2e', function () {
13561356
let logBasePath: string;
13571357
let historyPath: string;
13581358
let readConfig: () => Promise<any>;
1359-
let readLogfile: () => Promise<LogEntry[]>;
1359+
let readLogFile: () => Promise<LogEntry[]>;
13601360
let startTestShell: (...extraArgs: string[]) => Promise<TestShell>;
13611361

13621362
beforeEach(function () {
@@ -1393,7 +1393,7 @@ describe('e2e', function () {
13931393
}
13941394
readConfig = async () =>
13951395
EJSON.parse(await fs.readFile(configPath, 'utf8'));
1396-
readLogfile = async () => {
1396+
readLogFile = async () => {
13971397
if (!shell.logId) {
13981398
throw new Error('Shell does not have a logId associated with it');
13991399
}
@@ -1508,7 +1508,7 @@ describe('e2e', function () {
15081508
});
15091509

15101510
describe('log file', function () {
1511-
it('does not create a log if global config has disableLogging', async function () {
1511+
it('does not get created if global config has disableLogging', async function () {
15121512
const globalConfig = path.join(homedir, 'globalconfig.conf');
15131513
await fs.writeFile(globalConfig, 'mongosh:\n disableLogging: true');
15141514
shell = this.startTestShell({
@@ -1529,9 +1529,38 @@ describe('e2e', function () {
15291529
expect(shell.logId).equals(null);
15301530
});
15311531

1532+
it('gets created if global config has disableLogging set to false', async function () {
1533+
const globalConfig = path.join(homedir, 'globalconfig.conf');
1534+
await fs.writeFile(globalConfig, 'mongosh:\n disableLogging: false');
1535+
shell = this.startTestShell({
1536+
args: ['--nodb'],
1537+
env: {
1538+
...env,
1539+
MONGOSH_GLOBAL_CONFIG_FILE_FOR_TESTING: globalConfig,
1540+
},
1541+
forceTerminal: true,
1542+
});
1543+
await shell.waitForPrompt();
1544+
expect(
1545+
await shell.executeLine('config.get("disableLogging")')
1546+
).to.include('false');
1547+
shell.assertNoErrors();
1548+
1549+
expect(await shell.executeLine('print(123 + 456)')).to.include('579');
1550+
expect(shell.logId).not.equal(null);
1551+
1552+
const log = await readLogFile();
1553+
expect(
1554+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
1555+
log.filter(
1556+
(logEntry) => logEntry.attr?.input === 'print(123 + 456)'
1557+
)
1558+
).to.have.lengthOf(1);
1559+
});
1560+
15321561
it('creates a log file that keeps track of session events', async function () {
15331562
expect(await shell.executeLine('print(123 + 456)')).to.include('579');
1534-
const log = await readLogfile();
1563+
const log = await readLogFile();
15351564
expect(
15361565
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
15371566
log.filter(
@@ -1540,9 +1569,9 @@ describe('e2e', function () {
15401569
).to.have.lengthOf(1);
15411570
});
15421571

1543-
it('does not write to the log file after disableLogging is set to true', async function () {
1572+
it('does not write to the log after disableLogging is set to true', async function () {
15441573
expect(await shell.executeLine('print(123 + 456)')).to.include('579');
1545-
const log = await readLogfile();
1574+
const log = await readLogFile();
15461575
expect(
15471576
log.filter(
15481577
(logEntry) => logEntry.attr?.input === 'print(123 + 456)'
@@ -1552,7 +1581,7 @@ describe('e2e', function () {
15521581
await shell.executeLine(`config.set("disableLogging", true)`);
15531582
expect(await shell.executeLine('print(579 - 123)')).to.include('456');
15541583

1555-
const logAfterDisabling = await readLogfile();
1584+
const logAfterDisabling = await readLogFile();
15561585
expect(
15571586
logAfterDisabling.filter(
15581587
(logEntry) => logEntry.attr?.input === 'print(579 - 123)'
@@ -1565,6 +1594,43 @@ describe('e2e', function () {
15651594
).to.have.lengthOf(1);
15661595
});
15671596

1597+
it('starts writing to a new log from the point where disableLogging is set to false', async function () {
1598+
await shell.executeLine(`config.set("disableLogging", true)`);
1599+
expect(await shell.executeLine('print(123 + 456)')).to.include('579');
1600+
const log = await readLogFile();
1601+
const oldLogId = shell.logId;
1602+
expect(oldLogId).not.null;
1603+
1604+
expect(
1605+
log.filter(
1606+
(logEntry) => logEntry.attr?.input === 'print(123 + 456)'
1607+
)
1608+
).to.have.lengthOf(0);
1609+
1610+
await shell.executeLine(`config.set("disableLogging", false)`);
1611+
1612+
expect(
1613+
await shell.executeLine('config.get("disableLogging")')
1614+
).to.include('false');
1615+
1616+
expect(await shell.executeLine('print(579 - 123)')).to.include('456');
1617+
1618+
const newLogId = shell.logId;
1619+
expect(newLogId).not.null;
1620+
expect(oldLogId).not.equal(newLogId);
1621+
const logsAfterEnabling = await readLogFile();
1622+
expect(
1623+
logsAfterEnabling.filter(
1624+
(logEntry) => logEntry.attr?.input === 'print(579 - 123)'
1625+
)
1626+
).to.have.lengthOf(1);
1627+
expect(
1628+
logsAfterEnabling.filter(
1629+
(logEntry) => logEntry.attr?.input === 'print(123 + 456)'
1630+
)
1631+
).to.have.lengthOf(0);
1632+
});
1633+
15681634
it('includes information about the driver version', async function () {
15691635
const connectionString = await testServer.connectionString();
15701636
expect(
@@ -1573,7 +1639,7 @@ describe('e2e', function () {
15731639
)
15741640
).to.include('test');
15751641
await eventually(async () => {
1576-
const log = await readLogfile();
1642+
const log = await readLogFile();
15771643
expect(
15781644
log.filter(
15791645
(logEntry) => typeof logEntry.attr?.driver?.version === 'string'

packages/e2e-tests/test/test-shell.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -362,12 +362,16 @@ export class TestShell {
362362
}
363363

364364
get logId(): string | null {
365-
const match = /^Current Mongosh Log ID:\s*(?<logId>[a-z0-9]{24})$/m.exec(
366-
this._output
365+
const matches = this._output.match(
366+
/^Current Mongosh Log ID:\s*([a-z0-9]{24})$/gm
367367
);
368-
if (!match) {
368+
if (!matches || matches.length === 0) {
369369
return null;
370370
}
371-
return match.groups!.logId;
371+
const lastMatch = matches[matches.length - 1];
372+
const logIdMatch = /^Current Mongosh Log ID:\s*([a-z0-9]{24})$/.exec(
373+
lastMatch
374+
);
375+
return logIdMatch ? logIdMatch[1] : null;
372376
}
373377
}

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,22 @@ export class MongoshLoggingAndTelemetry {
118118
redactURICredentials(uri)
119119
);
120120

121-
for (const pendingEvent of this.pendingLogEvents) {
122-
pendingEvent();
123-
}
124-
this.pendingLogEvents = [];
121+
this.runAndCleanPendingEvents();
125122

126123
this.bus.emit('mongosh:log-initialized');
127124
}
128125

129126
public detachLogger() {
130-
this.pendingLogEvents = [];
131127
this.log = null;
128+
// Still run any remaining pending events for telemetry purposes.
129+
this.runAndCleanPendingEvents();
130+
}
131+
132+
private runAndCleanPendingEvents() {
133+
for (const pendingEvent of this.pendingLogEvents) {
134+
pendingEvent();
135+
}
136+
this.pendingLogEvents = [];
132137
}
133138

134139
private _getTelemetryUserIdentity(): MongoshAnalyticsIdentity {

0 commit comments

Comments
 (0)