1
0
Fork 0
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:
Marcos Casagrande 2020-05-20 01:01:06 +02:00 committed by GitHub
parent 0fb5f23466
commit 62c34bc21e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 12 deletions

View file

@ -3,20 +3,14 @@
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);
});
}
});
}
export function closeSync(fd: number): void {

View file

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