/
file-rotationFormat.test.js
136 lines (119 loc) · 3.51 KB
/
file-rotationFormat.test.js
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
'use strict';
const path = require('path');
const winston = require('../../../../lib/winston');
const helpers = require('../../../helpers');
const fs = require('fs');
const { MESSAGE } = require('triple-beam');
const split = require('split2');
const assume = require('assume');
const { rimraf } = require('rimraf');
const testFileFixturesPath = path.join(
__dirname,
'..',
'..',
'..',
'fixtures',
'file'
);
//
// Remove all log fixtures
//
function removeFixtures(done) {
rimraf(path.join(testFileFixturesPath, 'rotation*'), {glob: true}).then(() => done());
}
// Validate Filename according to rotation
function isCorrectFormat(filename) {
let time = filename.split('rotation')[1].split('.')[0];
return new Date(time).getTime() > 0;
}
describe('winston/transports/file/rotationFormat', function () {
this.timeout(10000);
let testDone = false;
before(removeFixtures);
after(done => {
testDone = true;
removeFixtures(done);
});
it('should create multiple files correctly with rotation Function', function (done) {
const fillWith = ['a', 'b', 'c', 'd', 'e'];
const rotationTransport = new winston.transports.File({
level: 'info',
format: winston.format.printf(info => info.message),
filename: path.join(testFileFixturesPath, 'rotation.log'),
maxsize: 4096,
rotationFormat: () => {
return new Date().getTime();
}
});
//
// Have to wait for `fs.stats` to be done in `rotationTransport.open()`.
// Otherwise the rotationTransport._dest is undefined. See https://github.com/winstonjs/winston/issues/1174
//
//
// Setup a list of files which we will later stat.
//
const files = [];
//
// Assets all the files have been created with the
// correct filesize
//
function assumeFilesCreated() {
files.map(function (file, i) {
let stats;
try {
stats = fs.statSync(file);
} catch (ex) {
assume(stats).is.an(
'object',
`${file} failed to open: ${ex.message}`
);
}
const text = fs.readFileSync(file, 'utf8');
assume(text[0]).equals(fillWith[i]);
assume(isCorrectFormat(file));
// Either 4096 on Unix or 4100 on Windows
// because of the eol.
if (process.platform === 'win32') {
assume(stats.size).equals(4100);
} else {
assume(stats.size).equals(4096);
}
});
done();
}
//
// Log the specified kbytes to the transport
//
function logKbytes(kbytes) {
//
// Shift the next fill char off the array then push it back
// to rotate the chars.
//
const filler = fillWith.shift();
fillWith.push(filler);
//
//
// To not make each file not fail the assertion of the filesize we can
// make the array 1023 characters long.
//
const kbStr = Array(1023).fill(filler).join('');
//
// With printf format that displays the message only
// winston adds exactly 0 characters.
//
for (var i = 0; i < kbytes; i++) {
rotationTransport.log({ level: 'info', [MESSAGE]: kbStr });
}
}
rotationTransport.on('open', function (file) {
if (testDone) return; // ignore future notifications
const match = file.match(/(\d+)\.log$/);
const count = match ? match[1] : 0;
if (files.length === 5) {
return assumeFilesCreated();
}
files.push(file);
setImmediate(() => logKbytes(4));
});
});
});