Skip to content

Commit 40bcf7e

Browse files
committed
adding basic tests w/ pytest regressions
1 parent 2cb2bed commit 40bcf7e

File tree

12 files changed

+191
-0
lines changed

12 files changed

+191
-0
lines changed

docs/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ pandas
55
jupyter_sphinx
66
plotly
77
numpy
8+
pytest
9+
pytest-regressions
10+
beautifulsoup4

tests/sites/base/Makefile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Minimal makefile for Sphinx documentation
2+
#
3+
4+
# You can set these variables from the command line, and also
5+
# from the environment for the first two.
6+
SPHINXOPTS ?=
7+
SPHINXBUILD ?= sphinx-build
8+
SOURCEDIR = .
9+
BUILDDIR = _build
10+
11+
# Put it first so that "make" without argument is like "make help".
12+
help:
13+
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
14+
15+
.PHONY: help Makefile
16+
17+
# Catch-all target: route all unknown targets to Sphinx using the new
18+
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
19+
%: Makefile
20+
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

tests/sites/base/conf.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -- Project information -----------------------------------------------------
2+
3+
project = "PyData Tests"
4+
copyright = "2020, Pydata community"
5+
author = "Pydata community"
6+
7+
master_doc = "index"
8+
9+
# -- General configuration ---------------------------------------------------
10+
11+
# Add any Sphinx extension module names here, as strings. They can be
12+
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
13+
# ones.
14+
extensions = []
15+
html_theme = "pydata_sphinx_theme"
16+
html_copy_source = True
17+
html_sourcelink_suffix = ""
18+
19+
# Base options, we can add other key/vals later
20+
html_theme_options = {
21+
}

tests/sites/base/index.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Index ``with code`` in title
2+
============================
3+
4+
.. toctree::
5+
:caption: My caption
6+
:numbered:
7+
8+
page1
9+
page2
10+
section1/index

tests/sites/base/page1.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Page 1
2+
======

tests/sites/base/page2.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Page 2
2+
======
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Section 1 index
2+
===============
3+
.. toctree::
4+
5+
page1
6+
https://google.com
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Section 1 page1
2+
===============

tests/test_build.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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()

tests/test_build/navbar_ix.html

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<div class="col-lg-9 collapse navbar-collapse" id="navbar-menu">
2+
<ul class="navbar-nav mr-auto" id="navbar-main-elements">
3+
<li class="nav-item">
4+
<a class="nav-link" href="page1.html">
5+
Page 1
6+
</a>
7+
</li>
8+
<li class="nav-item">
9+
<a class="nav-link" href="page2.html">
10+
Page 2
11+
</a>
12+
</li>
13+
<li class="nav-item">
14+
<a class="nav-link" href="section1/index.html">
15+
Section 1 index
16+
</a>
17+
</li>
18+
</ul>
19+
<ul class="navbar-nav">
20+
</ul>
21+
</div>

0 commit comments

Comments
 (0)