diff --git a/testing/_test_suite.ts b/testing/_test_suite.ts index a0450c3defec..a3a85f4e4591 100644 --- a/testing/_test_suite.ts +++ b/testing/_test_suite.ts @@ -3,6 +3,11 @@ import { getAssertionState } from "@std/internal/assertion-state"; import { AssertionError } from "@std/assert/assertion-error"; +export const globalSanitizersState = { + sanitizeExit: undefined as boolean | undefined, + sanitizeOps: undefined as boolean | undefined, + sanitizeResources: undefined as boolean | undefined, +}; const assertionState = getAssertionState(); /** The options for creating a test suite with the describe function. */ @@ -88,9 +93,9 @@ export class TestSuiteInternal implements TestSuite { name, only, permissions, - sanitizeExit, - sanitizeOps, - sanitizeResources, + sanitizeExit = globalSanitizersState.sanitizeExit, + sanitizeOps = globalSanitizersState.sanitizeOps, + sanitizeResources = globalSanitizersState.sanitizeResources, } = describe; const options: Deno.TestDefinition = { name, @@ -139,9 +144,9 @@ export class TestSuiteInternal implements TestSuite { name, ignore, permissions, - sanitizeExit, - sanitizeOps, - sanitizeResources, + sanitizeExit = globalSanitizersState.sanitizeExit, + sanitizeOps = globalSanitizersState.sanitizeOps, + sanitizeResources = globalSanitizersState.sanitizeResources, } = describe; let { only } = describe; if (!ignore && this.hasOnlyStep) { @@ -331,9 +336,9 @@ export class TestSuiteInternal implements TestSuite { fn, ignore, permissions, - sanitizeExit, - sanitizeOps, - sanitizeResources, + sanitizeExit = globalSanitizersState.sanitizeExit, + sanitizeOps = globalSanitizersState.sanitizeOps, + sanitizeResources = globalSanitizersState.sanitizeResources, } = step instanceof TestSuiteInternal ? step.describe : step; const options: Deno.TestStepDefinition = { diff --git a/testing/bdd.ts b/testing/bdd.ts index 66a754de231f..1c0c32d7ebb6 100644 --- a/testing/bdd.ts +++ b/testing/bdd.ts @@ -406,6 +406,7 @@ import { getAssertionState } from "@std/internal/assertion-state"; import { AssertionError } from "@std/assert/assertion-error"; import { type DescribeDefinition, + globalSanitizersState, type HookNames, type ItDefinition, type TestSuite, @@ -584,9 +585,9 @@ export function it(...args: ItArgs) { ignore, only, permissions, - sanitizeExit, - sanitizeOps, - sanitizeResources, + sanitizeExit = globalSanitizersState.sanitizeExit, + sanitizeOps = globalSanitizersState.sanitizeOps, + sanitizeResources = globalSanitizersState.sanitizeResources, } = options; const opts: Deno.TestDefinition = { name, diff --git a/testing/deno.json b/testing/deno.json index 4459a3a362d1..6a614b943a30 100644 --- a/testing/deno.json +++ b/testing/deno.json @@ -7,7 +7,8 @@ "./snapshot": "./snapshot.ts", "./time": "./time.ts", "./types": "./types.ts", - "./unstable-types": "./unstable_types.ts", - "./unstable-stub": "./unstable_stub.ts" + "./unstable-bdd": "./unstable_bdd.ts", + "./unstable-stub": "./unstable_stub.ts", + "./unstable-types": "./unstable_types.ts" } } diff --git a/testing/testdata/configure_global_sanitizers/disable_sanitize_exit.ts b/testing/testdata/configure_global_sanitizers/disable_sanitize_exit.ts new file mode 100644 index 000000000000..6952c0458ae7 --- /dev/null +++ b/testing/testdata/configure_global_sanitizers/disable_sanitize_exit.ts @@ -0,0 +1,14 @@ +// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. + +import { describe, it } from "@std/testing/bdd"; +import { configureGlobalSanitizers } from "@std/testing/unstable-bdd"; + +configureGlobalSanitizers({ + sanitizeExit: false, +}); + +describe("does not sanitize exit", () => { + it("does not sanitize exit", () => { + Deno.exit(42); + }); +}); diff --git a/testing/testdata/configure_global_sanitizers/disable_sanitize_ops_and_resources.ts b/testing/testdata/configure_global_sanitizers/disable_sanitize_ops_and_resources.ts new file mode 100644 index 000000000000..2aa997b42880 --- /dev/null +++ b/testing/testdata/configure_global_sanitizers/disable_sanitize_ops_and_resources.ts @@ -0,0 +1,19 @@ +// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. + +import { describe, it } from "@std/testing/bdd"; +import { configureGlobalSanitizers } from "@std/testing/unstable-bdd"; + +configureGlobalSanitizers({ + sanitizeOps: false, + sanitizeResources: false, +}); + +it("leaks ops", () => { + setTimeout(() => {}, 1000); +}); + +describe("leaks ops", () => { + it("leaks ops", () => { + setTimeout(() => {}, 1000); + }); +}); diff --git a/testing/testdata/configure_global_sanitizers/disable_sanitize_resources.ts b/testing/testdata/configure_global_sanitizers/disable_sanitize_resources.ts new file mode 100644 index 000000000000..9a8a59aac24d --- /dev/null +++ b/testing/testdata/configure_global_sanitizers/disable_sanitize_resources.ts @@ -0,0 +1,16 @@ +// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. + +import { describe, it } from "@std/testing/bdd"; +import { configureGlobalSanitizers } from "@std/testing/unstable-bdd"; + +configureGlobalSanitizers({ sanitizeResources: false }); + +it("leaks resources", async () => { + const _file = await Deno.open("README.md"); +}); + +describe("leaking ops and resource", () => { + it("leaks resources", async () => { + const _file = await Deno.open("README.md"); + }); +}); diff --git a/testing/unstable_bdd.ts b/testing/unstable_bdd.ts new file mode 100644 index 000000000000..5bc73f70d625 --- /dev/null +++ b/testing/unstable_bdd.ts @@ -0,0 +1,17 @@ +// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. + +import { globalSanitizersState } from "./_test_suite.ts"; + +export type ConfigureGlobalSanitizersOptions = { + sanitizeOps?: boolean; + sanitizeResources?: boolean; + sanitizeExit?: boolean; +}; + +export function configureGlobalSanitizers( + options: ConfigureGlobalSanitizersOptions, +): void { + globalSanitizersState.sanitizeOps = options.sanitizeOps; + globalSanitizersState.sanitizeResources = options.sanitizeResources; + globalSanitizersState.sanitizeExit = options.sanitizeExit; +} diff --git a/testing/unstable_bdd_test.ts b/testing/unstable_bdd_test.ts new file mode 100644 index 000000000000..2e74c3bd2066 --- /dev/null +++ b/testing/unstable_bdd_test.ts @@ -0,0 +1,38 @@ +// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. + +import { assertEquals } from "@std/assert"; + +Deno.test("configureGlobalSanitizers() modifies the test sanitizers globally", async () => { + { + const output = await new Deno.Command(Deno.execPath(), { + args: [ + "test", + "-R", + "testing/testdata/configure_global_sanitizers/disable_sanitize_resources.ts", + ], + }).output(); + assertEquals(output.code, 0); + } + + { + const output = await new Deno.Command(Deno.execPath(), { + args: [ + "test", + "-R", + "testing/testdata/configure_global_sanitizers/disable_sanitize_ops_and_resources.ts", + ], + }).output(); + assertEquals(output.code, 0); + } + + { + const output = await new Deno.Command(Deno.execPath(), { + args: [ + "test", + "-R", + "testing/testdata/configure_global_sanitizers/disable_sanitize_exit.ts", + ], + }).output(); + assertEquals(output.code, 42); + } +});