diff --git a/pyproject.toml b/pyproject.toml index 93b01778..0b587240 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -114,6 +114,7 @@ xfail_strict = true junit_family = "xunit2" norecursedirs = "tests/integration/*" markers = [ + "contextvars", "isolated", "pypy3323bug", "network", diff --git a/tests/conftest.py b/tests/conftest.py index 807a1060..7076bdce 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,6 +1,7 @@ # SPDX-License-Identifier: MIT import contextlib +import contextvars import importlib.metadata import os import os.path @@ -10,6 +11,8 @@ import sysconfig import tempfile +from functools import partial, update_wrapper + import pytest import build.env @@ -60,6 +63,16 @@ def is_integration(item): return os.path.basename(item.location[0]) == 'test_integration.py' +def pytest_runtest_call(item: pytest.Item): + if item.get_closest_marker('contextvars'): + if isinstance(item, pytest.Function): + wrapped_function = partial(contextvars.copy_context().run, item.obj) + item.obj = update_wrapper(wrapped_function, item.obj) + else: + msg = 'cannot rewrap non-function item' + raise RuntimeError(msg) + + @pytest.fixture() def local_pip(monkeypatch): monkeypatch.setattr(build.env, '_valid_global_pip', lambda: None) diff --git a/tests/test_ctx_logger.py b/tests/test_ctx_logger.py index 49f9165f..5c609a53 100644 --- a/tests/test_ctx_logger.py +++ b/tests/test_ctx_logger.py @@ -9,6 +9,9 @@ import build._ctx +pytestmark = pytest.mark.contextvars + + def test_default_ctx_logger(caplog: pytest.LogCaptureFixture): build._ctx.log('foo') diff --git a/tests/test_main.py b/tests/test_main.py index f837be82..ecede883 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -14,6 +14,9 @@ import build.__main__ +pytestmark = pytest.mark.contextvars + + build_open_owner = 'builtins' cwd = os.getcwd() diff --git a/tests/test_projectbuilder.py b/tests/test_projectbuilder.py index ba0b8606..77837ead 100644 --- a/tests/test_projectbuilder.py +++ b/tests/test_projectbuilder.py @@ -375,6 +375,7 @@ def demo_pkg_inline(tmp_path_factory): return next(out.iterdir()) +@pytest.mark.contextvars @pytest.mark.isolated def test_build_with_dep_on_console_script(tmp_path, demo_pkg_inline, capfd, mocker): """