mirror of
https://github.com/denoland/deno.git
synced 2024-11-22 15:06:54 -05:00
fix(std/node) improve fs.close compatibility (#5649)
This commit is contained in:
parent
0fb5f23466
commit
62c34bc21e
2 changed files with 45 additions and 12 deletions
|
@ -3,19 +3,13 @@
|
|||
import { CallbackWithError } from "./_fs_common.ts";
|
||||
|
||||
export function close(fd: number, callback: CallbackWithError): void {
|
||||
new Promise((resolve, reject) => {
|
||||
queueMicrotask(() => {
|
||||
try {
|
||||
Deno.close(fd);
|
||||
resolve();
|
||||
callback(null);
|
||||
} catch (err) {
|
||||
reject(err);
|
||||
}
|
||||
})
|
||||
.then(() => {
|
||||
callback();
|
||||
})
|
||||
.catch((err) => {
|
||||
callback(err);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||
const { test } = Deno;
|
||||
import { fail, assert } from "../../testing/asserts.ts";
|
||||
import { fail, assert, assertThrows } from "../../testing/asserts.ts";
|
||||
import { close, closeSync } from "./_fs_close.ts";
|
||||
|
||||
test({
|
||||
|
@ -12,7 +12,7 @@ test({
|
|||
assert(Deno.resources()[file.rid]);
|
||||
await new Promise((resolve, reject) => {
|
||||
close(file.rid, (err) => {
|
||||
if (err) reject();
|
||||
if (err !== null) reject();
|
||||
else resolve();
|
||||
});
|
||||
})
|
||||
|
@ -28,6 +28,38 @@ test({
|
|||
},
|
||||
});
|
||||
|
||||
test({
|
||||
name: "ASYNC: Invalid fd",
|
||||
async fn() {
|
||||
await new Promise((resolve, reject) => {
|
||||
close(-1, (err) => {
|
||||
if (err !== null) return resolve();
|
||||
reject();
|
||||
});
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
test({
|
||||
name: "close callback should be asynchronous",
|
||||
async fn() {
|
||||
const tempFile: string = Deno.makeTempFileSync();
|
||||
const file: Deno.File = Deno.openSync(tempFile);
|
||||
|
||||
let foo: string;
|
||||
const promise = new Promise((resolve) => {
|
||||
close(file.rid, () => {
|
||||
assert(foo === "bar");
|
||||
resolve();
|
||||
});
|
||||
foo = "bar";
|
||||
});
|
||||
|
||||
await promise;
|
||||
Deno.removeSync(tempFile);
|
||||
},
|
||||
});
|
||||
|
||||
test({
|
||||
name: "SYNC: File is closed",
|
||||
fn() {
|
||||
|
@ -40,3 +72,10 @@ test({
|
|||
Deno.removeSync(tempFile);
|
||||
},
|
||||
});
|
||||
|
||||
test({
|
||||
name: "SYNC: Invalid fd",
|
||||
fn() {
|
||||
assertThrows(() => closeSync(-1));
|
||||
},
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue