2022-01-20 02:10:16 -05:00
|
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2019-06-25 12:05:41 -04:00
|
|
|
|
import {
|
|
|
|
|
assert,
|
|
|
|
|
assertEquals,
|
2019-12-15 00:14:20 -05:00
|
|
|
|
assertNotEquals,
|
2020-03-04 11:31:14 -05:00
|
|
|
|
assertThrows,
|
2019-06-25 12:05:41 -04:00
|
|
|
|
} from "./test_util.ts";
|
2018-08-30 13:49:24 -04:00
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test({ permissions: { env: true } }, function envSuccess() {
|
2020-04-29 14:48:19 -04: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 07:51:12 -04:00
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test({ permissions: { env: true } }, function envNotFound() {
|
2020-04-29 14:48:19 -04:00
|
|
|
|
const r = Deno.env.get("env_var_does_not_exist!");
|
2019-10-02 11:55:28 -04:00
|
|
|
|
assertEquals(r, undefined);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test({ 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-11-23 11:45:18 -05:00
|
|
|
|
Deno.test({ permissions: { env: true } }, function avoidEmptyNamedEnv() {
|
2021-02-23 04:24:59 -05: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-11-23 11:45:18 -05:00
|
|
|
|
Deno.test({ permissions: { env: false } }, function envPermissionDenied1() {
|
2020-06-24 18:57:08 -04:00
|
|
|
|
assertThrows(() => {
|
2020-04-29 14:48:19 -04:00
|
|
|
|
Deno.env.toObject();
|
2020-06-24 18:57:08 -04:00
|
|
|
|
}, Deno.errors.PermissionDenied);
|
2019-10-02 11:55:28 -04:00
|
|
|
|
});
|
2018-08-31 07:51:12 -04:00
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test({ permissions: { env: false } }, function envPermissionDenied2() {
|
2020-06-24 18:57:08 -04:00
|
|
|
|
assertThrows(() => {
|
2020-04-29 14:48:19 -04:00
|
|
|
|
Deno.env.get("PATH");
|
2020-06-24 18:57:08 -04:00
|
|
|
|
}, Deno.errors.PermissionDenied);
|
2018-08-31 07:51:12 -04:00
|
|
|
|
});
|
2019-01-06 14:16:42 -05:00
|
|
|
|
|
2020-03-04 11:31:14 -05: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().
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(
|
2020-05-06 15:51:33 -04:00
|
|
|
|
{
|
|
|
|
|
ignore: Deno.build.os !== "windows",
|
2021-09-22 19:50:50 -04:00
|
|
|
|
permissions: { read: true, env: true, run: true },
|
2020-05-06 15:51:33 -04:00
|
|
|
|
},
|
2020-03-04 11:31:14 -05: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 15:36:18 -05:00
|
|
|
|
const checkChildEnv = async (
|
|
|
|
|
inputEnv: Record<string, string>,
|
2020-07-14 15:24:17 -04:00
|
|
|
|
expectedEnv: Record<string, string>,
|
2021-08-05 07:08:58 -04:00
|
|
|
|
) => {
|
2019-10-02 11:55:28 -04:00
|
|
|
|
const src = `
|
2020-03-04 11:31:14 -05:00
|
|
|
|
console.log(
|
2020-04-29 14:48:19 -04:00
|
|
|
|
${JSON.stringify(Object.keys(expectedEnv))}.map(k => Deno.env.get(k))
|
2020-03-04 11:31:14 -05:00
|
|
|
|
)`;
|
2022-05-18 16:00:11 -04:00
|
|
|
|
const { status, stdout } = await Deno.spawn(Deno.execPath(), {
|
|
|
|
|
args: ["eval", src],
|
2020-09-14 06:46:50 -04:00
|
|
|
|
env: { ...inputEnv, NO_COLOR: "1" },
|
2019-10-02 11:55:28 -04:00
|
|
|
|
});
|
|
|
|
|
assertEquals(status.success, true);
|
|
|
|
|
const expectedValues = Object.values(expectedEnv);
|
2022-05-18 16:00:11 -04:00
|
|
|
|
const actualValues = JSON.parse(new TextDecoder().decode(stdout));
|
2019-10-02 11:55:28 -04:00
|
|
|
|
assertEquals(actualValues, expectedValues);
|
|
|
|
|
};
|
|
|
|
|
|
2020-04-29 14:48:19 -04: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 11:31:14 -05:00
|
|
|
|
);
|
2019-10-02 11:55:28 -04:00
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function osPid() {
|
2019-02-12 10:08:56 -05:00
|
|
|
|
assert(Deno.pid > 0);
|
2019-01-06 14:16:42 -05:00
|
|
|
|
});
|
2019-02-02 22:05:30 -05:00
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function osPpid() {
|
2020-07-08 10:35:45 -04:00
|
|
|
|
assert(Deno.ppid > 0);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(
|
2021-09-22 19:50:50 -04:00
|
|
|
|
{ permissions: { run: true, read: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
|
async function osPpidIsEqualToPidOfParentProcess() {
|
2020-07-08 10:35:45 -04:00
|
|
|
|
const decoder = new TextDecoder();
|
2022-05-18 16:00:11 -04:00
|
|
|
|
const { stdout } = await Deno.spawn(Deno.execPath(), {
|
|
|
|
|
args: ["eval", "-p", "--unstable", "Deno.ppid"],
|
2020-07-08 10:35:45 -04:00
|
|
|
|
env: { NO_COLOR: "true" },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const expected = Deno.pid;
|
2022-05-18 16:00:11 -04:00
|
|
|
|
const actual = parseInt(decoder.decode(stdout));
|
2020-07-08 10:35:45 -04:00
|
|
|
|
assertEquals(actual, expected);
|
2020-07-14 15:24:17 -04:00
|
|
|
|
},
|
2020-07-08 10:35:45 -04:00
|
|
|
|
);
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test({ permissions: { read: true } }, function execPath() {
|
2019-08-06 17:05:47 -04:00
|
|
|
|
assertNotEquals(Deno.execPath(), "");
|
2019-08-03 21:34:13 -04:00
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test({ permissions: { read: false } }, function execPathPerm() {
|
2020-05-29 11:27:43 -04:00
|
|
|
|
assertThrows(
|
|
|
|
|
() => {
|
|
|
|
|
Deno.execPath();
|
|
|
|
|
},
|
|
|
|
|
Deno.errors.PermissionDenied,
|
2021-03-17 17:45:12 -04:00
|
|
|
|
"Requires read access to <exec_path>, run again with the --allow-read flag",
|
2020-05-29 11:27:43 -04:00
|
|
|
|
);
|
2019-08-03 21:34:13 -04:00
|
|
|
|
});
|
2019-09-27 19:09:42 -04:00
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test({ permissions: { env: true } }, function loadavgSuccess() {
|
2020-02-22 18:46:52 -05:00
|
|
|
|
const load = Deno.loadavg();
|
|
|
|
|
assertEquals(load.length, 3);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test({ permissions: { env: false } }, function loadavgPerm() {
|
2020-06-24 18:57:08 -04:00
|
|
|
|
assertThrows(() => {
|
2020-02-22 18:46:52 -05:00
|
|
|
|
Deno.loadavg();
|
2020-06-24 18:57:08 -04:00
|
|
|
|
}, Deno.errors.PermissionDenied);
|
2020-02-22 18:46:52 -05:00
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test({ permissions: { env: true } }, function hostnameDir() {
|
2019-09-27 19:09:42 -04:00
|
|
|
|
assertNotEquals(Deno.hostname(), "");
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test({ permissions: { env: false } }, function hostnamePerm() {
|
2020-06-24 18:57:08 -04:00
|
|
|
|
assertThrows(() => {
|
2019-09-27 19:09:42 -04:00
|
|
|
|
Deno.hostname();
|
2020-06-24 18:57:08 -04:00
|
|
|
|
}, Deno.errors.PermissionDenied);
|
2019-09-27 19:09:42 -04:00
|
|
|
|
});
|
2020-02-24 08:35:45 -05:00
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test({ permissions: { env: true } }, function releaseDir() {
|
2020-02-24 08:35:45 -05:00
|
|
|
|
assertNotEquals(Deno.osRelease(), "");
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test({ permissions: { env: false } }, function releasePerm() {
|
2020-06-24 18:57:08 -04:00
|
|
|
|
assertThrows(() => {
|
2020-02-24 08:35:45 -05:00
|
|
|
|
Deno.osRelease();
|
2020-06-24 18:57:08 -04:00
|
|
|
|
}, Deno.errors.PermissionDenied);
|
2020-02-24 08:35:45 -05:00
|
|
|
|
});
|
2020-09-10 04:38:17 -04:00
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test({ permissions: { env: true } }, function systemMemoryInfo() {
|
2020-09-10 04:38:17 -04:00
|
|
|
|
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);
|
|
|
|
|
});
|
2022-01-31 00:44:19 -05:00
|
|
|
|
|
|
|
|
|
Deno.test({ permissions: { env: true } }, function getUid() {
|
|
|
|
|
if (Deno.build.os === "windows") {
|
|
|
|
|
assertEquals(Deno.getUid(), null);
|
|
|
|
|
} else {
|
|
|
|
|
const uid = Deno.getUid();
|
|
|
|
|
assert(typeof uid === "number");
|
|
|
|
|
assert(uid > 0);
|
|
|
|
|
}
|
|
|
|
|
});
|