mirror of
https://github.com/getpelican/pelican.git
synced 2026-10-15 16:16:55 +02:00
fix: Clarify errors for missing or empty metadata
Report whether required metadata is missing, or present but empty, instead of the vague "could not find information about" message, which was misleading when a field such as `:title:` was present but empty.
This commit is contained in:
parent
dc93585dc7
commit
6dc2507907
2 changed files with 15 additions and 7 deletions
|
|
@ -170,13 +170,16 @@ class Content:
|
|||
return self.source_path or repr(self)
|
||||
|
||||
def _has_valid_mandatory_properties(self) -> bool:
|
||||
"""Test mandatory properties are set."""
|
||||
"""Test mandatory properties are set and not empty."""
|
||||
for prop in self.mandatory_properties:
|
||||
if not hasattr(self, prop) or getattr(self, prop) in (None, ""):
|
||||
if not hasattr(self, prop):
|
||||
logger.error(
|
||||
"Skipping %s: could not find information about '%s'", self, prop
|
||||
"Skipping %s: required metadata '%s' is missing", self, prop
|
||||
)
|
||||
return False
|
||||
if getattr(self, prop) in (None, ""):
|
||||
logger.error("Skipping %s: required metadata '%s' is empty", self, prop)
|
||||
return False
|
||||
return True
|
||||
|
||||
def _has_valid_slug(self) -> bool:
|
||||
|
|
|
|||
|
|
@ -81,17 +81,22 @@ class TestPage(TestBase):
|
|||
self.assertEqual(page.content, TEST_CONTENT)
|
||||
|
||||
def test_mandatory_properties(self):
|
||||
# If the title is not set, must throw an exception.
|
||||
# A missing title must make the content invalid.
|
||||
page = Page("content")
|
||||
self.assertFalse(page._has_valid_mandatory_properties())
|
||||
# An empty or `None` title must be treated the same as a missing title.
|
||||
self.assertLogCountEqual(
|
||||
count=1,
|
||||
msg="Skipping .*: required metadata 'title' is missing",
|
||||
level=logging.ERROR,
|
||||
)
|
||||
# So must an empty or `None` title, with a message saying it is empty.
|
||||
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=3,
|
||||
msg="Skipping .*: could not find information about 'title'",
|
||||
count=2,
|
||||
msg="Skipping .*: required metadata 'title' is empty",
|
||||
level=logging.ERROR,
|
||||
)
|
||||
page = Page("content", metadata={"title": "foobar"})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue