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
1 change: 1 addition & 0 deletions roots/test-excerpt/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
extensions = ["ablog"]
9 changes: 9 additions & 0 deletions roots/test-excerpt/excerpt-default.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
:blogpost: true
:date: December 03, 2020

excerpt-default
===============

DEFAULTFIRSTPARA must be shown.

DEFAULTSECONDPARA must not be shown.
10 changes: 10 additions & 0 deletions roots/test-excerpt/excerpt-one.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
:blogpost: true
:date: December 02, 2020
:excerpt: 1

excerpt-one
===========

ONEFIRSTPARA must be shown.

ONESECONDPARA must not be shown.
10 changes: 10 additions & 0 deletions roots/test-excerpt/excerpt-zero.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
:blogpost: true
:date: December 01, 2020
:excerpt: 0

excerpt-zero
============

ZEROFIRSTPARA must not be shown.

ZEROSECONDPARA must not be shown.
10 changes: 10 additions & 0 deletions roots/test-excerpt/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
test-excerpt
============

.. toctree::
:maxdepth: 1

excerpt-zero
excerpt-one
excerpt-default
postlist
5 changes: 5 additions & 0 deletions roots/test-excerpt/postlist.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
postlist
========

.. postlist::
:excerpts:
2 changes: 1 addition & 1 deletion src/ablog/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def apply(self):
node.document = self.document
node = _update_post_node(node, metadata, [])
node["date"] = metadata.get("date")
if not metadata.get("excerpt"):
if metadata.get("excerpt") is None:
blog = Blog(self.app)
node["excerpt"] = blog.post_auto_excerpt
sections = list(self.document.findall(nodes.section))
Expand Down
76 changes: 76 additions & 0 deletions src/ablog/tests/test_excerpt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import pytest


def read_text(path):
"""
Support function to give backward compatibility with older sphinx (v2).
"""
if hasattr(path, "read_text"):
return path.read_text()
return path.text()


# ``CheckFrontMatter`` reaches the Sphinx application through the deprecated
# ``SphinxTransform.app`` property. That is orthogonal to the excerpt handling
# exercised here, and the suite turns warnings into errors, so it is filtered
# rather than worked around.
pytestmark = pytest.mark.filterwarnings("ignore:'ablog.post.CheckFrontMatter.app' is deprecated")


@pytest.mark.sphinx("html", testroot="excerpt") # using roots/test-excerpt
def test_excerpt_zero_in_front_matter_is_respected(app, status, warning):
"""
An ``:excerpt: 0`` given as page front-matter must suppress the excerpt.

Regression test: ``0`` is falsy, so it used to be overridden by
``post_auto_excerpt`` and the first paragraph leaked into the postlist.
"""
app.build()

assert app.statuscode == 0
html = read_text(app.outdir / "postlist.html")

assert "ZEROFIRSTPARA" not in html
assert "ZEROSECONDPARA" not in html


@pytest.mark.sphinx("html", testroot="excerpt")
def test_excerpt_count_in_front_matter_is_respected(app, status, warning):
"""
A non-zero ``:excerpt:`` still yields that many paragraphs.
"""
app.build()

assert app.statuscode == 0
html = read_text(app.outdir / "postlist.html")

assert "ONEFIRSTPARA" in html
assert "ONESECONDPARA" not in html


@pytest.mark.sphinx("html", testroot="excerpt")
def test_excerpt_absent_falls_back_to_auto_excerpt(app, status, warning):
"""
With no ``:excerpt:`` at all, ``post_auto_excerpt`` still applies.
"""
app.build()

assert app.statuscode == 0
html = read_text(app.outdir / "postlist.html")

assert "DEFAULTFIRSTPARA" in html
assert "DEFAULTSECONDPARA" not in html


@pytest.mark.sphinx("html", testroot="excerpt", confoverrides={"post_auto_excerpt": 2})
def test_excerpt_zero_overrides_post_auto_excerpt(app, status, warning):
"""
An explicit ``0`` wins over a non-default ``post_auto_excerpt``.
"""
app.build()

assert app.statuscode == 0
html = read_text(app.outdir / "postlist.html")

assert "ZEROFIRSTPARA" not in html
assert "DEFAULTSECONDPARA" in html
Loading