2021-01-11 12:13:41 -05:00
|
|
|
|
// Copyright 2018-2021 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,
|
2020-03-28 13:03:49 -04:00
|
|
|
|
unitTest,
|
2019-06-25 12:05:41 -04:00
|
|
|
|
} from "./test_util.ts";
|
2018-08-30 13:49:24 -04:00
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest({ perms: { env: true } }, function envSuccess(): void {
|
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
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest({ perms: { env: true } }, function envNotFound(): void {
|
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);
|
|
|
|
|
});
|
|
|
|
|
|
2020-06-09 08:58:30 -04:00
|
|
|
|
unitTest({ perms: { env: true } }, function deleteEnv(): void {
|
|
|
|
|
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);
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function envPermissionDenied1(): void {
|
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
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function envPermissionDenied2(): void {
|
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().
|
|
|
|
|
unitTest(
|
2020-05-06 15:51:33 -04:00
|
|
|
|
{
|
|
|
|
|
ignore: Deno.build.os !== "windows",
|
|
|
|
|
perms: { read: true, env: true, run: true },
|
|
|
|
|
},
|
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>,
|
2020-02-19 15:36:18 -05:00
|
|
|
|
): Promise<void> => {
|
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
|
|
|
|
)`;
|
2019-10-02 11:55:28 -04:00
|
|
|
|
const proc = Deno.run({
|
2020-03-21 17:44:18 -04:00
|
|
|
|
cmd: [Deno.execPath(), "eval", src],
|
2020-09-14 06:46:50 -04:00
|
|
|
|
env: { ...inputEnv, NO_COLOR: "1" },
|
2020-03-28 13:03:49 -04: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 12:45:47 -05:00
|
|
|
|
proc.close();
|
2019-10-02 11:55:28 -04:00
|
|
|
|
};
|
|
|
|
|
|
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
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function osPid(): void {
|
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
|
|
|
|
|
2020-07-08 10:35:45 -04:00
|
|
|
|
unitTest(function osPpid(): void {
|
|
|
|
|
assert(Deno.ppid > 0);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
unitTest(
|
|
|
|
|
{ perms: { run: true, read: true } },
|
|
|
|
|
async function osPpidIsEqualToPidOfParentProcess(): Promise<void> {
|
|
|
|
|
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 10:35:45 -04:00
|
|
|
|
);
|
|
|
|
|
|
2020-05-06 15:51:33 -04:00
|
|
|
|
unitTest({ perms: { read: true } }, function execPath(): void {
|
2019-08-06 17:05:47 -04:00
|
|
|
|
assertNotEquals(Deno.execPath(), "");
|
2019-08-03 21:34:13 -04:00
|
|
|
|
});
|
|
|
|
|
|
2020-05-06 15:51:33 -04:00
|
|
|
|
unitTest({ perms: { read: false } }, function execPathPerm(): void {
|
2020-05-29 11:27:43 -04:00
|
|
|
|
assertThrows(
|
|
|
|
|
() => {
|
|
|
|
|
Deno.execPath();
|
|
|
|
|
},
|
|
|
|
|
Deno.errors.PermissionDenied,
|
2020-07-14 15:24:17 -04:00
|
|
|
|
"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
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest({ perms: { env: true } }, function loadavgSuccess(): void {
|
2020-02-22 18:46:52 -05:00
|
|
|
|
const load = Deno.loadavg();
|
|
|
|
|
assertEquals(load.length, 3);
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest({ perms: { env: false } }, function loadavgPerm(): void {
|
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
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest({ perms: { env: true } }, function hostnameDir(): void {
|
2019-09-27 19:09:42 -04:00
|
|
|
|
assertNotEquals(Deno.hostname(), "");
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest({ perms: { env: false } }, function hostnamePerm(): void {
|
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
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest({ perms: { env: true } }, function releaseDir(): void {
|
2020-02-24 08:35:45 -05:00
|
|
|
|
assertNotEquals(Deno.osRelease(), "");
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest({ perms: { env: false } }, function releasePerm(): void {
|
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
|
|
|
|
|
|
|
|
|
unitTest({ perms: { env: true } }, function systemMemoryInfo(): void {
|
|
|
|
|
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);
|
|
|
|
|
});
|
2020-10-26 10:54:27 -04:00
|
|
|
|
|
|
|
|
|
unitTest({ perms: { env: true } }, function systemCpuInfo(): void {
|
|
|
|
|
const { cores, speed } = Deno.systemCpuInfo();
|
|
|
|
|
assert(cores === undefined || cores > 0);
|
|
|
|
|
assert(speed === undefined || speed > 0);
|
|
|
|
|
});
|