Skip to content

Commit e385c9e

Browse files
committed
fix: suggestions from code review
Change typing of LogEntry and use EJSON.parse, remove unnecessary eslint comment
1 parent 82cb3d1 commit e385c9e

File tree

4 files changed

+34
-33
lines changed

4 files changed

+34
-33
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,11 @@ describe('e2e TLS', function () {
243243
const logPath = path.join(logBasePath, `${shell.logId}_log`);
244244
const logContents = await readReplLogfile(logPath);
245245
expect(
246-
logContents.find((line) => line.id === 1_000_000_049).attr
246+
logContents.find((line) => line.id.__value === 1_000_000_049)?.attr
247247
.asyncFallbackError
248248
).to.equal(null); // Ensure that system CA loading happened asynchronously.
249249
expect(
250-
logContents.find((line) => line.id === 1_000_000_049).attr
250+
logContents.find((line) => line.id.__value === 1_000_000_049)?.attr
251251
.systemCertsError
252252
).to.equal(null); // Ensure that system CA could be loaded successfully.
253253
});
@@ -280,11 +280,11 @@ describe('e2e TLS', function () {
280280
const logPath = path.join(logBasePath, `${shell.logId}_log`);
281281
const logContents = await readReplLogfile(logPath);
282282
expect(
283-
logContents.find((line) => line.id === 1_000_000_049).attr
283+
logContents.find((line) => line.id.__value === 1_000_000_049)?.attr
284284
.asyncFallbackError
285285
).to.equal(null); // Ensure that system CA loading happened asynchronously.
286286
expect(
287-
logContents.find((line) => line.id === 1_000_000_049).attr
287+
logContents.find((line) => line.id.__value === 1_000_000_049)?.attr
288288
.systemCertsError
289289
).to.equal(null); // Ensure that system CA could be loaded successfully.
290290
});
@@ -309,7 +309,8 @@ describe('e2e TLS', function () {
309309

310310
const logPath = path.join(logBasePath, `${shell.logId}_log`);
311311
const logContents = await readReplLogfile(logPath);
312-
expect(logContents.find((line) => line.id === 1_000_000_049)).to.exist;
312+
expect(logContents.find((line) => line.id.__value === 1_000_000_049)).to
313+
.exist;
313314
});
314315
}
315316
);

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

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { once } from 'events';
2222
import type { AddressInfo } from 'net';
2323
const { EJSON } = bson;
2424
import { sleep } from './util-helpers';
25-
import type { LogEntry } from '@mongosh/shell-api/src/log-entry';
25+
import type { MongoLogEntry } from 'mongodb-log-writer';
2626

