Skip to content

Commit

Permalink
Isolate logging context changes
Browse files Browse the repository at this point in the history
  • Loading branch information
layday committed Mar 5, 2024
1 parent b0c56a7 commit 27c1c32
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ xfail_strict = true
junit_family = "xunit2"
norecursedirs = "tests/integration/*"
markers = [
"contextvars",
"isolated",
"pypy3323bug",
"network",
Expand Down
13 changes: 13 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: MIT

import contextlib
import contextvars
import importlib.metadata
import os
import os.path
Expand All @@ -10,6 +11,8 @@
import sysconfig
import tempfile

from functools import partial, update_wrapper

import pytest

import build.env
Expand Down Expand Up @@ -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)

Check warning on line 73 in tests/conftest.py

View check run for this annotation

Codecov / codecov/patch

tests/conftest.py#L72-L73

Added lines #L72 - L73 were not covered by tests


@pytest.fixture()
def local_pip(monkeypatch):
monkeypatch.setattr(build.env, '_valid_global_pip', lambda: None)
Expand Down
3 changes: 3 additions & 0 deletions tests/test_ctx_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import build._ctx


pytestmark = pytest.mark.contextvars


def test_default_ctx_logger(caplog: pytest.LogCaptureFixture):
build._ctx.log('foo')

Expand Down
3 changes: 3 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import build.__main__


pytestmark = pytest.mark.contextvars


build_open_owner = 'builtins'

cwd = os.getcwd()
Expand Down
1 change: 1 addition & 0 deletions tests/test_projectbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down

0 comments on commit 27c1c32

Please sign in to comment.