[pp:metadata] fix handling of empty directory paths (#7296)

This commit is contained in:
Mike Fährmann
2025-04-08 15:12:05 +02:00
parent f02dcb720a
commit 8eb365d4b6
3 changed files with 16 additions and 2 deletions

View File

@@ -269,7 +269,7 @@ class PathFormat():
try:
for fmt in self.directory_formatters:
segment = fmt(kwdict).strip()
if strip and segment != "..":
if strip and segment not in {".", ".."}:
# remove trailing dots and spaces (#647)
segment = segment.rstrip(strip)
if segment:

View File

@@ -180,7 +180,10 @@ class MetadataPP(PostProcessor):
pathfmt.directory_formatters = self._directory_formatters
pathfmt.directory_conditions = ()
segments = pathfmt.build_directory(pathfmt.kwdict)
directory = pathfmt.clean_path(os.sep.join(segments) + os.sep)
if segments:
directory = pathfmt.clean_path(os.sep.join(segments) + os.sep)
else:
directory = "." + os.sep
return os.path.join(self._base(pathfmt), directory)
finally:
pathfmt.directory_conditions = conditions

View File

@@ -511,6 +511,17 @@ class MetadataTest(BasePostprocessorTest):
path = self.pathfmt.realdirectory + "../json/12500/file.ext.json"
m.assert_called_once_with(path, "w", encoding="utf-8")
def test_metadata_directory_empty(self):
self._create(
{"directory": []},
)
with patch("builtins.open", mock_open()) as m:
self._trigger()
path = self.pathfmt.realdirectory + "./file.ext.json"
m.assert_called_once_with(path, "w", encoding="utf-8")
def test_metadata_basedirectory(self):
self._create({"base-directory": True})