2020-01-02 15:13:47 -05:00
|
|
|
|
// Copyright 2018-2020 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 {
|
2019-02-12 10:08:56 -05:00
|
|
|
|
const env = Deno.env();
|
2018-08-31 07:51:12 -04:00
|
|
|
|
assert(env !== null);
|
2019-03-09 12:30:38 -05:00
|
|
|
|
// eslint-disable-next-line @typescript-eslint/camelcase
|
2018-08-31 07:51:12 -04:00
|
|
|
|
env.test_var = "Hello World";
|
2019-02-12 10:08:56 -05:00
|
|
|
|
const newEnv = Deno.env();
|
2019-03-06 20:48:46 -05:00
|
|
|
|
assertEquals(env.test_var, newEnv.test_var);
|
2019-10-02 11:55:28 -04:00
|
|
|
|
assertEquals(Deno.env("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 {
|
2019-10-02 11:55:28 -04:00
|
|
|
|
const r = Deno.env("env_var_does_not_exist!");
|
|
|
|
|
assertEquals(r, undefined);
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function envPermissionDenied1(): void {
|
2019-10-02 11:55:28 -04:00
|
|
|
|
let err;
|
2018-08-31 07:51:12 -04:00
|
|
|
|
try {
|
2019-03-09 12:30:38 -05:00
|
|
|
|
Deno.env();
|
2019-10-02 11:55:28 -04:00
|
|
|
|
} catch (e) {
|
|
|
|
|
err = e;
|
2018-08-31 07:51:12 -04:00
|
|
|
|
}
|
2019-10-02 11:55:28 -04:00
|
|
|
|
assertNotEquals(err, undefined);
|
2020-02-24 15:48:35 -05:00
|
|
|
|
assert(err instanceof Deno.errors.PermissionDenied);
|
2019-10-02 11:55:28 -04:00
|
|
|
|
assertEquals(err.name, "PermissionDenied");
|
|
|
|
|
});
|
2018-08-31 07:51:12 -04:00
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function envPermissionDenied2(): void {
|
2019-10-02 11:55:28 -04:00
|
|
|
|
let err;
|
|
|
|
|
try {
|
|
|
|
|
Deno.env("PATH");
|
|
|
|
|
} catch (e) {
|
|
|
|
|
err = e;
|
|
|
|
|
}
|
|
|
|
|
assertNotEquals(err, undefined);
|
2020-02-24 15:48:35 -05:00
|
|
|
|
assert(err instanceof Deno.errors.PermissionDenied);
|
2019-10-02 11:55:28 -04:00
|
|
|
|
assertEquals(err.name, "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-04-28 12:35:23 -04:00
|
|
|
|
{ ignore: Deno.build.os !== "windows", perms: { 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>,
|
|
|
|
|
expectedEnv: Record<string, string>
|
|
|
|
|
): Promise<void> => {
|
2019-10-02 11:55:28 -04:00
|
|
|
|
const src = `
|
2020-03-04 11:31:14 -05:00
|
|
|
|
console.log(
|
|
|
|
|
${JSON.stringify(Object.keys(expectedEnv))}.map(k => Deno.env(k))
|
|
|
|
|
)`;
|
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],
|
2019-10-02 11:55:28 -04:00
|
|
|
|
env: inputEnv,
|
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(
|
|
|
|
|
new TextDecoder().decode(await proc.output())
|
|
|
|
|
);
|
|
|
|
|
assertEquals(actualValues, expectedValues);
|
2020-02-29 12:45:47 -05:00
|
|
|
|
proc.close();
|
2019-10-02 11:55:28 -04:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
assertEquals(Deno.env("path"), Deno.env("PATH"));
|
|
|
|
|
assertEquals(Deno.env("Path"), Deno.env("PATH"));
|
|
|
|
|
|
|
|
|
|
// 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" },
|
|
|
|
|
{ [lc1]: "mu", [uc1]: "MU" }
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 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" },
|
|
|
|
|
{ [c2]: "Dz", [lc2]: "dz", [uc2]: "dz" }
|
|
|
|
|
);
|
|
|
|
|
await checkChildEnv(
|
|
|
|
|
{ [c2]: "Dz", [uc2]: "DZ" },
|
|
|
|
|
{ [c2]: "Dz", [uc2]: "DZ", [lc2]: "DZ" }
|
|
|
|
|
);
|
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-03-04 11:31:14 -05:00
|
|
|
|
unitTest({ perms: { env: true } }, function getDir(): void {
|
2020-04-28 12:35:23 -04:00
|
|
|
|
type supportOS = "darwin" | "windows" | "linux";
|
2019-12-15 00:14:20 -05:00
|
|
|
|
|
|
|
|
|
interface Runtime {
|
|
|
|
|
os: supportOS;
|
|
|
|
|
shouldHaveValue: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface Scenes {
|
2019-12-18 09:29:00 -05:00
|
|
|
|
kind: Deno.DirKind;
|
2019-12-15 00:14:20 -05:00
|
|
|
|
runtime: Runtime[];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const scenes: Scenes[] = [
|
|
|
|
|
{
|
2019-12-18 09:29:00 -05:00
|
|
|
|
kind: "config",
|
2019-12-15 00:14:20 -05:00
|
|
|
|
runtime: [
|
2020-04-28 12:35:23 -04:00
|
|
|
|
{ os: "darwin", shouldHaveValue: true },
|
|
|
|
|
{ os: "windows", shouldHaveValue: true },
|
2020-03-28 13:03:49 -04:00
|
|
|
|
{ os: "linux", shouldHaveValue: true },
|
|
|
|
|
],
|
2019-12-15 00:14:20 -05:00
|
|
|
|
},
|
|
|
|
|
{
|
2019-12-18 09:29:00 -05:00
|
|
|
|
kind: "cache",
|
2019-12-15 00:14:20 -05:00
|
|
|
|
runtime: [
|
2020-04-28 12:35:23 -04:00
|
|
|
|
{ os: "darwin", shouldHaveValue: true },
|
|
|
|
|
{ os: "windows", shouldHaveValue: true },
|
2020-03-28 13:03:49 -04:00
|
|
|
|
{ os: "linux", shouldHaveValue: true },
|
|
|
|
|
],
|
2019-12-15 00:14:20 -05:00
|
|
|
|
},
|
2019-12-21 06:30:13 -05:00
|
|
|
|
{
|
|
|
|
|
kind: "executable",
|
|
|
|
|
runtime: [
|
2020-04-28 12:35:23 -04:00
|
|
|
|
{ os: "darwin", shouldHaveValue: false },
|
|
|
|
|
{ os: "windows", shouldHaveValue: false },
|
2020-03-28 13:03:49 -04:00
|
|
|
|
{ os: "linux", shouldHaveValue: true },
|
|
|
|
|
],
|
2019-12-21 06:30:13 -05:00
|
|
|
|
},
|
2019-12-15 00:14:20 -05:00
|
|
|
|
{
|
2019-12-18 09:29:00 -05:00
|
|
|
|
kind: "data",
|
2019-12-15 00:14:20 -05:00
|
|
|
|
runtime: [
|
2020-04-28 12:35:23 -04:00
|
|
|
|
{ os: "darwin", shouldHaveValue: true },
|
|
|
|
|
{ os: "windows", shouldHaveValue: true },
|
2020-03-28 13:03:49 -04:00
|
|
|
|
{ os: "linux", shouldHaveValue: true },
|
|
|
|
|
],
|
2019-12-15 00:14:20 -05:00
|
|
|
|
},
|
|
|
|
|
{
|
2019-12-18 09:29:00 -05:00
|
|
|
|
kind: "data_local",
|
2019-12-15 00:14:20 -05:00
|
|
|
|
runtime: [
|
2020-04-28 12:35:23 -04:00
|
|
|
|
{ os: "darwin", shouldHaveValue: true },
|
|
|
|
|
{ os: "windows", shouldHaveValue: true },
|
2020-03-28 13:03:49 -04:00
|
|
|
|
{ os: "linux", shouldHaveValue: true },
|
|
|
|
|
],
|
2019-12-15 00:14:20 -05:00
|
|
|
|
},
|
|
|
|
|
{
|
2019-12-18 09:29:00 -05:00
|
|
|
|
kind: "audio",
|
2019-12-15 00:14:20 -05:00
|
|
|
|
runtime: [
|
2020-04-28 12:35:23 -04:00
|
|
|
|
{ os: "darwin", shouldHaveValue: true },
|
|
|
|
|
{ os: "windows", shouldHaveValue: true },
|
2020-03-28 13:03:49 -04:00
|
|
|
|
{ os: "linux", shouldHaveValue: false },
|
|
|
|
|
],
|
2019-12-15 00:14:20 -05:00
|
|
|
|
},
|
|
|
|
|
{
|
2019-12-18 09:29:00 -05:00
|
|
|
|
kind: "desktop",
|
2019-12-15 00:14:20 -05:00
|
|
|
|
runtime: [
|
2020-04-28 12:35:23 -04:00
|
|
|
|
{ os: "darwin", shouldHaveValue: true },
|
|
|
|
|
{ os: "windows", shouldHaveValue: true },
|
2020-03-28 13:03:49 -04:00
|
|
|
|
{ os: "linux", shouldHaveValue: false },
|
|
|
|
|
],
|
2019-12-15 00:14:20 -05:00
|
|
|
|
},
|
|
|
|
|
{
|
2019-12-18 09:29:00 -05:00
|
|
|
|
kind: "document",
|
2019-12-15 00:14:20 -05:00
|
|
|
|
runtime: [
|
2020-04-28 12:35:23 -04:00
|
|
|
|
{ os: "darwin", shouldHaveValue: true },
|
|
|
|
|
{ os: "windows", shouldHaveValue: true },
|
2020-03-28 13:03:49 -04:00
|
|
|
|
{ os: "linux", shouldHaveValue: false },
|
|
|
|
|
],
|
2019-12-15 00:14:20 -05:00
|
|
|
|
},
|
|
|
|
|
{
|
2019-12-18 09:29:00 -05:00
|
|
|
|
kind: "download",
|
2019-12-15 00:14:20 -05:00
|
|
|
|
runtime: [
|
2020-04-28 12:35:23 -04:00
|
|
|
|
{ os: "darwin", shouldHaveValue: true },
|
|
|
|
|
{ os: "windows", shouldHaveValue: true },
|
2020-03-28 13:03:49 -04:00
|
|
|
|
{ os: "linux", shouldHaveValue: false },
|
|
|
|
|
],
|
2019-12-15 00:14:20 -05:00
|
|
|
|
},
|
|
|
|
|
{
|
2019-12-18 09:29:00 -05:00
|
|
|
|
kind: "font",
|
2019-12-15 00:14:20 -05:00
|
|
|
|
runtime: [
|
2020-04-28 12:35:23 -04:00
|
|
|
|
{ os: "darwin", shouldHaveValue: true },
|
|
|
|
|
{ os: "windows", shouldHaveValue: false },
|
2020-03-28 13:03:49 -04:00
|
|
|
|
{ os: "linux", shouldHaveValue: true },
|
|
|
|
|
],
|
2019-12-15 00:14:20 -05:00
|
|
|
|
},
|
|
|
|
|
{
|
2019-12-18 09:29:00 -05:00
|
|
|
|
kind: "picture",
|
2019-12-15 00:14:20 -05:00
|
|
|
|
runtime: [
|
2020-04-28 12:35:23 -04:00
|
|
|
|
{ os: "darwin", shouldHaveValue: true },
|
|
|
|
|
{ os: "windows", shouldHaveValue: true },
|
2020-03-28 13:03:49 -04:00
|
|
|
|
{ os: "linux", shouldHaveValue: false },
|
|
|
|
|
],
|
2019-12-15 00:14:20 -05:00
|
|
|
|
},
|
|
|
|
|
{
|
2019-12-18 09:29:00 -05:00
|
|
|
|
kind: "public",
|
2019-12-15 00:14:20 -05:00
|
|
|
|
runtime: [
|
2020-04-28 12:35:23 -04:00
|
|
|
|
{ os: "darwin", shouldHaveValue: true },
|
|
|
|
|
{ os: "windows", shouldHaveValue: true },
|
2020-03-28 13:03:49 -04:00
|
|
|
|
{ os: "linux", shouldHaveValue: false },
|
|
|
|
|
],
|
2019-12-15 00:14:20 -05:00
|
|
|
|
},
|
|
|
|
|
{
|
2019-12-18 09:29:00 -05:00
|
|
|
|
kind: "template",
|
2019-12-15 00:14:20 -05:00
|
|
|
|
runtime: [
|
2020-04-28 12:35:23 -04:00
|
|
|
|
{ os: "darwin", shouldHaveValue: false },
|
|
|
|
|
{ os: "windows", shouldHaveValue: true },
|
2020-03-28 13:03:49 -04:00
|
|
|
|
{ os: "linux", shouldHaveValue: false },
|
|
|
|
|
],
|
2019-12-15 00:14:20 -05:00
|
|
|
|
},
|
2020-03-01 19:05:04 -05:00
|
|
|
|
{
|
|
|
|
|
kind: "tmp",
|
|
|
|
|
runtime: [
|
2020-04-28 12:35:23 -04:00
|
|
|
|
{ os: "darwin", shouldHaveValue: true },
|
|
|
|
|
{ os: "windows", shouldHaveValue: true },
|
2020-03-28 13:03:49 -04:00
|
|
|
|
{ os: "linux", shouldHaveValue: true },
|
|
|
|
|
],
|
2020-03-01 19:05:04 -05:00
|
|
|
|
},
|
2019-12-15 00:14:20 -05:00
|
|
|
|
{
|
2019-12-18 09:29:00 -05:00
|
|
|
|
kind: "video",
|
2019-12-15 00:14:20 -05:00
|
|
|
|
runtime: [
|
2020-04-28 12:35:23 -04:00
|
|
|
|
{ os: "darwin", shouldHaveValue: true },
|
|
|
|
|
{ os: "windows", shouldHaveValue: true },
|
2020-03-28 13:03:49 -04:00
|
|
|
|
{ os: "linux", shouldHaveValue: false },
|
|
|
|
|
],
|
|
|
|
|
},
|
2019-12-15 00:14:20 -05:00
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
for (const s of scenes) {
|
|
|
|
|
for (const r of s.runtime) {
|
|
|
|
|
if (Deno.build.os !== r.os) continue;
|
|
|
|
|
if (r.shouldHaveValue) {
|
2020-01-05 09:19:29 -05:00
|
|
|
|
const d = Deno.dir(s.kind);
|
2020-02-19 15:36:18 -05:00
|
|
|
|
assert(d);
|
2020-01-05 09:19:29 -05:00
|
|
|
|
assert(d.length > 0);
|
2019-12-15 00:14:20 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function getDirWithoutPermission(): void {
|
2019-12-18 09:29:00 -05:00
|
|
|
|
assertThrows(
|
|
|
|
|
() => Deno.dir("home"),
|
2020-02-24 15:48:35 -05:00
|
|
|
|
Deno.errors.PermissionDenied,
|
2019-12-18 09:29:00 -05:00
|
|
|
|
`run again with the --allow-env flag`
|
|
|
|
|
);
|
2019-12-15 00:14:20 -05:00
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest({ perms: { env: true } }, function execPath(): void {
|
2019-08-06 17:05:47 -04:00
|
|
|
|
assertNotEquals(Deno.execPath(), "");
|
2019-08-03 21:34:13 -04:00
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest({ perms: { env: false } }, function execPathPerm(): void {
|
2019-08-06 17:05:47 -04:00
|
|
|
|
let caughtError = false;
|
|
|
|
|
try {
|
|
|
|
|
Deno.execPath();
|
|
|
|
|
} catch (err) {
|
|
|
|
|
caughtError = true;
|
2020-02-24 15:48:35 -05:00
|
|
|
|
assert(err instanceof Deno.errors.PermissionDenied);
|
2019-08-06 17:05:47 -04:00
|
|
|
|
assertEquals(err.name, "PermissionDenied");
|
|
|
|
|
}
|
|
|
|
|
assert(caughtError);
|
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-02-22 18:46:52 -05:00
|
|
|
|
let caughtError = false;
|
|
|
|
|
try {
|
|
|
|
|
Deno.loadavg();
|
|
|
|
|
} catch (err) {
|
|
|
|
|
caughtError = true;
|
2020-02-24 15:48:35 -05:00
|
|
|
|
assert(err instanceof Deno.errors.PermissionDenied);
|
2020-02-22 18:46:52 -05:00
|
|
|
|
assertEquals(err.name, "PermissionDenied");
|
|
|
|
|
}
|
|
|
|
|
assert(caughtError);
|
|
|
|
|
});
|
|
|
|
|
|
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 {
|
2019-09-27 19:09:42 -04:00
|
|
|
|
let caughtError = false;
|
|
|
|
|
try {
|
|
|
|
|
Deno.hostname();
|
|
|
|
|
} catch (err) {
|
|
|
|
|
caughtError = true;
|
2020-02-24 15:48:35 -05:00
|
|
|
|
assert(err instanceof Deno.errors.PermissionDenied);
|
2019-09-27 19:09:42 -04:00
|
|
|
|
assertEquals(err.name, "PermissionDenied");
|
|
|
|
|
}
|
|
|
|
|
assert(caughtError);
|
|
|
|
|
});
|
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-02-24 08:35:45 -05:00
|
|
|
|
let caughtError = false;
|
|
|
|
|
try {
|
|
|
|
|
Deno.osRelease();
|
|
|
|
|
} catch (err) {
|
|
|
|
|
caughtError = true;
|
2020-02-24 16:36:12 -05:00
|
|
|
|
assert(err instanceof Deno.errors.PermissionDenied);
|
2020-02-24 08:35:45 -05:00
|
|
|
|
assertEquals(err.name, "PermissionDenied");
|
|
|
|
|
}
|
|
|
|
|
assert(caughtError);
|
|
|
|
|
});
|