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:
Georgijs
2024-04-12 17:11:58 -07:00
committed by GitHub
parent d785d30eaf
commit 472bd6c7de
2 changed files with 19 additions and 5 deletions

View File

@@ -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);

View File

@@ -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);
});