summaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
authorKevin (Kun) "Kassimo" Qian <kevinkassimo@gmail.com>2018-09-10 20:40:03 -0700
committerRyan Dahl <ry@tinyclouds.org>2018-09-12 10:24:17 -0400
commit1ffae651655746b95dd40207d91ba7c360b90c40 (patch)
tree4e37fafa7f712fc25b59f67de1faeb9b7190dafa /js
parent7c50c11f40556240a3693662e2cbae6da3090b89 (diff)
Add remove(), removeAll().
and removeSync(), removeAllSync().
Diffstat (limited to 'js')
-rw-r--r--js/deno.ts8
-rw-r--r--js/remove.ts63
-rw-r--r--js/remove_test.ts336
-rw-r--r--js/stat_test.ts1
4 files changed, 401 insertions, 7 deletions
diff --git a/js/deno.ts b/js/deno.ts
index a1eb639bb..3d0352a80 100644
--- a/js/deno.ts
+++ b/js/deno.ts
@@ -1,13 +1,9 @@
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
// Public deno module.
/// <amd-module name="deno"/>
-export {
- env,
- exit,
- makeTempDirSync,
- renameSync,
-} from "./os";
+export { env, exit, makeTempDirSync, renameSync } from "./os";
export { mkdirSync, mkdir } from "./mkdir";
+export { removeSync, remove, removeAllSync, removeAll } from "./remove";
export { readFileSync, readFile } from "./read_file";
export { FileInfo, statSync, lstatSync, stat, lstat } from "./stat";
export { writeFileSync, writeFile } from "./write_file";
diff --git a/js/remove.ts b/js/remove.ts
new file mode 100644
index 000000000..921110789
--- /dev/null
+++ b/js/remove.ts
@@ -0,0 +1,63 @@
+// Copyright 2018 the Deno authors. All rights reserved. MIT license.
+import * as fbs from "gen/msg_generated";
+import { flatbuffers } from "flatbuffers";
+import * as dispatch from "./dispatch";
+
+/**
+ * Removes the named file or (empty) directory synchronously.
+ * Would throw error if permission denied, not found, or
+ * directory not empty.
+ *
+ * import { removeSync } from "deno";
+ * removeSync("/path/to/empty_dir/or/file");
+ */
+export function removeSync(path: string): void {
+ dispatch.sendSync(...req(path, false));
+}
+
+/**
+ * Removes the named file or (empty) directory.
+ * Would throw error if permission denied, not found, or
+ * directory not empty.
+ *
+ * import { remove } from "deno";
+ * await remove("/path/to/empty_dir/or/file");
+ */
+export async function remove(path: string): Promise<void> {
+ await dispatch.sendAsync(...req(path, false));
+}
+
+/**
+ * Recursively removes the named file or directory synchronously.
+ * Would throw error if permission denied or not found
+ *
+ * import { removeAllSync } from "deno";
+ * removeAllSync("/path/to/dir/or/file");
+ */
+export function removeAllSync(path: string): void {
+ dispatch.sendSync(...req(path, true));
+}
+
+/**
+ * Recursively removes the named file or directory.
+ * Would throw error if permission denied or not found
+ *
+ * import { removeAll } from "deno";
+ * await removeAll("/path/to/dir/or/file");
+ */
+export async function removeAll(path: string): Promise<void> {
+ await dispatch.sendAsync(...req(path, true));
+}
+
+function req(
+ path: string,
+ recursive: boolean
+): [flatbuffers.Builder, fbs.Any, flatbuffers.Offset] {
+ const builder = new flatbuffers.Builder();
+ const path_ = builder.createString(path);
+ fbs.Remove.startRemove(builder);
+ fbs.Remove.addPath(builder, path_);
+ fbs.Remove.addRecursive(builder, recursive);
+ const msg = fbs.Remove.endRemove(builder);
+ return [builder, fbs.Any.Remove, msg];
+}
diff --git a/js/remove_test.ts b/js/remove_test.ts
new file mode 100644
index 000000000..eb31f1ba3
--- /dev/null
+++ b/js/remove_test.ts
@@ -0,0 +1,336 @@
+// Copyright 2018 the Deno authors. All rights reserved. MIT license.
+import { testPerm, assert, assertEqual } from "./test_util.ts";
+import * as deno from "deno";
+
+// SYNC
+
+testPerm({ write: true }, function removeSyncDirSuccess() {
+ // REMOVE EMPTY DIRECTORY
+ const path = deno.makeTempDirSync() + "/dir/subdir";
+ deno.mkdirSync(path);
+ const pathInfo = deno.statSync(path);
+ assert(pathInfo.isDirectory()); // check exist first
+ deno.removeSync(path); // remove
+ // We then check again after remove
+ let err;
+ try {
+ deno.statSync(path);
+ } catch (e) {
+ err = e;
+ }
+ // Directory is gone
+ assertEqual(err.kind, deno.ErrorKind.NotFound);
+ assertEqual(err.name, "NotFound");
+});
+
+testPerm({ write: true }, function removeSyncFileSuccess() {
+ // REMOVE FILE
+ const enc = new TextEncoder();
+ const data = enc.encode("Hello");
+ const filename = deno.makeTempDirSync() + "/test.txt";
+ deno.writeFileSync(filename, data, 0o666);
+ const fileInfo = deno.statSync(filename);
+ assert(fileInfo.isFile()); // check exist first
+ deno.removeSync(filename); // remove
+ // We then check again after remove
+ let err;
+ try {
+ deno.statSync(filename);
+ } catch (e) {
+ err = e;
+ }
+ // File is gone
+ assertEqual(err.kind, deno.ErrorKind.NotFound);
+ assertEqual(err.name, "NotFound");
+});
+
+testPerm({ write: true }, function removeSyncFail() {
+ // NON-EMPTY DIRECTORY
+ const path = deno.makeTempDirSync() + "/dir/subdir";
+ const subPath = path + "/subsubdir";
+ deno.mkdirSync(path);
+ deno.mkdirSync(subPath);
+ const pathInfo = deno.statSync(path);
+ assert(pathInfo.isDirectory()); // check exist first
+ const subPathInfo = deno.statSync(subPath);
+ assert(subPathInfo.isDirectory()); // check exist first
+ let err;
+ try {
+ // Should not be able to recursively remove
+ deno.removeSync(path);
+ } catch (e) {
+ err = e;
+ }
+ // TODO(ry) Is Other really the error we should get here? What would Go do?
+ assertEqual(err.kind, deno.ErrorKind.Other);
+ assertEqual(err.name, "Other");
+ // NON-EXISTENT DIRECTORY/FILE
+ try {
+ // Non-existent
+ deno.removeSync("/baddir");
+ } catch (e) {
+ err = e;
+ }
+ assertEqual(err.kind, deno.ErrorKind.NotFound);
+ assertEqual(err.name, "NotFound");
+});
+
+testPerm({ write: false }, function removeSyncPerm() {
+ let err;
+ try {
+ deno.removeSync("/baddir");
+ } catch (e) {
+ err = e;
+ }
+ assertEqual(err.kind, deno.ErrorKind.PermissionDenied);
+ assertEqual(err.name, "PermissionDenied");
+});
+
+testPerm({ write: true }, function removeAllSyncDirSuccess() {
+ // REMOVE EMPTY DIRECTORY
+ let path = deno.makeTempDirSync() + "/dir/subdir";
+ deno.mkdirSync(path);
+ let pathInfo = deno.statSync(path);
+ assert(pathInfo.isDirectory()); // check exist first
+ deno.removeAllSync(path); // remove
+ // We then check again after remove
+ let err;
+ try {
+ deno.statSync(path);
+ } catch (e) {
+ err = e;
+ }
+ // Directory is gone
+ assertEqual(err.kind, deno.ErrorKind.NotFound);
+ assertEqual(err.name, "NotFound");
+ // REMOVE NON-EMPTY DIRECTORY
+ path = deno.makeTempDirSync() + "/dir/subdir";
+ const subPath = path + "/subsubdir";
+ deno.mkdirSync(path);
+ deno.mkdirSync(subPath);
+ pathInfo = deno.statSync(path);
+ assert(pathInfo.isDirectory()); // check exist first
+ const subPathInfo = deno.statSync(subPath);
+ assert(subPathInfo.isDirectory()); // check exist first
+ deno.removeAllSync(path); // remove
+ // We then check parent directory again after remove
+ try {
+ deno.statSync(path);
+ } catch (e) {
+ err = e;
+ }
+ // Directory is gone
+ assertEqual(err.kind, deno.ErrorKind.NotFound);
+ assertEqual(err.name, "NotFound");
+});
+
+testPerm({ write: true }, function removeAllSyncFileSuccess() {
+ // REMOVE FILE
+ const enc = new TextEncoder();
+ const data = enc.encode("Hello");
+ const filename = deno.makeTempDirSync() + "/test.txt";
+ deno.writeFileSync(filename, data, 0o666);
+ const fileInfo = deno.statSync(filename);
+ assert(fileInfo.isFile()); // check exist first
+ deno.removeAllSync(filename); // remove
+ // We then check again after remove
+ let err;
+ try {
+ deno.statSync(filename);
+ } catch (e) {
+ err = e;
+ }
+ // File is gone
+ assertEqual(err.kind, deno.ErrorKind.NotFound);
+ assertEqual(err.name, "NotFound");
+});
+
+testPerm({ write: true }, function removeAllSyncFail() {
+ // NON-EXISTENT DIRECTORY/FILE
+ let err;
+ try {
+ // Non-existent
+ deno.removeAllSync("/baddir");
+ } catch (e) {
+ err = e;
+ }
+ assertEqual(err.kind, deno.ErrorKind.NotFound);
+ assertEqual(err.name, "NotFound");
+});
+
+testPerm({ write: false }, function removeAllSyncPerm() {
+ let err;
+ try {
+ deno.removeAllSync("/baddir");
+ } catch (e) {
+ err = e;
+ }
+ assertEqual(err.kind, deno.ErrorKind.PermissionDenied);
+ assertEqual(err.name, "PermissionDenied");
+});
+
+// ASYNC
+
+testPerm({ write: true }, async function removeDirSuccess() {
+ // REMOVE EMPTY DIRECTORY
+ const path = deno.makeTempDirSync() + "/dir/subdir";
+ deno.mkdirSync(path);
+ const pathInfo = deno.statSync(path);
+ assert(pathInfo.isDirectory()); // check exist first
+ await deno.remove(path); // remove
+ // We then check again after remove
+ let err;
+ try {
+ deno.statSync(path);
+ } catch (e) {
+ err = e;
+ }
+ // Directory is gone
+ assertEqual(err.kind, deno.ErrorKind.NotFound);
+ assertEqual(err.name, "NotFound");
+});
+
+testPerm({ write: true }, async function removeFileSuccess() {
+ // REMOVE FILE
+ const enc = new TextEncoder();
+ const data = enc.encode("Hello");
+ const filename = deno.makeTempDirSync() + "/test.txt";
+ deno.writeFileSync(filename, data, 0o666);
+ const fileInfo = deno.statSync(filename);
+ assert(fileInfo.isFile()); // check exist first
+ await deno.remove(filename); // remove
+ // We then check again after remove
+ let err;
+ try {
+ deno.statSync(filename);
+ } catch (e) {
+ err = e;
+ }
+ // File is gone
+ assertEqual(err.kind, deno.ErrorKind.NotFound);
+ assertEqual(err.name, "NotFound");
+});
+
+testPerm({ write: true }, async function removeFail() {
+ // NON-EMPTY DIRECTORY
+ const path = deno.makeTempDirSync() + "/dir/subdir";
+ const subPath = path + "/subsubdir";
+ deno.mkdirSync(path);
+ deno.mkdirSync(subPath);
+ const pathInfo = deno.statSync(path);
+ assert(pathInfo.isDirectory()); // check exist first
+ const subPathInfo = deno.statSync(subPath);
+ assert(subPathInfo.isDirectory()); // check exist first
+ let err;
+ try {
+ // Should not be able to recursively remove
+ await deno.remove(path);
+ } catch (e) {
+ err = e;
+ }
+ assertEqual(err.kind, deno.ErrorKind.Other);
+ assertEqual(err.name, "Other");
+ // NON-EXISTENT DIRECTORY/FILE
+ try {
+ // Non-existent
+ await deno.remove("/baddir");
+ } catch (e) {
+ err = e;
+ }
+ assertEqual(err.kind, deno.ErrorKind.NotFound);
+ assertEqual(err.name, "NotFound");
+});
+
+testPerm({ write: false }, async function removePerm() {
+ let err;
+ try {
+ await deno.remove("/baddir");
+ } catch (e) {
+ err = e;
+ }
+ assertEqual(err.kind, deno.ErrorKind.PermissionDenied);
+ assertEqual(err.name, "PermissionDenied");
+});
+
+testPerm({ write: true }, async function removeAllDirSuccess() {
+ // REMOVE EMPTY DIRECTORY
+ let path = deno.makeTempDirSync() + "/dir/subdir";
+ deno.mkdirSync(path);
+ let pathInfo = deno.statSync(path);
+ assert(pathInfo.isDirectory()); // check exist first
+ await deno.removeAll(path); // remove
+ // We then check again after remove
+ let err;
+ try {
+ deno.statSync(path);
+ } catch (e) {
+ err = e;
+ }
+ // Directory is gone
+ assertEqual(err.kind, deno.ErrorKind.NotFound);
+ assertEqual(err.name, "NotFound");
+ // REMOVE NON-EMPTY DIRECTORY
+ path = deno.makeTempDirSync() + "/dir/subdir";
+ const subPath = path + "/subsubdir";
+ deno.mkdirSync(path);
+ deno.mkdirSync(subPath);
+ pathInfo = deno.statSync(path);
+ assert(pathInfo.isDirectory()); // check exist first
+ const subPathInfo = deno.statSync(subPath);
+ assert(subPathInfo.isDirectory()); // check exist first
+ await deno.removeAll(path); // remove
+ // We then check parent directory again after remove
+ try {
+ deno.statSync(path);
+ } catch (e) {
+ err = e;
+ }
+ // Directory is gone
+ assertEqual(err.kind, deno.ErrorKind.NotFound);
+ assertEqual(err.name, "NotFound");
+});
+
+testPerm({ write: true }, async function removeAllFileSuccess() {
+ // REMOVE FILE
+ const enc = new TextEncoder();
+ const data = enc.encode("Hello");
+ const filename = deno.makeTempDirSync() + "/test.txt";
+ deno.writeFileSync(filename, data, 0o666);
+ const fileInfo = deno.statSync(filename);
+ assert(fileInfo.isFile()); // check exist first
+ await deno.removeAll(filename); // remove
+ // We then check again after remove
+ let err;
+ try {
+ deno.statSync(filename);
+ } catch (e) {
+ err = e;
+ }
+ // File is gone
+ assertEqual(err.kind, deno.ErrorKind.NotFound);
+ assertEqual(err.name, "NotFound");
+});
+
+testPerm({ write: true }, async function removeAllFail() {
+ // NON-EXISTENT DIRECTORY/FILE
+ let err;
+ try {
+ // Non-existent
+ await deno.removeAll("/baddir");
+ } catch (e) {
+ err = e;
+ }
+ assertEqual(err.kind, deno.ErrorKind.NotFound);
+ assertEqual(err.name, "NotFound");
+});
+
+testPerm({ write: false }, async function removeAllPerm() {
+ let err;
+ try {
+ await deno.removeAll("/baddir");
+ } catch (e) {
+ err = e;
+ }
+ assertEqual(err.kind, deno.ErrorKind.PermissionDenied);
+ assertEqual(err.name, "PermissionDenied");
+});
diff --git a/js/stat_test.ts b/js/stat_test.ts
index c811b23ab..424077dc5 100644
--- a/js/stat_test.ts
+++ b/js/stat_test.ts
@@ -123,4 +123,3 @@ test(async function lstatNotFound() {
assert(caughtError);
assertEqual(badInfo, undefined);
});
-