summaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
authorAaron Power <theaaronepower@gmail.com>2018-08-31 12:51:12 +0100
committerRyan Dahl <ry@tinyclouds.org>2018-08-31 13:18:24 -0400
commitf131445a46555f1634aecae0fc1d4979b4cefa6d (patch)
treefb9847c7f1b6aebc98bc8ef788366dd90b3f9b12 /js
parent45dafe15ee87b34d0c3c9b4bc72905c176514051 (diff)
Implemented deno.env and refactored flags.rs
Diffstat (limited to 'js')
-rw-r--r--js/deno.ts1
-rw-r--r--js/os.ts58
-rw-r--r--js/os_test.ts21
-rw-r--r--js/test_util.ts21
4 files changed, 93 insertions, 8 deletions
diff --git a/js/deno.ts b/js/deno.ts
index e24345ccd..c9611d116 100644
--- a/js/deno.ts
+++ b/js/deno.ts
@@ -1,6 +1,7 @@
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
// Public deno module.
export {
+ env,
exit,
FileInfo,
makeTempDirSync,
diff --git a/js/os.ts b/js/os.ts
index 8730a6ddf..e98f1557d 100644
--- a/js/os.ts
+++ b/js/os.ts
@@ -128,6 +128,64 @@ export function readFileSync(filename: string): Uint8Array {
return new Uint8Array(dataArray!);
}
+function createEnv(_msg: fbs.EnvironRes): { [index:string]: string } {
+ const env: { [index:string]: string } = {};
+
+ for (let i = 0; i < _msg.mapLength(); i++) {
+ const item = _msg.map(i)!;
+
+ env[item.key()!] = item.value()!;
+ }
+
+ return new Proxy(env, {
+ set(obj, prop: string, value: string | number) {
+ setEnv(prop, value.toString());
+ return Reflect.set(obj, prop, value);
+ }
+ });
+}
+
+function setEnv(key: string, value: string): void {
+ const builder = new flatbuffers.Builder();
+ const _key = builder.createString(key);
+ const _value = builder.createString(value);
+ fbs.SetEnv.startSetEnv(builder);
+ fbs.SetEnv.addKey(builder, _key);
+ fbs.SetEnv.addValue(builder, _value);
+ const msg = fbs.SetEnv.endSetEnv(builder);
+ send(builder, fbs.Any.SetEnv, msg);
+}
+
+/**
+ * Returns a snapshot of the environment variables at invocation. Mutating a
+ * property in the object will set that variable in the environment for
+ * the process. The environment object will only accept `string`s or `number`s
+ * as values.
+ * import { env } from "deno";
+ * const env = deno.env();
+ * console.log(env.SHELL)
+ * env.TEST_VAR = "HELLO";
+ *
+ * const newEnv = deno.env();
+ * console.log(env.TEST_VAR == newEnv.TEST_VAR);
+ */
+export function env(): { [index:string]: string } {
+ /* Ideally we could write
+ const res = send({
+ command: fbs.Command.ENV,
+ });
+ */
+ const builder = new flatbuffers.Builder();
+ fbs.Environ.startEnviron(builder);
+ const msg = fbs.Environ.endEnviron(builder);
+ const baseRes = send(builder, fbs.Any.Environ, msg)!;
+ assert(fbs.Any.EnvironRes === baseRes.msgType());
+ const res = new fbs.EnvironRes();
+ assert(baseRes.msg(res) != null);
+ // TypeScript cannot track assertion above, therefore not null assertion
+ return createEnv(res);
+}
+
export class FileInfo {
private _isFile: boolean;
private _isSymlink: boolean;
diff --git a/js/os_test.ts b/js/os_test.ts
index 7bf1d86a5..cd5ede221 100644
--- a/js/os_test.ts
+++ b/js/os_test.ts
@@ -2,6 +2,27 @@
import { test, testPerm, assert, assertEqual } from "./test_util.ts";
import * as deno from "deno";
+testPerm({ env: true }, async function envSuccess() {
+ const env = deno.env();
+ assert(env !== null);
+ env.test_var = "Hello World";
+ const newEnv = deno.env();
+ assertEqual(env.test_var, newEnv.test_var);
+});
+
+test(async function envFailure() {
+ let caughtError = false;
+ try {
+ const env = deno.env();
+ } catch (err) {
+ caughtError = true;
+ // TODO assert(err instanceof deno.PermissionDenied).
+ assertEqual(err.name, "deno.PermissionDenied");
+ }
+
+ assert(caughtError);
+});
+
// TODO Add tests for modified, accessed, and created fields once there is a way
// to create temp files.
test(async function statSyncSuccess() {
diff --git a/js/test_util.ts b/js/test_util.ts
index 116b96ff2..433bbf11b 100644
--- a/js/test_util.ts
+++ b/js/test_util.ts
@@ -17,23 +17,26 @@ testing.setFilter(deno.argv[1]);
interface DenoPermissions {
write?: boolean;
net?: boolean;
+ env?: boolean;
}
function permToString(perms: DenoPermissions): string {
const w = perms.write ? 1 : 0;
const n = perms.net ? 1 : 0;
- return `permW${w}N${n}`;
+ const e = perms.env ? 1 : 0;
+ return `permW${w}N${n}E${e}`;
}
function permFromString(s: string): DenoPermissions {
- const re = /^permW([01])N([01])$/;
+ const re = /^permW([01])N([01])E([01])$/;
const found = s.match(re);
if (!found) {
throw Error("Not a permission string");
}
return {
write: Boolean(Number(found[1])),
- net: Boolean(Number(found[2]))
+ net: Boolean(Number(found[2])),
+ env: Boolean(Number(found[3])),
};
}
@@ -43,14 +46,16 @@ export function testPerm(perms: DenoPermissions, fn: testing.TestFunction) {
}
export function test(fn: testing.TestFunction) {
- testPerm({ write: false, net: false }, fn);
+ testPerm({ write: false, net: false, env: false }, fn);
}
test(function permSerialization() {
- for (let write of [true, false]) {
- for (let net of [true, false]) {
- let perms: DenoPermissions = { write, net };
- testing.assertEqual(perms, permFromString(permToString(perms)));
+ for (const write of [true, false]) {
+ for (const net of [true, false]) {
+ for (const env of [true, false]) {
+ const perms: DenoPermissions = { write, net, env };
+ testing.assertEqual(perms, permFromString(permToString(perms)));
+ }
}
}
});