summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAxetroy <troy450409405@gmail.com>2019-03-12 02:19:52 +0800
committerRyan Dahl <ry@tinyclouds.org>2019-03-11 14:19:52 -0400
commitd4ba2978a6e1c2a38a98ad95d0915e492e5a3621 (patch)
treefebcf4fbb5cf4fe71843a7f254da7012600b7f53
parentef6d93235800161343281972e9bdc1b6b69e310e (diff)
feat: add emptyDir for fs modules (denoland/deno_std#263)
Original: https://github.com/denoland/deno_std/commit/64d6bfca565acda9151ebb0b84911cc8d63c8c77
-rw-r--r--fs/empty_dir.ts52
-rw-r--r--fs/empty_dir_test.ts117
-rwxr-xr-xtest.ts1
3 files changed, 170 insertions, 0 deletions
diff --git a/fs/empty_dir.ts b/fs/empty_dir.ts
new file mode 100644
index 000000000..2ca9efb0c
--- /dev/null
+++ b/fs/empty_dir.ts
@@ -0,0 +1,52 @@
+// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
+/**
+ * Ensures that a directory is empty.
+ * Deletes directory contents if the directory is not empty.
+ * If the directory does not exist, it is created.
+ * The directory itself is not deleted.
+ * @export
+ * @param {string} dir
+ * @returns {Promise<void>}
+ */
+export async function emptyDir(dir: string): Promise<void> {
+ let items: Deno.FileInfo[] = [];
+ try {
+ items = await Deno.readDir(dir);
+ } catch {
+ // if not exist. then create it
+ await Deno.mkdir(dir, true);
+ return;
+ }
+ while (items.length) {
+ const item = items.shift();
+ if (item && item.path) {
+ await Deno.remove(item.path, { recursive: true });
+ }
+ }
+}
+
+/**
+ * Ensures that a directory is empty.
+ * Deletes directory contents if the directory is not empty.
+ * If the directory does not exist, it is created.
+ * The directory itself is not deleted.
+ * @export
+ * @param {string} dir
+ * @returns {void}
+ */
+export function emptyDirSync(dir: string): void {
+ let items: Deno.FileInfo[] = [];
+ try {
+ items = Deno.readDirSync(dir);
+ } catch {
+ // if not exist. then create it
+ Deno.mkdirSync(dir, true);
+ return;
+ }
+ while (items.length) {
+ const item = items.shift();
+ if (item && item.path) {
+ Deno.removeSync(item.path, { recursive: true });
+ }
+ }
+}
diff --git a/fs/empty_dir_test.ts b/fs/empty_dir_test.ts
new file mode 100644
index 000000000..d4a0de77a
--- /dev/null
+++ b/fs/empty_dir_test.ts
@@ -0,0 +1,117 @@
+// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
+import { test } from "../testing/mod.ts";
+import { assertEquals, assertThrowsAsync } from "../testing/asserts.ts";
+import { emptyDir, emptyDirSync } from "./empty_dir.ts";
+import * as path from "./path/mod.ts";
+
+const testdataDir = path.resolve("fs", "testdata");
+
+test(async function emptyDirIfItNotExist() {
+ const testDir = path.join(testdataDir, "empty_dir_test_1");
+ const testNestDir = path.join(testDir, "nest");
+ // empty a dir which not exist. then it will create new one
+ await emptyDir(testNestDir);
+
+ try {
+ // check the dir
+ const stat = await Deno.stat(testNestDir);
+ assertEquals(stat.isDirectory(), true);
+ } finally {
+ // remove the test dir
+ Deno.remove(testDir, { recursive: true });
+ }
+});
+
+test(function emptyDirSyncIfItNotExist() {
+ const testDir = path.join(testdataDir, "empty_dir_test_2");
+ const testNestDir = path.join(testDir, "nest");
+ // empty a dir which not exist. then it will create new one
+ emptyDirSync(testNestDir);
+
+ try {
+ // check the dir
+ const stat = Deno.statSync(testNestDir);
+ assertEquals(stat.isDirectory(), true);
+ } finally {
+ // remove the test dir
+ Deno.remove(testDir, { recursive: true });
+ }
+});
+
+test(async function emptyDirIfItExist() {
+ const testDir = path.join(testdataDir, "empty_dir_test_3");
+ const testNestDir = path.join(testDir, "nest");
+ // create test dir
+ await emptyDir(testNestDir);
+ const testDirFile = path.join(testNestDir, "test.ts");
+ // create test file in test dir
+ await Deno.writeFile(testDirFile, new Uint8Array());
+
+ // before empty: make sure file/directory exist
+ const beforeFileStat = await Deno.stat(testDirFile);
+ assertEquals(beforeFileStat.isFile(), true);
+
+ const beforeDirStat = await Deno.stat(testNestDir);
+ assertEquals(beforeDirStat.isDirectory(), true);
+
+ await emptyDir(testDir);
+
+ // after empty: file/directory have already remove
+ try {
+ // test dir still there
+ const stat = await Deno.stat(testDir);
+ assertEquals(stat.isDirectory(), true);
+
+ // nest directory have been remove
+ assertThrowsAsync(async () => {
+ await Deno.stat(testNestDir);
+ });
+
+ // test file have been remove
+ assertThrowsAsync(async () => {
+ await Deno.stat(testDirFile);
+ });
+ } finally {
+ // remote test dir
+ await Deno.remove(testDir, { recursive: true });
+ }
+});
+
+test(function emptyDirSyncIfItExist() {
+ const testDir = path.join(testdataDir, "empty_dir_test_4");
+ const testNestDir = path.join(testDir, "nest");
+ // create test dir
+ emptyDirSync(testNestDir);
+ const testDirFile = path.join(testNestDir, "test.ts");
+ // create test file in test dir
+ Deno.writeFileSync(testDirFile, new Uint8Array());
+
+ // before empty: make sure file/directory exist
+ const beforeFileStat = Deno.statSync(testDirFile);
+ assertEquals(beforeFileStat.isFile(), true);
+
+ const beforeDirStat = Deno.statSync(testNestDir);
+ assertEquals(beforeDirStat.isDirectory(), true);
+
+ emptyDirSync(testDir);
+
+ // after empty: file/directory have already remove
+ try {
+ // test dir still there
+ const stat = Deno.statSync(testDir);
+ assertEquals(stat.isDirectory(), true);
+
+ // nest directory have been remove
+ assertThrowsAsync(async () => {
+ Deno.statSync(testNestDir);
+ });
+
+ // test file have been remove
+ assertThrowsAsync(async () => {
+ Deno.statSync(testDirFile);
+ });
+ } finally {
+ // remote test dir
+ Deno.removeSync(testDir, { recursive: true });
+ }
+});
diff --git a/test.ts b/test.ts
index 63e5ded7b..4c51943d3 100755
--- a/test.ts
+++ b/test.ts
@@ -15,6 +15,7 @@ import "./fs/walk_test.ts";
import "./fs/globrex_test.ts";
import "./fs/glob_test.ts";
import "./fs/exists_test.ts";
+import "./fs/empty_dir_test.ts";
import "./io/test.ts";
import "./http/server_test.ts";
import "./http/file_server_test.ts";