Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A fix for the sphinx extension #742

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/static.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@ jobs:
- name: Run docs and check extensions
run: |
tox -e testdocs

- name: Run sphinx extension tests
run: |
tox -e test-sphinx-ext
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ _trial_temp/
apidocs/
*.egg-info
.eggs
.hypothesis
__pycache__
17 changes: 13 additions & 4 deletions pydoctor/sphinx_ext/build_apidocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ def on_build_finished(app: Sphinx, exception: Exception) -> None:

temp_path = output_path.with_suffix('.pydoctor_temp')
shutil.rmtree(sphinx_files, ignore_errors=True)
output_path.rename(sphinx_files)
try:
output_path.rename(sphinx_files)
except FileNotFoundError as e:
msg = str(e)
msg += '\nMake sur the pydoctor option --html-output is correctly configured.'
raise FileNotFoundError(msg) from e
temp_path.rename(output_path)


Expand Down Expand Up @@ -117,11 +122,11 @@ def on_builder_inited(app: Sphinx) -> None:

# Build the API docs in temporary path.
shutil.rmtree(temp_path, ignore_errors=True)
_run_pydoctor(key, arguments)
_run_pydoctor(key, arguments, app.warningiserror)
output_path.rename(temp_path)


def _run_pydoctor(name: str, arguments: Sequence[str]) -> None:
def _run_pydoctor(name: str, arguments: Sequence[str], warningiserror:bool) -> None:
"""
Call pydoctor with arguments.

Expand All @@ -135,8 +140,12 @@ def _run_pydoctor(name: str, arguments: Sequence[str]) -> None:
with redirect_stdout(stream):
main(args=arguments)

has_warnings = False
for line in stream.getvalue().splitlines():
logger.warning(line)
has_warnings = True
logger.info(line)
if has_warnings and warningiserror:
logger.warning('Pydocor build is not clean and sphinx option -W (turn warnings into errors) is enabled.')


def _get_arguments(arguments: Sequence[str], placeholders: Mapping[str, str]) -> Sequence[str]:
Expand Down
10 changes: 10 additions & 0 deletions pydoctor/test/testpackages/epytext_syntax_error/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

"""
Title
~~~~~


Hello
~~~~~
"""
pass
2 changes: 2 additions & 0 deletions pydoctor/test/testsphinxextension/source/api/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
API ref
=======
43 changes: 43 additions & 0 deletions pydoctor/test/testsphinxextension/source/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Configuration file for the Sphinx documentation builder.
#
# For the full list of built-in configuration values, see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html

# -- Project information -----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information

project = 'test-sphinx-ext'
copyright = '2023, Contributors'
author = 'Contributors'
release = '0.0.1'

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration

extensions = []
templates_path = []
exclude_patterns = []

# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output

html_theme = 'alabaster'
html_static_path = []

# ----------------------------------------------------------------------------
# Test configuration for pydoctor.sphinx_ext.build_apidocs

from pathlib import Path
extensions.append("pydoctor.sphinx_ext.build_apidocs")

_testpackages = Path(__file__).parent.parent.parent.joinpath('testpackages')

pydoctor_args = [
'--project-name=test-sphinx-ext-api',
f'--project-version={release}',
'--docformat=epytext',
'--intersphinx=https://docs.python.org/3/objects.inv',
'--html-output={outdir}/api',
f'{_testpackages / "report_trigger"}',
f'{_testpackages / "epytext_syntax_error"}',
]
21 changes: 21 additions & 0 deletions pydoctor/test/testsphinxextension/source/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
.. test-sphinx-ext documentation master file, created by
sphinx-quickstart on Thu Oct 26 13:13:53 2023.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.

Welcome to test-sphinx-ext's documentation!
===========================================

.. toctree::
:maxdepth: 2
:caption: Contents:

api/index


Indices and tables
==================

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
Empty file.
19 changes: 19 additions & 0 deletions pydoctor/test/testsphinxextension/tests/test_build_apidocs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import os
import pathlib
from typing import List
import xml.etree.ElementTree as ET
import json

from pydoctor import __version__

BASE_DIR = pathlib.Path(os.environ.get('TOX_INI_DIR', os.getcwd())) / 'build' / 'test-sphinx-ext'

def test_rtd_pydoctor_call():
"""
With the pydoctor Sphinx extension, the pydoctor API HTML files are
generated.
"""
# The pydoctor index is generated and overwrites the Sphinx files.
with open(BASE_DIR / 'api' / 'index.html', 'r') as stream:
page = stream.read()
assert 'moduleIndex.html' in page, page
14 changes: 14 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,17 @@ commands =
echo "::endgroup::"

pytest -vv docs/tests/test.py

[testenv:test-sphinx-ext]
description = Run integration tests for the sphinx extension

extras = docs
deps = pytest

setenv =
TOX_INI_DIR = {toxinidir}

commands =

sphinx-build {posargs:} -aE -b html {toxinidir}/pydoctor/test/testsphinxextension/source {toxinidir}/build/test-sphinx-ext
pytest -vv {toxinidir}/pydoctor/test/testsphinxextension/tests/test_build_apidocs.py
Loading