tools.conftest

Configure doctest tests for the install script.

 1"""Configure `doctest` tests for the `install` script."""
 2
 3from __future__ import annotations
 4
 5from collections.abc import Callable
 6from pathlib import Path
 7from typing import Any, TypeAlias
 8from unittest.mock import MagicMock
 9
10import pytest
11
12PathT: TypeAlias = Callable[..., Path]
13
14
15# pylint: disable=redefined-outer-name
16
17
18@pytest.fixture
19def mock_path(tmp_path: Path) -> PathT:
20    """Mock `pathlib.Path` to return a temporary directory."""
21    count = 0
22
23    def _mock_path(*args: str | Path) -> Path:
24        """Mock `pathlib.Path` to return a temporary directory instead of '.cn'."""
25        if len(args) == 1 and args[0] == '.cn':
26            nonlocal count, tmp_path
27            args = (tmp_path / str(count) / '.cn',)
28            count += 1
29        return Path(*args)
30
31    return _mock_path
32
33
34@pytest.fixture(autouse=True)
35def install_doctest_namespace(
36    _mock_install_io: None,  # pylint: disable=unused-argument
37    _mock_contextlib_closing: None,  # pylint: disable=unused-argument
38    _mock_urlopen_for_pypi: MagicMock,  # pylint: disable=unused-argument
39    mock_path: PathT,
40    doctest_namespace: dict[str, Any],
41) -> dict[str, Any]:
42    """Configure globals for `doctest` tests in the `install` script."""
43    doctest_namespace['Path'] = mock_path
44    doctest_namespace['pytest'] = pytest
45    return doctest_namespace
PathT: TypeAlias = Callable[..., pathlib.Path]
@pytest.fixture
def mock_path(tmp_path: pathlib.Path) -> Callable[..., pathlib.Path]:
19@pytest.fixture
20def mock_path(tmp_path: Path) -> PathT:
21    """Mock `pathlib.Path` to return a temporary directory."""
22    count = 0
23
24    def _mock_path(*args: str | Path) -> Path:
25        """Mock `pathlib.Path` to return a temporary directory instead of '.cn'."""
26        if len(args) == 1 and args[0] == '.cn':
27            nonlocal count, tmp_path
28            args = (tmp_path / str(count) / '.cn',)
29            count += 1
30        return Path(*args)
31
32    return _mock_path

Mock pathlib.Path to return a temporary directory.

@pytest.fixture(autouse=True)
def install_doctest_namespace( _mock_install_io: None, _mock_contextlib_closing: None, _mock_urlopen_for_pypi: unittest.mock.MagicMock, mock_path: Callable[..., pathlib.Path], doctest_namespace: dict[str, typing.Any]) -> dict[str, typing.Any]:
35@pytest.fixture(autouse=True)
36def install_doctest_namespace(
37    _mock_install_io: None,  # pylint: disable=unused-argument
38    _mock_contextlib_closing: None,  # pylint: disable=unused-argument
39    _mock_urlopen_for_pypi: MagicMock,  # pylint: disable=unused-argument
40    mock_path: PathT,
41    doctest_namespace: dict[str, Any],
42) -> dict[str, Any]:
43    """Configure globals for `doctest` tests in the `install` script."""
44    doctest_namespace['Path'] = mock_path
45    doctest_namespace['pytest'] = pytest
46    return doctest_namespace

Configure globals for doctest tests in the install script.