2020-03-14 23:01:34 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-09-27 06:22:32 -04:00
|
|
|
import { assert, assertThrows, fail } from "../../testing/asserts.ts";
|
2020-03-14 23:01:34 -04:00
|
|
|
import { close, closeSync } from "./_fs_close.ts";
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-03-14 23:01:34 -04:00
|
|
|
name: "ASYNC: File is closed",
|
|
|
|
async fn() {
|
|
|
|
const tempFile: string = await Deno.makeTempFile();
|
|
|
|
const file: Deno.File = await Deno.open(tempFile);
|
|
|
|
|
|
|
|
assert(Deno.resources()[file.rid]);
|
2020-11-23 17:31:10 -05:00
|
|
|
await new Promise<void>((resolve, reject) => {
|
2020-03-28 13:03:49 -04:00
|
|
|
close(file.rid, (err) => {
|
2020-05-19 19:01:06 -04:00
|
|
|
if (err !== null) reject();
|
2020-03-14 23:01:34 -04:00
|
|
|
else resolve();
|
|
|
|
});
|
|
|
|
})
|
2020-03-20 09:38:34 -04:00
|
|
|
.then(() => {
|
2020-03-14 23:01:34 -04:00
|
|
|
assert(!Deno.resources()[file.rid]);
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
fail("No error expected");
|
|
|
|
})
|
|
|
|
.finally(async () => {
|
|
|
|
await Deno.remove(tempFile);
|
|
|
|
});
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-03-14 23:01:34 -04:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-05-19 19:01:06 -04:00
|
|
|
name: "ASYNC: Invalid fd",
|
|
|
|
async fn() {
|
2020-11-23 17:31:10 -05:00
|
|
|
await new Promise<void>((resolve, reject) => {
|
2020-05-19 19:01:06 -04:00
|
|
|
close(-1, (err) => {
|
|
|
|
if (err !== null) return resolve();
|
|
|
|
reject();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-05-19 19:01:06 -04:00
|
|
|
name: "close callback should be asynchronous",
|
|
|
|
async fn() {
|
|
|
|
const tempFile: string = Deno.makeTempFileSync();
|
|
|
|
const file: Deno.File = Deno.openSync(tempFile);
|
|
|
|
|
|
|
|
let foo: string;
|
2020-11-23 17:31:10 -05:00
|
|
|
const promise = new Promise<void>((resolve) => {
|
2020-05-19 19:01:06 -04:00
|
|
|
close(file.rid, () => {
|
|
|
|
assert(foo === "bar");
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
foo = "bar";
|
|
|
|
});
|
|
|
|
|
|
|
|
await promise;
|
|
|
|
Deno.removeSync(tempFile);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-03-14 23:01:34 -04:00
|
|
|
name: "SYNC: File is closed",
|
|
|
|
fn() {
|
|
|
|
const tempFile: string = Deno.makeTempFileSync();
|
|
|
|
const file: Deno.File = Deno.openSync(tempFile);
|
|
|
|
|
|
|
|
assert(Deno.resources()[file.rid]);
|
|
|
|
closeSync(file.rid);
|
|
|
|
assert(!Deno.resources()[file.rid]);
|
|
|
|
Deno.removeSync(tempFile);
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-03-14 23:01:34 -04:00
|
|
|
});
|
2020-05-19 19:01:06 -04:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-05-19 19:01:06 -04:00
|
|
|
name: "SYNC: Invalid fd",
|
|
|
|
fn() {
|
|
|
|
assertThrows(() => closeSync(-1));
|
|
|
|
},
|
|
|
|
});
|