|
| 1 | +from bs4 import BeautifulSoup |
| 2 | +from pathlib import Path |
| 3 | +from subprocess import run |
| 4 | +from shutil import copytree, rmtree |
| 5 | +import pytest |
| 6 | + |
| 7 | + |
| 8 | +path_tests = Path(__file__).parent.resolve() |
| 9 | +path_base = path_tests.joinpath("sites", "base") |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture(scope="session") |
| 13 | +def sphinx_build(tmpdir_factory): |
| 14 | + class SphinxBuild: |
| 15 | + path_tmp = Path(tmpdir_factory.mktemp("build")) |
| 16 | + path_docs = path_tmp.joinpath("testdocs") |
| 17 | + path_build = path_docs.joinpath("_build") |
| 18 | + path_html = path_build.joinpath("html") |
| 19 | + path_pg_index = path_html.joinpath("index.html") |
| 20 | + cmd_base = ["sphinx-build", ".", "_build/html", "-a", "-W"] |
| 21 | + |
| 22 | + def copy(self, path=None): |
| 23 | + """Copy the specified book to our tests folder for building.""" |
| 24 | + if path is None: |
| 25 | + path = path_base |
| 26 | + if not self.path_docs.exists(): |
| 27 | + copytree(path, self.path_docs) |
| 28 | + |
| 29 | + def build(self, cmd=None): |
| 30 | + """Build the test book""" |
| 31 | + cmd = [] if cmd is None else cmd |
| 32 | + run(self.cmd_base + cmd, cwd=self.path_docs, check=True) |
| 33 | + |
| 34 | + def get(self, pagename): |
| 35 | + path_page = self.path_html.joinpath(pagename) |
| 36 | + if not path_page.exists(): |
| 37 | + raise ValueError(f"{path_page} does not exist") |
| 38 | + return BeautifulSoup(path_page.read_text(), "html.parser") |
| 39 | + |
| 40 | + def clean(self): |
| 41 | + """Clean the _build folder so files don't clash with new tests.""" |
| 42 | + rmtree(self.path_build) |
| 43 | + |
| 44 | + return SphinxBuild() |
| 45 | + |
| 46 | + |
| 47 | +def test_build_book(file_regression, sphinx_build): |
| 48 | + """Test building the base book template and config.""" |
| 49 | + sphinx_build.copy() |
| 50 | + |
| 51 | + # Basic build with defaults |
| 52 | + sphinx_build.build() |
| 53 | + index_html = sphinx_build.get("index.html") |
| 54 | + subpage_html = sphinx_build.get("section1/index.html") |
| 55 | + |
| 56 | + # Navbar structure |
| 57 | + navbar = index_html.select("div#navbar-menu")[0] |
| 58 | + file_regression.check(navbar.prettify(), basename="navbar_ix", extension=".html") |
| 59 | + |
| 60 | + # Sidebar structure |
| 61 | + sidebar = index_html.select(".bd-sidebar")[0] |
| 62 | + file_regression.check(sidebar.prettify(), basename="sidebar_ix", extension=".html") |
| 63 | + |
| 64 | + # Sidebar subpage |
| 65 | + sidebar = subpage_html.select(".bd-sidebar")[0] |
| 66 | + file_regression.check(sidebar.prettify(), basename="sidebar_subpage", extension=".html") |
| 67 | + |
| 68 | + sphinx_build.clean() |
0 commit comments