Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions packages/cli-repl/src/cli-repl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import type { DevtoolsConnectOptions } from '@mongosh/service-provider-node-driv
import type { AddressInfo } from 'net';
import sinon from 'sinon';
import type { CliUserConfig } from '@mongosh/types';
import { MongoLogWriter } from 'mongodb-log-writer';
import { MongoLogWriter, MongoLogManager } from 'mongodb-log-writer';
const { EJSON } = bson;

const delay = promisify(setTimeout);
Expand Down Expand Up @@ -478,12 +478,14 @@ describe('CliRepl', function () {
cliRepl = new CliRepl(cliReplOptions);
await cliRepl.start('', {});
await fs.stat(newerlogfile);
try {
await fs.stat(oldlogfile);
expect.fail('missed exception');
} catch (err: any) {
expect(err.code).to.equal('ENOENT');
}
await eventually(async () => {
try {
await fs.stat(oldlogfile);
expect.fail('missed exception');
} catch (err: any) {
expect(err.code).to.equal('ENOENT');
}
});
});

it('verifies the Node.js version', async function () {
Expand Down Expand Up @@ -1382,6 +1384,7 @@ describe('CliRepl', function () {
srv.close();
await once(srv, 'close');
setTelemetryDelay(0);
sinon.restore();
});

context('logging configuration', function () {
Expand Down Expand Up @@ -1409,6 +1412,20 @@ describe('CliRepl', function () {

expect(cliRepl.logWriter).is.undefined;
});

it('logs cleanup errors', async function () {
sinon
.stub(MongoLogManager.prototype, 'cleanupOldLogFiles')
.rejects(new Error('Method not implemented'));
await cliRepl.start(await testServer.connectionString(), {});
expect(
(await log()).filter(
(entry) =>
entry.ctx === 'log' &&
entry.msg === 'Error: Method not implemented'
)
).to.have.lengthOf(1);
});
});

it('times out fast', async function () {
Expand Down
6 changes: 5 additions & 1 deletion packages/cli-repl/src/cli-repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,11 @@ export class CliRepl implements MongoshIOProvider {
this.warnAboutInaccessibleFile(err, path),
});

await this.logManager.cleanupOldLogFiles();
// Do not wait for log cleanup and log errors if MongoLogManager throws any.
void this.logManager.cleanupOldLogFiles().catch((err) => {
this.bus.emit('mongosh:error', err, 'log');
});

markTime(TimingCategories.Logging, 'cleaned up log files');

if (!this.logWriter) {
Expand Down