mirror of
https://github.com/denoland/deno.git
synced 2025-09-27 12:49:10 +00:00
refactor: rewrite testPerm into unitTest (#4231)
Rewrite "testPerm" helper function used for testing of internal runtime code. It's been renamed to "unitTest" and provides API that is extensible in the future by accepting optional "UnitTestOptions" argument. "test" helper was also removed and replaced by overloaded version of "unitTest" that takes only function argument. "UnitTestOptions" currently supports "perms" and "skip" options, where former works exactly as first argument to "testPerm" did, while the latter allows to conditionally skip tests.
This commit is contained in:
parent
30682cf74f
commit
8d96dffa41
60 changed files with 2389 additions and 2047 deletions
|
@ -137,42 +137,65 @@ function assertOps(fn: Deno.TestFunction): Deno.TestFunction {
|
|||
// the test has exactly the same contents as before the test.
|
||||
function assertResources(fn: Deno.TestFunction): Deno.TestFunction {
|
||||
return async function resourceSanitizer(): Promise<void> {
|
||||
const preResources = Deno.resources();
|
||||
const pre = Deno.resources();
|
||||
await fn();
|
||||
const postResources = Deno.resources();
|
||||
const post = Deno.resources();
|
||||
const msg = `Test case is leaking resources.
|
||||
Before: ${JSON.stringify(preResources, null, 2)}
|
||||
After: ${JSON.stringify(postResources, null, 2)}`;
|
||||
|
||||
assertEquals(preResources, postResources, msg);
|
||||
Before: ${JSON.stringify(pre, null, 2)}
|
||||
After: ${JSON.stringify(post, null, 2)}`;
|
||||
assertEquals(pre, post, msg);
|
||||
};
|
||||
}
|
||||
|
||||
export function testPerm(perms: TestPermissions, fn: Deno.TestFunction): void {
|
||||
const normalizedPerms = normalizeTestPermissions(perms);
|
||||
interface UnitTestOptions {
|
||||
skip?: boolean;
|
||||
perms?: TestPermissions;
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
if (options.skip) {
|
||||
return;
|
||||
}
|
||||
|
||||
const normalizedPerms = normalizeTestPermissions(options.perms || {});
|
||||
registerPermCombination(normalizedPerms);
|
||||
|
||||
if (!permissionsMatch(processPerms, normalizedPerms)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Deno.test(fn.name, assertResources(assertOps(fn)));
|
||||
}
|
||||
|
||||
export function test(fn: Deno.TestFunction): void {
|
||||
testPerm(
|
||||
{
|
||||
read: false,
|
||||
write: false,
|
||||
net: false,
|
||||
env: false,
|
||||
run: false,
|
||||
plugin: false,
|
||||
hrtime: false
|
||||
},
|
||||
fn
|
||||
);
|
||||
const testDefinition: Deno.TestDefinition = {
|
||||
name,
|
||||
fn: assertResources(assertOps(fn))
|
||||
};
|
||||
Deno.test(testDefinition);
|
||||
}
|
||||
|
||||
function extractNumber(re: RegExp, str: string): number | undefined {
|
||||
|
@ -231,7 +254,7 @@ export function createResolvable<T>(): Resolvable<T> {
|
|||
return Object.assign(promise, methods!) as Resolvable<T>;
|
||||
}
|
||||
|
||||
test(function permissionsMatches(): void {
|
||||
unitTest(function permissionsMatches(): void {
|
||||
assert(
|
||||
permissionsMatch(
|
||||
{
|
||||
|
@ -318,46 +341,49 @@ test(function permissionsMatches(): void {
|
|||
);
|
||||
});
|
||||
|
||||
testPerm({ read: true }, async function parsingUnitTestOutput(): Promise<void> {
|
||||
const cwd = Deno.cwd();
|
||||
const testDataPath = `${cwd}/tools/testdata/`;
|
||||
unitTest(
|
||||
{ perms: { read: true } },
|
||||
async function parsingUnitTestOutput(): Promise<void> {
|
||||
const cwd = Deno.cwd();
|
||||
const testDataPath = `${cwd}/tools/testdata/`;
|
||||
|
||||
let result;
|
||||
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 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 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();
|
||||
// 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);
|
||||
});
|
||||
// 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);
|
||||
}
|
||||
);
|
||||
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
testPerm(
|
||||
{ read: true },
|
||||
unitTest(
|
||||
{ perms: { read: true } },
|
||||
async function assertAllUnitTestFilesImported(): Promise<void> {
|
||||
const directoryTestFiles = Deno.readDirSync("./cli/js")
|
||||
.map(k => k.name)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue