sphinx/tests/test_builders/conftest.py

29 lines
711 B
Python
Raw Normal View History

2024-01-17 18:08:30 -06:00
from __future__ import annotations
2024-01-17 18:54:27 -06:00
from typing import TYPE_CHECKING
2024-01-17 18:08:30 -06:00
import pytest
from html5lib import HTMLParser
if TYPE_CHECKING:
from collections.abc import Callable, Generator
2024-01-17 18:08:30 -06:00
from pathlib import Path
from xml.etree.ElementTree import Element
2024-01-17 18:08:30 -06:00
etree_cache: dict[Path, Element] = {}
2024-01-17 18:08:30 -06:00
def _parse(fname: Path) -> Element:
2024-01-17 18:08:30 -06:00
if fname in etree_cache:
return etree_cache[fname]
with fname.open('rb') as fp:
etree = HTMLParser(namespaceHTMLElements=False).parse(fp)
etree_cache[fname] = etree
return etree
@pytest.fixture(scope='package')
def cached_etree_parse() -> Generator[Callable[[Path], Element], None, None]:
2024-01-17 18:08:30 -06:00
yield _parse
etree_cache.clear()