2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-03-13 10:57:32 -04:00
|
|
|
|
2020-03-09 20:06:47 -04:00
|
|
|
import { assert, assertEquals } from "../../../std/testing/asserts.ts";
|
2019-03-06 20:48:46 -05:00
|
|
|
export {
|
|
|
|
assert,
|
2019-09-07 12:20:30 -04:00
|
|
|
assertThrows,
|
2020-06-10 11:54:54 -04:00
|
|
|
assertThrowsAsync,
|
2019-05-17 14:03:01 -04:00
|
|
|
assertEquals,
|
2019-08-27 11:33:39 -04:00
|
|
|
assertMatch,
|
2019-05-17 14:03:01 -04:00
|
|
|
assertNotEquals,
|
2020-06-05 23:43:00 -04:00
|
|
|
assertStrictEquals,
|
|
|
|
assertStringContains,
|
2020-02-03 09:54:47 -05:00
|
|
|
unreachable,
|
2020-03-28 13:03:49 -04:00
|
|
|
fail,
|
2020-03-09 20:06:47 -04:00
|
|
|
} from "../../../std/testing/asserts.ts";
|
2020-03-13 10:57:32 -04:00
|
|
|
export { readLines } from "../../../std/io/bufio.ts";
|
2020-03-14 06:53:20 -04:00
|
|
|
export { parse as parseArgs } from "../../../std/flags/mod.ts";
|
2018-08-23 19:47:43 -04:00
|
|
|
|
2019-10-27 11:22:53 -04:00
|
|
|
export interface Permissions {
|
|
|
|
read: boolean;
|
|
|
|
write: boolean;
|
|
|
|
net: boolean;
|
|
|
|
env: boolean;
|
|
|
|
run: boolean;
|
2019-12-05 15:30:20 -05:00
|
|
|
plugin: boolean;
|
2019-10-27 11:22:53 -04:00
|
|
|
hrtime: boolean;
|
|
|
|
}
|
|
|
|
|
2020-03-13 10:57:32 -04:00
|
|
|
export function fmtPerms(perms: Permissions): string {
|
|
|
|
const p = Object.keys(perms)
|
|
|
|
.filter((e): boolean => perms[e as keyof Permissions] === true)
|
2020-03-28 13:03:49 -04:00
|
|
|
.map((key) => `--allow-${key}`);
|
2020-03-13 10:57:32 -04:00
|
|
|
|
|
|
|
if (p.length) {
|
|
|
|
return p.join(" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
return "<no permissions>";
|
|
|
|
}
|
|
|
|
|
2019-10-27 11:22:53 -04:00
|
|
|
const isGranted = async (name: Deno.PermissionName): Promise<boolean> =>
|
|
|
|
(await Deno.permissions.query({ name })).state === "granted";
|
|
|
|
|
2020-03-13 10:57:32 -04:00
|
|
|
export async function getProcessPermissions(): Promise<Permissions> {
|
2019-10-27 11:22:53 -04:00
|
|
|
return {
|
|
|
|
run: await isGranted("run"),
|
|
|
|
read: await isGranted("read"),
|
|
|
|
write: await isGranted("write"),
|
|
|
|
net: await isGranted("net"),
|
|
|
|
env: await isGranted("env"),
|
2019-12-05 15:30:20 -05:00
|
|
|
plugin: await isGranted("plugin"),
|
2020-03-28 13:03:49 -04:00
|
|
|
hrtime: await isGranted("hrtime"),
|
2019-10-27 11:22:53 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-03-13 10:57:32 -04:00
|
|
|
export function permissionsMatch(
|
2019-10-27 11:22:53 -04:00
|
|
|
processPerms: Permissions,
|
|
|
|
requiredPerms: Permissions
|
2019-05-08 19:15:24 -04:00
|
|
|
): boolean {
|
|
|
|
for (const permName in processPerms) {
|
2020-02-19 15:36:18 -05:00
|
|
|
if (
|
|
|
|
processPerms[permName as keyof Permissions] !==
|
|
|
|
requiredPerms[permName as keyof Permissions]
|
|
|
|
) {
|
2019-05-08 19:15:24 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-10-27 11:22:53 -04:00
|
|
|
export const permissionCombinations: Map<string, Permissions> = new Map();
|
2019-05-08 19:15:24 -04:00
|
|
|
|
2019-10-27 11:22:53 -04:00
|
|
|
function permToString(perms: Permissions): string {
|
2019-02-08 15:59:38 -05:00
|
|
|
const r = perms.read ? 1 : 0;
|
2018-08-23 19:47:43 -04:00
|
|
|
const w = perms.write ? 1 : 0;
|
|
|
|
const n = perms.net ? 1 : 0;
|
2018-08-31 07:51:12 -04:00
|
|
|
const e = perms.env ? 1 : 0;
|
2019-02-08 15:59:38 -05:00
|
|
|
const u = perms.run ? 1 : 0;
|
2019-12-05 15:30:20 -05:00
|
|
|
const p = perms.plugin ? 1 : 0;
|
2019-05-23 12:28:29 -04:00
|
|
|
const h = perms.hrtime ? 1 : 0;
|
2019-12-05 15:30:20 -05:00
|
|
|
return `permR${r}W${w}N${n}E${e}U${u}P${p}H${h}`;
|
2018-08-23 19:47:43 -04:00
|
|
|
}
|
|
|
|
|
2019-10-27 11:22:53 -04:00
|
|
|
function registerPermCombination(perms: Permissions): void {
|
2019-05-08 19:15:24 -04:00
|
|
|
const key = permToString(perms);
|
|
|
|
if (!permissionCombinations.has(key)) {
|
|
|
|
permissionCombinations.set(key, perms);
|
2018-08-23 19:47:43 -04:00
|
|
|
}
|
2019-05-08 19:15:24 -04:00
|
|
|
}
|
|
|
|
|
2020-03-13 10:57:32 -04:00
|
|
|
export async function registerUnitTests(): Promise<void> {
|
|
|
|
const processPerms = await getProcessPermissions();
|
|
|
|
|
|
|
|
for (const unitTestDefinition of REGISTERED_UNIT_TESTS) {
|
|
|
|
if (!permissionsMatch(processPerms, unitTestDefinition.perms)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Deno.test(unitTestDefinition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function normalizeTestPermissions(perms: UnitTestPermissions): Permissions {
|
2018-08-23 19:47:43 -04:00
|
|
|
return {
|
2019-05-08 19:15:24 -04:00
|
|
|
read: !!perms.read,
|
|
|
|
write: !!perms.write,
|
|
|
|
net: !!perms.net,
|
|
|
|
run: !!perms.run,
|
|
|
|
env: !!perms.env,
|
2019-12-05 15:30:20 -05:00
|
|
|
plugin: !!perms.plugin,
|
2020-03-28 13:03:49 -04:00
|
|
|
hrtime: !!perms.hrtime,
|
2018-08-23 19:47:43 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-03-13 10:57:32 -04:00
|
|
|
interface UnitTestPermissions {
|
|
|
|
read?: boolean;
|
|
|
|
write?: boolean;
|
|
|
|
net?: boolean;
|
|
|
|
env?: boolean;
|
|
|
|
run?: boolean;
|
|
|
|
plugin?: boolean;
|
|
|
|
hrtime?: boolean;
|
|
|
|
}
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
interface UnitTestOptions {
|
2020-03-19 05:58:12 -04:00
|
|
|
ignore?: boolean;
|
2020-03-13 10:57:32 -04:00
|
|
|
perms?: UnitTestPermissions;
|
2020-03-04 11:31:14 -05:00
|
|
|
}
|
2019-05-08 19:15:24 -04:00
|
|
|
|
2020-03-15 12:58:59 -04:00
|
|
|
interface UnitTestDefinition extends Deno.TestDefinition {
|
2020-03-19 05:58:12 -04:00
|
|
|
ignore: boolean;
|
2020-03-13 10:57:32 -04:00
|
|
|
perms: Permissions;
|
|
|
|
}
|
|
|
|
|
2020-04-01 04:47:23 -04:00
|
|
|
type TestFunction = () => void | Promise<void>;
|
|
|
|
|
2020-03-13 10:57:32 -04:00
|
|
|
export const REGISTERED_UNIT_TESTS: UnitTestDefinition[] = [];
|
|
|
|
|
2020-04-01 04:47:23 -04:00
|
|
|
export function unitTest(fn: TestFunction): void;
|
|
|
|
export function unitTest(options: UnitTestOptions, fn: TestFunction): void;
|
2020-03-04 11:31:14 -05:00
|
|
|
export function unitTest(
|
2020-04-01 04:47:23 -04:00
|
|
|
optionsOrFn: UnitTestOptions | TestFunction,
|
|
|
|
maybeFn?: TestFunction
|
2020-03-04 11:31:14 -05:00
|
|
|
): void {
|
|
|
|
assert(optionsOrFn, "At least one argument is required");
|
|
|
|
|
|
|
|
let options: UnitTestOptions;
|
|
|
|
let name: string;
|
2020-04-01 04:47:23 -04:00
|
|
|
let fn: TestFunction;
|
2020-03-04 11:31:14 -05:00
|
|
|
|
|
|
|
if (typeof optionsOrFn === "function") {
|
|
|
|
options = {};
|
|
|
|
fn = optionsOrFn;
|
|
|
|
name = fn.name;
|
|
|
|
assert(name, "Missing test function name");
|
|
|
|
} else {
|
|
|
|
options = optionsOrFn;
|
|
|
|
assert(maybeFn, "Missing test function definition");
|
|
|
|
assert(
|
|
|
|
typeof maybeFn === "function",
|
|
|
|
"Second argument should be test function definition"
|
|
|
|
);
|
|
|
|
fn = maybeFn;
|
|
|
|
name = fn.name;
|
|
|
|
assert(name, "Missing test function name");
|
|
|
|
}
|
2019-05-08 19:15:24 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
const normalizedPerms = normalizeTestPermissions(options.perms || {});
|
|
|
|
registerPermCombination(normalizedPerms);
|
2018-08-23 19:47:43 -04:00
|
|
|
|
2020-03-13 10:57:32 -04:00
|
|
|
const unitTestDefinition: UnitTestDefinition = {
|
2020-03-04 11:31:14 -05:00
|
|
|
name,
|
2020-03-18 19:25:55 -04:00
|
|
|
fn,
|
2020-03-19 05:58:12 -04:00
|
|
|
ignore: !!options.ignore,
|
2020-03-28 13:03:49 -04:00
|
|
|
perms: normalizedPerms,
|
2020-03-04 11:31:14 -05:00
|
|
|
};
|
2019-05-08 19:15:24 -04:00
|
|
|
|
2020-03-13 10:57:32 -04:00
|
|
|
REGISTERED_UNIT_TESTS.push(unitTestDefinition);
|
2019-05-08 19:15:24 -04:00
|
|
|
}
|
|
|
|
|
2020-02-29 12:45:47 -05:00
|
|
|
export interface ResolvableMethods<T> {
|
|
|
|
resolve: (value?: T | PromiseLike<T>) => void;
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
reject: (reason?: any) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export type Resolvable<T> = Promise<T> & ResolvableMethods<T>;
|
|
|
|
|
|
|
|
export function createResolvable<T>(): Resolvable<T> {
|
|
|
|
let methods: ResolvableMethods<T>;
|
|
|
|
const promise = new Promise<T>((resolve, reject): void => {
|
|
|
|
methods = { resolve, reject };
|
|
|
|
});
|
|
|
|
// TypeScript doesn't know that the Promise callback occurs synchronously
|
|
|
|
// therefore use of not null assertion (`!`)
|
|
|
|
return Object.assign(promise, methods!) as Resolvable<T>;
|
|
|
|
}
|
|
|
|
|
2020-03-28 13:03:49 -04:00
|
|
|
const encoder = new TextEncoder();
|
|
|
|
|
2020-04-01 04:47:23 -04:00
|
|
|
// Replace functions with null, errors with their stack strings, and JSONify.
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2020-04-27 08:51:22 -04:00
|
|
|
function serializeTestMessage(message: any): string {
|
2020-04-01 04:47:23 -04:00
|
|
|
return JSON.stringify({
|
|
|
|
start: message.start && {
|
|
|
|
...message.start,
|
2020-04-27 08:51:22 -04:00
|
|
|
tests: message.start.tests.map((test: Deno.TestDefinition) => ({
|
|
|
|
...test,
|
|
|
|
fn: null,
|
|
|
|
})),
|
2020-04-01 04:47:23 -04:00
|
|
|
},
|
|
|
|
testStart: message.testStart && { ...message.testStart, fn: null },
|
|
|
|
testEnd: message.testEnd && {
|
|
|
|
...message.testEnd,
|
|
|
|
error: String(message.testEnd.error?.stack),
|
|
|
|
},
|
|
|
|
end: message.end && {
|
|
|
|
...message.end,
|
2020-04-27 08:51:22 -04:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
results: message.end.results.map((result: any) => ({
|
2020-04-01 04:47:23 -04:00
|
|
|
...result,
|
|
|
|
error: result.error?.stack,
|
|
|
|
})),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2020-03-13 10:57:32 -04:00
|
|
|
|
2020-04-01 04:47:23 -04:00
|
|
|
export async function reportToConn(
|
|
|
|
conn: Deno.Conn,
|
2020-04-27 08:51:22 -04:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
message: any
|
2020-04-01 04:47:23 -04:00
|
|
|
): Promise<void> {
|
|
|
|
const line = serializeTestMessage(message);
|
|
|
|
const encodedMsg = encoder.encode(line + (message.end == null ? "\n" : ""));
|
|
|
|
await Deno.writeAll(conn, encodedMsg);
|
|
|
|
if (message.end != null) {
|
|
|
|
conn.closeWrite();
|
2020-03-13 10:57:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function permissionsMatches(): void {
|
2019-05-08 19:15:24 -04:00
|
|
|
assert(
|
|
|
|
permissionsMatch(
|
|
|
|
{
|
|
|
|
read: true,
|
|
|
|
write: false,
|
|
|
|
net: false,
|
|
|
|
env: false,
|
|
|
|
run: false,
|
2019-12-05 15:30:20 -05:00
|
|
|
plugin: false,
|
2020-03-28 13:03:49 -04:00
|
|
|
hrtime: false,
|
2019-05-08 19:15:24 -04:00
|
|
|
},
|
|
|
|
normalizeTestPermissions({ read: true })
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
assert(
|
|
|
|
permissionsMatch(
|
|
|
|
{
|
|
|
|
read: false,
|
|
|
|
write: false,
|
|
|
|
net: false,
|
|
|
|
env: false,
|
|
|
|
run: false,
|
2019-12-05 15:30:20 -05:00
|
|
|
plugin: false,
|
2020-03-28 13:03:49 -04:00
|
|
|
hrtime: false,
|
2019-05-08 19:15:24 -04:00
|
|
|
},
|
|
|
|
normalizeTestPermissions({})
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
permissionsMatch(
|
|
|
|
{
|
|
|
|
read: false,
|
|
|
|
write: true,
|
|
|
|
net: true,
|
|
|
|
env: true,
|
|
|
|
run: true,
|
2019-12-05 15:30:20 -05:00
|
|
|
plugin: true,
|
2020-03-28 13:03:49 -04:00
|
|
|
hrtime: true,
|
2019-05-08 19:15:24 -04:00
|
|
|
},
|
|
|
|
normalizeTestPermissions({ read: true })
|
|
|
|
),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
permissionsMatch(
|
|
|
|
{
|
|
|
|
read: true,
|
|
|
|
write: false,
|
|
|
|
net: true,
|
|
|
|
env: false,
|
|
|
|
run: false,
|
2019-12-05 15:30:20 -05:00
|
|
|
plugin: false,
|
2020-03-28 13:03:49 -04:00
|
|
|
hrtime: false,
|
2019-05-08 19:15:24 -04:00
|
|
|
},
|
|
|
|
normalizeTestPermissions({ read: true })
|
|
|
|
),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
|
|
|
assert(
|
|
|
|
permissionsMatch(
|
|
|
|
{
|
|
|
|
read: true,
|
|
|
|
write: true,
|
|
|
|
net: true,
|
|
|
|
env: true,
|
|
|
|
run: true,
|
2019-12-05 15:30:20 -05:00
|
|
|
plugin: true,
|
2020-03-28 13:03:49 -04:00
|
|
|
hrtime: true,
|
2019-05-08 19:15:24 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
read: true,
|
|
|
|
write: true,
|
|
|
|
net: true,
|
|
|
|
env: true,
|
|
|
|
run: true,
|
2019-12-05 15:30:20 -05:00
|
|
|
plugin: true,
|
2020-03-28 13:03:49 -04:00
|
|
|
hrtime: true,
|
2019-05-08 19:15:24 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-02-05 03:23:23 -05:00
|
|
|
/*
|
|
|
|
* Ensure all unit test files (e.g. xxx_test.ts) are present as imports in
|
2020-05-20 17:52:51 -04:00
|
|
|
* cli/tests/unit/unit_tests.ts as it is easy to miss this out
|
2020-02-05 03:23:23 -05:00
|
|
|
*/
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true } },
|
2020-03-20 09:38:34 -04:00
|
|
|
function assertAllUnitTestFilesImported(): void {
|
2020-05-20 17:52:51 -04:00
|
|
|
const directoryTestFiles = [...Deno.readDirSync("./cli/tests/unit/")]
|
2020-03-28 13:03:49 -04:00
|
|
|
.map((k) => k.name)
|
2020-03-09 20:06:47 -04:00
|
|
|
.filter(
|
2020-03-28 13:03:49 -04:00
|
|
|
(file) =>
|
2020-03-09 20:06:47 -04:00
|
|
|
file!.endsWith(".ts") &&
|
|
|
|
!file!.endsWith("unit_tests.ts") &&
|
|
|
|
!file!.endsWith("test_util.ts") &&
|
|
|
|
!file!.endsWith("unit_test_runner.ts")
|
|
|
|
);
|
2020-02-05 03:23:23 -05:00
|
|
|
const unitTestsFile: Uint8Array = Deno.readFileSync(
|
2020-05-20 17:52:51 -04:00
|
|
|
"./cli/tests/unit/unit_tests.ts"
|
2020-02-05 03:23:23 -05:00
|
|
|
);
|
|
|
|
const importLines = new TextDecoder("utf-8")
|
|
|
|
.decode(unitTestsFile)
|
|
|
|
.split("\n")
|
2020-03-28 13:03:49 -04:00
|
|
|
.filter((line) => line.startsWith("import"));
|
2020-02-05 03:23:23 -05:00
|
|
|
const importedTestFiles = importLines.map(
|
2020-03-28 13:03:49 -04:00
|
|
|
(relativeFilePath) => relativeFilePath.match(/\/([^\/]+)";/)![1]
|
2020-02-05 03:23:23 -05:00
|
|
|
);
|
|
|
|
|
2020-03-28 13:03:49 -04:00
|
|
|
directoryTestFiles.forEach((dirFile) => {
|
2020-02-19 15:36:18 -05:00
|
|
|
if (!importedTestFiles.includes(dirFile!)) {
|
2020-02-05 03:23:23 -05:00
|
|
|
throw new Error(
|
2020-05-20 17:52:51 -04:00
|
|
|
"cil/tests/unit/unit_tests.ts is missing import of test file: cli/js/" +
|
2020-02-05 03:23:23 -05:00
|
|
|
dirFile
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|