|
4 | 4 |
|
5 | 5 | import os
|
6 | 6 | import sys
|
| 7 | +from dataclasses import replace |
7 | 8 | from glob import glob
|
| 9 | +from io import BytesIO |
8 | 10 | from pathlib import Path
|
9 | 11 | from textwrap import dedent
|
10 | 12 | from typing import TYPE_CHECKING
|
11 | 13 |
|
| 14 | +import mkdocstrings |
12 | 15 | import pytest
|
13 | 16 | from griffe import (
|
14 | 17 | Docstring,
|
|
20 | 23 | )
|
21 | 24 | from mkdocstrings import CollectionError
|
22 | 25 |
|
23 |
| -from mkdocstrings_handlers.python import PythonConfig, PythonHandler, PythonOptions |
| 26 | +from mkdocstrings_handlers.python import Inventory, PythonConfig, PythonHandler, PythonOptions |
24 | 27 |
|
25 | 28 | if TYPE_CHECKING:
|
26 | 29 | from mkdocstrings import MkdocstringsPlugin
|
@@ -298,3 +301,33 @@ class B(A): ...
|
298 | 301 | module,
|
299 | 302 | handler.get_options({"inherited_members": True}),
|
300 | 303 | )
|
| 304 | + |
| 305 | + |
| 306 | +def test_specifying_inventory_base_url(handler: PythonHandler) -> None: |
| 307 | + """Assert that the handler renders inventory URLs using the specified base_url.""" |
| 308 | + # Update handler config to include an inventory with a base URL |
| 309 | + base_url = "https://docs.com/my_library" |
| 310 | + inventory = Inventory(url="https://example.com/objects.inv", base_url=base_url) |
| 311 | + handler.config = replace(handler.config, inventories=[inventory]) |
| 312 | + |
| 313 | + # Mock inventory bytes |
| 314 | + item_name = "my_library.my_module.MyClass" |
| 315 | + mocked_inventory = mkdocstrings.Inventory() |
| 316 | + mocked_inventory.register( |
| 317 | + name=item_name, |
| 318 | + domain="py", |
| 319 | + role="class", |
| 320 | + uri=f"api-reference/#{item_name}", |
| 321 | + dispname=item_name, |
| 322 | + ) |
| 323 | + mocked_bytes = BytesIO(mocked_inventory.format_sphinx()) |
| 324 | + |
| 325 | + # Get inventory URL and config |
| 326 | + url, config = handler.get_inventory_urls()[0] |
| 327 | + |
| 328 | + # Load the mocked inventory |
| 329 | + _, item_url = next(handler.load_inventory(mocked_bytes, url, **config)) |
| 330 | + |
| 331 | + # Assert the URL is based on the provided base URL |
| 332 | + msg = "Expected inventory URL to start with base_url" |
| 333 | + assert item_url.startswith(base_url), msg |
0 commit comments