1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-12 08:54:00 +02:00
joplin/packages/lib/RotatingLogs.test.ts
Hubert 33ba934937
All: Resolves #5521: Rotating log files (#8376)
Co-authored-by: Laurent Cozic <laurent22@users.noreply.github.com>
2023-07-14 14:24:25 +01:00

46 lines
1.5 KiB
TypeScript

import { writeFile, readdir, remove } from 'fs-extra';
import { createTempDir, msleep } from './testing/test-utils';
import RotatingLogs from './RotatingLogs';
const createTestLogFile = async (dir: string) => {
await writeFile(`${dir}/log.txt`, 'some content');
};
describe('RotatingLogs', () => {
test('should rename log.txt to log-TIMESTAMP.txt', async () => {
let dir: string;
try {
dir = await createTempDir();
await createTestLogFile(dir);
let files: string[] = await readdir(dir);
expect(files.find(file => file.match(/^log.txt$/gi))).toBeTruthy();
expect(files.length).toBe(1);
const rotatingLogs: RotatingLogs = new RotatingLogs(dir, 1, 1);
await rotatingLogs.cleanActiveLogFile();
files = await readdir(dir);
expect(files.find(file => file.match(/^log.txt$/gi))).toBeFalsy();
expect(files.find(file => file.match(/^log-[0-9]+.txt$/gi))).toBeTruthy();
expect(files.length).toBe(1);
} finally {
await remove(dir);
}
});
test('should delete inative log file after 1ms', async () => {
let dir: string;
try {
dir = await createTempDir();
await createTestLogFile(dir);
const rotatingLogs: RotatingLogs = new RotatingLogs(dir, 1, 1);
await rotatingLogs.cleanActiveLogFile();
await msleep(1);
await rotatingLogs.deleteNonActiveLogFiles();
const files = await readdir(dir);
expect(files.find(file => file.match(/^log-[0-9]+.txt$/gi))).toBeFalsy();
expect(files.length).toBe(0);
} finally {
await remove(dir);
}
});
});