aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Alex Lam S.L <alexlamsl@gmail.com> 2023-01-19 05:56:34 +0200
committerGravatar GitHub <noreply@github.com> 2023-01-18 19:56:34 -0800
commitd4e323b997bfda0bb140cac4fb0a621a35953fb7 (patch)
tree353e3a3e56dcfa0d784b19a2963565b59c0833dc
parent58cbd6b211410593a2c3e4ac98fada50789433cd (diff)
downloadbun-d4e323b997bfda0bb140cac4fb0a621a35953fb7.tar.gz
bun-d4e323b997bfda0bb140cac4fb0a621a35953fb7.tar.zst
bun-d4e323b997bfda0bb140cac4fb0a621a35953fb7.zip
add `fs.rmdir` & friends (#1838)
-rw-r--r--src/bun.js/fs.exports.js6
-rw-r--r--test/bun.js/fs.test.js118
2 files changed, 123 insertions, 1 deletions
diff --git a/src/bun.js/fs.exports.js b/src/bun.js/fs.exports.js
index a6b132392..158e929fa 100644
--- a/src/bun.js/fs.exports.js
+++ b/src/bun.js/fs.exports.js
@@ -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,
diff --git a/test/bun.js/fs.test.js b/test/bun.js/fs.test.js
index 058a40828..5e8a27b05 100644
--- a/test/bun.js/fs.test.js
+++ b/test/bun.js/fs.test.js
@@ -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);
+ // });
+ });
});