Skip to content

Commit

Permalink
Ensure pre-commit-hooks.yml is written for tests that require it.
Browse files Browse the repository at this point in the history
  • Loading branch information
domdfcoding committed Feb 1, 2024
1 parent d1b14c3 commit c5060b6
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 31 deletions.
33 changes: 30 additions & 3 deletions tests/test_output/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@
#

# stdlib
from contextlib import contextmanager
from typing import Any, Dict, Sequence, Tuple

# 3rd party
import pytest
from bs4 import BeautifulSoup # type: ignore[import]
from domdf_python_tools.paths import PathPlus
from domdf_python_tools.paths import PathPlus, in_directory
from sphinx.application import Sphinx
from sphinx.testing.fixtures import app as testing_app
from sphinx.testing.fixtures import make_app, shared_result, sphinx_test_tempdir, test_params
Expand All @@ -44,6 +45,32 @@
fixtures = [make_app, shared_result, sphinx_test_tempdir, test_params, testing_app]


@pytest.fixture()
def pre_commit_hooks(tmp_pathplus: PathPlus):
(tmp_pathplus / ".pre-commit-hooks.yaml").write_text(
"""
- id: flake2lint
name: Flake8 -> PyLint
description: Augment Flake8 noqa comments with PyLint comments.
entry: flake2lint
language: python
types_or: [python, pyi]
"""
)


@pytest.fixture()
def pre_commit_flake8_contextmanager(tmp_pathplus, pre_commit_hooks):

@contextmanager
def cm():
with pytest.warns(UserWarning, match="(No codes specified|No such code 'F401')"), in_directory(tmp_pathplus):
yield

return cm


@pytest.fixture(scope="session")
def rootdir() -> path:
rdir = PathPlus(__file__).parent.absolute() / "doc-test"
Expand Down Expand Up @@ -92,8 +119,8 @@ def app_params(


@pytest.fixture()
def page(testing_app: Sphinx, request) -> BeautifulSoup:
with pytest.warns(UserWarning, match="(No codes specified|No such code 'F401')"):
def page(testing_app: Sphinx, request, tmp_pathplus: PathPlus) -> BeautifulSoup:
with pytest.warns(UserWarning, match="(No codes specified|No such code 'F401')"), in_directory(tmp_pathplus):
testing_app.build(force_all=True)

pagename = request.param
Expand Down
72 changes: 44 additions & 28 deletions tests/test_output/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from coincidence.regressions import AdvancedFileRegressionFixture
from coincidence.selectors import min_version
from docutils import nodes
from domdf_python_tools.paths import PathPlus, in_directory
from domdf_python_tools.paths import PathPlus
from domdf_python_tools.stringlist import StringList
from domdf_python_tools.typing import PathLike
from jinja2 import Template
Expand All @@ -34,13 +34,14 @@
from sphinx_toolbox.utils import Config


def test_build_example(testing_app: Sphinx):
with pytest.warns(UserWarning, match="(No codes specified|No such code 'F401')"):
@pytest.mark.usefixtures("pre_commit_hooks")
def test_build_example(testing_app: Sphinx, tmp_pathplus: PathPlus, pre_commit_flake8_contextmanager):
with pre_commit_flake8_contextmanager():
testing_app.build()
testing_app.build()


@pytest.mark.usefixtures("docutils_17_compat")
@pytest.mark.usefixtures("docutils_17_compat", "pre_commit_hooks")
@pytest.mark.parametrize("page", ["example.html"], indirect=True)
def test_example_html_output(page: BeautifulSoup):
# Make sure the page title is what you expect
Expand Down Expand Up @@ -130,25 +131,18 @@ def test_example_html_output(page: BeautifulSoup):
]


@pytest.mark.usefixtures("docutils_17_compat")
def test_html_output(testing_app: Sphinx, html_regression: HTMLRegressionFixture, tmp_pathplus: PathPlus):
@pytest.mark.usefixtures("docutils_17_compat", "pre_commit_hooks")
def test_html_output(
testing_app: Sphinx,
html_regression: HTMLRegressionFixture,
tmp_pathplus: PathPlus,
pre_commit_flake8_contextmanager
):
"""
Parametrize new files here rather than as their own function.
"""

(tmp_pathplus / ".pre-commit-hooks.yaml").write_text(
"""
- id: flake2lint
name: Flake8 -> PyLint
description: Augment Flake8 noqa comments with PyLint comments.
entry: flake2lint
language: python
types_or: [python, pyi]
"""
)

with pytest.warns(UserWarning, match="(No codes specified|No such code 'F401')"), in_directory(tmp_pathplus):
with pre_commit_flake8_contextmanager():
testing_app.build(force_all=True)

