Skip to content

Commit d789f31

Browse files
authored
feat(cli-repl): add configuration to set max file count MONGOSH-1987 (#2354)
1 parent 27a51ae commit d789f31

File tree

5 files changed

+101
-17
lines changed

5 files changed

+101
-17
lines changed

Diff for: packages/cli-repl/src/cli-repl.spec.ts

+20
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ describe('CliRepl', function () {
324324
'disableLogging',
325325
'logLocation',
326326
'logRetentionDays',
327+
'logMaxFileCount',
327328
] satisfies (keyof CliUserConfig)[]);
328329
});
329330

@@ -1457,6 +1458,25 @@ describe('CliRepl', function () {
14571458
testRetentionDays
14581459
);
14591460
});
1461+
1462+
it('can set log max file count', async function () {
1463+
const testMaxFileCount = 123;
1464+
cliRepl.config.logMaxFileCount = testMaxFileCount;
1465+
const oldEnvironmentLimit =
1466+
process.env.MONGOSH_TEST_ONLY_MAX_LOG_FILE_COUNT;
1467+
delete process.env.MONGOSH_TEST_ONLY_MAX_LOG_FILE_COUNT;
1468+
await cliRepl.start(await testServer.connectionString(), {});
1469+
1470+
expect(await cliRepl.getConfig('logMaxFileCount')).equals(
1471+
testMaxFileCount
1472+
);
1473+
expect(cliRepl.logManager?._options.maxLogFileCount).equals(
1474+
testMaxFileCount
1475+
);
1476+
1477+
process.env.MONGOSH_TEST_ONLY_MAX_LOG_FILE_COUNT =
1478+
oldEnvironmentLimit;
1479+
});
14601480
});
14611481

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

Diff for: packages/cli-repl/src/cli-repl.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,8 @@ export class CliRepl implements MongoshIOProvider {
261261
this.shellHomeDirectory.localPath('.'),
262262
retentionDays: await this.getConfig('logRetentionDays'),
263263
maxLogFileCount: +(
264-
process.env.MONGOSH_TEST_ONLY_MAX_LOG_FILE_COUNT || 100
264+
process.env.MONGOSH_TEST_ONLY_MAX_LOG_FILE_COUNT ||
265+
(await this.getConfig('logMaxFileCount'))
265266
),
266267
onerror: (err: Error) => this.bus.emit('mongosh:error', err, 'log'),
267268
onwarn: (err: Error, path: string) =>

Diff for: packages/e2e-tests/test/e2e.spec.ts

+71-16
Original file line numberDiff line numberDiff line change
@@ -1747,6 +1747,58 @@ describe('e2e', function () {
17471747
});
17481748
});
17491749

1750+
describe('with custom log retention max file count', function () {
1751+
const customLogDir = useTmpdir();
1752+
1753+
it('should delete files once it is above the max file count limit', async function () {
1754+
const globalConfig = path.join(homedir, 'globalconfig.conf');
1755+
await fs.writeFile(
1756+
globalConfig,
1757+
`mongosh:\n logLocation: ${JSON.stringify(
1758+
customLogDir.path
1759+
)}\n logMaxFileCount: 4`
1760+
);
1761+
const paths: string[] = [];
1762+
const offset = Math.floor(Date.now() / 1000);
1763+
1764+
// Create 10 log files
1765+
for (let i = 9; i >= 0; i--) {
1766+
const filename = path.join(
1767+
customLogDir.path,
1768+
ObjectId.createFromTime(offset - i).toHexString() + '_log'
1769+
);
1770+
await fs.writeFile(filename, '');
1771+
paths.push(filename);
1772+
}
1773+
1774+
// All 10 existing log files exist.
1775+
expect(await getFilesState(paths)).to.equal('1111111111');
1776+
shell = this.startTestShell({
1777+
args: ['--nodb'],
1778+
env: {
1779+
...env,
1780+
MONGOSH_TEST_ONLY_MAX_LOG_FILE_COUNT: '',
1781+
MONGOSH_GLOBAL_CONFIG_FILE_FOR_TESTING: globalConfig,
1782+
},
1783+
forceTerminal: true,
1784+
});
1785+
1786+
await shell.waitForPrompt();
1787+
1788+
// Add the newly created log to the file list.
1789+
paths.push(
1790+
path.join(customLogDir.path, `${shell.logId as string}_log`)
1791+
);
1792+
1793+
expect(
1794+
await shell.executeLine('config.get("logMaxFileCount")')
1795+
).contains('4');
1796+
1797+
// Expect 7 files to be deleted and 4 to remain (including the new log file)
1798+
expect(await getFilesState(paths)).to.equal('00000001111');
1799+
});
1800+
});
1801+
17501802
it('creates a log file that keeps track of session events', async function () {
17511803
expect(await shell.executeLine('print(123 + 456)')).to.include('579');
17521804
const log = await readLogFile();
@@ -1816,23 +1868,26 @@ describe('e2e', function () {
18161868
const newLogId = shell.logId;
18171869
expect(newLogId).not.null;
18181870
expect(oldLogId).equals(newLogId);
1819-
log = await readLogFile();
18201871

1821-
expect(
1822-
log.filter(
1823-
(logEntry) => logEntry.attr?.input === 'print(111 + 222)'
1824-
)
1825-
).to.have.lengthOf(1);
1826-
expect(
1827-
log.filter(
1828-
(logEntry) => logEntry.attr?.input === 'print(579 - 123)'
1829-
)
1830-
).to.have.lengthOf(1);
1831-
expect(
1832-
log.filter(
1833-
(logEntry) => logEntry.attr?.input === 'print(123 + 456)'
1834-
)
1835-
).to.have.lengthOf(0);
1872+
await eventually(async () => {
1873+
log = await readLogFile();
1874+
1875+
expect(
1876+
log.filter(
1877+
(logEntry) => logEntry.attr?.input === 'print(111 + 222)'
1878+
)
1879+
).to.have.lengthOf(1);
1880+
expect(
1881+
log.filter(
1882+
(logEntry) => logEntry.attr?.input === 'print(579 - 123)'
1883+
)
1884+
).to.have.lengthOf(1);
1885+
expect(
1886+
log.filter(
1887+
(logEntry) => logEntry.attr?.input === 'print(123 + 456)'
1888+
)
1889+
).to.have.lengthOf(0);
1890+
});
18361891
});
18371892

18381893
it('includes information about the driver version', async function () {

Diff for: packages/types/src/index.spec.ts

+6
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ describe('config validation', function () {
3131
expect(await validate('logRetentionDays', -1)).to.equal(
3232
'logRetentionDays must be a positive integer'
3333
);
34+
expect(await validate('logMaxFileCount', 'foo')).to.equal(
35+
'logMaxFileCount must be a positive integer'
36+
);
37+
expect(await validate('logMaxFileCount', -1)).to.equal(
38+
'logMaxFileCount must be a positive integer'
39+
);
3440
expect(await validate('showStackTraces', 'foo')).to.equal(
3541
'showStackTraces must be a boolean'
3642
);

Diff for: packages/types/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@ export class CliUserConfig extends SnippetShellUserConfig {
510510
disableLogging = false;
511511
logLocation: string | undefined = undefined;
512512
logRetentionDays = 30;
513+
logMaxFileCount = 100;
513514
}
514515

515516
export class CliUserConfigValidator extends SnippetShellUserConfigValidator {
@@ -533,6 +534,7 @@ export class CliUserConfigValidator extends SnippetShellUserConfigValidator {
533534
case 'inspectDepth':
534535
case 'historyLength':
535536
case 'logRetentionDays':
537+
case 'logMaxFileCount':
536538
if (typeof value !== 'number' || value < 0) {
537539
return `${key} must be a positive integer`;
538540
}

0 commit comments

Comments
 (0)