2727
const jsContextFlagCombinations: `--jsContext=${'plain-vm' | 'repl'}`[][] = [
2828
[],
@@ -1356,7 +1356,7 @@ describe('e2e', function () {
13561356
let logBasePath: string;
13571357
let historyPath: string;
13581358
let readConfig: () => Promise<any>;
1359-
let readLogFile: <T = LogEntry>() => Promise<T[]>;
1359+
let readLogFile: <T extends MongoLogEntry>() => Promise<T[]>;
13601360
let startTestShell: (...extraArgs: string[]) => Promise<TestShell>;
13611361

13621362
beforeEach(function () {
@@ -1393,12 +1393,12 @@ describe('e2e', function () {
13931393
}
13941394
readConfig = async () =>
13951395
EJSON.parse(await fs.readFile(configPath, 'utf8'));
1396-
readLogFile = async () => {
1396+
readLogFile = async <T extends MongoLogEntry>(): Promise<T[]> => {
13971397
if (!shell.logId) {
13981398
throw new Error('Shell does not have a logId associated with it');
13991399
}
14001400
const logPath = path.join(logBasePath, `${shell.logId}_log`);
1401-
return readReplLogfile(logPath);
1401+
return readReplLogfile<T>(logPath);
14021402
};
14031403
startTestShell = async (...extraArgs: string[]) => {
14041404
const shell = this.startTestShell({
@@ -1551,7 +1551,6 @@ describe('e2e', function () {
15511551

15521552
const log = await readLogFile();
15531553
expect(
1554-
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
15551554
log.filter(
15561555
(logEntry) => logEntry.attr?.input === 'print(123 + 456)'
15571556
)
@@ -1562,7 +1561,6 @@ describe('e2e', function () {
15621561
expect(await shell.executeLine('print(123 + 456)')).to.include('579');
15631562
const log = await readLogFile();
15641563
expect(
1565-
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
15661564
log.filter(
15671565
(logEntry) => logEntry.attr?.input === 'print(123 + 456)'
15681566
)
@@ -1595,12 +1593,12 @@ describe('e2e', function () {
15951593
});
15961594

15971595
it('starts writing to the same log from the point where disableLogging is set to false', async function () {
1598-
expect(await shell.executeLine('print(222 - 111)')).to.include('111');
1596+
expect(await shell.executeLine('print(111 + 222)')).to.include('333');
15991597

16001598
let log = await readLogFile();
16011599
expect(
16021600
log.filter(
1603-
(logEntry) => logEntry.attr?.input === 'print(222 - 111)'
1601+
(logEntry) => logEntry.attr?.input === 'print(111 + 222)'
16041602
)
16051603
).to.have.lengthOf(1);
16061604

@@ -1632,7 +1630,7 @@ describe('e2e', function () {
16321630

16331631
expect(
16341632
log.filter(
1635-
(logEntry) => logEntry.attr?.input === 'print(222 - 111)'
1633+
(logEntry) => logEntry.attr?.input === 'print(111 + 222)'
16361634
)
16371635
).to.have.lengthOf(1);
16381636
expect(
@@ -1668,12 +1666,12 @@ describe('e2e', function () {
16681666
await shell.executeLine("log.info('This is a custom entry')");
16691667
expect(shell.assertNoErrors());
16701668
await eventually(async () => {
1671-
const log = await readLogFile<{
1672-
msg: string;
1673-
s: string;
1674-
c: string;
1675-
ctx: string;
1676-
}>();
1669+
const log = await readLogFile<
1670+
MongoLogEntry & {
1671+
c: string;
1672+
ctx: string;
1673+
}
1674+
>();
16771675
const customLogEntry = log.filter((logEntry) =>
16781676
logEntry.msg.includes('This is a custom entry')
16791677
);
@@ -1697,7 +1695,7 @@ describe('e2e', function () {
16971695
await shell.executeLine(`load(${JSON.stringify(filename)})`);
16981696
expect(shell.assertNoErrors());
16991697
await eventually(async () => {
1700-
const log = await readLogFile<{ msg: string }>();
1698+
const log = await readLogFile();
17011699
expect(
17021700
log.filter((logEntry) =>
17031701
logEntry.msg.includes('Initiating connection attemp')

packages/e2e-tests/test/repl-helpers.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import chai from 'chai';
66
import sinonChai from 'sinon-chai';
77
import chaiAsPromised from 'chai-as-promised';
88
import type { MongodSetup } from '../../../testing/integration-testing-hooks';
9+
import type { MongoLogEntry } from 'mongodb-log-writer';
10+
import { EJSON } from 'bson';
911

1012
chai.use(sinonChai);
1113
chai.use(chaiAsPromised);
@@ -47,11 +49,13 @@ function useTmpdir(): { readonly path: string } {
4749
};
4850
}
4951

50-
async function readReplLogfile(logPath: string) {
52+
async function readReplLogfile<T extends MongoLogEntry>(
53+
logPath: string
54+
): Promise<T[]> {
5155
return (await fs.readFile(logPath, 'utf8'))
5256
.split('\n')
5357
.filter((line) => line.trim())
54-
.map((line) => JSON.parse(line));
58+
.map((line) => EJSON.parse(line));
5559
}
5660

5761
const fakeExternalEditor = async ({

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -351,16 +351,14 @@ class LoggingAndTelemetry implements MongoshLoggingAndTelemetry {
351351
});
352352

353353
onBus('mongosh:write-custom-log', (event: WriteCustomLogEvent) => {
354-
if (this.log) {
355-
this.log[event.method](
356-
'MONGOSH-SCRIPTS',
357-
mongoLogId(1_000_000_054),
358-
'custom-log',
359-
event.message,
360-
event.attr,
361-
event.level
362-
);
363-
}
354+
this.log[event.method](
355+
'MONGOSH-SCRIPTS',
356+
mongoLogId(1_000_000_054),
357+
'custom-log',
358+
event.message,
359+
event.attr,
360+
event.level
361+
);
364362
});
365363

366364
onBus('mongosh:globalconfig-load', (args: GlobalConfigFileLoadEvent) => {

0 commit comments

Comments
 (0)