Skip to content

Commit

Permalink
Move sphinx fixtures to conftest.py
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjsewell committed Mar 2, 2020
1 parent c852db5 commit eebff69
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 128 deletions.
76 changes: 54 additions & 22 deletions tests/test_sphinx/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,41 @@ def test_basic(app, status, warning, get_sphinx_app_output):
"""
import os
import pathlib
import pickle
import shutil

# import pathlib
# import shutil
import re

from docutils.nodes import document
import pytest
from sphinx.testing.path import path

SOURCE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "sourcedirs"))


# TODO autouse not working, may need to be in root conftest
# (ideally _build folder should be in tempdir)
# @pytest.fixture(scope="session", autouse=True)
# def remove_sphinx_builds():
# """ remove all build directories from the test folder
# """
# srcdirs = pathlib.Path(get_test_source_dir())
# for entry in srcdirs.iterdir(): # type: pathlib.Path
# if entry.is_dir() and entry.joinpath("_build").exists():
# shutil.rmtree(str(entry.joinpath("_build")))
@pytest.fixture()
def remove_sphinx_builds():
""" remove all build directories from the test folder
"""
yield
srcdirs = pathlib.Path(SOURCE_DIR)
for entry in srcdirs.iterdir(): # type: pathlib.Path
if entry.is_dir() and entry.joinpath("_build").exists():
shutil.rmtree(str(entry.joinpath("_build")))


@pytest.fixture
def get_sphinx_app_output():
def get_sphinx_app_output(file_regression):
def read(
app,
buildername="html",
filename="index.html",
encoding="utf-8",
extract_body=False,
remove_scripts=False,
regress_html=False,
):

outpath = path(os.path.join(str(app.srcdir), "_build", buildername, filename))
Expand All @@ -68,18 +75,43 @@ def read(

content = outpath.text(encoding=encoding)

if extract_body:
body_rgx = re.compile("\\<body\\>(.*)\\</body\\>", re.DOTALL)
body_search = body_rgx.search(content)
if not body_search:
raise IOError("could not find body content of {}".format(path))
content = body_search.group(1)
if regress_html:
# only regress the inner body, since other sections are non-deterministic
from bs4 import BeautifulSoup

if remove_scripts:
# remove script environments which can change
script_rgx = re.compile("\\<script\\>(.*)\\</script\\>", re.DOTALL)
content = script_rgx.sub("<script></script>", content)
soup = BeautifulSoup(content, "html.parser")
doc_div = soup.findAll("div", {"class": "documentwrapper"})[0]
file_regression.check(doc_div.prettify(), extension=".html")

return content

return read


@pytest.fixture
def get_sphinx_app_doctree(file_regression):
def read(
app,
filename="index.doctree",
folder="doctrees",
encoding="utf-8",
regress=False,
):

outpath = path(os.path.join(str(app.srcdir), "_build", folder, filename))
if not outpath.exists():
raise IOError("no output file exists: {}".format(outpath))

with open(outpath, "rb") as handle:
doctree = pickle.load(handle) # type: document

# convert absolute filenames
for node in doctree.traverse(lambda n: "source" in n):
node["source"] = pathlib.Path(node["source"]).name

if regress:
file_regression.check(doctree.pformat(), extension=".xml")

return doctree

return read
108 changes: 2 additions & 106 deletions tests/test_sphinx/test_sphinx_builds.py
Original file line number Diff line number Diff line change
@@ -1,119 +1,15 @@
"""
Uses sphinx's pytest fixture to run builds
usage:
.. code-block:: python
@pytest.mark.sphinx(
buildername='html',
srcdir='path/to/source')
def test_basic(app, status, warning, get_sphinx_app_output):
app.build()
assert 'build succeeded' in status.getvalue() # Build succeeded
warnings = warning.getvalue().strip()
assert warnings == ""
output = get_sphinx_app_output(app, buildername='html')
parameters available to parse to ``@pytest.mark.sphinx``:
- buildername='html'
- srcdir=None
- testroot='root' (only used if srcdir not set)
- freshenv=False
- confoverrides=None
- status=None
- warning=None
- tags=None
- docutilsconf=None
"""Uses sphinx's pytest fixture to run builds.
see conftest.py for fixture usage
"""
import os
import pathlib
import pickle
import shutil

import pytest
from docutils.nodes import document
from sphinx.testing.path import path


SOURCE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "sourcedirs"))


@pytest.fixture()
def remove_sphinx_builds():
""" remove all build directories from the test folder
"""
yield
srcdirs = pathlib.Path(SOURCE_DIR)
for entry in srcdirs.iterdir(): # type: pathlib.Path
if entry.is_dir() and entry.joinpath("_build").exists():
shutil.rmtree(str(entry.joinpath("_build")))


@pytest.fixture
def get_sphinx_app_output(file_regression):
def read(
app,
buildername="html",
filename="index.html",
encoding="utf-8",
extract_body=False,
remove_scripts=False,
regress_html=False,
):

outpath = path(os.path.join(str(app.srcdir), "_build", buildername, filename))
if not outpath.exists():
raise IOError("no output file exists: {}".format(outpath))

content = outpath.text(encoding=encoding)

if regress_html:
from bs4 import BeautifulSoup

soup = BeautifulSoup(content, "html.parser")
doc_div = soup.findAll("div", {"class": "documentwrapper"})[0]
file_regression.check(doc_div.prettify(), extension=".html")

return content

return read


@pytest.fixture
def get_sphinx_app_doctree(file_regression):
def read(
app,
filename="index.doctree",
folder="doctrees",
encoding="utf-8",
regress=False,
):

outpath = path(os.path.join(str(app.srcdir), "_build", folder, filename))
if not outpath.exists():
raise IOError("no output file exists: {}".format(outpath))

with open(outpath, "rb") as handle:
doctree = pickle.load(handle) # type: document

# convert absolute filenames
for node in doctree.traverse(lambda n: "source" in n):
node["source"] = pathlib.Path(node["source"]).name

if regress:
file_regression.check(doctree.pformat(), extension=".xml")

return doctree

return read


@pytest.mark.sphinx(
buildername="html", srcdir=os.path.join(SOURCE_DIR, "basic"), freshenv=True
)
Expand Down

0 comments on commit eebff69

Please sign in to comment.