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
7 changes: 5 additions & 2 deletions src/CacheFS.js
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -149,15 +149,18 @@ 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);
parent.delete(basename);
}
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 }) {
Expand Down
22 changes: 22 additions & 0 deletions src/__tests__/fs.promises.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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", () => {
Expand Down
3 changes: 2 additions & 1 deletion src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };