Skip to content

Commit 214ca17

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

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
@@ -1363,7 +1363,7 @@ describe('e2e', function () {
13631363
let logBasePath: string;
13641364
let historyPath: string;
13651365
let readConfig: () => Promise<any>;
1366-
let readLogfile: () => Promise<LogEntry[]>;
1366+
let readLogFile: () => Promise<LogEntry[]>;
13671367
let startTestShell: (...extraArgs: string[]) => Promise<TestShell>;
13681368

13691369
beforeEach(function () {
@@ -1400,7 +1400,7 @@ describe('e2e', function () {
14001400
}
14011401
readConfig = async () =>
14021402
EJSON.parse(await fs.readFile(configPath, 'utf8'));
1403-
readLogfile = async () => {
1403+
readLogFile = async () => {
14041404
if (!shell.logId) {
14051405
throw new Error('Shell does not have a logId associated with it');
14061406
}
@@ -1515,7 +1515,7 @@ describe('e2e', function () {
15151515
});
15161516

15171517
describe('log file', function () {
1518-
it('does not create a log if global config has disableLogging', async function () {
1518+
it('does not get created if global config has disableLogging', async function () {
15191519
const globalConfig = path.join(homedir, 'globalconfig.conf');
15201520
await fs.writeFile(globalConfig, 'mongosh:\n disableLogging: true');
15211521
shell = this.startTestShell({
@@ -1536,9 +1536,38 @@ describe('e2e', function () {
15361536
expect(shell.logId).equals(null);
15371537
});
15381538

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

1550-
it('does not write to the log file after disableLogging is set to true', async function () {
1579+
it('does not write to the log after disableLogging is set to true', async function () {
15511580
expect(await shell.executeLine('print(123 + 456)')).to.include('579');
1552-
const log = await readLogfile();
1581+
const log = await readLogFile();
15531582
expect(
15541583
log.filter(
15551584
(logEntry) => logEntry.attr?.input === 'print(123 + 456)'
@@ -1559,7 +1588,7 @@ describe('e2e', function () {
15591588
await shell.executeLine(`config.set("disableLogging", true)`);
15601589
expect(await shell.executeLine('print(579 - 123)')).to.include('456');
15611590

1562-
const logAfterDisabling = await readLogfile();
1591+
const logAfterDisabling = await readLogFile();
15631592
expect(
15641593
logAfterDisabling.filter(
15651594
(logEntry) => logEntry.attr?.input === 'print(579 - 123)'
@@ -1572,6 +1601,43 @@ describe('e2e', function () {
15721601
).to.have.lengthOf(1);
15731602
});
15741603

1604+
it('starts writing to a new log from the point where disableLogging is set to false', async function () {
1605+
await shell.executeLine(`config.set("disableLogging", true)`);
1606+
expect(await shell.executeLine('print(123 + 456)')).to.include('579');
1607+
const log = await readLogFile();
1608+
const oldLogId = shell.logId;
1609+
expect(oldLogId).not.null;
1610+
1611+
expect(
1612+
log.filter(
1613+
(logEntry) => logEntry.attr?.input === 'print(123 + 456)'
1614+
)
1615+
).to.have.lengthOf(0);
1616+
1617+
await shell.executeLine(`config.set("disableLogging", false)`);
1618+
1619+
expect(
1620+
await shell.executeLine('config.get("disableLogging")')
1621+
).to.include('false');
1622+
1623+
expect(await shell.executeLine('print(579 - 123)')).to.include('456');
1624+
1625+
const newLogId = shell.logId;
1626+
expect(newLogId).not.null;
1627+
expect(oldLogId).not.equal(newLogId);
1628+
const logsAfterEnabling = await readLogFile();
1629+
expect(
1630+
logsAfterEnabling.filter(
1631+
(logEntry) => logEntry.attr?.input === 'print(579 - 123)'
1632+
)
1633+
).to.have.lengthOf(1);
1634+
expect(
1635+
logsAfterEnabling.filter(
1636+
(logEntry) => logEntry.attr?.input === 'print(123 + 456)'
1637+
)
1638+
).to.have.lengthOf(0);
1639+
});
1640+
15751641
it('includes information about the driver version', async function () {
15761642
const connectionString = await testServer.connectionString();
15771643
expect(
@@ -1580,7 +1646,7 @@ describe('e2e', function () {
15801646
)
15811647
).to.include('test');
15821648
await eventually(async () => {
1583-
const log = await readLogfile();
1649+
const log = await readLogFile();
15841650
expect(
15851651
log.filter(
15861652
(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)