summaryrefslogtreecommitdiff
path: root/cli/js
diff options
context:
space:
mode:
authorKevin (Kun) "Kassimo" Qian <kevinkassimo@gmail.com>2019-11-26 00:40:57 -0800
committerRy Dahl <ry@tinyclouds.org>2019-11-26 00:40:57 -0800
commitf88dc4e197f36842b13843dd88da3d74d67578e5 (patch)
treeb74a0da664bf0c126c345efa73815ea96db0b70b /cli/js
parent658ec2aaf9c7e0d0b4ded4e97a3d89dc2fa25806 (diff)
Add Deno.realpath (#3404)
Diffstat (limited to 'cli/js')
-rw-r--r--cli/js/deno.ts1
-rw-r--r--cli/js/dispatch.ts2
-rw-r--r--cli/js/lib.deno_runtime.d.ts15
-rw-r--r--cli/js/realpath.ts19
-rw-r--r--cli/js/realpath_test.ts91
-rw-r--r--cli/js/unit_tests.ts1
6 files changed, 129 insertions, 0 deletions
diff --git a/cli/js/deno.ts b/cli/js/deno.ts
index 2a7274727..6f07bef67 100644
--- a/cli/js/deno.ts
+++ b/cli/js/deno.ts
@@ -56,6 +56,7 @@ export { chownSync, chown } from "./chown.ts";
export { utimeSync, utime } from "./utime.ts";
export { removeSync, remove, RemoveOption } from "./remove.ts";
export { renameSync, rename } from "./rename.ts";
+export { realpathSync, realpath } from "./realpath.ts";
export { readFileSync, readFile } from "./read_file.ts";
export { readDirSync, readDir } from "./read_dir.ts";
export { copyFileSync, copyFile } from "./copy_file.ts";
diff --git a/cli/js/dispatch.ts b/cli/js/dispatch.ts
index c2690ad32..35806c3ad 100644
--- a/cli/js/dispatch.ts
+++ b/cli/js/dispatch.ts
@@ -55,6 +55,7 @@ export let OP_CHOWN: number;
export let OP_REMOVE: number;
export let OP_COPY_FILE: number;
export let OP_STAT: number;
+export let OP_REALPATH: number;
export let OP_READ_DIR: number;
export let OP_RENAME: number;
export let OP_LINK: number;
@@ -97,6 +98,7 @@ export function asyncMsgFromRust(opId: number, ui8: Uint8Array): void {
case OP_REMOVE:
case OP_COPY_FILE:
case OP_STAT:
+ case OP_REALPATH:
case OP_READ_DIR:
case OP_RENAME:
case OP_LINK:
diff --git a/cli/js/lib.deno_runtime.d.ts b/cli/js/lib.deno_runtime.d.ts
index 3fdc28a54..fb7767aa6 100644
--- a/cli/js/lib.deno_runtime.d.ts
+++ b/cli/js/lib.deno_runtime.d.ts
@@ -605,6 +605,21 @@ declare namespace Deno {
isSymlink(): boolean;
}
+ // @url js/realpath.d.ts
+
+ /** Returns absolute normalized path with symbolic links resolved
+ * synchronously.
+ *
+ * const realPath = Deno.realpathSync("./some/path");
+ */
+ export function realpathSync(path: string): string;
+
+ /** Returns absolute normalized path with symbolic links resolved.
+ *
+ * const realPath = await Deno.realpath("./some/path");
+ */
+ export function realpath(path: string): Promise<string>;
+
// @url js/read_dir.d.ts
/** Reads the directory given by path and returns a list of file info
diff --git a/cli/js/realpath.ts b/cli/js/realpath.ts
new file mode 100644
index 000000000..c17a0f564
--- /dev/null
+++ b/cli/js/realpath.ts
@@ -0,0 +1,19 @@
+// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
+import { sendSync, sendAsync } from "./dispatch_json.ts";
+import * as dispatch from "./dispatch.ts";
+
+/** Returns absolute normalized path with symbolic links resolved synchronously.
+ *
+ * const realPath = Deno.realpathSync("./some/path");
+ */
+export function realpathSync(path: string): string {
+ return sendSync(dispatch.OP_REALPATH, { path });
+}
+
+/** Returns absolute normalized path with symbolic links resolved.
+ *
+ * const realPath = await Deno.realpath("./some/path");
+ */
+export async function realpath(path: string): Promise<string> {
+ return await sendAsync(dispatch.OP_REALPATH, { path });
+}
diff --git a/cli/js/realpath_test.ts b/cli/js/realpath_test.ts
new file mode 100644
index 000000000..1dc976578
--- /dev/null
+++ b/cli/js/realpath_test.ts
@@ -0,0 +1,91 @@
+// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
+import { testPerm, assert, assertEquals } from "./test_util.ts";
+
+testPerm({ read: true }, function realpathSyncSuccess(): void {
+ const incompletePath = "cli/tests/fixture.json";
+ const realPath = Deno.realpathSync(incompletePath);
+ assert(realPath.startsWith("/"));
+ assert(realPath.endsWith(incompletePath));
+});
+
+if (Deno.build.os !== "win") {
+ testPerm({ read: true, write: true }, function realpathSyncSymlink(): void {
+ const testDir = Deno.makeTempDirSync();
+ const target = testDir + "/target";
+ const symlink = testDir + "/symln";
+ Deno.mkdirSync(target);
+ Deno.symlinkSync(target, symlink);
+ const targetPath = Deno.realpathSync(symlink);
+ assert(targetPath.startsWith("/"));
+ assert(targetPath.endsWith("/target"));
+ });
+}
+
+testPerm({ read: false }, function realpathSyncPerm(): void {
+ let caughtError = false;
+ try {
+ Deno.realpathSync("some_file");
+ } catch (e) {
+ caughtError = true;
+ assertEquals(e.kind, Deno.ErrorKind.PermissionDenied);
+ assertEquals(e.name, "PermissionDenied");
+ }
+ assert(caughtError);
+});
+
+testPerm({ read: true }, function realpathSyncNotFound(): void {
+ let caughtError = false;
+ try {
+ Deno.realpathSync("bad_filename");
+ } catch (e) {
+ caughtError = true;
+ assertEquals(e.kind, Deno.ErrorKind.NotFound);
+ }
+ assert(caughtError);
+});
+
+testPerm({ read: true }, async function realpathSuccess(): Promise<void> {
+ const incompletePath = "cli/tests/fixture.json";
+ const realPath = await Deno.realpath(incompletePath);
+ assert(realPath.startsWith("/"));
+ assert(realPath.endsWith(incompletePath));
+});
+
+if (Deno.build.os !== "win") {
+ testPerm(
+ { read: true, write: true },
+ async function realpathSymlink(): Promise<void> {
+ const testDir = Deno.makeTempDirSync();
+ const target = testDir + "/target";
+ const symlink = testDir + "/symln";
+ Deno.mkdirSync(target);
+ Deno.symlinkSync(target, symlink);
+ const targetPath = await Deno.realpath(symlink);
+ assert(targetPath.startsWith("/"));
+ assert(targetPath.endsWith("/target"));
+ }
+ );
+}
+
+testPerm({ read: false }, async function realpathPerm(): Promise<void> {
+ let caughtError = false;
+ try {
+ await Deno.realpath("some_file");
+ } catch (e) {
+ caughtError = true;
+ assertEquals(e.kind, Deno.ErrorKind.PermissionDenied);
+ assertEquals(e.name, "PermissionDenied");
+ }
+ assert(caughtError);
+});
+
+testPerm({ read: true }, async function realpathNotFound(): Promise<void> {
+ let caughtError = false;
+ try {
+ await Deno.realpath("bad_filename");
+ } catch (e) {
+ caughtError = true;
+ assertEquals(e.kind, Deno.ErrorKind.NotFound);
+ }
+ assert(caughtError);
+});
diff --git a/cli/js/unit_tests.ts b/cli/js/unit_tests.ts
index b5d85fcc8..c63fc5f26 100644
--- a/cli/js/unit_tests.ts
+++ b/cli/js/unit_tests.ts
@@ -33,6 +33,7 @@ import "./mkdir_test.ts";
import "./net_test.ts";
import "./os_test.ts";
import "./process_test.ts";
+import "./realpath_test.ts";
import "./read_dir_test.ts";
import "./read_file_test.ts";
import "./read_link_test.ts";