caught_exceptions: List[BaseException] = []
Expand Down Expand Up @@ -187,10 +181,12 @@ def test_html_output(testing_app: Sphinx, html_regression: HTMLRegressionFixture
raise exception


@pytest.mark.usefixtures("pre_commit_hooks")
def test_sidebar_links_output(
testing_app: Sphinx,
advanced_file_regression: AdvancedFileRegressionFixture,
monkeypatch,
pre_commit_flake8_contextmanager
):

def visit_caption(self, node: nodes.Node) -> None:
Expand All @@ -207,7 +203,7 @@ def depart_caption(self, node: nodes.Node) -> None:
monkeypatch.setattr(sphinx.writers.html5.HTML5Translator, "visit_caption", visit_caption)
monkeypatch.setattr(sphinx.writers.html5.HTML5Translator, "depart_caption", depart_caption)

with pytest.warns(UserWarning, match="(No codes specified|No such code 'F401')"):
with pre_commit_flake8_contextmanager():
testing_app.build(force_all=True)

content = (PathPlus(testing_app.outdir) / "index.html").read_text()
Expand Down Expand Up @@ -242,21 +238,31 @@ def check_fn(obtained_filename: Path, expected_filename: PathLike): # noqa: MAN
)


@pytest.mark.usefixtures("pre_commit_hooks")
@pytest.mark.sphinx("latex", srcdir="test-root")
def test_latex_output(app: Sphinx, latex_regression: LaTeXRegressionFixture):
def test_latex_output(
app: Sphinx,
latex_regression: LaTeXRegressionFixture,
pre_commit_flake8_contextmanager,
):

assert app.builder is not None
assert app.builder.name.lower() == "latex"

with pytest.warns(UserWarning, match="(No codes specified|No such code 'F401')"):
with pre_commit_flake8_contextmanager():
app.build()

output_file = PathPlus(app.outdir) / "python.tex"
latex_regression.check(StringList(output_file.read_lines()), jinja2=True)


@pytest.mark.usefixtures("pre_commit_hooks")
@pytest.mark.sphinx("latex", srcdir="test-root")
def test_latex_output_latex_layout(app: Sphinx, latex_regression: LaTeXRegressionFixture):
def test_latex_output_latex_layout(
app: Sphinx,
latex_regression: LaTeXRegressionFixture,
pre_commit_flake8_contextmanager,
):

assert app.builder is not None
assert app.builder.name.lower() == "latex"
Expand All @@ -265,37 +271,47 @@ def test_latex_output_latex_layout(app: Sphinx, latex_regression: LaTeXRegressio
app.config.needspace_amount = r"4\baselineskip" # type: ignore[attr-defined]
app.events.emit("config-inited", app.config)

with pytest.warns(UserWarning, match="(No codes specified|No such code 'F401')") as w:
with pre_commit_flake8_contextmanager():
app.build(force_all=True)

output_file = PathPlus(app.outdir) / "python.tex"
latex_regression.check(StringList(output_file.read_lines()), jinja2=True)


@pytest.mark.usefixtures("pre_commit_hooks")
@pytest.mark.sphinx("latex", srcdir="test-root")
def test_latex_output_better_header_layout(app: Sphinx, latex_regression: LaTeXRegressionFixture):
def test_latex_output_better_header_layout(
app: Sphinx,
latex_regression: LaTeXRegressionFixture,
pre_commit_flake8_contextmanager,
):

assert app.builder is not None
assert app.builder.name.lower() == "latex"

better_header_layout(cast(Config, app.config), 9, 19)
app.builder.context.update(app.config.latex_elements) # type: ignore[attr-defined]

with pytest.warns(UserWarning, match="(No codes specified|No such code 'F401')"):
with pre_commit_flake8_contextmanager():
app.build(force_all=True)

output_file = PathPlus(app.outdir) / "python.tex"
latex_regression.check(StringList(output_file.read_lines()), jinja2=True)


@pytest.mark.usefixtures("pre_commit_hooks")
@pytest.mark.sphinx("latex", srcdir="test-root")
def test_latex_output_autosummary_col_type(app: Sphinx, latex_regression: LaTeXRegressionFixture):
def test_latex_output_autosummary_col_type(
app: Sphinx,
latex_regression: LaTeXRegressionFixture,
pre_commit_flake8_contextmanager,
):

assert app.builder is not None
assert app.builder.name.lower() == "latex"
app.config.autosummary_col_type = r"\Y" # type: ignore[attr-defined]

with pytest.warns(UserWarning, match="(No codes specified|No such code 'F401')"):
with pre_commit_flake8_contextmanager():
app.build()

output_file = PathPlus(app.outdir) / "python.tex"
Expand Down

0 comments on commit c5060b6

Please sign in to comment.