0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-02-01 12:16:11 -05:00

feat(test): Add Deno.test.disableSanitizers API

This commit is contained in:
Bartek Iwańczuk 2025-01-27 23:30:58 +01:00
parent 679902a108
commit 09db17338c
No known key found for this signature in database
GPG key ID: 0C6BCDDC3B3AD750
14 changed files with 121 additions and 2 deletions

View file

@ -224,8 +224,9 @@ function testInner(
const defaults = {
ignore: false,
only: false,
sanitizeOps: true,
sanitizeResources: true,
sanitizeOps: !sanitizersDisabled,
sanitizeResources: !sanitizersDisabled,
// Exit sanitizer is never disabled
sanitizeExit: true,
permissions: null,
};
@ -355,6 +356,12 @@ test.only = function (
return testInner(nameOrFnOrOptions, optionsOrFn, maybeFn, { only: true });
};
let sanitizersDisabled = false;
test.disableSanitizers = function () {
sanitizersDisabled = true;
};
function getFullName(desc) {
if ("parent" in desc) {
return `${getFullName(desc.parent)} ... ${desc.name}`;

View file

@ -1166,6 +1166,14 @@ declare namespace Deno {
options: Omit<TestDefinition, "fn" | "only">,
fn: (t: TestContext) => void | Promise<void>,
): void;
/** Disable op and resource sanitizers for the whole test file.
*
* Individual tests can still use sanitizers, by specifying relevant
* `sanitizeOps` and `sanitizeResources` options.
* @category Testing
*/
disableSanitizers(): void;
}
/**

View file

@ -0,0 +1,5 @@
{
"args": "test main.ts",
"exitCode": 0,
"output": "main.out"
}

View file

@ -0,0 +1,7 @@
Check [WILDCARD]/main.ts
running 2 tests from [WILDCARD]/main.ts
no-op ... ok ([WILDCARD])
leak interval ... ok ([WILDCARD])
ok | 2 passed | 0 failed ([WILDCARD])

View file

@ -0,0 +1,9 @@
Deno.test.disableSanitizers();
Deno.test("no-op", function () {});
Deno.test({
name: "leak interval",
fn() {
setInterval(function () {}, 100000);
},
});

View file

@ -0,0 +1,5 @@
{
"args": "test --trace-leaks main.ts",
"exitCode": 1,
"output": "main.out"
}

View file

@ -0,0 +1,22 @@
Check [WILDCARD]/main.ts
running 2 tests from [WILDCARD]/main.ts
no-op ... ok ([WILDCARD])
leak interval ... FAILED ([WILDCARD])
ERRORS
leak interval => [WILDCARD]/main.ts:[WILDCARD]
error: Leaks detected:
- An interval was started in this test, but never completed. This is often caused by not calling `clearInterval`. The operation was started here:
at [WILDCARD]
at setInterval ([WILDCARD])
at fn ([WILDCARD]/main.ts:[WILDCARD])
at [WILDCARD]
FAILURES
leak interval => [WILDCARD]/main.ts:[WILDCARD]
FAILED | 1 passed | 1 failed ([WILDCARD])
error: Test failed

View file

@ -0,0 +1,10 @@
Deno.test.disableSanitizers();
Deno.test("no-op", function () {});
Deno.test({
name: "leak interval",
sanitizeOps: true,
fn() {
setInterval(function () {}, 100000);
},
});

View file

@ -0,0 +1,5 @@
{
"args": "test --allow-read main.ts",
"output": "main.out",
"exitCode": 0
}

View file

@ -0,0 +1,6 @@
Check [WILDCARD]main.ts
running 1 test from [WILDCARD]main.ts
leak ... ok ([WILDCARD])
ok | 1 passed | 0 failed ([WILDCARD])

View file

@ -0,0 +1,6 @@
Deno.test.disableSanitizers();
Deno.test("leak", function () {
Deno.openSync("../../../testdata/run/001_hello.js");
Deno.stdin.close();
});

View file

@ -0,0 +1,5 @@
{
"args": "test --allow-read main.ts",
"output": "main.out",
"exitCode": 1
}

View file

@ -0,0 +1,18 @@
Check [WILDCARD]main.ts
running 1 test from [WILDCARD]main.ts
leak ... FAILED (1ms)
ERRORS
leak => [WILDCARD]main.ts:3:6
error: Leaks detected:
- A file was opened during the test, but not closed during the test. Close the file handle by calling `file.close()`.
- The stdin pipe was opened before the test started, but was closed during the test. Do not close resources in a test that were not created during that test.
FAILURES
leak => [WILDCARD]main.ts:3:6
FAILED | 0 passed | 1 failed ([WILDCARD])
error: Test failed

View file

@ -0,0 +1,6 @@
Deno.test.disableSanitizers();
Deno.test("leak", { sanitizeResources: true }, function () {
Deno.openSync("../../../testdata/run/001_hello.js");
Deno.stdin.close();
});