Skip to content
This repository has been archived by the owner on Aug 15, 2024. It is now read-only.

Allow writefile to be called with empty string #58

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 1 addition & 2 deletions lib/MemoryFileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ class MemoryFileSystem {
}

writeFileSync(_path, content, optionsOrEncoding) {
if(!content && !optionsOrEncoding) throw new Error("No content");
const path = pathToArray(_path);
if(path.length === 0) {
throw new MemoryFileSystemError(errors.code.EISDIR, _path, "writeFile");
Expand All @@ -214,7 +213,7 @@ class MemoryFileSystem {
if(isDir(current[path[i]]))
throw new MemoryFileSystemError(errors.code.EISDIR, _path, "writeFile");
const encoding = typeof optionsOrEncoding === "object" ? optionsOrEncoding.encoding : optionsOrEncoding;
current[path[i]] = optionsOrEncoding || typeof content === "string" ? new Buffer(content, encoding) : content;
current[path[i]] = Buffer.isBuffer(content) ? content : new Buffer(String(content), encoding);
return;
}

Expand Down
15 changes: 9 additions & 6 deletions test/MemoryFileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ describe("files", function() {
fs.writeFileSync("/b", "Test", {encoding: "utf-8"});
fs.readFileSync("/b", "utf-8").should.be.eql("Test");
});
it("should allow creating empty files", function() {
var fs = new MemoryFileSystem();
fs.writeFileSync("/empty-buffer", new Buffer(""));
fs.readFileSync("/empty-buffer", "utf-8").should.be.eql("");
fs.writeFileSync("/empty-string", "");
fs.readFileSync("/empty-string", "utf-8").should.be.eql("");
fs.writeFileSync("/no-args");
fs.readFileSync("/no-args", "utf-8").should.be.eql("undefined");
});
});
describe("errors", function() {
it("should fail on invalid paths", function() {
Expand Down Expand Up @@ -146,12 +155,6 @@ describe("errors", function() {
err.should.be.instanceof(Error);
});
});
it("should fail incorrect arguments", function() {
var fs = new MemoryFileSystem();
(function() {
fs.writeFileSync("/test");
}).should.throw();
});
it("should fail on wrong type", function() {
var fs = new MemoryFileSystem();
fs.mkdirpSync("/test/dir");
Expand Down