Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion lib/RollingFileWriteStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,38 @@ class RollingFileWriteStream extends Writable {
}

_renewWriteStream() {
fs.ensureDirSync(this.fileObject.dir);
const mkdir = (dir) => {
try {
return fs.mkdirSync(dir, {recursive: true});
}
// backward-compatible fs.mkdirSync for nodejs pre-10.12.0 (without recursive option)
catch (e) {
// recursive creation of parent first
if (e.code === "ENOENT") {
mkdir(path.dirname(dir));
return mkdir(dir);
}

// throw error for all except EEXIST and EROFS (read-only filesystem)
if (e.code !== "EEXIST" && e.code !== "EROFS") {
throw e;
}

// EEXIST: throw if file and not directory
// EROFS : throw if directory not found
else {
try {
if (fs.statSync(dir).isDirectory()) {
return dir;
}
throw e;
} catch (err) {
throw e;
}
}
}
};
mkdir(this.fileObject.dir);
const filePath = this.fileFormatter({
date: this.state.currentDate,
index: 0
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"mocha": true
},
"parserOptions": {
"ecmaVersion": 8
"ecmaVersion": 2018
},
"extends": "eslint:recommended",
"rules": {
Expand Down
141 changes: 141 additions & 0 deletions test/RollingFileWriteStream-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1838,6 +1838,147 @@ describe("RollingFileWriteStream", () => {
});
});

describe("when multi-dir does not exist (recursive, nodejs >= 10.12.0)", () => {
const testFile = "tmp_/tmpA/tmpB/tmpC/ignored.log";
let s;

before(done => {
fs.removeSync("tmp_/tmpA");
fakeNow = new Date(2012, 8, 12, 10, 37, 11);
s = new RollingFileWriteStream(testFile);
s.write("test", "utf8", done);
});

after(done => {
s.end(() => {
fs.removeSync("tmp_/tmpA");
done();
});
});

it("should create the dir", () => {
const files = fs.readdirSync(path.dirname(testFile));
const expectedFileList = ["ignored.log"];
files.should.containDeep(expectedFileList);
files.length.should.equal(expectedFileList.length);

fs.readFileSync(testFile)
.toString()
.should.equal("test");
});
});

describe("when multi-dir does not exist (non-recursive, nodejs < 10.12.0)", () => {
const testFile = "tmp_/tmpA/tmpB/tmpC/ignored.log";
let s;

before(done => {
fs.removeSync("tmp_/tmpA");
fakeNow = new Date(2012, 8, 12, 10, 37, 11);
const RollingFileWriteStream = proxyquire("../lib/RollingFileWriteStream", {
"./now": mockNow,
"fs-extra": {
mkdirSync(dirPath, options) {
return fs.mkdirSync(dirPath, { ...options, ...{ recursive: false } });
}
}
});
s = new RollingFileWriteStream(testFile);
s.write("test", "utf8", done);
});

after(done => {
s.end(() => {
fs.removeSync("tmp_/tmpA");
done();
});
});

it("should create the dir", () => {
const files = fs.readdirSync(path.dirname(testFile));
const expectedFileList = ["ignored.log"];
files.should.containDeep(expectedFileList);
files.length.should.equal(expectedFileList.length);

fs.readFileSync(testFile)
.toString()
.should.equal("test");
});
});

describe("when multi-dir does not exist (error handling)", () => {
const testFile = "tmp_/tmpA/tmpB/tmpC/ignored.log";
let s;

before(done => {
fs.removeSync("tmp_/tmpA");
fakeNow = new Date(2012, 8, 12, 10, 37, 11);
done();
});

after(done => {
try {
s.end(() => {
fs.removeSync("tmp_/tmpA");
done();
});
} catch (e) {
done();
}
});

it("should throw EPERM error", () => {
const errorEPERM = new Error("EPERM");
errorEPERM.code = "EPERM";
(() => {
const RollingFileWriteStream = proxyquire("../lib/RollingFileWriteStream", {
"./now": mockNow,
"fs-extra": {
mkdirSync() {
throw errorEPERM;
}
}
});
s = new RollingFileWriteStream(testFile);
}).should.throw(errorEPERM);
});

const errorEROFS = new Error("EROFS");
errorEROFS.code = "EROFS";

it("should throw EROFS error", () => {
(() => {
const RollingFileWriteStream = proxyquire("../lib/RollingFileWriteStream", {
"./now": mockNow,
"fs-extra": {
mkdirSync() {
throw errorEROFS;
},
statSync() {
return { isDirectory() { return false; } };
}
}
});
s = new RollingFileWriteStream(testFile);
}).should.throw(errorEROFS);
});

it("should not throw EROFS error", () => {
(() => {
fs.mkdirSync("tmp_/tmpA/tmpB/tmpC", { recursive: true });
const RollingFileWriteStream = proxyquire("../lib/RollingFileWriteStream", {
"./now": mockNow,
"fs-extra": {
mkdirSync() {
throw errorEROFS;
}
}
});
s = new RollingFileWriteStream(testFile);
}).should.not.throw(errorEROFS);
});
});

describe("when given just a base filename with no dir", () => {
let s;
before(done => {
Expand Down