-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
RotatingLogs.test.ts
61 lines (57 loc) · 1.99 KB
/
RotatingLogs.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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 = 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 inactive 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);
}
});
test('should not delete the log-timestamp.txt right after its be created', async () => {
let dir: string;
try {
dir = await createTempDir();
await createTestLogFile(dir);
const rotatingLogs: RotatingLogs = new RotatingLogs(dir, 1, 5000);
await rotatingLogs.cleanActiveLogFile();
await rotatingLogs.deleteNonActiveLogFiles();
const files = await readdir(dir);
expect(files.find(file => file.match(/^log-[0-9]+.txt$/gi))).toBeTruthy();
expect(files.length).toBe(1);
} finally {
await remove(dir);
}
});
});