summaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
authorMani Maghsoudlou <manidlou@gmail.com>2018-09-24 21:20:49 -0700
committerRyan Dahl <ry@tinyclouds.org>2018-09-25 00:20:49 -0400
commitad5065e23ec33af1422eeffdbb877ef8fd5f6da4 (patch)
tree5b90e2c1e59ad97ef0aa3f93868f80c90b8a365b /js
parentd957f8ebc24bcbdb94c0c5297c0072aa8d2ebec8 (diff)
Implement deno.readlink() (#797)
Diffstat (limited to 'js')
-rw-r--r--js/deno.ts3
-rw-r--r--js/read_link.ts46
-rw-r--r--js/read_link_test.ts44
-rw-r--r--js/unit_tests.ts1
4 files changed, 93 insertions, 1 deletions
diff --git a/js/deno.ts b/js/deno.ts
index dd6539163..3bbda69e8 100644
--- a/js/deno.ts
+++ b/js/deno.ts
@@ -5,8 +5,9 @@ export { env, exit } from "./os";
export { mkdirSync, mkdir } from "./mkdir";
export { makeTempDirSync, makeTempDir } from "./make_temp_dir";
export { removeSync, remove, removeAllSync, removeAll } from "./remove";
-export { readFileSync, readFile } from "./read_file";
export { renameSync, rename } from "./rename";
+export { readFileSync, readFile } from "./read_file";
+export { readlinkSync, readlink } from "./read_link";
export { FileInfo, statSync, lstatSync, stat, lstat } from "./stat";
export { symlinkSync, symlink } from "./symlink";
export { writeFileSync, writeFile } from "./write_file";
diff --git a/js/read_link.ts b/js/read_link.ts
new file mode 100644
index 000000000..6bd613389
--- /dev/null
+++ b/js/read_link.ts
@@ -0,0 +1,46 @@
+// Copyright 2018 the Deno authors. All rights reserved. MIT license.
+import * as fbs from "gen/msg_generated";
+import { flatbuffers } from "flatbuffers";
+import { assert } from "./util";
+import * as dispatch from "./dispatch";
+
+/**
+ * Returns the destination of the named symbolic link synchronously.
+ *
+ * import { readlinkSync } from "deno";
+ * const targetPath = readlinkSync("symlink/path");
+ */
+export function readlinkSync(name: string): string {
+ return res(dispatch.sendSync(...req(name)));
+}
+
+/**
+ * Returns the destination of the named symbolic link.
+ *
+ * import { readlink } from "deno";
+ * const targetPath = await readlink("symlink/path");
+ */
+export async function readlink(name: string): Promise<string> {
+ return res(await dispatch.sendAsync(...req(name)));
+}
+
+function req(
+ name: string
+): [flatbuffers.Builder, fbs.Any, flatbuffers.Offset] {
+ const builder = new flatbuffers.Builder();
+ const name_ = builder.createString(name);
+ fbs.Readlink.startReadlink(builder);
+ fbs.Readlink.addName(builder, name_);
+ const msg = fbs.Readlink.endReadlink(builder);
+ return [builder, fbs.Any.Readlink, msg];
+}
+
+function res(baseRes: null | fbs.Base): string {
+ assert(baseRes !== null);
+ assert(fbs.Any.ReadlinkRes === baseRes!.msgType());
+ const res = new fbs.ReadlinkRes();
+ assert(baseRes!.msg(res) !== null);
+ const path = res.path();
+ assert(path !== null);
+ return path!;
+}
diff --git a/js/read_link_test.ts b/js/read_link_test.ts
new file mode 100644
index 000000000..2df6b30d0
--- /dev/null
+++ b/js/read_link_test.ts
@@ -0,0 +1,44 @@
+// 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 readlinkSyncSuccess() {
+ const testDir = deno.makeTempDirSync() + "/test-readlink-sync";
+ const target = testDir + "/target";
+ const symlink = testDir + "/symln";
+ deno.mkdirSync(target);
+ // TODO Add test for Windows once symlink is implemented for Windows.
+ // See https://github.com/denoland/deno/issues/815.
+ if (deno.platform !== "win32") {
+ deno.symlinkSync(target, symlink);
+ const targetPath = deno.readlinkSync(symlink);
+ assertEqual(targetPath, target);
+ }
+});
+
+test(function readlinkSyncNotFound() {
+ let caughtError = false;
+ let data;
+ try {
+ data = deno.readlinkSync("bad_filename");
+ } catch (e) {
+ caughtError = true;
+ assertEqual(e.kind, deno.ErrorKind.NotFound);
+ }
+ assert(caughtError);
+ assertEqual(data, undefined);
+});
+
+testPerm({ write: true }, async function readlinkSuccess() {
+ const testDir = deno.makeTempDirSync() + "/test-readlink";
+ const target = testDir + "/target";
+ const symlink = testDir + "/symln";
+ deno.mkdirSync(target);
+ // TODO Add test for Windows once symlink is implemented for Windows.
+ // See https://github.com/denoland/deno/issues/815.
+ if (deno.platform !== "win32") {
+ deno.symlinkSync(target, symlink);
+ const targetPath = await deno.readlink(symlink);
+ assertEqual(targetPath, target);
+ }
+});
diff --git a/js/unit_tests.ts b/js/unit_tests.ts
index ae9db2a28..578a65c6d 100644
--- a/js/unit_tests.ts
+++ b/js/unit_tests.ts
@@ -11,6 +11,7 @@ import "./mkdir_test.ts";
import "./make_temp_dir_test.ts";
import "./stat_test.ts";
import "./rename_test.ts";
+import "./read_link_test.ts";
import "./blob_test.ts";
import "./timers_test.ts";
import "./symlink_test.ts";