2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2018-08-23 19:47:43 -04:00
|
|
|
//
|
|
|
|
// We want to test many ops in deno which have different behavior depending on
|
|
|
|
// the permissions set. These tests can specify which permissions they expect,
|
|
|
|
// which appends a special string like "permW1N0" to the end of the test name.
|
|
|
|
// Here we run several copies of deno with different permissions, filtering the
|
2019-04-20 15:12:00 -04:00
|
|
|
// tests by the special string. permW1N0 means allow-write but not allow-net.
|
2018-08-23 19:47:43 -04:00
|
|
|
// See tools/unit_tests.py for more details.
|
|
|
|
|
2020-02-26 14:33:18 -05:00
|
|
|
import { readLines } from "../../std/io/bufio.ts";
|
2019-10-04 20:28:51 -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,
|
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,
|
2019-06-21 19:00:14 -04:00
|
|
|
assertStrictEq,
|
2019-08-27 11:33:39 -04:00
|
|
|
assertStrContains,
|
2020-02-03 09:54:47 -05:00
|
|
|
unreachable,
|
|
|
|
fail
|
2019-10-04 20:28:51 -04:00
|
|
|
} from "../../std/testing/asserts.ts";
|
2018-08-23 19:47:43 -04:00
|
|
|
|
2019-05-08 19:15:24 -04:00
|
|
|
interface TestPermissions {
|
2019-02-08 15:59:38 -05:00
|
|
|
read?: boolean;
|
2018-08-23 19:47:43 -04:00
|
|
|
write?: boolean;
|
|
|
|
net?: boolean;
|
2018-08-31 07:51:12 -04:00
|
|
|
env?: boolean;
|
2018-11-15 23:07:40 -05:00
|
|
|
run?: boolean;
|
2019-12-05 15:30:20 -05:00
|
|
|
plugin?: boolean;
|
2019-05-23 12:28:29 -04:00
|
|
|
hrtime?: boolean;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
const isGranted = async (name: Deno.PermissionName): Promise<boolean> =>
|
|
|
|
(await Deno.permissions.query({ name })).state === "granted";
|
|
|
|
|
|
|
|
async function getProcessPermissions(): Promise<Permissions> {
|
|
|
|
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"),
|
2019-10-27 11:22:53 -04:00
|
|
|
hrtime: await isGranted("hrtime")
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const processPerms = await getProcessPermissions();
|
2019-05-08 19:15:24 -04:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-10-27 11:22:53 -04:00
|
|
|
function normalizeTestPermissions(perms: TestPermissions): 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,
|
2019-05-23 12:28:29 -04:00
|
|
|
hrtime: !!perms.hrtime
|
2018-08-23 19:47:43 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-03-03 12:22:53 -05:00
|
|
|
// Wrap `TestFunction` in additional assertion that makes sure
|
|
|
|
// the test case does not leak async "ops" - ie. number of async
|
|
|
|
// completed ops after the test is the same as number of dispatched
|
|
|
|
// ops. Note that "unref" ops are ignored since in nature that are
|
|
|
|
// optional.
|
|
|
|
function assertOps(fn: Deno.TestFunction): Deno.TestFunction {
|
|
|
|
return async function asyncOpSanitizer(): Promise<void> {
|
|
|
|
const pre = Deno.metrics();
|
|
|
|
await fn();
|
|
|
|
const post = Deno.metrics();
|
|
|
|
// We're checking diff because one might spawn HTTP server in the background
|
|
|
|
// that will be a pending async op before test starts.
|
|
|
|
assertEquals(
|
|
|
|
post.opsDispatchedAsync - pre.opsDispatchedAsync,
|
|
|
|
post.opsCompletedAsync - pre.opsCompletedAsync,
|
|
|
|
`Test case is leaking async ops.
|
|
|
|
Before:
|
|
|
|
- dispatched: ${pre.opsDispatchedAsync}
|
|
|
|
- completed: ${pre.opsCompletedAsync}
|
|
|
|
After:
|
|
|
|
- dispatched: ${post.opsDispatchedAsync}
|
|
|
|
- completed: ${post.opsCompletedAsync}`
|
|
|
|
);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-02-29 12:45:47 -05:00
|
|
|
// Wrap `TestFunction` in additional assertion that makes sure
|
|
|
|
// the test case does not "leak" resources - ie. resource table after
|
|
|
|
// the test has exactly the same contents as before the test.
|
|
|
|
function assertResources(fn: Deno.TestFunction): Deno.TestFunction {
|
2020-03-03 12:22:53 -05:00
|
|
|
return async function resourceSanitizer(): Promise<void> {
|
2020-03-04 11:31:14 -05:00
|
|
|
const pre = Deno.resources();
|
2020-02-29 12:45:47 -05:00
|
|
|
await fn();
|
2020-03-04 11:31:14 -05:00
|
|
|
const post = Deno.resources();
|
2020-02-29 12:45:47 -05:00
|
|
|
const msg = `Test case is leaking resources.
|
2020-03-04 11:31:14 -05:00
|
|
|
Before: ${JSON.stringify(pre, null, 2)}
|
|
|
|
After: ${JSON.stringify(post, null, 2)}`;
|
|
|
|
assertEquals(pre, post, msg);
|
2020-02-29 12:45:47 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
interface UnitTestOptions {
|
|
|
|
skip?: boolean;
|
|
|
|
perms?: TestPermissions;
|
|
|
|
}
|
2019-05-08 19:15:24 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
export function unitTest(fn: Deno.TestFunction): void;
|
|
|
|
export function unitTest(options: UnitTestOptions, fn: Deno.TestFunction): void;
|
|
|
|
export function unitTest(
|
|
|
|
optionsOrFn: UnitTestOptions | Deno.TestFunction,
|
|
|
|
maybeFn?: Deno.TestFunction
|
|
|
|
): void {
|
|
|
|
assert(optionsOrFn, "At least one argument is required");
|
|
|
|
|
|
|
|
let options: UnitTestOptions;
|
|
|
|
let name: string;
|
|
|
|
let fn: Deno.TestFunction;
|
|
|
|
|
|
|
|
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
|
|
|
if (options.skip) {
|
2019-05-08 19:15:24 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
const normalizedPerms = normalizeTestPermissions(options.perms || {});
|
|
|
|
registerPermCombination(normalizedPerms);
|
|
|
|
if (!permissionsMatch(processPerms, normalizedPerms)) {
|
|
|
|
return;
|
|
|
|
}
|
2018-08-23 19:47:43 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
const testDefinition: Deno.TestDefinition = {
|
|
|
|
name,
|
|
|
|
fn: assertResources(assertOps(fn))
|
|
|
|
};
|
|
|
|
Deno.test(testDefinition);
|
2018-08-23 19:47:43 -04:00
|
|
|
}
|
|
|
|
|
2019-05-08 19:15:24 -04:00
|
|
|
function extractNumber(re: RegExp, str: string): number | undefined {
|
|
|
|
const match = str.match(re);
|
|
|
|
|
|
|
|
if (match) {
|
|
|
|
return Number.parseInt(match[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-26 14:33:18 -05:00
|
|
|
export async function parseUnitTestOutput(
|
|
|
|
reader: Deno.Reader,
|
2019-05-08 19:15:24 -04:00
|
|
|
print: boolean
|
2020-02-26 14:33:18 -05:00
|
|
|
): Promise<{ actual?: number; expected?: number; resultOutput?: string }> {
|
2019-05-08 19:15:24 -04:00
|
|
|
let expected, actual, result;
|
|
|
|
|
2020-02-26 14:33:18 -05:00
|
|
|
for await (const line of readLines(reader)) {
|
2019-05-08 19:15:24 -04:00
|
|
|
if (!expected) {
|
|
|
|
// expect "running 30 tests"
|
|
|
|
expected = extractNumber(/running (\d+) tests/, line);
|
|
|
|
} else if (line.indexOf("test result:") !== -1) {
|
|
|
|
result = line;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (print) {
|
|
|
|
console.log(line);
|
2018-08-23 19:47:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-08 19:15:24 -04:00
|
|
|
// Check that the number of expected tests equals what was reported at the
|
|
|
|
// bottom.
|
|
|
|
if (result) {
|
|
|
|
// result should be a string like this:
|
|
|
|
// "test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; ..."
|
|
|
|
actual = extractNumber(/(\d+) passed/, result);
|
2018-08-23 19:47:43 -04:00
|
|
|
}
|
2019-05-08 19:15:24 -04:00
|
|
|
|
|
|
|
return { actual, expected, resultOutput: result };
|
|
|
|
}
|
|
|
|
|
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-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,
|
2019-05-23 12:28:29 -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,
|
2019-05-23 12:28:29 -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,
|
2019-05-23 12:28:29 -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,
|
2019-05-23 12:28:29 -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,
|
2019-05-23 12:28:29 -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,
|
2019-05-23 12:28:29 -04:00
|
|
|
hrtime: true
|
2019-05-08 19:15:24 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true } },
|
|
|
|
async function parsingUnitTestOutput(): Promise<void> {
|
|
|
|
const cwd = Deno.cwd();
|
|
|
|
const testDataPath = `${cwd}/tools/testdata/`;
|
|
|
|
|
|
|
|
let result;
|
|
|
|
|
|
|
|
// This is an example of a successful unit test output.
|
|
|
|
const f1 = await Deno.open(`${testDataPath}/unit_test_output1.txt`);
|
|
|
|
result = await parseUnitTestOutput(f1, false);
|
|
|
|
assertEquals(result.actual, 96);
|
|
|
|
assertEquals(result.expected, 96);
|
|
|
|
f1.close();
|
|
|
|
|
|
|
|
// This is an example of a silently dying unit test.
|
|
|
|
const f2 = await Deno.open(`${testDataPath}/unit_test_output2.txt`);
|
|
|
|
result = await parseUnitTestOutput(f2, false);
|
|
|
|
assertEquals(result.actual, undefined);
|
|
|
|
assertEquals(result.expected, 96);
|
|
|
|
f2.close();
|
|
|
|
|
|
|
|
// This is an example of compiling before successful unit tests.
|
|
|
|
const f3 = await Deno.open(`${testDataPath}/unit_test_output3.txt`);
|
|
|
|
result = await parseUnitTestOutput(f3, false);
|
|
|
|
assertEquals(result.actual, 96);
|
|
|
|
assertEquals(result.expected, 96);
|
|
|
|
f3.close();
|
|
|
|
|
|
|
|
// Check what happens on empty output.
|
|
|
|
const f = new Deno.Buffer(new TextEncoder().encode("\n\n\n"));
|
|
|
|
result = await parseUnitTestOutput(f, false);
|
|
|
|
assertEquals(result.actual, undefined);
|
|
|
|
assertEquals(result.expected, undefined);
|
|
|
|
}
|
|
|
|
);
|
2020-02-05 03:23:23 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Ensure all unit test files (e.g. xxx_test.ts) are present as imports in
|
|
|
|
* cli/js/unit_tests.ts as it is easy to miss this out
|
|
|
|
*/
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true } },
|
2020-02-05 03:23:23 -05:00
|
|
|
async function assertAllUnitTestFilesImported(): Promise<void> {
|
|
|
|
const directoryTestFiles = Deno.readDirSync("./cli/js")
|
|
|
|
.map(k => k.name)
|
2020-02-19 15:36:18 -05:00
|
|
|
.filter(file => file!.endsWith("_test.ts"));
|
2020-02-05 03:23:23 -05:00
|
|
|
const unitTestsFile: Uint8Array = Deno.readFileSync(
|
|
|
|
"./cli/js/unit_tests.ts"
|
|
|
|
);
|
|
|
|
const importLines = new TextDecoder("utf-8")
|
|
|
|
.decode(unitTestsFile)
|
|
|
|
.split("\n")
|
|
|
|
.filter(line => line.startsWith("import") && line.includes("_test.ts"));
|
|
|
|
const importedTestFiles = importLines.map(
|
2020-02-19 15:36:18 -05:00
|
|
|
relativeFilePath => relativeFilePath.match(/\/([^\/]+)";/)![1]
|
2020-02-05 03:23:23 -05: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(
|
|
|
|
"cil/js/unit_tests.ts is missing import of test file: cli/js/" +
|
|
|
|
dirFile
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|