summaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
Diffstat (limited to 'js')
-rw-r--r--js/deno.ts1
-rw-r--r--js/dir.ts37
-rw-r--r--js/dir_test.ts54
-rw-r--r--js/unit_tests.ts1
4 files changed, 93 insertions, 0 deletions
diff --git a/js/deno.ts b/js/deno.ts
index 212544541..565d4a7e6 100644
--- a/js/deno.ts
+++ b/js/deno.ts
@@ -2,6 +2,7 @@
// Public deno module.
/// <amd-module name="deno"/>
export { env, exit } from "./os";
+export { chdir, cwd } from "./dir";
export { File, open, stdin, stdout, stderr, read, write, close } from "./files";
export {
copy,
diff --git a/js/dir.ts b/js/dir.ts
new file mode 100644
index 000000000..4bd3e6bd7
--- /dev/null
+++ b/js/dir.ts
@@ -0,0 +1,37 @@
+// Copyright 2018 the Deno authors. All rights reserved. MIT license.
+import * as msg from "gen/msg_generated";
+import { assert } from "./util";
+import { flatbuffers } from "flatbuffers";
+import { sendSync } from "./dispatch";
+
+/**
+ * cwd() Return a string representing the current working directory.
+ * If the current directory can be reached via multiple paths
+ * (due to symbolic links), cwd() may return
+ * any one of them.
+ * throws NotFound exception if directory not available
+ */
+export function cwd(): string {
+ const builder = new flatbuffers.Builder(0);
+ msg.Cwd.startCwd(builder);
+ const inner = msg.Cwd.endCwd(builder);
+ const baseRes = sendSync(builder, msg.Any.Cwd, inner);
+ assert(baseRes != null);
+ assert(msg.Any.CwdRes === baseRes!.innerType());
+ const res = new msg.CwdRes();
+ assert(baseRes!.inner(res) != null);
+ return res.cwd()!;
+}
+
+/**
+ * chdir() Change the current working directory to path.
+ * throws NotFound exception if directory not available
+ */
+export function chdir(directory: string): void {
+ const builder = new flatbuffers.Builder();
+ const directory_ = builder.createString(directory);
+ msg.Chdir.startChdir(builder);
+ msg.Chdir.addDirectory(builder, directory_);
+ const inner = msg.Chdir.endChdir(builder);
+ sendSync(builder, msg.Any.Chdir, inner);
+}
diff --git a/js/dir_test.ts b/js/dir_test.ts
new file mode 100644
index 000000000..ff55a0e69
--- /dev/null
+++ b/js/dir_test.ts
@@ -0,0 +1,54 @@
+import { test, testPerm, assert, assertEqual } from "./test_util.ts";
+import * as deno from "deno";
+
+test(function dirCwdNotNull() {
+ assert(deno.cwd() != null);
+});
+
+testPerm({ write: true }, function dirCwdChdirSuccess() {
+ const initialdir = deno.cwd();
+ const path = deno.makeTempDirSync();
+ deno.chdir(path);
+ const current = deno.cwd();
+ if (deno.platform.os === "mac") {
+ assertEqual(current, "/private" + path);
+ } else {
+ assertEqual(current, path);
+ }
+ deno.chdir(initialdir);
+});
+
+testPerm({ write: true }, function dirCwdError() {
+ // excluding windows since it throws resource busy, while removeSync
+ if (["linux", "mac"].includes(deno.platform.os)) {
+ const initialdir = deno.cwd();
+ const path = deno.makeTempDirSync();
+ deno.chdir(path);
+ deno.removeSync(path);
+ try {
+ deno.cwd();
+ throw Error("current directory removed, should throw error");
+ } catch (err) {
+ if (err instanceof deno.DenoError) {
+ console.log(err.name === "NotFound");
+ } else {
+ throw Error("raised different exception");
+ }
+ }
+ deno.chdir(initialdir);
+ }
+});
+
+testPerm({ write: true }, function dirChdirError() {
+ const path = deno.makeTempDirSync() + "test";
+ try {
+ deno.chdir(path);
+ throw Error("directory not available, should throw error");
+ } catch (err) {
+ if (err instanceof deno.DenoError) {
+ console.log(err.name === "NotFound");
+ } else {
+ throw Error("raised different exception");
+ }
+ }
+});
diff --git a/js/unit_tests.ts b/js/unit_tests.ts
index 24fdac823..e09d0aba0 100644
--- a/js/unit_tests.ts
+++ b/js/unit_tests.ts
@@ -11,6 +11,7 @@ import "./read_dir_test.ts";
import "./write_file_test.ts";
import "./copy_file_test.ts";
import "./mkdir_test.ts";
+import "./dir_test";
import "./make_temp_dir_test.ts";
import "./stat_test.ts";
import "./rename_test.ts";