| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import logging |
| 4 |
import sys |
| 5 |
|
| 6 |
import pytest |
| 7 |
import pytest_mock |
| 8 |
|
| 9 |
import build._ctx |
| 10 |
|
| 11 |
|
| 12 |
pytestmark = pytest.mark.contextvars |
| 13 |
|
| 14 |
|
| 15 |
def test_default_ctx_logger(caplog: pytest.LogCaptureFixture) -> None: |
| 16 |
build._ctx.log('foo') |
| 17 |
|
| 18 |
[record] = caplog.records |
| 19 |
assert record.name == 'build' |
| 20 |
assert record.levelno == logging.INFO |
| 21 |
assert record.message == 'foo' |
| 22 |
|
| 23 |
|
| 24 |
def test_ctx_custom_logger(mocker: pytest_mock.MockerFixture) -> None: |
| 25 |
log_stub = mocker.stub('custom_logger') |
| 26 |
|
| 27 |
build._ctx.LOGGER.set(log_stub) |
| 28 |
build._ctx.log('foo') |
| 29 |
|
| 30 |
log_stub.assert_called_once_with('foo') |
| 31 |
|
| 32 |
|
| 33 |
def test_ctx_custom_logger_with_custom_verbosity(mocker: pytest_mock.MockerFixture) -> None: |
| 34 |
log_stub = mocker.stub('custom_logger') |
| 35 |
|
| 36 |
def log(message: str, **_kwargs: object) -> None: |
| 37 |
if build._ctx.verbosity >= 9000: |
| 38 |
log_stub(message) |
| 39 |
|
| 40 |
build._ctx.LOGGER.set(log) |
| 41 |
build._ctx.log('foo') |
| 42 |
build._ctx.VERBOSITY.set(9000) |
| 43 |
build._ctx.log('bar') |
| 44 |
|
| 45 |
log_stub.assert_called_once_with('bar') |
| 46 |
|
| 47 |
|
| 48 |
@pytest.mark.parametrize( |
| 49 |
('verbosity', 'kwarg_origins'), |
| 50 |
[ |
| 51 |
(0, []), |
| 52 |
(1, [('subprocess', 'cmd'), ('subprocess', 'stdout')]), |
| 53 |
], |
| 54 |
) |
| 55 |
def test_custom_subprocess_runner_ctx_logging( |
| 56 |
mocker: pytest_mock.MockerFixture, verbosity: int, kwarg_origins: list[tuple[str, ...]] |
| 57 |
) -> None: |
| 58 |
log_stub = mocker.stub('custom_logger') |
| 59 |
|
| 60 |
build._ctx.LOGGER.set(log_stub) |
| 61 |
build._ctx.VERBOSITY.set(verbosity) |
| 62 |
|
| 63 |
build._ctx.run_subprocess([sys.executable, '-m', 'build', '-V']) |
| 64 |
|
| 65 |
assert log_stub.call_count == len(kwarg_origins) |
| 66 |
assert [c.kwargs['kind'] for c in log_stub.call_args_list] == kwarg_origins |