File: test_inventory.py

package info (click to toggle)
mkdocstrings 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 708 kB
  • sloc: python: 2,337; makefile: 39; sh: 18; javascript: 13
file content (84 lines) | stat: -rw-r--r-- 3,433 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""Tests for the inventory module."""

from __future__ import annotations

from io import BytesIO
from os.path import join

import pytest
from mkdocs.commands.build import build
from mkdocs.config import load_config

from mkdocstrings import Inventory, InventoryItem


@pytest.mark.parametrize(
    "our_inv",
    [
        Inventory(),
        Inventory([InventoryItem(name="object_path", domain="py", role="obj", uri="page_url")]),
        Inventory([InventoryItem(name="object_path", domain="py", role="obj", uri="page_url#object_path")]),
        Inventory([InventoryItem(name="object_path", domain="py", role="obj", uri="page_url#other_anchor")]),
        Inventory([InventoryItem(name="o", domain="py", role="obj", uri="u#o", dispname="first line\nsecond line")]),
    ],
)
def test_sphinx_load_inventory_file(our_inv: Inventory) -> None:
    """Perform the 'live' inventory load test."""
    sphinx = pytest.importorskip("sphinx.util.inventory", reason="Sphinx is not installed")

    buffer = BytesIO(our_inv.format_sphinx())
    sphinx_inv = sphinx.InventoryFile.load(buffer, "", join)

    sphinx_inv_length = sum(len(sphinx_inv[key]) for key in sphinx_inv)
    assert sphinx_inv_length == len(our_inv.values())

    for item in our_inv.values():
        assert item.name in sphinx_inv[f"{item.domain}:{item.role}"]


def test_sphinx_load_mkdocstrings_inventory_file() -> None:
    """Perform the 'live' inventory load test on mkdocstrings own inventory."""
    sphinx = pytest.importorskip("sphinx.util.inventory", reason="Sphinx is not installed")

    mkdocs_config = load_config()
    mkdocs_config["plugins"].run_event("startup", command="build", dirty=False)
    try:
        build(mkdocs_config)
    finally:
        mkdocs_config["plugins"].run_event("shutdown")
    own_inv = mkdocs_config["plugins"]["mkdocstrings"].handlers.inventory

    with open("site/objects.inv", "rb") as fp:
        sphinx_inv = sphinx.InventoryFile.load(fp, "", join)

    sphinx_inv_length = sum(len(sphinx_inv[key]) for key in sphinx_inv)
    assert sphinx_inv_length == len(own_inv.values())

    for item in own_inv.values():
        assert item.name in sphinx_inv[f"{item.domain}:{item.role}"]


@pytest.mark.parametrize(
    "our_inv",
    [
        Inventory(),
        Inventory([InventoryItem(name="object_path", domain="py", role="obj", uri="page_url")]),
        Inventory([InventoryItem(name="object_path", domain="py", role="obj", uri="page_url#object_path")]),
        Inventory([InventoryItem(name="object_path", domain="py", role="obj", uri="page_url#other_anchor")]),
        Inventory([InventoryItem(name="o", domain="py", role="obj", uri="u#o", dispname="first line\nsecond line")]),
    ],
)
def test_mkdocstrings_roundtrip_inventory_file(our_inv: Inventory) -> None:
    """Save some inventory files, then load them in again."""
    buffer = BytesIO(our_inv.format_sphinx())
    round_tripped = Inventory.parse_sphinx(buffer)

    assert our_inv.keys() == round_tripped.keys()
    for key, value in our_inv.items():
        round_tripped_item = round_tripped[key]
        assert round_tripped_item.name == value.name
        assert round_tripped_item.domain == value.domain
        assert round_tripped_item.role == value.role
        assert round_tripped_item.uri == value.uri
        assert round_tripped_item.priority == value.priority
        assert round_tripped_item.dispname == value.dispname.splitlines()[0]