|
11 | 11 | from textwrap import dedent |
12 | 12 | from typing import TYPE_CHECKING |
13 | 13 |
|
| 14 | +import bs4 |
14 | 15 | import mkdocstrings |
15 | 16 | import pytest |
16 | 17 | from griffe import ( |
@@ -333,3 +334,77 @@ def test_specifying_inventory_base_url(handler: PythonHandler) -> None: |
333 | 334 | # Assert the URL is based on the provided base URL |
334 | 335 | msg = "Expected inventory URL to start with base_url" |
335 | 336 | assert item_url.startswith(base_url), msg |
| 337 | + |
| 338 | + |
| 339 | +def _source_labels(html: str) -> list[Path]: |
| 340 | + soup = bs4.BeautifulSoup(html, features="html.parser") |
| 341 | + labels = [] |
| 342 | + for summary in soup.find_all("summary"): |
| 343 | + if "Source code in" in summary.get_text(): |
| 344 | + code_tag = summary.find("code") |
| 345 | + assert code_tag is not None |
| 346 | + labels.append(Path(code_tag.get_text(strip=True))) |
| 347 | + return labels |
| 348 | + |
| 349 | + |
| 350 | +def _write_site_packages_package(tmp_path: Path, *, single_module: bool) -> None: |
| 351 | + """Lay out the issue-333 scenario: a package installed in a virtual environment inside the project. |
| 352 | +
|
| 353 | + The environment's `site-packages` directory is relative to the current |
| 354 | + working directory, so the package's `relative_filepath` is relative too, |
| 355 | + slipping past `is_absolute()` checks. |
| 356 | + """ |
| 357 | + code = """ |
| 358 | + class Model: |
| 359 | + '''Model docstring.''' |
| 360 | +
|
| 361 | + def __init__(self) -> None: |
| 362 | + '''Init docstring.''' |
| 363 | + self.model_attribute = 0 |
| 364 | +
|
| 365 | + def method(self) -> None: |
| 366 | + '''Method docstring.''' |
| 367 | + """ |
| 368 | + site = tmp_path / "site-packages" |
| 369 | + module_path = site / "pkg.py" if single_module else site / "pkg" / "__init__.py" |
| 370 | + module_path.parent.mkdir(parents=True) |
| 371 | + module_path.write_text(dedent(code), encoding="utf-8") |
| 372 | + |
| 373 | + |
| 374 | +@pytest.mark.parametrize( |
| 375 | + "handler", |
| 376 | + [ |
| 377 | + {"theme": "readthedocs"}, |
| 378 | + {"theme": {"name": "material"}}, |
| 379 | + ], |
| 380 | + indirect=["handler"], |
| 381 | +) |
| 382 | +@pytest.mark.parametrize( |
| 383 | + ("single_module", "extra_options", "expected_label"), |
| 384 | + [ |
| 385 | + pytest.param(False, {"merge_init_into_class": True}, Path("pkg", "__init__.py"), id="merged-init"), |
| 386 | + pytest.param(False, {}, Path("pkg", "__init__.py"), id="class-and-methods"), |
| 387 | + pytest.param(True, {"merge_init_into_class": True}, Path("pkg.py"), id="single-module"), |
| 388 | + ], |
| 389 | +) |
| 390 | +def test_no_environment_path_in_source_labels( |
| 391 | + *, |
| 392 | + tmp_path: Path, |
| 393 | + monkeypatch: pytest.MonkeyPatch, |
| 394 | + handler: PythonHandler, |
| 395 | + single_module: bool, |
| 396 | + extra_options: dict, |
| 397 | + expected_label: Path, |
| 398 | +) -> None: |
| 399 | + """Assert source labels never show an environment path.""" |
| 400 | + _write_site_packages_package(tmp_path, single_module=single_module) |
| 401 | + monkeypatch.chdir(tmp_path) |
| 402 | + # `collect()` reads the search paths lazily from this attribute. |
| 403 | + handler._paths = [str(tmp_path / "site-packages")] |
| 404 | + options = handler.get_options({"show_source": True, **extra_options}) |
| 405 | + html = handler.render(handler.collect("pkg.Model", options), options) |
| 406 | + labels = _source_labels(html) |
| 407 | + assert labels |
| 408 | + assert set(labels) == {expected_label} |
| 409 | + # The source bodies themselves are still rendered. |
| 410 | + assert "model_attribute" in html |
0 commit comments