2021-01-12 02:13:41 +09:00
|
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2019-06-25 23:05:41 +07:00
|
|
|
|
import {
|
|
|
|
|
assert,
|
|
|
|
|
assertEquals,
|
2019-12-15 13:14:20 +08:00
|
|
|
|
assertNotEquals,
|
2020-03-04 17:31:14 +01:00
|
|
|
|
assertThrows,
|
2020-03-29 04:03:49 +11:00
|
|
|
|
unitTest,
|
2019-06-25 23:05:41 +07:00
|
|
|
|
} from "./test_util.ts";
|
2018-08-30 13:49:24 -04:00
|
|
|
|
|
2021-09-23 07:50:50 +08:00
|
|
|
|
unitTest({ permissions: { env: true } }, function envSuccess() {
|
2020-04-29 20:48:19 +02:00
|
|
|
|
Deno.env.set("TEST_VAR", "A");
|
|
|
|
|
const env = Deno.env.toObject();
|
|
|
|
|
Deno.env.set("TEST_VAR", "B");
|
|
|
|
|
assertEquals(env["TEST_VAR"], "A");
|
|
|
|
|
assertNotEquals(Deno.env.get("TEST_VAR"), env["TEST_VAR"]);
|
2018-08-31 12:51:12 +01:00
|
|
|
|
});
|
|
|
|
|
|
2021-09-23 07:50:50 +08:00
|
|
|
|
unitTest({ permissions: { env: true } }, function envNotFound() {
|
2020-04-29 20:48:19 +02:00
|
|
|
|
const r = Deno.env.get("env_var_does_not_exist!");
|
2019-10-02 11:55:28 -04:00
|
|
|
|
assertEquals(r, undefined);
|
|
|
|
|
});
|
|
|
|
|
|
2021-09-23 07:50:50 +08:00
|
|
|
|
unitTest({ permissions: { env: true } }, function deleteEnv() {
|
2020-06-09 08:58:30 -04:00
|
|
|
|
Deno.env.set("TEST_VAR", "A");
|
|
|
|
|
assertEquals(Deno.env.get("TEST_VAR"), "A");
|
|
|
|
|
assertEquals(Deno.env.delete("TEST_VAR"), undefined);
|
|
|
|
|
assertEquals(Deno.env.get("TEST_VAR"), undefined);
|
|
|
|
|
});
|
|
|
|
|
|
2021-09-23 07:50:50 +08:00
|
|
|
|
unitTest({ permissions: { env: true } }, function avoidEmptyNamedEnv() {
|
2021-02-23 18:24:59 +09:00
|
|
|
|
assertThrows(() => Deno.env.set("", "v"), TypeError);
|
|
|
|
|
assertThrows(() => Deno.env.set("a=a", "v"), TypeError);
|
|
|
|
|
assertThrows(() => Deno.env.set("a\0a", "v"), TypeError);
|
|
|
|
|
assertThrows(() => Deno.env.set("TEST_VAR", "v\0v"), TypeError);
|
|
|
|
|
|
|
|
|
|
assertThrows(() => Deno.env.get(""), TypeError);
|
|
|
|
|
assertThrows(() => Deno.env.get("a=a"), TypeError);
|
|
|
|
|
assertThrows(() => Deno.env.get("a\0a"), TypeError);
|
|
|
|
|
|
|
|
|
|
assertThrows(() => Deno.env.delete(""), TypeError);
|
|
|
|
|
assertThrows(() => Deno.env.delete("a=a"), TypeError);
|
|
|
|
|
assertThrows(() => Deno.env.delete("a\0a"), TypeError);
|
|
|
|
|
});
|
|
|
|
|
|
2021-08-05 13:08:58 +02:00
|
|
|
|
unitTest(function envPermissionDenied1() {
|
2020-06-25 06:57:08 +08:00
|
|
|
|
assertThrows(() => {
|
2020-04-29 20:48:19 +02:00
|
|
|
|
Deno.env.toObject();
|
2020-06-25 06:57:08 +08:00
|
|
|
|
}, Deno.errors.PermissionDenied);
|
2019-10-02 11:55:28 -04:00
|
|
|
|
});
|
2018-08-31 12:51:12 +01:00
|
|
|
|
|
2021-08-05 13:08:58 +02:00
|
|
|
|
unitTest(function envPermissionDenied2() {
|
2020-06-25 06:57:08 +08:00
|
|
|
|
assertThrows(() => {
|
2020-04-29 20:48:19 +02:00
|
|
|
|
Deno.env.get("PATH");
|
2020-06-25 06:57:08 +08:00
|
|
|
|
}, Deno.errors.PermissionDenied);
|
2018-08-31 12:51:12 +01:00
|
|
|
|
});
|
2019-01-06 14:16:42 -05:00
|
|
|
|
|
2020-03-04 17:31:14 +01:00
|
|
|
|
// This test verifies that on Windows, environment variables are
|
|
|
|
|
// case-insensitive. Case normalization needs be done using the collation
|
|
|
|
|
// that Windows uses, rather than naively using String.toLowerCase().
|
|
|
|
|
unitTest(
|
2020-05-06 15:51:33 -04:00
|
|
|
|
{
|
|
|
|
|
ignore: Deno.build.os !== "windows",
|
2021-09-23 07:50:50 +08:00
|
|
|
|
permissions: { read: true, env: true, run: true },
|
2020-05-06 15:51:33 -04:00
|
|
|
|
},
|
2020-03-04 17:31:14 +01:00
|
|
|
|
async function envCaseInsensitive() {
|
2019-10-02 11:55:28 -04:00
|
|
|
|
// Utility function that runs a Deno subprocess with the environment
|
|
|
|
|
// specified in `inputEnv`. The subprocess reads the environment variables
|
|
|
|
|
// which are in the keys of `expectedEnv` and writes them to stdout as JSON.
|
|
|
|
|
// It is then verified that these match with the values of `expectedEnv`.
|
2020-02-19 21:36:18 +01:00
|
|
|
|
const checkChildEnv = async (
|
|
|
|
|
inputEnv: Record<string, string>,
|
2020-07-14 15:24:17 -04:00
|
|
|
|
expectedEnv: Record<string, string>,
|
2021-08-05 13:08:58 +02:00
|
|
|
|
) => {
|
2019-10-02 11:55:28 -04:00
|
|
|
|
const src = `
|
2020-03-04 17:31:14 +01:00
|
|
|
|
console.log(
|
2020-04-29 20:48:19 +02:00
|
|
|
|
${JSON.stringify(Object.keys(expectedEnv))}.map(k => Deno.env.get(k))
|
2020-03-04 17:31:14 +01:00
|
|
|
|
)`;
|
2019-10-02 11:55:28 -04:00
|
|
|
|
const proc = Deno.run({
|
2020-03-22 03:14:18 +05:30
|
|
|
|
cmd: [Deno.execPath(), "eval", src],
|
2020-09-14 11:46:50 +01:00
|
|
|
|
env: { ...inputEnv, NO_COLOR: "1" },
|
2020-03-29 04:03:49 +11:00
|
|
|
|
stdout: "piped",
|
2019-10-02 11:55:28 -04:00
|
|
|
|
});
|
|
|
|
|
const status = await proc.status();
|
|
|
|
|
assertEquals(status.success, true);
|
|
|
|
|
const expectedValues = Object.values(expectedEnv);
|
|
|
|
|
const actualValues = JSON.parse(
|
2020-07-14 15:24:17 -04:00
|
|
|
|
new TextDecoder().decode(await proc.output()),
|
2019-10-02 11:55:28 -04:00
|
|
|
|
);
|
|
|
|
|
assertEquals(actualValues, expectedValues);
|
2020-02-29 18:45:47 +01:00
|
|
|
|
proc.close();
|
2019-10-02 11:55:28 -04:00
|
|
|
|
};
|
|
|
|
|
|
2020-04-29 20:48:19 +02:00
|
|
|
|
assertEquals(Deno.env.get("path"), Deno.env.get("PATH"));
|
|
|
|
|
assertEquals(Deno.env.get("Path"), Deno.env.get("PATH"));
|
2019-10-02 11:55:28 -04:00
|
|
|
|
|
|
|
|
|
// Check 'foo', 'Foo' and 'Foo' are case folded.
|
|
|
|
|
await checkChildEnv({ foo: "X" }, { foo: "X", Foo: "X", FOO: "X" });
|
|
|
|
|
|
|
|
|
|
// Check that 'µ' and 'Μ' are not case folded.
|
|
|
|
|
const lc1 = "µ";
|
|
|
|
|
const uc1 = lc1.toUpperCase();
|
|
|
|
|
assertNotEquals(lc1, uc1);
|
|
|
|
|
await checkChildEnv(
|
|
|
|
|
{ [lc1]: "mu", [uc1]: "MU" },
|
2020-07-14 15:24:17 -04:00
|
|
|
|
{ [lc1]: "mu", [uc1]: "MU" },
|
2019-10-02 11:55:28 -04:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Check that 'dž' and 'DŽ' are folded, but 'Dž' is preserved.
|
|
|
|
|
const c2 = "Dž";
|
|
|
|
|
const lc2 = c2.toLowerCase();
|
|
|
|
|
const uc2 = c2.toUpperCase();
|
|
|
|
|
assertNotEquals(c2, lc2);
|
|
|
|
|
assertNotEquals(c2, uc2);
|
|
|
|
|
await checkChildEnv(
|
|
|
|
|
{ [c2]: "Dz", [lc2]: "dz" },
|
2020-07-14 15:24:17 -04:00
|
|
|
|
{ [c2]: "Dz", [lc2]: "dz", [uc2]: "dz" },
|
2019-10-02 11:55:28 -04:00
|
|
|
|
);
|
|
|
|
|
await checkChildEnv(
|
|
|
|
|
{ [c2]: "Dz", [uc2]: "DZ" },
|
2020-07-14 15:24:17 -04:00
|
|
|
|
{ [c2]: "Dz", [uc2]: "DZ", [lc2]: "DZ" },
|
2019-10-02 11:55:28 -04:00
|
|
|
|
);
|
2020-07-14 15:24:17 -04:00
|
|
|
|
},
|
2020-03-04 17:31:14 +01:00
|
|
|
|
);
|
2019-10-02 11:55:28 -04:00
|
|
|
|
|
2021-08-05 13:08:58 +02:00
|
|
|
|
unitTest(function osPid() {
|
2019-02-13 02:08:56 +11:00
|
|
|
|
assert(Deno.pid > 0);
|
2019-01-06 14:16:42 -05:00
|
|
|
|
});
|
2019-02-03 06:05:30 +03:00
|
|
|
|
|
2021-08-05 13:08:58 +02:00
|
|
|
|
unitTest(function osPpid() {
|
2020-07-08 23:35:45 +09:00
|
|
|
|
assert(Deno.ppid > 0);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
unitTest(
|
2021-09-23 07:50:50 +08:00
|
|
|
|
{ permissions: { run: true, read: true } },
|
2021-08-05 13:08:58 +02:00
|
|
|
|
async function osPpidIsEqualToPidOfParentProcess() {
|
2020-07-08 23:35:45 +09:00
|
|
|
|
const decoder = new TextDecoder();
|
|
|
|
|
const process = Deno.run({
|
|
|
|
|
cmd: [Deno.execPath(), "eval", "-p", "--unstable", "Deno.ppid"],
|
|
|
|
|
stdout: "piped",
|
|
|
|
|
env: { NO_COLOR: "true" },
|
|
|
|
|
});
|
|
|
|
|
const output = await process.output();
|
|
|
|
|
process.close();
|
|
|
|
|
|
|
|
|
|
const expected = Deno.pid;
|
|
|
|
|
const actual = parseInt(decoder.decode(output));
|
|
|
|
|
assertEquals(actual, expected);
|
2020-07-14 15:24:17 -04:00
|
|
|
|
},
|
2020-07-08 23:35:45 +09:00
|
|
|
|
);
|
|
|
|
|
|
2021-09-23 07:50:50 +08:00
|
|
|
|
unitTest({ permissions: { read: true } }, function execPath() {
|
2019-08-06 14:05:47 -07:00
|
|
|
|
assertNotEquals(Deno.execPath(), "");
|
2019-08-03 18:34:13 -07:00
|
|
|
|
});
|
|
|
|
|
|
2021-09-23 07:50:50 +08:00
|
|
|
|
unitTest({ permissions: { read: false } }, function execPathPerm() {
|
2020-05-29 16:27:43 +01:00
|
|
|
|
assertThrows(
|
|
|
|
|
() => {
|
|
|
|
|
Deno.execPath();
|
|
|
|
|
},
|
|
|
|
|
Deno.errors.PermissionDenied,
|
2021-03-17 22:45:12 +01:00
|
|
|
|
"Requires read access to <exec_path>, run again with the --allow-read flag",
|
2020-05-29 16:27:43 +01:00
|
|
|
|
);
|
2019-08-03 18:34:13 -07:00
|
|
|
|
});
|
2019-09-27 16:09:42 -07:00
|
|
|
|
|
2021-09-23 07:50:50 +08:00
|
|
|
|
unitTest({ permissions: { env: true } }, function loadavgSuccess() {
|
2020-02-23 00:46:52 +01:00
|
|
|
|
const load = Deno.loadavg();
|
|
|
|
|
assertEquals(load.length, 3);
|
|
|
|
|
});
|
|
|
|
|
|
2021-09-23 07:50:50 +08:00
|
|
|
|
unitTest({ permissions: { env: false } }, function loadavgPerm() {
|
2020-06-25 06:57:08 +08:00
|
|
|
|
assertThrows(() => {
|
2020-02-23 00:46:52 +01:00
|
|
|
|
Deno.loadavg();
|
2020-06-25 06:57:08 +08:00
|
|
|
|
}, Deno.errors.PermissionDenied);
|
2020-02-23 00:46:52 +01:00
|
|
|
|
});
|
|
|
|
|
|
2021-09-23 07:50:50 +08:00
|
|
|
|
unitTest({ permissions: { env: true } }, function hostnameDir() {
|
2019-09-27 16:09:42 -07:00
|
|
|
|
assertNotEquals(Deno.hostname(), "");
|
|
|
|
|
});
|
|
|
|
|
|
2021-09-23 07:50:50 +08:00
|
|
|
|
unitTest({ permissions: { env: false } }, function hostnamePerm() {
|
2020-06-25 06:57:08 +08:00
|
|
|
|
assertThrows(() => {
|
2019-09-27 16:09:42 -07:00
|
|
|
|
Deno.hostname();
|
2020-06-25 06:57:08 +08:00
|
|
|
|
}, Deno.errors.PermissionDenied);
|
2019-09-27 16:09:42 -07:00
|
|
|
|
});
|
2020-02-24 14:35:45 +01:00
|
|
|
|
|
2021-09-23 07:50:50 +08:00
|
|
|
|
unitTest({ permissions: { env: true } }, function releaseDir() {
|
2020-02-24 14:35:45 +01:00
|
|
|
|
assertNotEquals(Deno.osRelease(), "");
|
|
|
|
|
});
|
|
|
|
|
|
2021-09-23 07:50:50 +08:00
|
|
|
|
unitTest({ permissions: { env: false } }, function releasePerm() {
|
2020-06-25 06:57:08 +08:00
|
|
|
|
assertThrows(() => {
|
2020-02-24 14:35:45 +01:00
|
|
|
|
Deno.osRelease();
|
2020-06-25 06:57:08 +08:00
|
|
|
|
}, Deno.errors.PermissionDenied);
|
2020-02-24 14:35:45 +01:00
|
|
|
|
});
|
2020-09-10 14:08:17 +05:30
|
|
|
|
|
2021-09-23 07:50:50 +08:00
|
|
|
|
unitTest({ permissions: { env: true } }, function systemMemoryInfo() {
|
2020-09-10 14:08:17 +05:30
|
|
|
|
const info = Deno.systemMemoryInfo();
|
|
|
|
|
assert(info.total >= 0);
|
|
|
|
|
assert(info.free >= 0);
|
|
|
|
|
assert(info.available >= 0);
|
|
|
|
|
assert(info.buffers >= 0);
|
|
|
|
|
assert(info.cached >= 0);
|
|
|
|
|
assert(info.swapTotal >= 0);
|
|
|
|
|
assert(info.swapFree >= 0);
|
|
|
|
|
});
|