mirror of
https://github.com/denoland/deno.git
synced 2024-12-04 17:18:23 -05:00
89 lines
2.4 KiB
TypeScript
89 lines
2.4 KiB
TypeScript
|
import { assertEquals, fail } from "../../testing/asserts.ts";
|
||
|
import { rmdir, rmdirSync } from "./_fs_rmdir.ts";
|
||
|
import { closeSync } from "./_fs_close.ts";
|
||
|
import { existsSync } from "../../fs/mod.ts";
|
||
|
import { join } from "../../path/mod.ts";
|
||
|
|
||
|
Deno.test({
|
||
|
name: "ASYNC: removing empty folder",
|
||
|
async fn() {
|
||
|
const dir = Deno.makeTempDirSync();
|
||
|
await new Promise((resolve, reject) => {
|
||
|
rmdir(dir, (err) => {
|
||
|
if (err) reject(err);
|
||
|
resolve();
|
||
|
});
|
||
|
})
|
||
|
.then(() => assertEquals(existsSync(dir), false))
|
||
|
.catch(() => fail())
|
||
|
.finally(() => {
|
||
|
if (existsSync(dir)) Deno.removeSync(dir);
|
||
|
});
|
||
|
},
|
||
|
});
|
||
|
|
||
|
Deno.test({
|
||
|
name: "SYNC: removing empty folder",
|
||
|
fn() {
|
||
|
const dir = Deno.makeTempDirSync();
|
||
|
rmdirSync(dir);
|
||
|
assertEquals(existsSync(dir), false);
|
||
|
},
|
||
|
});
|
||
|
|
||
|
function closeRes(before: Deno.ResourceMap, after: Deno.ResourceMap) {
|
||
|
for (const key in after) {
|
||
|
if (!before[key]) {
|
||
|
try {
|
||
|
closeSync(Number(key));
|
||
|
} catch (error) {
|
||
|
return error;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Deno.test({
|
||
|
name: "ASYNC: removing non-empty folder",
|
||
|
async fn() {
|
||
|
const rBefore = Deno.resources();
|
||
|
const dir = Deno.makeTempDirSync();
|
||
|
Deno.createSync(join(dir, "file1.txt"));
|
||
|
Deno.createSync(join(dir, "file2.txt"));
|
||
|
Deno.mkdirSync(join(dir, "some_dir"));
|
||
|
Deno.createSync(join(dir, "some_dir", "file.txt"));
|
||
|
await new Promise((resolve, reject) => {
|
||
|
rmdir(dir, { recursive: true }, (err) => {
|
||
|
if (err) reject(err);
|
||
|
resolve();
|
||
|
});
|
||
|
})
|
||
|
.then(() => assertEquals(existsSync(dir), false))
|
||
|
.catch(() => fail())
|
||
|
.finally(() => {
|
||
|
if (existsSync(dir)) Deno.removeSync(dir, { recursive: true });
|
||
|
const rAfter = Deno.resources();
|
||
|
closeRes(rBefore, rAfter);
|
||
|
});
|
||
|
},
|
||
|
ignore: Deno.build.os === "windows",
|
||
|
});
|
||
|
|
||
|
Deno.test({
|
||
|
name: "SYNC: removing non-empty folder",
|
||
|
fn() {
|
||
|
const rBefore = Deno.resources();
|
||
|
const dir = Deno.makeTempDirSync();
|
||
|
Deno.createSync(join(dir, "file1.txt"));
|
||
|
Deno.createSync(join(dir, "file2.txt"));
|
||
|
Deno.mkdirSync(join(dir, "some_dir"));
|
||
|
Deno.createSync(join(dir, "some_dir", "file.txt"));
|
||
|
rmdirSync(dir, { recursive: true });
|
||
|
assertEquals(existsSync(dir), false);
|
||
|
// closing resources
|
||
|
const rAfter = Deno.resources();
|
||
|
closeRes(rBefore, rAfter);
|
||
|
},
|
||
|
ignore: Deno.build.os === "windows",
|
||
|
});
|