source: trip-planner-front/node_modules/streamroller/test/DateRollingFileStream-test.js

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 16.4 KB
Line 
1require("should");
2
3const fs = require("fs-extra"),
4 path = require("path"),
5 zlib = require("zlib"),
6 proxyquire = require("proxyquire").noPreserveCache(),
7 util = require("util"),
8 streams = require("stream");
9
10let fakeNow = new Date(2012, 8, 12, 10, 37, 11);
11const mockNow = () => fakeNow;
12const RollingFileWriteStream = proxyquire("../lib/RollingFileWriteStream", {
13 "./now": mockNow
14});
15const DateRollingFileStream = proxyquire("../lib/DateRollingFileStream", {
16 "./RollingFileWriteStream": RollingFileWriteStream
17});
18
19const gunzip = util.promisify(zlib.gunzip);
20const gzip = util.promisify(zlib.gzip);
21const remove = filename => fs.unlink(filename).catch(() => {});
22const close = async (stream) => new Promise(
23 (resolve, reject) => stream.end(e => e ? reject(e) : resolve())
24);
25
26describe("DateRollingFileStream", function() {
27 describe("arguments", function() {
28 let stream;
29
30 before(function() {
31 stream = new DateRollingFileStream(
32 path.join(__dirname, "test-date-rolling-file-stream-1"),
33 "yyyy-MM-dd.hh"
34 );
35 });
36
37 after(async function() {
38 await close(stream);
39 await remove(path.join(__dirname, "test-date-rolling-file-stream-1"));
40 });
41
42 it("should take a filename and a pattern and return a WritableStream", function() {
43 stream.filename.should.eql(
44 path.join(__dirname, "test-date-rolling-file-stream-1")
45 );
46 stream.options.pattern.should.eql("yyyy-MM-dd.hh");
47 stream.should.be.instanceOf(streams.Writable);
48 });
49
50 it("with default settings for the underlying stream", function() {
51 stream.currentFileStream.mode.should.eql(420);
52 stream.currentFileStream.flags.should.eql("a");
53 });
54 });
55
56 describe("default arguments", function() {
57 var stream;
58
59 before(function() {
60 stream = new DateRollingFileStream(
61 path.join(__dirname, "test-date-rolling-file-stream-2")
62 );
63 });
64
65 after(async function() {
66 await close(stream);
67 await remove(path.join(__dirname, "test-date-rolling-file-stream-2"));
68 });
69
70 it("should have pattern of .yyyy-MM-dd", function() {
71 stream.options.pattern.should.eql("yyyy-MM-dd");
72 });
73 });
74
75 describe("with stream arguments", function() {
76 var stream;
77
78 before(function() {
79 stream = new DateRollingFileStream(
80 path.join(__dirname, "test-date-rolling-file-stream-3"),
81 "yyyy-MM-dd",
82 { mode: parseInt("0666", 8) }
83 );
84 });
85
86 after(async function() {
87 await close(stream);
88 await remove(path.join(__dirname, "test-date-rolling-file-stream-3"));
89 });
90
91 it("should pass them to the underlying stream", function() {
92 stream.theStream.mode.should.eql(parseInt("0666", 8));
93 });
94 });
95
96 describe("with stream arguments but no pattern", function() {
97 var stream;
98
99 before(function() {
100 stream = new DateRollingFileStream(
101 path.join(__dirname, "test-date-rolling-file-stream-4"),
102 { mode: parseInt("0666", 8) }
103 );
104 });
105
106 after(async function() {
107 await close(stream);
108 await remove(path.join(__dirname, "test-date-rolling-file-stream-4"));
109 });
110
111 it("should pass them to the underlying stream", function() {
112 stream.theStream.mode.should.eql(parseInt("0666", 8));
113 });
114
115 it("should use default pattern", function() {
116 stream.options.pattern.should.eql("yyyy-MM-dd");
117 });
118 });
119
120 describe("with a pattern of .yyyy-MM-dd", function() {
121 var stream;
122
123 before(function(done) {
124 stream = new DateRollingFileStream(
125 path.join(__dirname, "test-date-rolling-file-stream-5"),
126 ".yyyy-MM-dd",
127 null
128 );
129 stream.write("First message\n", "utf8", done);
130 });
131
132 after(async function() {
133 await close(stream);
134 await remove(path.join(__dirname, "test-date-rolling-file-stream-5"));
135 });
136
137 it("should create a file with the base name", async function() {
138 const contents = await fs.readFile(
139 path.join(__dirname, "test-date-rolling-file-stream-5"),
140 "utf8"
141 );
142 contents.should.eql("First message\n");
143 });
144
145 describe("when the day changes", function() {
146 before(function(done) {
147 fakeNow = new Date(2012, 8, 13, 0, 10, 12);
148 stream.write("Second message\n", "utf8", done);
149 });
150
151 after(async function() {
152 await remove(
153 path.join(__dirname, "test-date-rolling-file-stream-5.2012-09-12")
154 );
155 });
156
157 describe("the number of files", function() {
158 it("should be two", async function() {
159 const files = await fs.readdir(__dirname);
160 files
161 .filter(
162 file => file.indexOf("test-date-rolling-file-stream-5") > -1
163 )
164 .should.have.length(2);
165 });
166 });
167
168 describe("the file without a date", function() {
169 it("should contain the second message", async function() {
170 const contents = await fs.readFile(
171 path.join(__dirname, "test-date-rolling-file-stream-5"),
172 "utf8"
173 );
174 contents.should.eql("Second message\n");
175 });
176 });
177
178 describe("the file with the date", function() {
179 it("should contain the first message", async function() {
180 const contents = await fs.readFile(
181 path.join(__dirname, "test-date-rolling-file-stream-5.2012-09-12"),
182 "utf8"
183 );
184 contents.should.eql("First message\n");
185 });
186 });
187 });
188 });
189
190 describe("with alwaysIncludePattern", function() {
191 var stream;
192
193 before(async function() {
194 fakeNow = new Date(2012, 8, 12, 11, 10, 12);
195 await remove(
196 path.join(
197 __dirname,
198 "test-date-rolling-file-stream-pattern.2012-09-12-11.log"
199 )
200 );
201 stream = new DateRollingFileStream(
202 path.join(__dirname, "test-date-rolling-file-stream-pattern"),
203 ".yyyy-MM-dd-hh.log",
204 { alwaysIncludePattern: true }
205 );
206
207 await new Promise(resolve => {
208 setTimeout(function() {
209 stream.write("First message\n", "utf8", () => resolve());
210 }, 50);
211 });
212 });
213
214 after(async function() {
215 await close(stream);
216 await remove(
217 path.join(
218 __dirname,
219 "test-date-rolling-file-stream-pattern.2012-09-12-11.log"
220 )
221 );
222 });
223
224 it("should create a file with the pattern set", async function() {
225 const contents = await fs.readFile(
226 path.join(
227 __dirname,
228 "test-date-rolling-file-stream-pattern.2012-09-12-11.log"
229 ),
230 "utf8"
231 );
232 contents.should.eql("First message\n");
233 });
234
235 describe("when the day changes", function() {
236 before(function(done) {
237 fakeNow = new Date(2012, 8, 12, 12, 10, 12);
238 stream.write("Second message\n", "utf8", done);
239 });
240
241 after(async function() {
242 await remove(
243 path.join(
244 __dirname,
245 "test-date-rolling-file-stream-pattern.2012-09-12-12.log"
246 )
247 );
248 });
249
250 describe("the number of files", function() {
251 it("should be two", async function() {
252 const files = await fs.readdir(__dirname);
253 files
254 .filter(
255 file => file.indexOf("test-date-rolling-file-stream-pattern") > -1
256 )
257 .should.have.length(2);
258 });
259 });
260
261 describe("the file with the later date", function() {
262 it("should contain the second message", async function() {
263 const contents = await fs.readFile(
264 path.join(
265 __dirname,
266 "test-date-rolling-file-stream-pattern.2012-09-12-12.log"
267 ),
268 "utf8"
269 );
270 contents.should.eql("Second message\n");
271 });
272 });
273
274 describe("the file with the date", function() {
275 it("should contain the first message", async function() {
276 const contents = await fs.readFile(
277 path.join(
278 __dirname,
279 "test-date-rolling-file-stream-pattern.2012-09-12-11.log"
280 ),
281 "utf8"
282 );
283 contents.should.eql("First message\n");
284 });
285 });
286 });
287 });
288
289 describe("with a pattern that evaluates to digits", function() {
290 let stream;
291 before(done => {
292 fakeNow = new Date(2012, 8, 12, 0, 10, 12);
293 stream = new DateRollingFileStream(
294 path.join(__dirname, "digits.log"),
295 ".yyyyMMdd"
296 );
297 stream.write("First message\n", "utf8", done);
298 });
299
300 describe("when the day changes", function() {
301 before(function(done) {
302 fakeNow = new Date(2012, 8, 13, 0, 10, 12);
303 stream.write("Second message\n", "utf8", done);
304 });
305
306 it("should be two files (it should not get confused by indexes)", async function() {
307 const files = await fs.readdir(__dirname);
308 var logFiles = files.filter(file => file.indexOf("digits.log") > -1);
309 logFiles.should.have.length(2);
310
311 const contents = await fs.readFile(
312 path.join(__dirname, "digits.log.20120912"),
313 "utf8"
314 );
315 contents.should.eql("First message\n");
316 const c = await fs.readFile(path.join(__dirname, "digits.log"), "utf8");
317 c.should.eql("Second message\n");
318 });
319 });
320
321 after(async function() {
322 await close(stream);
323 await remove(path.join(__dirname, "digits.log"));
324 await remove(path.join(__dirname, "digits.log.20120912"));
325 });
326 });
327
328 describe("with compress option", function() {
329 var stream;
330
331 before(function(done) {
332 fakeNow = new Date(2012, 8, 12, 0, 10, 12);
333 stream = new DateRollingFileStream(
334 path.join(__dirname, "compressed.log"),
335 ".yyyy-MM-dd",
336 { compress: true }
337 );
338 stream.write("First message\n", "utf8", done);
339 });
340
341 describe("when the day changes", function() {
342 before(function(done) {
343 fakeNow = new Date(2012, 8, 13, 0, 10, 12);
344 stream.write("Second message\n", "utf8", done);
345 });
346
347 it("should be two files, one compressed", async function() {
348 const files = await fs.readdir(__dirname);
349 var logFiles = files.filter(
350 file => file.indexOf("compressed.log") > -1
351 );
352 logFiles.should.have.length(2);
353
354 const gzipped = await fs.readFile(
355 path.join(__dirname, "compressed.log.2012-09-12.gz")
356 );
357 const contents = await gunzip(gzipped);
358 contents.toString("utf8").should.eql("First message\n");
359
360 (await fs.readFile(
361 path.join(__dirname, "compressed.log"),
362 "utf8"
363 )).should.eql("Second message\n");
364 });
365 });
366
367 after(async function() {
368 await close(stream);
369 await remove(path.join(__dirname, "compressed.log"));
370 await remove(path.join(__dirname, "compressed.log.2012-09-12.gz"));
371 });
372 });
373
374 describe("with keepFileExt option", function() {
375 var stream;
376
377 before(function(done) {
378 fakeNow = new Date(2012, 8, 12, 0, 10, 12);
379 stream = new DateRollingFileStream(
380 path.join(__dirname, "keepFileExt.log"),
381 ".yyyy-MM-dd",
382 { keepFileExt: true }
383 );
384 stream.write("First message\n", "utf8", done);
385 });
386
387 describe("when the day changes", function() {
388 before(function(done) {
389 fakeNow = new Date(2012, 8, 13, 0, 10, 12);
390 stream.write("Second message\n", "utf8", done);
391 });
392
393 it("should be two files", async function() {
394 const files = await fs.readdir(__dirname);
395 var logFiles = files.filter(file => file.indexOf("keepFileExt") > -1);
396 logFiles.should.have.length(2);
397
398 (await fs.readFile(
399 path.join(__dirname, "keepFileExt.2012-09-12.log"),
400 "utf8"
401 )).should.eql("First message\n");
402 (await fs.readFile(
403 path.join(__dirname, "keepFileExt.log"),
404 "utf8"
405 )).should.eql("Second message\n");
406 });
407 });
408
409 after(async function() {
410 await close(stream);
411 await remove(path.join(__dirname, "keepFileExt.log"));
412 await remove(path.join(__dirname, "keepFileExt.2012-09-12.log"));
413 });
414 });
415
416 describe("with compress option and keepFileExt option", function() {
417 var stream;
418
419 before(function(done) {
420 fakeNow = new Date(2012, 8, 12, 0, 10, 12);
421 stream = new DateRollingFileStream(
422 path.join(__dirname, "compressedAndKeepExt.log"),
423 ".yyyy-MM-dd",
424 { compress: true, keepFileExt: true }
425 );
426 stream.write("First message\n", "utf8", done);
427 });
428
429 describe("when the day changes", function() {
430 before(function(done) {
431 fakeNow = new Date(2012, 8, 13, 0, 10, 12);
432 stream.write("Second message\n", "utf8", done);
433 });
434
435 it("should be two files, one compressed", async function() {
436 const files = await fs.readdir(__dirname);
437 var logFiles = files.filter(
438 file => file.indexOf("compressedAndKeepExt") > -1
439 );
440 logFiles.should.have.length(2);
441
442 const gzipped = await fs.readFile(
443 path.join(__dirname, "compressedAndKeepExt.2012-09-12.log.gz")
444 );
445 const contents = await gunzip(gzipped);
446 contents.toString("utf8").should.eql("First message\n");
447 (await fs.readFile(
448 path.join(__dirname, "compressedAndKeepExt.log"),
449 "utf8"
450 )).should.eql("Second message\n");
451 });
452 });
453
454 after(async function() {
455 await close(stream);
456 await remove(path.join(__dirname, "compressedAndKeepExt.log"));
457 await remove(
458 path.join(__dirname, "compressedAndKeepExt.2012-09-12.log.gz")
459 );
460 });
461 });
462
463 describe("with daysToKeep option", function() {
464 let stream;
465 var daysToKeep = 4;
466 var numOriginalLogs = 10;
467
468 before(async function() {
469 for (let i = 0; i < numOriginalLogs; i += 1) {
470 await fs.writeFile(
471 path.join(__dirname, `daysToKeep.log.2012-09-${20-i}`),
472 `Message on day ${i}\n`,
473 { encoding: "utf-8" }
474 );
475 }
476 stream = new DateRollingFileStream(
477 path.join(__dirname, "daysToKeep.log"),
478 ".yyyy-MM-dd",
479 {
480 alwaysIncludePattern: true,
481 daysToKeep: daysToKeep
482 }
483 );
484 });
485
486 describe("when the day changes", function() {
487 before(function(done) {
488 fakeNow = new Date(2012, 8, 21, 0, 10, 12);
489 stream.write("Second message\n", "utf8", done);
490 });
491
492 it("should be daysToKeep + 1 files left from numOriginalLogs", async function() {
493 const files = await fs.readdir(__dirname);
494 var logFiles = files.filter(
495 file => file.indexOf("daysToKeep.log") > -1
496 );
497 logFiles.should.have.length(daysToKeep + 1);
498 });
499 });
500
501 after(async function() {
502 await close(stream);
503 const files = await fs.readdir(__dirname);
504 const logFiles = files
505 .filter(file => file.indexOf("daysToKeep.log") > -1)
506 .map(f => remove(path.join(__dirname, f)));
507 await Promise.all(logFiles);
508 });
509 });
510
511 describe("with daysToKeep and compress options", function() {
512 let stream;
513 const daysToKeep = 4;
514 const numOriginalLogs = 10;
515
516 before(async function() {
517 for (let i = numOriginalLogs; i >= 0; i -= 1) {
518 fakeNow = new Date(2012, 8, 20 - i, 0, 10, 12);
519 const contents = await gzip(`Message on day ${i}\n`);
520 await fs.writeFile(
521 path.join(__dirname, `compressedDaysToKeep.log.2012-09-${20-i}.gz`),
522 contents
523 );
524 }
525 stream = new DateRollingFileStream(
526 path.join(__dirname, "compressedDaysToKeep.log"),
527 ".yyyy-MM-dd",
528 {
529 alwaysIncludePattern: true,
530 compress: true,
531 daysToKeep: daysToKeep
532 }
533 );
534 });
535
536 describe("when the day changes", function() {
537 before(function(done) {
538 fakeNow = new Date(2012, 8, 21, 0, 10, 12);
539 stream.write("New file message\n", "utf8", done);
540 });
541
542 it("should be 4 files left from original 3", async function() {
543 const files = await fs.readdir(__dirname);
544 var logFiles = files.filter(
545 file => file.indexOf("compressedDaysToKeep.log") > -1
546 );
547 logFiles.should.have.length(daysToKeep + 1);
548 });
549 });
550
551 after(async function() {
552 await close(stream);
553 const files = await fs.readdir(__dirname);
554 const logFiles = files
555 .filter(file => file.indexOf("compressedDaysToKeep.log") > -1)
556 .map(f => remove(path.join(__dirname, f)));
557 await Promise.all(logFiles);
558 });
559 });
560});
Note: See TracBrowser for help on using the repository browser.