tools.conftest

Configure doctest tests for the install script.

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

Configure globals for doctest tests in the install script.