test: Ensure empty and None titles are judged invalid

- Check that pages with an empty or `None` title fail validation.
- Use a fixed date in the article test.
- Add a reader-level test using the empty `:title:` source from #3566 to
  confirm readers produce an empty title that is then rejected.
This commit is contained in:
Justin Mayer 2026-10-11 20:40:29 +02:00
commit e54689de4e
2 changed files with 29 additions and 5 deletions

View file

@ -84,8 +84,13 @@ class TestPage(TestBase):
# If the title is not set, must throw an exception.
page = Page("content")
self.assertFalse(page._has_valid_mandatory_properties())
# An empty or `None` title must be treated the same as a missing title.
for title in ("", None):
with self.subTest(title=title):
page = Page("content", metadata={"title": title})
self.assertFalse(page._has_valid_mandatory_properties())
self.assertLogCountEqual(
count=1,
count=3,
msg="Skipping .*: could not find information about 'title'",
level=logging.ERROR,
)
@ -735,12 +740,10 @@ class TestArticle(TestBase):
self.assertEqual("custom", custom_article.template)
def test_empty_title_invalid(self):
# articles with empty title should be invalid
settings = get_settings()
# Articles with an empty title should be invalid
article_kwargs = self._copy_page_kwargs()
article_kwargs["metadata"]["title"] = ""
article_kwargs["metadata"]["date"] = datetime.datetime.now()
article_kwargs["settings"] = settings
article_kwargs["metadata"]["date"] = datetime.datetime(2026, 2, 9)
article = Article(**article_kwargs)
self.assertFalse(article.is_valid())

View file

@ -1,7 +1,9 @@
import os
from tempfile import TemporaryDirectory
from unittest.mock import PropertyMock, patch
from pelican import readers
from pelican.contents import Article
from pelican.tests.support import get_settings, unittest
from pelican.utils import SafeDatetime
@ -83,6 +85,25 @@ class DefaultReaderTest(ReaderTest):
with self.assertRaises(TypeError):
self.read_file(path="article_with_metadata.unknownextension")
def test_empty_title_is_invalid(self):
"""A source file with an empty title must be considered invalid rather
than producing a file named ".html"."""
sources = {"empty_title.rst": ":title:\n:date: 2026-02-09\n\nContent.\n"}
if readers.Markdown:
sources["empty_title.md"] = "Title:\nDate: 2026-02-09\n\nContent.\n"
with TemporaryDirectory() as content_path:
r = readers.Readers(settings=get_settings(PATH=content_path))
for filename, source in sources.items():
with self.subTest(filename=filename):
with open(os.path.join(content_path, filename), "w") as f:
f.write(source)
article = r.read_file(
base_path=content_path, path=filename, content_class=Article
)
self.assertEqual(article.title, "")
self.assertFalse(article.is_valid())
def test_readfile_path_metadata_implicit_dates(self):
test_file = "article_with_metadata_implicit_dates.html"
page = self.read_file(path=test_file, DEFAULT_DATE="fs")