Skip to content
Open
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: 2 additions & 2 deletions notfound/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ def toctree(*args, **kwargs):
**kwargs,
)

# If no TOC is found, just return ``None`` instead of failing here
# Match Sphinx's toctree helper return type when no TOC is found.
if not toc:
return None
return ''

replace_uris(app, toc, docutils.nodes.reference, 'refuri')
return app.builder.render_partial(toc)['fragment']
Expand Down
6 changes: 6 additions & 0 deletions tests/examples/empty-toctree/_templates/toctree.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!doctype html>
<html>
<body>
<div id="toctree-result">{{ toctree() }}</div>
</body>
</html>
10 changes: 10 additions & 0 deletions tests/examples/empty-toctree/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# conf.py to run tests

project = 'Python'
master_doc = 'index'
extensions = [
'notfound.extension',
]

templates_path = ['_templates']
notfound_template = 'toctree.html'
4 changes: 4 additions & 0 deletions tests/examples/empty-toctree/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Single page
===========

This page does not define a toctree.
14 changes: 13 additions & 1 deletion tests/test_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@

extensiondir = Path(__file__).resolve().parent / 'examples' / 'extension'

emptytoctreedir = Path(__file__).resolve().parent / 'examples' / 'empty-toctree'

@pytest.fixture(autouse=True, scope='function')
def remove_sphinx_build_output():
"""Remove _build/ folder, if exist."""
for path in (srcdir, rstsrcdir, extensiondir):
for path in (srcdir, rstsrcdir, extensiondir, emptytoctreedir):
build_path = path / '_build'
if build_path.exists():
shutil.rmtree(build_path)
Expand Down Expand Up @@ -475,6 +477,16 @@ def test_toctree_links_custom_settings(app, status, warning):
assert chunk in content


@pytest.mark.sphinx(srcdir=emptytoctreedir)
def test_empty_toctree_returns_empty_string(app, status, warning):
app.build()
path = app.outdir / '404.html'
assert path.exists()

content = path.read_text()
assert '<div id="toctree-result"></div>' in content


@pytest.mark.sphinx(
srcdir=rstsrcdir,
)
Expand Down