Skip to content

Commit

Permalink
fix: tester not working
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Sep 9, 2024
1 parent 2d89d79 commit aae2d25
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 15 deletions.
5 changes: 5 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
extensions = ["sphinx_ape"]

doctest_global_setup = """
from sphinx_ape.build import BuildMode, DocumentationBuilder
from pathlib import Path
"""
3 changes: 2 additions & 1 deletion sphinx_ape/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ def extract_package_name(directory: Optional[Path] = None) -> str:
if pkg_name is None and (directory / "pyproject.toml").is_file():
pkg_name = _extract_name_from_pyproject_toml(directory / "pyproject.toml")
if pkg_name is None:
raise ApeDocsBuildError("No package name found.")
path = f"{directory}".replace(f"{Path.home()}", "$HOME")
raise ApeDocsBuildError(f"No package name found at '{path}'.")

return PACKAGE_ALIASES.get(pkg_name, pkg_name)

Expand Down
4 changes: 1 addition & 3 deletions sphinx_ape/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,9 @@ def build(self):
Build the documentation.
Example:
>>> from sphinx_ape.build import BuildMode, DocumentationBuilder
>>> from pathlib import Path
>>> builder = DocumentationBuilder(
... mode=BuildMode.LATEST,
... base_path=Path("."),
... base_path=Path.cwd(),
... name="sphinx-ape"
... )
>>> builder.build()
Expand Down
30 changes: 19 additions & 11 deletions sphinx_ape/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,19 @@ class DocumentationTester(Documentation):
Small wrapper around sphinx-build's doctest command.
"""

@property
def doctest_folder(self) -> Path:
"""
The path to doctest's build folder.
"""
return self.build_path.parent / "doctest"

@property
def doctest_output_file(self) -> Path:
"""
The path to doctest's output file.
"""
return self.build_path.parent / "doctest" / "output.txt"
return self.doctest_folder / "output.txt"

def test(self):
"""
Expand All @@ -24,18 +31,19 @@ def test(self):
Raises:
:class:`~sphinx_ape.exceptions.ApeDocsTestError`
"""
try:
subprocess.run(
["sphinx-build", "-b", "doctest", "docs", "docs/_build/doctest"], check=True
)
except subprocess.CalledProcessError as err:
raise ApeDocsBuildError(str(err)) from err

if self.doctest_output_file.is_file():
return

self._run_tests()
output = self.doctest_output_file.read_text() if self.doctest_output_file.is_file() else ""
if "0 failed" in output or "0 tests" in output:
# Either no failures or no tests ran.
return

# Failures.
raise ApeDocsTestError(output)

def _run_tests(self):
try:
subprocess.run(
["sphinx-build", "-b", "doctest", "docs", str(self.doctest_folder)], check=True
)
except subprocess.CalledProcessError as err:
raise ApeDocsBuildError(str(err)) from err
10 changes: 10 additions & 0 deletions tests/test_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from pathlib import Path

from sphinx_ape.testing import DocumentationTester


class TestDocumentationTester:
def test_test(self, temp_path):
tester = DocumentationTester(base_path=Path(__file__).parent.parent)
tester.test()
assert tester.doctest_output_file.is_file()

0 comments on commit aae2d25

Please sign in to comment.