2020-02-11 06:01:56 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-03-15 12:58:59 -04:00
|
|
|
import { gray, green, italic, red, yellow } from "./colors.ts";
|
2020-03-08 08:09:22 -04:00
|
|
|
import { exit } from "./ops/os.ts";
|
2020-03-15 12:58:59 -04:00
|
|
|
import { Console, stringifyArgs } from "./web/console.ts";
|
|
|
|
import { stdout } from "./files.ts";
|
|
|
|
import { TextEncoder } from "./web/text_encoding.ts";
|
2020-03-18 19:25:55 -04:00
|
|
|
import { metrics } from "./ops/runtime.ts";
|
|
|
|
import { resources } from "./ops/resources.ts";
|
|
|
|
import { assert } from "./util.ts";
|
2020-02-11 06:01:56 -05:00
|
|
|
|
2020-03-13 10:57:32 -04:00
|
|
|
const RED_FAILED = red("FAILED");
|
2020-03-15 12:58:59 -04:00
|
|
|
const GREEN_OK = green("ok");
|
2020-03-19 05:58:12 -04:00
|
|
|
const YELLOW_IGNORED = yellow("ignored");
|
2020-03-13 10:57:32 -04:00
|
|
|
const disabledConsole = new Console((_x: string, _isErr?: boolean): void => {});
|
|
|
|
|
2020-03-05 05:52:18 -05:00
|
|
|
function formatDuration(time = 0): string {
|
|
|
|
const timeStr = `(${time}ms)`;
|
|
|
|
return gray(italic(timeStr));
|
2020-02-11 06:01:56 -05:00
|
|
|
}
|
|
|
|
|
2020-03-18 19:25:55 -04: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: TestFunction): TestFunction {
|
|
|
|
return async function asyncOpSanitizer(): Promise<void> {
|
|
|
|
const pre = metrics();
|
|
|
|
await fn();
|
|
|
|
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}`
|
|
|
|
);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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: TestFunction): TestFunction {
|
|
|
|
return async function resourceSanitizer(): Promise<void> {
|
|
|
|
const pre = resources();
|
|
|
|
await fn();
|
|
|
|
const post = resources();
|
|
|
|
|
|
|
|
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}`;
|
|
|
|
assert(preStr === postStr, msg);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-02-11 06:01:56 -05:00
|
|
|
export type TestFunction = () => void | Promise<void>;
|
|
|
|
|
|
|
|
export interface TestDefinition {
|
|
|
|
fn: TestFunction;
|
|
|
|
name: string;
|
2020-03-19 05:58:12 -04:00
|
|
|
ignore?: boolean;
|
2020-03-18 19:25:55 -04:00
|
|
|
disableOpSanitizer?: boolean;
|
|
|
|
disableResourceSanitizer?: boolean;
|
2020-02-11 06:01:56 -05:00
|
|
|
}
|
|
|
|
|
2020-03-05 05:52:18 -05:00
|
|
|
const TEST_REGISTRY: TestDefinition[] = [];
|
2020-02-11 06:01:56 -05:00
|
|
|
|
|
|
|
export function test(t: TestDefinition): void;
|
|
|
|
export function test(fn: TestFunction): void;
|
|
|
|
export function test(name: string, fn: TestFunction): void;
|
|
|
|
// Main test function provided by Deno, as you can see it merely
|
|
|
|
// creates a new object with "name" and "fn" fields.
|
|
|
|
export function test(
|
|
|
|
t: string | TestDefinition | TestFunction,
|
|
|
|
fn?: TestFunction
|
|
|
|
): void {
|
2020-03-15 05:34:24 -04:00
|
|
|
let testDef: TestDefinition;
|
2020-02-11 06:01:56 -05:00
|
|
|
|
|
|
|
if (typeof t === "string") {
|
2020-03-15 05:34:24 -04:00
|
|
|
if (!fn || typeof fn != "function") {
|
|
|
|
throw new TypeError("Missing test function");
|
2020-02-11 06:01:56 -05:00
|
|
|
}
|
2020-03-15 05:34:24 -04:00
|
|
|
if (!t) {
|
|
|
|
throw new TypeError("The test name can't be empty");
|
2020-02-11 06:01:56 -05:00
|
|
|
}
|
2020-03-19 05:58:12 -04:00
|
|
|
testDef = { fn: fn as TestFunction, name: t, ignore: false };
|
2020-02-11 06:01:56 -05:00
|
|
|
} else if (typeof t === "function") {
|
2020-03-15 05:34:24 -04:00
|
|
|
if (!t.name) {
|
|
|
|
throw new TypeError("The test function can't be anonymous");
|
2020-02-11 06:01:56 -05:00
|
|
|
}
|
2020-03-19 05:58:12 -04:00
|
|
|
testDef = { fn: t, name: t.name, ignore: false };
|
2020-02-11 06:01:56 -05:00
|
|
|
} else {
|
2020-03-15 05:34:24 -04:00
|
|
|
if (!t.fn) {
|
|
|
|
throw new TypeError("Missing test function");
|
2020-02-11 06:01:56 -05:00
|
|
|
}
|
2020-03-15 05:34:24 -04:00
|
|
|
if (!t.name) {
|
|
|
|
throw new TypeError("The test name can't be empty");
|
2020-02-11 06:01:56 -05:00
|
|
|
}
|
2020-03-19 05:58:12 -04:00
|
|
|
testDef = { ...t, ignore: Boolean(t.ignore) };
|
2020-03-18 19:25:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (testDef.disableOpSanitizer !== true) {
|
|
|
|
testDef.fn = assertOps(testDef.fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (testDef.disableResourceSanitizer !== true) {
|
|
|
|
testDef.fn = assertResources(testDef.fn);
|
2020-02-11 06:01:56 -05:00
|
|
|
}
|
|
|
|
|
2020-03-15 05:34:24 -04:00
|
|
|
TEST_REGISTRY.push(testDef);
|
2020-02-11 06:01:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
interface TestStats {
|
|
|
|
filtered: number;
|
|
|
|
ignored: number;
|
|
|
|
measured: number;
|
|
|
|
passed: number;
|
|
|
|
failed: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface RunTestsOptions {
|
|
|
|
exitOnFail?: boolean;
|
2020-03-05 05:52:18 -05:00
|
|
|
failFast?: boolean;
|
|
|
|
only?: string | RegExp;
|
|
|
|
skip?: string | RegExp;
|
2020-02-11 06:01:56 -05:00
|
|
|
disableLog?: boolean;
|
2020-03-13 10:57:32 -04:00
|
|
|
reporter?: TestReporter;
|
|
|
|
}
|
|
|
|
|
2020-03-15 05:34:24 -04:00
|
|
|
enum TestStatus {
|
|
|
|
Passed = "passed",
|
|
|
|
Failed = "failed",
|
2020-03-19 05:58:12 -04:00
|
|
|
Ignored = "ignored"
|
2020-03-15 05:34:24 -04:00
|
|
|
}
|
|
|
|
|
2020-03-13 10:57:32 -04:00
|
|
|
interface TestResult {
|
|
|
|
name: string;
|
2020-03-15 05:34:24 -04:00
|
|
|
status: TestStatus;
|
2020-03-15 12:58:59 -04:00
|
|
|
duration: number;
|
2020-03-13 10:57:32 -04:00
|
|
|
error?: Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
export enum TestEvent {
|
|
|
|
Start = "start",
|
2020-03-15 12:58:59 -04:00
|
|
|
TestStart = "testStart",
|
|
|
|
TestEnd = "testEnd",
|
2020-03-13 10:57:32 -04:00
|
|
|
End = "end"
|
|
|
|
}
|
|
|
|
|
|
|
|
interface TestEventStart {
|
|
|
|
kind: TestEvent.Start;
|
|
|
|
tests: number;
|
|
|
|
}
|
|
|
|
|
2020-03-15 12:58:59 -04:00
|
|
|
interface TestEventTestStart {
|
|
|
|
kind: TestEvent.TestStart;
|
|
|
|
name: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface TestEventTestEnd {
|
|
|
|
kind: TestEvent.TestEnd;
|
2020-03-13 10:57:32 -04:00
|
|
|
result: TestResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface TestEventEnd {
|
|
|
|
kind: TestEvent.End;
|
|
|
|
stats: TestStats;
|
|
|
|
duration: number;
|
|
|
|
results: TestResult[];
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: already implements AsyncGenerator<RunTestsMessage>, but add as "implements to class"
|
|
|
|
// TODO: implements PromiseLike<TestsResult>
|
|
|
|
class TestApi {
|
|
|
|
readonly testsToRun: TestDefinition[];
|
|
|
|
readonly stats: TestStats = {
|
|
|
|
filtered: 0,
|
|
|
|
ignored: 0,
|
|
|
|
measured: 0,
|
|
|
|
passed: 0,
|
|
|
|
failed: 0
|
|
|
|
};
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
public tests: TestDefinition[],
|
|
|
|
public filterFn: (def: TestDefinition) => boolean,
|
|
|
|
public failFast: boolean
|
|
|
|
) {
|
|
|
|
this.testsToRun = tests.filter(filterFn);
|
|
|
|
this.stats.filtered = tests.length - this.testsToRun.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
async *[Symbol.asyncIterator](): AsyncIterator<
|
2020-03-15 12:58:59 -04:00
|
|
|
TestEventStart | TestEventTestStart | TestEventTestEnd | TestEventEnd
|
2020-03-13 10:57:32 -04:00
|
|
|
> {
|
|
|
|
yield {
|
|
|
|
kind: TestEvent.Start,
|
|
|
|
tests: this.testsToRun.length
|
|
|
|
};
|
|
|
|
|
2020-03-15 05:34:24 -04:00
|
|
|
const results: TestResult[] = [];
|
2020-03-13 10:57:32 -04:00
|
|
|
const suiteStart = +new Date();
|
2020-03-19 05:58:12 -04:00
|
|
|
for (const { name, fn, ignore } of this.testsToRun) {
|
2020-03-15 12:58:59 -04:00
|
|
|
const result: Partial<TestResult> = { name, duration: 0 };
|
|
|
|
yield { kind: TestEvent.TestStart, name };
|
2020-03-19 05:58:12 -04:00
|
|
|
if (ignore) {
|
|
|
|
result.status = TestStatus.Ignored;
|
2020-03-15 05:34:24 -04:00
|
|
|
this.stats.ignored++;
|
|
|
|
} else {
|
2020-03-13 10:57:32 -04:00
|
|
|
const start = +new Date();
|
2020-03-15 05:34:24 -04:00
|
|
|
try {
|
|
|
|
await fn();
|
|
|
|
result.status = TestStatus.Passed;
|
|
|
|
this.stats.passed++;
|
|
|
|
} catch (err) {
|
|
|
|
result.status = TestStatus.Failed;
|
|
|
|
result.error = err;
|
|
|
|
this.stats.failed++;
|
2020-03-15 12:58:59 -04:00
|
|
|
} finally {
|
|
|
|
result.duration = +new Date() - start;
|
2020-03-13 10:57:32 -04:00
|
|
|
}
|
|
|
|
}
|
2020-03-15 12:58:59 -04:00
|
|
|
yield { kind: TestEvent.TestEnd, result: result as TestResult };
|
2020-03-15 05:34:24 -04:00
|
|
|
results.push(result as TestResult);
|
|
|
|
if (this.failFast && result.error != null) {
|
|
|
|
break;
|
|
|
|
}
|
2020-03-13 10:57:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const duration = +new Date() - suiteStart;
|
|
|
|
|
|
|
|
yield {
|
|
|
|
kind: TestEvent.End,
|
|
|
|
stats: this.stats,
|
|
|
|
results,
|
|
|
|
duration
|
|
|
|
};
|
|
|
|
}
|
2020-02-11 06:01:56 -05:00
|
|
|
}
|
|
|
|
|
2020-03-13 10:57:32 -04:00
|
|
|
function createFilterFn(
|
2020-03-05 05:52:18 -05:00
|
|
|
only: undefined | string | RegExp,
|
|
|
|
skip: undefined | string | RegExp
|
2020-03-13 10:57:32 -04:00
|
|
|
): (def: TestDefinition) => boolean {
|
|
|
|
return (def: TestDefinition): boolean => {
|
2020-03-05 05:52:18 -05:00
|
|
|
let passes = true;
|
|
|
|
|
|
|
|
if (only) {
|
|
|
|
if (only instanceof RegExp) {
|
|
|
|
passes = passes && only.test(def.name);
|
|
|
|
} else {
|
|
|
|
passes = passes && def.name.includes(only);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (skip) {
|
|
|
|
if (skip instanceof RegExp) {
|
|
|
|
passes = passes && !skip.test(def.name);
|
|
|
|
} else {
|
|
|
|
passes = passes && !def.name.includes(skip);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return passes;
|
2020-03-13 10:57:32 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
interface TestReporter {
|
|
|
|
start(msg: TestEventStart): Promise<void>;
|
2020-03-15 12:58:59 -04:00
|
|
|
testStart(msg: TestEventTestStart): Promise<void>;
|
|
|
|
testEnd(msg: TestEventTestEnd): Promise<void>;
|
2020-03-13 10:57:32 -04:00
|
|
|
end(msg: TestEventEnd): Promise<void>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ConsoleTestReporter implements TestReporter {
|
2020-03-15 12:58:59 -04:00
|
|
|
private encoder: TextEncoder;
|
|
|
|
|
2020-03-13 10:57:32 -04:00
|
|
|
constructor() {
|
2020-03-15 12:58:59 -04:00
|
|
|
this.encoder = new TextEncoder();
|
|
|
|
}
|
|
|
|
|
2020-03-20 09:38:34 -04:00
|
|
|
private log(msg: string, noNewLine = false): Promise<void> {
|
2020-03-15 12:58:59 -04:00
|
|
|
if (!noNewLine) {
|
|
|
|
msg += "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Using `stdout` here because it doesn't force new lines
|
|
|
|
// compared to `console.log`; `core.print` on the other hand
|
|
|
|
// is line-buffered and doesn't output message without newline
|
|
|
|
stdout.writeSync(this.encoder.encode(msg));
|
2020-03-20 09:38:34 -04:00
|
|
|
return Promise.resolve();
|
2020-03-13 10:57:32 -04:00
|
|
|
}
|
|
|
|
|
2020-03-20 09:38:34 -04:00
|
|
|
start(event: TestEventStart): Promise<void> {
|
2020-03-15 12:58:59 -04:00
|
|
|
this.log(`running ${event.tests} tests`);
|
2020-03-20 09:38:34 -04:00
|
|
|
return Promise.resolve();
|
2020-03-13 10:57:32 -04:00
|
|
|
}
|
|
|
|
|
2020-03-20 09:38:34 -04:00
|
|
|
testStart(event: TestEventTestStart): Promise<void> {
|
2020-03-15 12:58:59 -04:00
|
|
|
const { name } = event;
|
|
|
|
|
|
|
|
this.log(`test ${name} ... `, true);
|
2020-03-20 09:38:34 -04:00
|
|
|
return Promise.resolve();
|
2020-03-15 12:58:59 -04:00
|
|
|
}
|
|
|
|
|
2020-03-20 09:38:34 -04:00
|
|
|
testEnd(event: TestEventTestEnd): Promise<void> {
|
2020-03-13 10:57:32 -04:00
|
|
|
const { result } = event;
|
|
|
|
|
2020-03-15 05:34:24 -04:00
|
|
|
switch (result.status) {
|
|
|
|
case TestStatus.Passed:
|
2020-03-15 12:58:59 -04:00
|
|
|
this.log(`${GREEN_OK} ${formatDuration(result.duration)}`);
|
2020-03-15 05:34:24 -04:00
|
|
|
break;
|
|
|
|
case TestStatus.Failed:
|
2020-03-15 12:58:59 -04:00
|
|
|
this.log(`${RED_FAILED} ${formatDuration(result.duration)}`);
|
2020-03-15 05:34:24 -04:00
|
|
|
break;
|
2020-03-19 05:58:12 -04:00
|
|
|
case TestStatus.Ignored:
|
|
|
|
this.log(`${YELLOW_IGNORED} ${formatDuration(result.duration)}`);
|
2020-03-15 05:34:24 -04:00
|
|
|
break;
|
2020-03-13 10:57:32 -04:00
|
|
|
}
|
2020-03-20 09:38:34 -04:00
|
|
|
|
|
|
|
return Promise.resolve();
|
2020-03-13 10:57:32 -04:00
|
|
|
}
|
|
|
|
|
2020-03-20 09:38:34 -04:00
|
|
|
end(event: TestEventEnd): Promise<void> {
|
2020-03-15 12:58:59 -04:00
|
|
|
const { stats, duration, results } = event;
|
2020-03-13 10:57:32 -04:00
|
|
|
// Attempting to match the output of Rust's test runner.
|
2020-03-15 12:58:59 -04:00
|
|
|
const failedTests = results.filter(r => r.error);
|
|
|
|
|
|
|
|
if (failedTests.length > 0) {
|
|
|
|
this.log(`\nfailures:\n`);
|
|
|
|
|
|
|
|
for (const result of failedTests) {
|
|
|
|
this.log(`${result.name}`);
|
|
|
|
this.log(`${stringifyArgs([result.error!])}`);
|
|
|
|
this.log("");
|
|
|
|
}
|
|
|
|
|
|
|
|
this.log(`failures:\n`);
|
|
|
|
|
|
|
|
for (const result of failedTests) {
|
|
|
|
this.log(`\t${result.name}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.log(
|
|
|
|
`\ntest result: ${stats.failed ? RED_FAILED : GREEN_OK}. ` +
|
2020-03-13 10:57:32 -04:00
|
|
|
`${stats.passed} passed; ${stats.failed} failed; ` +
|
|
|
|
`${stats.ignored} ignored; ${stats.measured} measured; ` +
|
|
|
|
`${stats.filtered} filtered out ` +
|
|
|
|
`${formatDuration(duration)}\n`
|
|
|
|
);
|
2020-03-20 09:38:34 -04:00
|
|
|
|
|
|
|
return Promise.resolve();
|
2020-03-13 10:57:32 -04:00
|
|
|
}
|
2020-03-05 05:52:18 -05:00
|
|
|
}
|
|
|
|
|
2020-02-11 06:01:56 -05:00
|
|
|
export async function runTests({
|
2020-03-05 05:52:18 -05:00
|
|
|
exitOnFail = true,
|
|
|
|
failFast = false,
|
|
|
|
only = undefined,
|
|
|
|
skip = undefined,
|
2020-03-13 10:57:32 -04:00
|
|
|
disableLog = false,
|
|
|
|
reporter = undefined
|
|
|
|
}: RunTestsOptions = {}): Promise<{
|
|
|
|
results: TestResult[];
|
|
|
|
stats: TestStats;
|
|
|
|
duration: number;
|
|
|
|
}> {
|
|
|
|
const filterFn = createFilterFn(only, skip);
|
|
|
|
const testApi = new TestApi(TEST_REGISTRY, filterFn, failFast);
|
2020-02-11 06:01:56 -05:00
|
|
|
|
2020-03-13 10:57:32 -04:00
|
|
|
if (!reporter) {
|
|
|
|
reporter = new ConsoleTestReporter();
|
|
|
|
}
|
2020-02-11 06:01:56 -05:00
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
const originalConsole = globalThis.console;
|
|
|
|
|
|
|
|
if (disableLog) {
|
|
|
|
// @ts-ignore
|
|
|
|
globalThis.console = disabledConsole;
|
|
|
|
}
|
|
|
|
|
2020-03-13 10:57:32 -04:00
|
|
|
let endMsg: TestEventEnd;
|
|
|
|
|
|
|
|
for await (const testMsg of testApi) {
|
|
|
|
switch (testMsg.kind) {
|
|
|
|
case TestEvent.Start:
|
|
|
|
await reporter.start(testMsg);
|
|
|
|
continue;
|
2020-03-15 12:58:59 -04:00
|
|
|
case TestEvent.TestStart:
|
|
|
|
await reporter.testStart(testMsg);
|
|
|
|
continue;
|
|
|
|
case TestEvent.TestEnd:
|
|
|
|
await reporter.testEnd(testMsg);
|
2020-03-13 10:57:32 -04:00
|
|
|
continue;
|
|
|
|
case TestEvent.End:
|
|
|
|
endMsg = testMsg;
|
|
|
|
delete endMsg!.kind;
|
|
|
|
await reporter.end(testMsg);
|
|
|
|
continue;
|
2020-02-11 06:01:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (disableLog) {
|
|
|
|
// @ts-ignore
|
|
|
|
globalThis.console = originalConsole;
|
|
|
|
}
|
|
|
|
|
2020-03-13 10:57:32 -04:00
|
|
|
if (endMsg!.stats.failed > 0 && exitOnFail) {
|
|
|
|
exit(1);
|
2020-02-11 06:01:56 -05:00
|
|
|
}
|
2020-03-13 10:57:32 -04:00
|
|
|
|
|
|
|
return endMsg!;
|
2020-02-11 06:01:56 -05:00
|
|
|
}
|