diff --git a/src/CacheFS.js b/src/CacheFS.js index bb8c6b2..0345b98 100755 --- a/src/CacheFS.js +++ b/src/CacheFS.js @@ -1,5 +1,5 @@ const path = require("./path.js"); -const { ENOENT, EEXIST, ENOTEMPTY } = require("./errors.js"); +const { EEXIST, ENOENT, ENOTDIR, ENOTEMPTY } = require("./errors.js"); const STAT = 0; @@ -149,8 +149,10 @@ module.exports = class CacheFS { dir.set(basename, entry); } rmdir(filepath) { + let dir = this._lookup(filepath); + if (dir.get(STAT).type !== 'dir') throw new ENOTDIR(); // check it's empty (size should be 1 for just StatSym) - if (this._lookup(filepath).size > 1) throw new ENOTEMPTY(); + if (dir.size > 1) throw new ENOTEMPTY(); // remove from parent let parent = this._lookup(path.dirname(filepath)); let basename = path.basename(filepath); @@ -158,6 +160,7 @@ module.exports = class CacheFS { } readdir(filepath) { let dir = this._lookup(filepath); + if (dir.get(STAT).type !== 'dir') throw new ENOTDIR(); return [...dir.keys()].filter(key => typeof key === "string"); } writeFile(filepath, data, { mode }) { diff --git a/src/__tests__/fs.promises.spec.js b/src/__tests__/fs.promises.spec.js index d194377..40a24c0 100755 --- a/src/__tests__/fs.promises.spec.js +++ b/src/__tests__/fs.promises.spec.js @@ -152,6 +152,17 @@ describe("fs.promises module", () => { }); }); }); + it("read a file throws", done => { + fs.mkdir("/readdir2").finally(() => { + fs.writeFile("/readdir2/not-a-dir", "").then(() => { + fs.readdir("/readdir2/not-a-dir").catch(err => { + expect(err).not.toBe(null); + expect(err.code).toBe('ENOTDIR'); + done(); + }); + }) + }) + }); }); describe("rmdir", () => { @@ -199,6 +210,17 @@ describe("fs.promises module", () => { }); }); }); + it("delete a file throws", done => { + fs.mkdir("/rmdir").finally(() => { + fs.writeFile("/rmdir/not-a-dir", "").then(() => { + fs.rmdir("/rmdir/not-a-dir").catch(err => { + expect(err).not.toBe(null); + expect(err.code).toBe('ENOTDIR'); + done(); + }); + }); + }); + }); }); describe("unlink", () => { diff --git a/src/errors.js b/src/errors.js index fe4408b..8ea3626 100755 --- a/src/errors.js +++ b/src/errors.js @@ -14,6 +14,7 @@ function Err(name) { const EEXIST = Err("EEXIST"); const ENOENT = Err("ENOENT"); +const ENOTDIR = Err("ENOTDIR"); const ENOTEMPTY = Err("ENOTEMPTY"); -module.exports = { EEXIST, ENOENT, ENOTEMPTY }; +module.exports = { EEXIST, ENOENT, ENOTDIR, ENOTEMPTY };