From a4c4331076ef252eab5af556c06cae0a947c3da6 Mon Sep 17 00:00:00 2001 From: cbornet Date: Mon, 24 Feb 2025 17:52:32 +0100 Subject: [PATCH] Detect blocking calls in coroutines using BlockBuster --- requirements.txt | 1 + tests/conftest.py | 8 ++++++++ tests/models/test_whatwg.py | 4 +++- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index caa094f33a..04bfe3fe5c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -28,3 +28,4 @@ trio-typing==0.10.0 trustme==1.1.0; python_version < '3.9' trustme==1.2.0; python_version >= '3.9' uvicorn==0.32.1 +blockbuster==1.5.23 diff --git a/tests/conftest.py b/tests/conftest.py index 858bca1397..f50edc4db4 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -7,6 +7,7 @@ import pytest import trustme +from blockbuster import blockbuster_ctx from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives.serialization import ( BestAvailableEncryption, @@ -31,6 +32,13 @@ } +@pytest.fixture(autouse=True) +def blockbuster(): + with blockbuster_ctx() as bb: + bb.functions["os.stat"].can_block_in("/mimetypes.py", "init") + yield bb + + @pytest.fixture(scope="function", autouse=True) def clean_environ(): """Keeps os.environ clean for every test without having to mock os.environ""" diff --git a/tests/models/test_whatwg.py b/tests/models/test_whatwg.py index 14af682586..8f3fa2b0a0 100644 --- a/tests/models/test_whatwg.py +++ b/tests/models/test_whatwg.py @@ -3,6 +3,7 @@ # https://url.spec.whatwg.org/ import json +from pathlib import Path import pytest @@ -10,7 +11,8 @@ # URL test cases from... # https://github.com/web-platform-tests/wpt/blob/master/url/resources/urltestdata.json -with open("tests/models/whatwg.json", "r", encoding="utf-8") as input: +whatwg_path = Path(__file__).parent.parent / "models" / "whatwg.json" +with whatwg_path.open("r", encoding="utf-8") as input: test_cases = json.load(input) test_cases = [ item