add fs.rmdir & friends (#1838)

This commit is contained in:
Alex Lam S.L
2023-01-19 05:56:34 +02:00
committed by GitHub
parent 58cbd6b211
commit d4e323b997
2 changed files with 123 additions and 1 deletions

View File

@@ -14,6 +14,9 @@ export var access = function access(...args) {
rm = function rm(...args) {
callbackify(fs.rmSync, args);
},
rmdir = function rmdir(...args) {
callbackify(fs.rmdirSync, args);
},
copyFile = function copyFile(...args) {
callbackify(fs.copyFileSync, args);
},
@@ -142,6 +145,7 @@ export var access = function access(...args) {
utimesSync = fs.utimesSync.bind(fs),
lutimesSync = fs.lutimesSync.bind(fs),
rmSync = fs.rmSync.bind(fs),
rmdirSync = fs.rmdirSync.bind(fs),
promises = import.meta.require("node:fs/promises");
function callbackify(fsFunction, args) {
@@ -945,6 +949,8 @@ export default {
renameSync,
rm,
rmSync,
rmdir,
rmdirSync,
stat,
statSync,
symlink,

View File

@@ -15,6 +15,8 @@ import {
lstatSync,
copyFileSync,
rmSync,
rmdir,
rmdirSync,
createReadStream,
createWriteStream,
promises,
@@ -432,6 +434,77 @@ describe("rm", () => {
});
});
describe("rmdir", () => {
it("removes a file", (done) => {
const path = `/tmp/${Date.now()}.rm.txt`;
writeFileSync(path, "File written successfully", "utf8");
expect(existsSync(path)).toBe(true);
rmdir(path, (err) => {
expect(err).toBeDefined();
expect(err.code).toBe("EPERM");
expect(err.message).toBe("Operation not permitted");
expect(existsSync(path)).toBe(true);
done();
});
});
it("removes a dir", (done) => {
const path = `/tmp/${Date.now()}.rm.dir`;
try {
mkdirSync(path);
} catch (e) {}
expect(existsSync(path)).toBe(true);
rmdir(path, (err) => {
expect(existsSync(path)).toBe(false);
done(err);
});
});
// TODO support `recursive: true`
// it("removes a dir recursively", (done) => {
// const path = `/tmp/${Date.now()}.rm.dir/foo/bar`;
// try {
// mkdirSync(path, { recursive: true });
// } catch (e) {}
// expect(existsSync(path)).toBe(true);
// rmdir(join(path, "../../"), { recursive: true }, (err) => {
// expect(existsSync(path)).toBe(false);
// done(err);
// });
// });
});
describe("rmdirSync", () => {
it("removes a file", () => {
const path = `/tmp/${Date.now()}.rm.txt`;
writeFileSync(path, "File written successfully", "utf8");
expect(existsSync(path)).toBe(true);
expect(() => {
rmdirSync(path);
}).toThrow("Operation not permitted");
expect(existsSync(path)).toBe(true);
});
it("removes a dir", () => {
const path = `/tmp/${Date.now()}.rm.dir`;
try {
mkdirSync(path);
} catch (e) {}
expect(existsSync(path)).toBe(true);
rmdirSync(path);
expect(existsSync(path)).toBe(false);
});
// TODO support `recursive: true`
// it("removes a dir recursively", () => {
// const path = `/tmp/${Date.now()}.rm.dir/foo/bar`;
// try {
// mkdirSync(path, { recursive: true });
// } catch (e) {}
// expect(existsSync(path)).toBe(true);
// rmdirSync(join(path, "../../"), { recursive: true });
// expect(existsSync(path)).toBe(false);
// });
});
describe("createReadStream", () => {
it("works (1 chunk)", async () => {
return await new Promise((resolve, reject) => {
@@ -545,7 +618,14 @@ describe("createWriteStream", () => {
});
describe("fs/promises", () => {
const { readFile, stat, writeFile } = promises;
const {
exists,
mkdir,
readFile,
rmdir,
stat,
writeFile,
} = promises;
it("should not segfault on exception", async () => {
try {
@@ -589,4 +669,40 @@ describe("fs/promises", () => {
}
}
});
describe("rmdir", () => {
it("removes a file", async () => {
const path = `/tmp/${Date.now()}.rm.txt`;
await writeFile(path, "File written successfully", "utf8");
expect(await exists(path)).toBe(true);
try {
await rmdir(path);
expect(() => {}).toThrow();
} catch (err) {
expect(err.code).toBe("EPERM");
expect(err.message).toBe("Operation not permitted");
expect(await exists(path)).toBe(true);
}
});
it("removes a dir", async () => {
const path = `/tmp/${Date.now()}.rm.dir`;
try {
await mkdir(path);
} catch (e) {}
expect(await exists(path)).toBe(true);
await rmdir(path);
expect(await exists(path)).toBe(false);
});
// TODO support `recursive: true`
// it("removes a dir recursively", async () => {
// const path = `/tmp/${Date.now()}.rm.dir/foo/bar`;
// try {
// await mkdir(path, { recursive: true });
// } catch (e) {}
// expect(await exists(path)).toBe(true);
// await rmdir(join(path, "../../"), { recursive: true });
// expect(await exists(path)).toBe(false);
// });
});
});