Skip to content

Commit 141ae65

Browse files
committed
tests: add file deletion e2e tests
1 parent dbe5b98 commit 141ae65

File tree

1 file changed

+70
-1
lines changed

1 file changed

+70
-1
lines changed

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

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable no-control-regex */
22
import { expect } from 'chai';
33
import type { Db } from 'mongodb';
4-
import { MongoClient } from 'mongodb';
4+
import { MongoClient, ObjectId } from 'mongodb';
55

66
import { eventually } from '../../../testing/eventually';
77
import { TestShell } from './test-shell';
@@ -1676,6 +1676,75 @@ describe('e2e', function () {
16761676
});
16771677
});
16781678

1679+
/** Helper to visualize and compare the existence of files in a specific order.
1680+
* Returns a string comprised of: 1 if a given file exists, 0 otherwise. */
1681+
const getFilesState = async (paths: string[]) => {
1682+
return (
1683+
await Promise.all(
1684+
paths.map((path) =>
1685+
fs.stat(path).then(
1686+
() => 1,
1687+
() => 0
1688+
)
1689+
)
1690+
)
1691+
).join('');
1692+
};
1693+
1694+
describe('with custom log retention days', function () {
1695+
const customLogDir = useTmpdir();
1696+
1697+
it('should delete older files according to the setting', async function () {
1698+
const paths: string[] = [];
1699+
const today = Math.floor(Date.now() / 1000);
1700+
const tenDaysAgo = today - 10 * 24 * 60 * 60;
1701+
// Create 6 files older than 7 days
1702+
for (let i = 5; i >= 0; i--) {
1703+
const filename = path.join(
1704+
customLogDir.path,
1705+
ObjectId.createFromTime(tenDaysAgo - i).toHexString() + '_log'
1706+
);
1707+
await fs.writeFile(filename, '');
1708+
paths.push(filename);
1709+
}
1710+
// Create 4 files newer than 10 days
1711+
for (let i = 3; i >= 0; i--) {
1712+
const filename = path.join(
1713+
customLogDir.path,
1714+
ObjectId.createFromTime(today - i).toHexString() + '_log'
1715+
);
1716+
await fs.writeFile(filename, '');
1717+
paths.push(filename);
1718+
}
1719+
1720+
const retentionDays = 7;
1721+
1722+
const globalConfig = path.join(homedir, 'globalconfig.conf');
1723+
await fs.writeFile(
1724+
globalConfig,
1725+
`mongosh:\n logLocation: "${customLogDir.path}"\n logRetentionDays: ${retentionDays}`
1726+
);
1727+
1728+
expect(await getFilesState(paths)).equals('1111111111');
1729+
1730+
shell = this.startTestShell({
1731+
args: ['--nodb'],
1732+
env: {
1733+
...env,
1734+
MONGOSH_GLOBAL_CONFIG_FILE_FOR_TESTING: globalConfig,
1735+
},
1736+
forceTerminal: true,
1737+
});
1738+
1739+
await shell.waitForPrompt();
1740+
1741+
// Add the newly created log file
1742+
paths.push(path.join(customLogDir.path, `${shell.logId}_log`));
1743+
// Expect 6 files to be deleted and 5 to remain (including the new log file)
1744+
expect(await getFilesState(paths)).equals('00000011111');
1745+
});
1746+
});
1747+
16791748
it('creates a log file that keeps track of session events', async function () {
16801749
expect(await shell.executeLine('print(123 + 456)')).to.include('579');
16811750
const log = await readLogFile();

0 commit comments

Comments
 (0)