|
| 1 | +"""Test HTML output the same way that Sphinx does in test_build_html.py.""" |
| 2 | +import re |
| 3 | +from itertools import chain, cycle |
| 4 | +from pathlib import Path |
| 5 | +from typing import Dict |
| 6 | + |
| 7 | +import pytest |
| 8 | +from docutils import nodes |
| 9 | +from lxml import etree as lxmltree |
| 10 | +from sphinx.testing.path import path as sphinx_path |
| 11 | +from sphinx.testing.util import SphinxTestApp |
| 12 | + |
| 13 | +pytest_plugins = "sphinx.testing.fixtures" |
| 14 | + |
| 15 | +etree_cache: Dict[str, str] = {} |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture(scope='session') |
| 19 | +def rootdir(): |
| 20 | + return sphinx_path(__file__).parent.abspath() / 'roots' |
| 21 | + |
| 22 | + |
| 23 | +class SphinxBuilder: |
| 24 | + def __init__(self, app: SphinxTestApp, src_path: Path): |
| 25 | + self.app = app |
| 26 | + self._src_path = src_path |
| 27 | + |
| 28 | + @property |
| 29 | + def src_path(self) -> Path: |
| 30 | + return self._src_path |
| 31 | + |
| 32 | + @property |
| 33 | + def out_path(self) -> Path: |
| 34 | + return Path(self.app.outdir) |
| 35 | + |
| 36 | + def build(self, assert_pass=True): |
| 37 | + self.app.build() |
| 38 | + if assert_pass: |
| 39 | + assert self.warnings == "", self.status |
| 40 | + return self |
| 41 | + |
| 42 | + @property |
| 43 | + def status(self): |
| 44 | + return self.app._status.getvalue() |
| 45 | + |
| 46 | + @property |
| 47 | + def warnings(self): |
| 48 | + return self.app._warning.getvalue() |
| 49 | + |
| 50 | + def get_doctree(self, docname: str, post_transforms: bool = False) -> nodes.document: |
| 51 | + assert self.app.env is not None |
| 52 | + doctree = self.app.env.get_doctree(docname) |
| 53 | + if post_transforms: |
| 54 | + self.app.env.apply_post_transforms(doctree, docname) |
| 55 | + return doctree |
| 56 | + |
| 57 | + |
| 58 | +@pytest.fixture(scope='module') |
| 59 | +def cached_etree_parse(): |
| 60 | + def parse(fname): |
| 61 | + if fname in etree_cache: |
| 62 | + return etree_cache[fname] |
| 63 | + with (fname).open('r') as fp: |
| 64 | + data = fp.read().replace('\n', '') |
| 65 | + etree = lxmltree.HTML(data) |
| 66 | + etree_cache.clear() |
| 67 | + etree_cache[fname] = etree |
| 68 | + return etree |
| 69 | + |
| 70 | + yield parse |
| 71 | + etree_cache.clear() |
| 72 | + |
| 73 | + |
| 74 | +def flat_dict(d): |
| 75 | + return chain.from_iterable([zip(cycle([fname]), values) for fname, values in d.items()]) |
| 76 | + |
| 77 | + |
| 78 | +def check_xpath(etree, fname, path, check, be_found=True): |
| 79 | + nodes = list(etree.xpath(path)) |
| 80 | + if check is None: |
| 81 | + assert nodes == [], f'found any nodes matching xpath {path!r} in file {fname}' |
| 82 | + return |
| 83 | + else: |
| 84 | + assert nodes != [], f'did not find any node matching xpath {path!r} in file {fname}' |
| 85 | + if callable(check): |
| 86 | + check(nodes) |
| 87 | + elif not check: |
| 88 | + # only check for node presence |
| 89 | + pass |
| 90 | + else: |
| 91 | + |
| 92 | + def get_text(node): |
| 93 | + if node.text is not None: |
| 94 | + # the node has only one text |
| 95 | + return node.text |
| 96 | + else: |
| 97 | + # the node has tags and text; gather texts just under the node |
| 98 | + return ''.join(n.tail or '' for n in node) |
| 99 | + |
| 100 | + rex = re.compile(check) |
| 101 | + if be_found: |
| 102 | + if any(rex.search(get_text(node)) for node in nodes): |
| 103 | + return |
| 104 | + else: |
| 105 | + if all(not rex.search(get_text(node)) for node in nodes): |
| 106 | + return |
| 107 | + |
| 108 | + raise AssertionError(f'{check!r} not found in any node matching path {path} in {fname}: {[node.text for node in nodes]!r}') |
0 commit comments