Skip to content

Commit

Permalink
feat(testing/unstable): add API for configuring global sanitizer state (
Browse files Browse the repository at this point in the history
  • Loading branch information
kt3k authored Dec 20, 2024
1 parent 450bdfb commit 990015d
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 14 deletions.
23 changes: 14 additions & 9 deletions testing/_test_suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -88,9 +93,9 @@ export class TestSuiteInternal<T> implements TestSuite<T> {
name,
only,
permissions,
sanitizeExit,
sanitizeOps,
sanitizeResources,
sanitizeExit = globalSanitizersState.sanitizeExit,
sanitizeOps = globalSanitizersState.sanitizeOps,
sanitizeResources = globalSanitizersState.sanitizeResources,
} = describe;
const options: Deno.TestDefinition = {
name,
Expand Down Expand Up @@ -139,9 +144,9 @@ export class TestSuiteInternal<T> implements TestSuite<T> {
name,
ignore,
permissions,
sanitizeExit,
sanitizeOps,
sanitizeResources,
sanitizeExit = globalSanitizersState.sanitizeExit,
sanitizeOps = globalSanitizersState.sanitizeOps,
sanitizeResources = globalSanitizersState.sanitizeResources,
} = describe;
let { only } = describe;
if (!ignore && this.hasOnlyStep) {
Expand Down Expand Up @@ -331,9 +336,9 @@ export class TestSuiteInternal<T> implements TestSuite<T> {
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 = {
Expand Down
7 changes: 4 additions & 3 deletions testing/bdd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -584,9 +585,9 @@ export function it<T>(...args: ItArgs<T>) {
ignore,
only,
permissions,
sanitizeExit,
sanitizeOps,
sanitizeResources,
sanitizeExit = globalSanitizersState.sanitizeExit,
sanitizeOps = globalSanitizersState.sanitizeOps,
sanitizeResources = globalSanitizersState.sanitizeResources,
} = options;
const opts: Deno.TestDefinition = {
name,
Expand Down
5 changes: 3 additions & 2 deletions testing/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
Original file line number Diff line number Diff line change
@@ -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);
});
});
Original file line number Diff line number Diff line change
@@ -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);
});
});
Original file line number Diff line number Diff line change
@@ -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");
});
});
17 changes: 17 additions & 0 deletions testing/unstable_bdd.ts
Original file line number Diff line number Diff line change
@@ -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;
}
38 changes: 38 additions & 0 deletions testing/unstable_bdd_test.ts
Original file line number Diff line number Diff line change
@@ -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);
}
});

0 comments on commit 990015d

Please sign in to comment.