|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import re |
| 4 | +import sys |
| 5 | +from pathlib import Path |
| 6 | +from textwrap import dedent, indent |
| 7 | +from typing import TYPE_CHECKING, Any, Callable, Literal, NewType, TypeVar # no type comments |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +if TYPE_CHECKING: |
| 12 | + from io import StringIO |
| 13 | + |
| 14 | + from sphinx.testing.util import SphinxTestApp |
| 15 | + |
| 16 | +T = TypeVar("T") |
| 17 | +W = NewType("W", str) |
| 18 | + |
| 19 | + |
| 20 | +def expected(expected: str, **options: dict[str, Any]) -> Callable[[T], T]: |
| 21 | + def dec(val: T) -> T: |
| 22 | + val.EXPECTED = expected |
| 23 | + val.OPTIONS = options |
| 24 | + return val |
| 25 | + |
| 26 | + return dec |
| 27 | + |
| 28 | + |
| 29 | +def warns(pattern: str) -> Callable[[T], T]: |
| 30 | + def dec(val: T) -> T: |
| 31 | + val.WARNING = pattern |
| 32 | + return val |
| 33 | + |
| 34 | + return dec |
| 35 | + |
| 36 | + |
| 37 | +ArrayLike = Literal["test"] |
| 38 | + |
| 39 | + |
| 40 | +@expected( |
| 41 | + """\ |
| 42 | +mod.function(x) |
| 43 | +
|
| 44 | + Function docstring. |
| 45 | +
|
| 46 | + Parameters: |
| 47 | + **x** (ArrayLike) -- foo |
| 48 | +
|
| 49 | + Returns: |
| 50 | + something |
| 51 | +
|
| 52 | + Return type: |
| 53 | + bytes |
| 54 | +""", |
| 55 | +) |
| 56 | +def function(x: ArrayLike) -> str: # noqa: ARG001 |
| 57 | + """ |
| 58 | + Function docstring. |
| 59 | +
|
| 60 | + :param x: foo |
| 61 | + :return: something |
| 62 | + :rtype: bytes |
| 63 | + """ |
| 64 | + |
| 65 | + |
| 66 | +# Config settings for each test run. |
| 67 | +# Config Name: Sphinx Options as Dict. |
| 68 | +configs = { |
| 69 | + "default_conf": { |
| 70 | + "autodoc_type_aliases": { |
| 71 | + "ArrayLike": "ArrayLike", |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | + |
| 77 | +@pytest.mark.parametrize("val", [x for x in globals().values() if hasattr(x, "EXPECTED")]) |
| 78 | +@pytest.mark.parametrize("conf_run", list(configs.keys())) |
| 79 | +@pytest.mark.sphinx("text", testroot="integration") |
| 80 | +def test_integration( |
| 81 | + app: SphinxTestApp, status: StringIO, warning: StringIO, monkeypatch: pytest.MonkeyPatch, val: Any, conf_run: str |
| 82 | +) -> None: |
| 83 | + template = ".. autofunction:: mod.{}" |
| 84 | + |
| 85 | + (Path(app.srcdir) / "index.rst").write_text(template.format(val.__name__)) |
| 86 | + app.config.__dict__.update(configs[conf_run]) |
| 87 | + app.config.__dict__.update(val.OPTIONS) |
| 88 | + monkeypatch.setitem(sys.modules, "mod", sys.modules[__name__]) |
| 89 | + app.build() |
| 90 | + assert "build succeeded" in status.getvalue() # Build succeeded |
| 91 | + |
| 92 | + regexp = getattr(val, "WARNING", None) |
| 93 | + value = warning.getvalue().strip() |
| 94 | + if regexp: |
| 95 | + msg = f"Regex pattern did not match.\n Regex: {regexp!r}\n Input: {value!r}" |
| 96 | + assert re.search(regexp, value), msg |
| 97 | + else: |
| 98 | + assert not value |
| 99 | + |
| 100 | + result = (Path(app.srcdir) / "_build/text/index.txt").read_text() |
| 101 | + |
| 102 | + expected = val.EXPECTED |
| 103 | + if sys.version_info < (3, 10): |
| 104 | + expected = expected.replace("NewType", "NewType()") |
| 105 | + try: |
| 106 | + assert result.strip() == dedent(expected).strip() |
| 107 | + except Exception: |
| 108 | + indented = indent(f'"""\n{result}\n"""', " " * 4) |
| 109 | + print(f"@expected(\n{indented}\n)\n") # noqa: T201 |
| 110 | + raise |
0 commit comments