mirror of
https://github.com/oven-sh/bun
synced 2026-02-12 03:48:56 +00:00
Allow fs.close with no callback (#10229)
* allow fs.close to only take one argument * add test * fix tests on windows
This commit is contained in:
@@ -138,8 +138,16 @@ var access = function access(...args) {
|
||||
appendFile = function appendFile(...args) {
|
||||
callbackify(fs.appendFile, args);
|
||||
},
|
||||
close = function close(...args) {
|
||||
callbackify(fs.close, args);
|
||||
close = function close(fd, callback) {
|
||||
if ($isCallable(callback)) {
|
||||
fs.close(fd).then(() => callback(), callback);
|
||||
} else if (callback == undefined) {
|
||||
fs.close(fd).then(() => {});
|
||||
} else {
|
||||
const err = new TypeError("Callback must be a function");
|
||||
err.code = "ERR_INVALID_ARG_TYPE";
|
||||
throw err;
|
||||
}
|
||||
},
|
||||
rm = function rm(...args) {
|
||||
callbackify(fs.rm, args);
|
||||
|
||||
@@ -2973,11 +2973,17 @@ describe.if(isWindows)("windows path handling", () => {
|
||||
});
|
||||
|
||||
it("using writeFile on an fd does not truncate it", () => {
|
||||
const temp = tmpdir();
|
||||
const fd = fs.openSync(join(temp, "file.txt"), "w+");
|
||||
const filepath = join(tmpdir(), `file-${Math.random().toString(32).slice(2)}.txt`);
|
||||
const fd = fs.openSync(filepath, "w+");
|
||||
fs.writeFileSync(fd, "x");
|
||||
fs.writeFileSync(fd, "x");
|
||||
fs.closeSync(fd);
|
||||
const content = fs.readFileSync(join(temp, "file.txt"), "utf8");
|
||||
const content = fs.readFileSync(filepath, "utf8");
|
||||
expect(content).toBe("xx");
|
||||
});
|
||||
|
||||
it("fs.close with one arg works", () => {
|
||||
const filepath = join(tmpdir(), `file-${Math.random().toString(32).slice(2)}.txt`);
|
||||
const fd = fs.openSync(filepath, "w+");
|
||||
fs.close(fd);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user