Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(testing/unstable): add API for configuring global sanitizer state #6282

Merged
merged 2 commits into from
Dec 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions testing/_test_suite.ts
Original file line number Diff line number Diff line change
@@ -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<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,
@@ -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) {
@@ -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 = {
7 changes: 4 additions & 3 deletions testing/bdd.ts
Original file line number Diff line number Diff line change
@@ -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<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,
5 changes: 3 additions & 2 deletions testing/deno.json
Original file line number Diff line number Diff line change
@@ -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);

Check warning on line 12 in testing/testdata/configure_global_sanitizers/disable_sanitize_ops_and_resources.ts

Codecov / codecov/patch

testing/testdata/configure_global_sanitizers/disable_sanitize_ops_and_resources.ts#L12

Added line #L12 was not covered by tests
});

describe("leaks ops", () => {
it("leaks ops", () => {
setTimeout(() => {}, 1000);

Check warning on line 17 in testing/testdata/configure_global_sanitizers/disable_sanitize_ops_and_resources.ts

Codecov / codecov/patch

testing/testdata/configure_global_sanitizers/disable_sanitize_ops_and_resources.ts#L17

Added line #L17 was not covered by tests
});
});
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);
}
});