Skip to content

Commit aa0140b

Browse files
committed
chore: added _final method, fixed tests for windows
1 parent 6b37166 commit aa0140b

File tree

6 files changed

+221
-172
lines changed

6 files changed

+221
-172
lines changed

lib/RollingFileWriteStream.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ class RollingFileWriteStream extends Writable {
111111
return options;
112112
}
113113

114+
_final(callback) {
115+
this.currentFileStream.end("", this.options.encoding, callback);
116+
}
117+
114118
_write(chunk, encoding, callback) {
115119
this._shouldRoll().then(() => {
116120
debug(

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"test": "test"
88
},
99
"scripts": {
10-
"codecheck": "eslint 'lib/*.js'",
10+
"codecheck": "eslint \"lib/*.js\" \"test/*.js\"",
1111
"prepublishOnly": "npm test",
1212
"pretest": "npm run codecheck",
1313
"clean": "rm -rf node_modules/",

test/DateRollingFileStream-test.js

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
/**
2-
* This file will be removed.
3-
*/
4-
"use strict";
5-
61
require("should");
72

83
const fs = require("fs-extra"),
@@ -24,6 +19,9 @@ const DateRollingFileStream = proxyquire("../lib/DateRollingFileStream", {
2419
const gunzip = util.promisify(zlib.gunzip);
2520
const gzip = util.promisify(zlib.gzip);
2621
const remove = filename => fs.unlink(filename).catch(() => {});
22+
const close = async (stream) => new Promise(
23+
(resolve, reject) => stream.end(e => e ? reject(e) : resolve())
24+
);
2725

2826
describe("DateRollingFileStream", function() {
2927
describe("arguments", function() {
@@ -37,6 +35,7 @@ describe("DateRollingFileStream", function() {
3735
});
3836

3937
after(async function() {
38+
await close(stream);
4039
await remove(path.join(__dirname, "test-date-rolling-file-stream-1"));
4140
});
4241

@@ -64,6 +63,7 @@ describe("DateRollingFileStream", function() {
6463
});
6564

6665
after(async function() {
66+
await close(stream);
6767
await remove(path.join(__dirname, "test-date-rolling-file-stream-2"));
6868
});
6969

@@ -84,6 +84,7 @@ describe("DateRollingFileStream", function() {
8484
});
8585

8686
after(async function() {
87+
await close(stream);
8788
await remove(path.join(__dirname, "test-date-rolling-file-stream-3"));
8889
});
8990

@@ -103,6 +104,7 @@ describe("DateRollingFileStream", function() {
103104
});
104105

105106
after(async function() {
107+
await close(stream);
106108
await remove(path.join(__dirname, "test-date-rolling-file-stream-4"));
107109
});
108110

@@ -128,6 +130,7 @@ describe("DateRollingFileStream", function() {
128130
});
129131

130132
after(async function() {
133+
await close(stream);
131134
await remove(path.join(__dirname, "test-date-rolling-file-stream-5"));
132135
});
133136

@@ -209,6 +212,7 @@ describe("DateRollingFileStream", function() {
209212
});
210213

211214
after(async function() {
215+
await close(stream);
212216
await remove(
213217
path.join(
214218
__dirname,
@@ -315,6 +319,7 @@ describe("DateRollingFileStream", function() {
315319
});
316320

317321
after(async function() {
322+
await close(stream);
318323
await remove(path.join(__dirname, "digits.log"));
319324
await remove(path.join(__dirname, "digits.log.20120912"));
320325
});
@@ -360,6 +365,7 @@ describe("DateRollingFileStream", function() {
360365
});
361366

362367
after(async function() {
368+
await close(stream);
363369
await remove(path.join(__dirname, "compressed.log"));
364370
await remove(path.join(__dirname, "compressed.log.2012-09-12.gz"));
365371
});
@@ -401,6 +407,7 @@ describe("DateRollingFileStream", function() {
401407
});
402408

403409
after(async function() {
410+
await close(stream);
404411
await remove(path.join(__dirname, "keepFileExt.log"));
405412
await remove(path.join(__dirname, "keepFileExt.2012-09-12.log"));
406413
});
@@ -445,6 +452,7 @@ describe("DateRollingFileStream", function() {
445452
});
446453

447454
after(async function() {
455+
await close(stream);
448456
await remove(path.join(__dirname, "compressedAndKeepExt.log"));
449457
await remove(
450458
path.join(__dirname, "compressedAndKeepExt.2012-09-12.log.gz")
@@ -458,23 +466,21 @@ describe("DateRollingFileStream", function() {
458466
var numOriginalLogs = 10;
459467

460468
before(async function() {
461-
for (let i = numOriginalLogs; i >= 0; i -= 1) {
462-
fakeNow = new Date(2012, 8, 20 - i, 0, 10, 12);
463-
stream = new DateRollingFileStream(
464-
path.join(__dirname, "daysToKeep.log"),
465-
".yyyy-MM-dd",
466-
{
467-
alwaysIncludePattern: true,
468-
daysToKeep: daysToKeep
469-
}
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" }
470474
);
471-
await new Promise(resolve => {
472-
stream.write(util.format("Message on day %d\n", i), "utf8", () =>
473-
resolve()
474-
);
475-
});
476-
await fs.utimes(stream.filename, fakeNow, fakeNow);
477475
}
476+
stream = new DateRollingFileStream(
477+
path.join(__dirname, "daysToKeep.log"),
478+
".yyyy-MM-dd",
479+
{
480+
alwaysIncludePattern: true,
481+
daysToKeep: daysToKeep
482+
}
483+
);
478484
});
479485

480486
describe("when the day changes", function() {
@@ -493,6 +499,7 @@ describe("DateRollingFileStream", function() {
493499
});
494500

495501
after(async function() {
502+
await close(stream);
496503
const files = await fs.readdir(__dirname);
497504
const logFiles = files
498505
.filter(file => file.indexOf("daysToKeep.log") > -1)
@@ -509,27 +516,21 @@ describe("DateRollingFileStream", function() {
509516
before(async function() {
510517
for (let i = numOriginalLogs; i >= 0; i -= 1) {
511518
fakeNow = new Date(2012, 8, 20 - i, 0, 10, 12);
512-
stream = new DateRollingFileStream(
513-
path.join(__dirname, "compressedDaysToKeep.log"),
514-
".yyyy-MM-dd",
515-
{
516-
alwaysIncludePattern: true,
517-
compress: true,
518-
daysToKeep: daysToKeep
519-
}
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
520523
);
521-
await new Promise(resolve => {
522-
stream.write(util.format("Message on day %d\n", i), "utf8", () =>
523-
resolve()
524-
);
525-
});
526-
527-
const contents = await fs.readFile(stream.filename, "utf8");
528-
const gzipped = await gzip(contents);
529-
await fs.writeFile(stream.filename + ".gz", gzipped);
530-
await fs.unlink(stream.filename);
531-
await fs.utimes(stream.filename + ".gz", fakeNow, fakeNow);
532524
}
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+
);
533534
});
534535

535536
describe("when the day changes", function() {
@@ -548,6 +549,7 @@ describe("DateRollingFileStream", function() {
548549
});
549550

550551
after(async function() {
552+
await close(stream);
551553
const files = await fs.readdir(__dirname);
552554
const logFiles = files
553555
.filter(file => file.indexOf("compressedDaysToKeep.log") > -1)

test/RollingFileStream-test.js

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
"use strict";
2-
31
require("should");
42

53
const fs = require("fs-extra"),
@@ -35,6 +33,10 @@ const writeInSequence = async (stream, messages) => {
3533
});
3634
};
3735

36+
const close = async (stream) => new Promise(
37+
(resolve, reject) => stream.end(e => e ? reject(e) : resolve())
38+
);
39+
3840
describe("RollingFileStream", function() {
3941
describe("arguments", function() {
4042
let stream;
@@ -49,6 +51,7 @@ describe("RollingFileStream", function() {
4951
});
5052

5153
after(async function() {
54+
await close(stream);
5255
await remove("test-rolling-file-stream");
5356
});
5457

@@ -68,8 +71,9 @@ describe("RollingFileStream", function() {
6871
});
6972

7073
describe("with stream arguments", function() {
74+
let stream;
7175
it("should pass them to the underlying stream", function() {
72-
var stream = new RollingFileStream(
76+
stream = new RollingFileStream(
7377
path.join(__dirname, "test-rolling-file-stream"),
7478
1024,
7579
5,
@@ -79,33 +83,38 @@ describe("RollingFileStream", function() {
7983
});
8084

8185
after(async function() {
86+
await close(stream);
8287
await remove("test-rolling-file-stream");
8388
});
8489
});
8590

8691
describe("without size", function() {
92+
let stream;
8793
it("should default to max int size", function() {
88-
var stream = new RollingFileStream(
94+
stream = new RollingFileStream(
8995
path.join(__dirname, "test-rolling-file-stream")
9096
);
9197
stream.size.should.eql(Number.MAX_SAFE_INTEGER);
9298
});
9399

94100
after(async function() {
101+
await close(stream);
95102
await remove("test-rolling-file-stream");
96103
});
97104
});
98105

99106
describe("without number of backups", function() {
107+
let stream;
100108
it("should default to 1 backup", function() {
101-
var stream = new RollingFileStream(
109+
stream = new RollingFileStream(
102110
path.join(__dirname, "test-rolling-file-stream"),
103111
1024
104112
);
105113
stream.backups.should.eql(1);
106114
});
107115

108116
after(async function() {
117+
await close(stream);
109118
await remove("test-rolling-file-stream");
110119
});
111120
});
@@ -403,6 +412,9 @@ describe("RollingFileStream", function() {
403412
});
404413
});
405414

415+
// in windows, you can't delete a directory if there is an open file handle
416+
if (process.platform !== "win32") {
417+
406418
describe("when the directory gets deleted", function() {
407419
var stream;
408420
before(function(done) {
@@ -414,9 +426,9 @@ describe("RollingFileStream", function() {
414426
stream.write("initial", "utf8", done);
415427
});
416428

417-
after(function() {
418-
fs.unlinkSync(path.join("subdir", "test-rolling-file-stream"));
419-
fs.rmdirSync("subdir");
429+
after(async () => {
430+
await fs.unlink(path.join("subdir", "test-rolling-file-stream"));
431+
await fs.rmdir("subdir");
420432
});
421433

422434
it("handles directory deletion gracefully", async function() {
@@ -426,15 +438,14 @@ describe("RollingFileStream", function() {
426438

427439
await fs.unlink(path.join("subdir", "test-rolling-file-stream"));
428440
await fs.rmdir("subdir");
429-
await new Promise(resolve => {
430-
stream.write("rollover", "utf8", () => {
431-
fs.readFileSync(
432-
path.join("subdir", "test-rolling-file-stream"),
433-
"utf8"
434-
).should.eql("rollover");
435-
resolve();
436-
});
437-
});
441+
await new Promise(resolve => stream.write("rollover", "utf8", resolve));
442+
await close(stream);
443+
(await fs.readFile(
444+
path.join("subdir", "test-rolling-file-stream"),
445+
"utf8"
446+
)).should.eql("rollover");
438447
});
439448
});
449+
}
450+
440451
});

0 commit comments

Comments
 (0)