From c56c1695cb77d403b808d0e04affe32ede19d3b0 Mon Sep 17 00:00:00 2001 From: Justin Mayer Date: Sun, 11 Oct 2026 20:40:29 +0200 Subject: [PATCH] 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. --- pelican/tests/test_contents.py | 13 ++++++++----- pelican/tests/test_readers.py | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/pelican/tests/test_contents.py b/pelican/tests/test_contents.py index 780770ea..cec07982 100644 --- a/pelican/tests/test_contents.py +++ b/pelican/tests/test_contents.py @@ -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()) diff --git a/pelican/tests/test_readers.py b/pelican/tests/test_readers.py index 751088f7..7ed86cc3 100644 --- a/pelican/tests/test_readers.py +++ b/pelican/tests/test_readers.py @@ -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")