summaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
authorMani Maghsoudlou <manidlou@gmail.com>2018-09-12 08:44:58 -0700
committerRyan Dahl <ry@tinyclouds.org>2018-09-12 11:44:58 -0400
commit88d42f0b189a1daa3fb44eb24ecad77823557d9f (patch)
treeac4d166271d9708472b16150f5f6dcc712095fcb /js
parent26081a32dfaf34fdc8b6cf53222c15f3d4e4f30d (diff)
Implement deno.rename() (#731)
Diffstat (limited to 'js')
-rw-r--r--js/deno.ts7
-rw-r--r--js/os.ts19
-rw-r--r--js/os_test.ts37
-rw-r--r--js/rename.ts42
-rw-r--r--js/rename_test.ts60
-rw-r--r--js/unit_tests.ts1
6 files changed, 109 insertions, 57 deletions
diff --git a/js/deno.ts b/js/deno.ts
index 3d0352a80..6f71ee499 100644
--- a/js/deno.ts
+++ b/js/deno.ts
@@ -1,10 +1,15 @@
// 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
+} from "./os";
export { mkdirSync, mkdir } from "./mkdir";
export { removeSync, remove, removeAllSync, removeAll } from "./remove";
export { readFileSync, readFile } from "./read_file";
+export { renameSync, rename } from "./rename";
export { FileInfo, statSync, lstatSync, stat, lstat } from "./stat";
export { writeFileSync, writeFile } from "./write_file";
export { ErrorKind, DenoError } from "./errors";
diff --git a/js/os.ts b/js/os.ts
index 264cdcbaf..d73865c06 100644
--- a/js/os.ts
+++ b/js/os.ts
@@ -164,22 +164,3 @@ export function env(): { [index: string]: string } {
// TypeScript cannot track assertion above, therefore not null assertion
return createEnv(res);
}
-
-/**
- * Renames (moves) oldpath to newpath.
- * import { renameSync } from "deno";
- * const oldpath = 'from/path';
- * const newpath = 'to/path';
- *
- * renameSync(oldpath, newpath);
- */
-export function renameSync(oldpath: string, newpath: string): void {
- const builder = new flatbuffers.Builder();
- const _oldpath = builder.createString(oldpath);
- const _newpath = builder.createString(newpath);
- fbs.RenameSync.startRenameSync(builder);
- fbs.RenameSync.addOldpath(builder, _oldpath);
- fbs.RenameSync.addNewpath(builder, _newpath);
- const msg = fbs.RenameSync.endRenameSync(builder);
- sendSync(builder, fbs.Any.RenameSync, msg);
-}
diff --git a/js/os_test.ts b/js/os_test.ts
index c0198095c..61227a3b1 100644
--- a/js/os_test.ts
+++ b/js/os_test.ts
@@ -60,40 +60,3 @@ test(function makeTempDirSyncPerm() {
assertEqual(err.kind, deno.ErrorKind.PermissionDenied);
assertEqual(err.name, "PermissionDenied");
});
-
-testPerm({ write: true }, function renameSync() {
- const testDir = deno.makeTempDirSync() + "/test-rename";
- const oldpath = testDir + "/oldpath";
- const newpath = testDir + "/newpath";
- deno.mkdirSync(oldpath);
- deno.renameSync(oldpath, newpath);
- const newPathInfo = deno.statSync(newpath);
- assert(newPathInfo.isDirectory());
-
- let caughtErr = false;
- let oldPathInfo;
-
- try {
- oldPathInfo = deno.statSync(oldpath);
- } catch (err) {
- caughtErr = true;
- assertEqual(err.kind, deno.ErrorKind.NotFound);
- assertEqual(err.name, "NotFound");
- }
-
- assert(caughtErr);
- assertEqual(oldPathInfo, undefined);
-});
-
-test(function renameSyncPerm() {
- let err;
- try {
- const oldpath = "/oldbaddir";
- const newpath = "/newbaddir";
- deno.renameSync(oldpath, newpath);
- } catch (err_) {
- err = err_;
- }
- assertEqual(err.kind, deno.ErrorKind.PermissionDenied);
- assertEqual(err.name, "PermissionDenied");
-});
diff --git a/js/rename.ts b/js/rename.ts
new file mode 100644
index 000000000..241e27490
--- /dev/null
+++ b/js/rename.ts
@@ -0,0 +1,42 @@
+// 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";
+
+/**
+ * Synchronously renames (moves) oldpath to newpath. If newpath already exists
+ * and is not a directory, Rename replaces it. OS-specific restrictions may
+ * apply when oldpath and newpath are in different directories.
+ *
+ * import { renameSync } from "deno";
+ * renameSync("old/path", "new/path");
+ */
+export function renameSync(oldpath: string, newpath: string): void {
+ dispatch.sendSync(...req(oldpath, newpath));
+}
+
+/**
+ * Renames (moves) oldpath to newpath. If newpath already exists
+ * and is not a directory, Rename replaces it. OS-specific restrictions may
+ * apply when oldpath and newpath are in different directories.
+ *
+ * import { rename } from "deno";
+ * await rename("old/path", "new/path");
+ */
+export async function rename(oldpath: string, newpath: string): Promise<void> {
+ await dispatch.sendAsync(...req(oldpath, newpath));
+}
+
+function req(
+ oldpath: string,
+ newpath: string
+): [flatbuffers.Builder, fbs.Any, flatbuffers.Offset] {
+ const builder = new flatbuffers.Builder();
+ const oldpath_ = builder.createString(oldpath);
+ const newpath_ = builder.createString(newpath);
+ fbs.Rename.startRename(builder);
+ fbs.Rename.addOldpath(builder, oldpath_);
+ fbs.Rename.addNewpath(builder, newpath_);
+ const msg = fbs.Rename.endRename(builder);
+ return [builder, fbs.Any.Rename, msg];
+}
diff --git a/js/rename_test.ts b/js/rename_test.ts
new file mode 100644
index 000000000..450760dae
--- /dev/null
+++ b/js/rename_test.ts
@@ -0,0 +1,60 @@
+// Copyright 2018 the Deno authors. All rights reserved. MIT license.
+import { test, testPerm, assert, assertEqual } from "./test_util.ts";
+import * as deno from "deno";
+
+testPerm({ write: true }, function renameSyncSuccess() {
+ const testDir = deno.makeTempDirSync() + "/test-rename-sync";
+ const oldpath = testDir + "/oldpath";
+ const newpath = testDir + "/newpath";
+ deno.mkdirSync(oldpath);
+ deno.renameSync(oldpath, newpath);
+ const newPathInfo = deno.statSync(newpath);
+ assert(newPathInfo.isDirectory());
+
+ let caughtErr = false;
+ let oldPathInfo;
+
+ try {
+ oldPathInfo = deno.statSync(oldpath);
+ } catch (e) {
+ caughtErr = true;
+ assertEqual(e.kind, deno.ErrorKind.NotFound);
+ }
+ assert(caughtErr);
+ assertEqual(oldPathInfo, undefined);
+});
+
+testPerm({ write: false }, function renameSyncPerm() {
+ let err;
+ try {
+ const oldpath = "/oldbaddir";
+ const newpath = "/newbaddir";
+ deno.renameSync(oldpath, newpath);
+ } catch (e) {
+ err = e;
+ }
+ assertEqual(err.kind, deno.ErrorKind.PermissionDenied);
+ assertEqual(err.name, "PermissionDenied");
+});
+
+testPerm({ write: true }, async function renameSuccess() {
+ const testDir = deno.makeTempDirSync() + "/test-rename";
+ const oldpath = testDir + "/oldpath";
+ const newpath = testDir + "/newpath";
+ deno.mkdirSync(oldpath);
+ await deno.rename(oldpath, newpath);
+ const newPathInfo = deno.statSync(newpath);
+ assert(newPathInfo.isDirectory());
+
+ let caughtErr = false;
+ let oldPathInfo;
+
+ try {
+ oldPathInfo = deno.statSync(oldpath);
+ } catch (e) {
+ caughtErr = true;
+ assertEqual(e.kind, deno.ErrorKind.NotFound);
+ }
+ assert(caughtErr);
+ assertEqual(oldPathInfo, undefined);
+});
diff --git a/js/unit_tests.ts b/js/unit_tests.ts
index 88e6fb9d9..b406b63d0 100644
--- a/js/unit_tests.ts
+++ b/js/unit_tests.ts
@@ -9,3 +9,4 @@ import "./read_file_test.ts";
import "./write_file_test.ts";
import "./mkdir_test.ts";
import "./stat_test.ts";
+import "./rename_test.ts";