2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2021-02-04 17:18:32 -05:00
|
|
|
"use strict";
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
((window) => {
|
2020-09-17 12:09:50 -04:00
|
|
|
const core = window.Deno.core;
|
2021-04-25 17:38:59 -04:00
|
|
|
const { parsePermissions } = window.__bootstrap.worker;
|
2021-05-18 11:24:01 -04:00
|
|
|
const { setExitHandler } = window.__bootstrap.os;
|
2020-07-19 13:49:44 -04:00
|
|
|
const { Console, inspectArgs } = window.__bootstrap.console;
|
|
|
|
const { metrics } = window.__bootstrap.metrics;
|
|
|
|
const { assert } = window.__bootstrap.util;
|
|
|
|
|
|
|
|
// Wrap test function 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) {
|
|
|
|
return async function asyncOpSanitizer() {
|
|
|
|
const pre = metrics();
|
2021-02-21 11:21:25 -05:00
|
|
|
try {
|
|
|
|
await fn();
|
|
|
|
} finally {
|
|
|
|
// Defer until next event loop turn - that way timeouts and intervals
|
|
|
|
// cleared can actually be removed from resource table, otherwise
|
|
|
|
// false positives may occur (https://github.com/denoland/deno/issues/4591)
|
2021-04-28 14:17:04 -04:00
|
|
|
await new Promise((resolve) => setTimeout(resolve, 0));
|
2021-02-21 11:21:25 -05:00
|
|
|
}
|
2021-04-28 14:17:04 -04:00
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
const post = metrics();
|
|
|
|
// We're checking diff because one might spawn HTTP server in the background
|
|
|
|
// that will be a pending async op before test starts.
|
|
|
|
const dispatchedDiff = post.opsDispatchedAsync - pre.opsDispatchedAsync;
|
|
|
|
const completedDiff = post.opsCompletedAsync - pre.opsCompletedAsync;
|
|
|
|
assert(
|
|
|
|
dispatchedDiff === completedDiff,
|
|
|
|
`Test case is leaking async ops.
|
|
|
|
Before:
|
|
|
|
- dispatched: ${pre.opsDispatchedAsync}
|
|
|
|
- completed: ${pre.opsCompletedAsync}
|
|
|
|
After:
|
|
|
|
- dispatched: ${post.opsDispatchedAsync}
|
|
|
|
- completed: ${post.opsCompletedAsync}
|
|
|
|
|
|
|
|
Make sure to await all promises returned from Deno APIs before
|
|
|
|
finishing test case.`,
|
|
|
|
);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrap test function 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,
|
|
|
|
) {
|
|
|
|
return async function resourceSanitizer() {
|
2020-09-17 12:09:50 -04:00
|
|
|
const pre = core.resources();
|
2020-07-19 13:49:44 -04:00
|
|
|
await fn();
|
2020-09-17 12:09:50 -04:00
|
|
|
const post = core.resources();
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
const preStr = JSON.stringify(pre, null, 2);
|
|
|
|
const postStr = JSON.stringify(post, null, 2);
|
|
|
|
const msg = `Test case is leaking resources.
|
|
|
|
Before: ${preStr}
|
|
|
|
After: ${postStr}
|
|
|
|
|
|
|
|
Make sure to close all open resource handles returned from Deno APIs before
|
|
|
|
finishing test case.`;
|
|
|
|
assert(preStr === postStr, msg);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-02-24 07:55:50 -05:00
|
|
|
// Wrap test function in additional assertion that makes sure
|
|
|
|
// that the test case does not accidentally exit prematurely.
|
|
|
|
function assertExit(fn) {
|
|
|
|
return async function exitSanitizer() {
|
|
|
|
setExitHandler((exitCode) => {
|
|
|
|
assert(
|
|
|
|
false,
|
|
|
|
`Test case attempted to exit with exit code: ${exitCode}`,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
await fn();
|
|
|
|
} catch (err) {
|
|
|
|
throw err;
|
|
|
|
} finally {
|
|
|
|
setExitHandler(null);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-04-28 14:17:04 -04:00
|
|
|
const tests = [];
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
// Main test function provided by Deno, as you can see it merely
|
|
|
|
// creates a new object with "name" and "fn" fields.
|
|
|
|
function test(
|
|
|
|
t,
|
|
|
|
fn,
|
|
|
|
) {
|
|
|
|
let testDef;
|
|
|
|
const defaults = {
|
|
|
|
ignore: false,
|
|
|
|
only: false,
|
|
|
|
sanitizeOps: true,
|
|
|
|
sanitizeResources: true,
|
2021-02-24 07:55:50 -05:00
|
|
|
sanitizeExit: true,
|
2021-04-25 17:38:59 -04:00
|
|
|
permissions: null,
|
2020-07-19 13:49:44 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
if (typeof t === "string") {
|
|
|
|
if (!fn || typeof fn != "function") {
|
|
|
|
throw new TypeError("Missing test function");
|
|
|
|
}
|
|
|
|
if (!t) {
|
|
|
|
throw new TypeError("The test name can't be empty");
|
|
|
|
}
|
|
|
|
testDef = { fn: fn, name: t, ...defaults };
|
|
|
|
} else {
|
|
|
|
if (!t.fn) {
|
|
|
|
throw new TypeError("Missing test function");
|
|
|
|
}
|
|
|
|
if (!t.name) {
|
|
|
|
throw new TypeError("The test name can't be empty");
|
|
|
|
}
|
|
|
|
testDef = { ...defaults, ...t };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (testDef.sanitizeOps) {
|
|
|
|
testDef.fn = assertOps(testDef.fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (testDef.sanitizeResources) {
|
|
|
|
testDef.fn = assertResources(testDef.fn);
|
|
|
|
}
|
|
|
|
|
2021-02-24 07:55:50 -05:00
|
|
|
if (testDef.sanitizeExit) {
|
|
|
|
testDef.fn = assertExit(testDef.fn);
|
|
|
|
}
|
|
|
|
|
2021-04-28 14:17:04 -04:00
|
|
|
tests.push(testDef);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-04-28 14:17:04 -04:00
|
|
|
function postTestMessage(kind, data) {
|
|
|
|
return core.opSync("op_post_test_message", { message: { kind, data } });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-04-28 14:17:04 -04:00
|
|
|
function createTestFilter(filter) {
|
|
|
|
return (def) => {
|
|
|
|
if (filter) {
|
|
|
|
if (filter.startsWith("/") && filter.endsWith("/")) {
|
|
|
|
const regex = new RegExp(filter.slice(1, filter.length - 1));
|
|
|
|
return regex.test(def.name);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-04-28 14:17:04 -04:00
|
|
|
return def.name.includes(filter);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-04-28 14:17:04 -04:00
|
|
|
return true;
|
|
|
|
};
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-04-25 17:38:59 -04:00
|
|
|
function pledgeTestPermissions(permissions) {
|
|
|
|
return core.opSync(
|
|
|
|
"op_pledge_test_permissions",
|
|
|
|
parsePermissions(permissions),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function restoreTestPermissions(token) {
|
|
|
|
core.opSync("op_restore_test_permissions", token);
|
|
|
|
}
|
|
|
|
|
2021-04-28 14:17:04 -04:00
|
|
|
async function runTest({ name, ignore, fn, permissions }) {
|
|
|
|
let token = null;
|
|
|
|
const time = Date.now();
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-04-28 14:17:04 -04:00
|
|
|
try {
|
|
|
|
postTestMessage("wait", {
|
|
|
|
name,
|
|
|
|
});
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-04-28 14:17:04 -04:00
|
|
|
if (permissions) {
|
|
|
|
token = pledgeTestPermissions(permissions);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-04-28 14:17:04 -04:00
|
|
|
if (ignore) {
|
|
|
|
const duration = Date.now() - time;
|
|
|
|
postTestMessage("result", {
|
|
|
|
name,
|
|
|
|
duration,
|
|
|
|
result: "ignored",
|
|
|
|
});
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-04-28 14:17:04 -04:00
|
|
|
return;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-04-28 14:17:04 -04:00
|
|
|
await fn();
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-04-28 14:17:04 -04:00
|
|
|
const duration = Date.now() - time;
|
|
|
|
postTestMessage("result", {
|
|
|
|
name,
|
|
|
|
duration,
|
|
|
|
result: "ok",
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
const duration = Date.now() - time;
|
|
|
|
|
|
|
|
postTestMessage("result", {
|
|
|
|
name,
|
|
|
|
duration,
|
|
|
|
result: {
|
|
|
|
"failed": inspectArgs([error]),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} finally {
|
|
|
|
if (token) {
|
|
|
|
restoreTestPermissions(token);
|
|
|
|
}
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function runTests({
|
|
|
|
disableLog = false,
|
2021-04-28 14:17:04 -04:00
|
|
|
filter = null,
|
2020-07-19 13:49:44 -04:00
|
|
|
} = {}) {
|
|
|
|
const originalConsole = globalThis.console;
|
|
|
|
if (disableLog) {
|
2021-04-28 14:17:04 -04:00
|
|
|
globalThis.console = new Console(() => {});
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-04-28 14:17:04 -04:00
|
|
|
const only = tests.filter((test) => test.only);
|
|
|
|
const pending = (only.length > 0 ? only : tests).filter(
|
|
|
|
createTestFilter(filter),
|
|
|
|
);
|
|
|
|
postTestMessage("plan", {
|
|
|
|
filtered: tests.length - pending.length,
|
|
|
|
pending: pending.length,
|
|
|
|
only: only.length > 0,
|
|
|
|
});
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-04-28 14:17:04 -04:00
|
|
|
for (const test of pending) {
|
|
|
|
await runTest(test);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (disableLog) {
|
|
|
|
globalThis.console = originalConsole;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-12 15:23:59 -05:00
|
|
|
window.__bootstrap.internals = {
|
|
|
|
...window.__bootstrap.internals ?? {},
|
|
|
|
runTests,
|
|
|
|
};
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
window.__bootstrap.testing = {
|
|
|
|
test,
|
|
|
|
};
|
|
|
|
})(this);
|