fix: add a missing description to Office files instead of silently writing nothing

This commit is contained in:
randogoth 2026-10-24 22:19:13 +03:00
parent 93661d664a
commit c1bc07ffeb
3 changed files with 70 additions and 4 deletions

View file

@ -186,3 +186,46 @@ def test_cmd_messages_show_square_brackets_in_file_names(cwd, monkeypatch, capsy
monkeypatch.setattr(sys, 'argv', ['otag', 'a', 'a [demo].mp3', '#lofi'])
main()
assert 'a [demo].mp3' in capsys.readouterr().out
def make_zip(path, members):
with zipfile.ZipFile(path, 'w') as z:
for name, data in members.items():
z.writestr(name, data)
def core_xml(cwd, name):
with zipfile.ZipFile(cwd / name) as z:
return z.read('docProps/core.xml').decode()
def test_docx_without_description_gets_one(cwd):
make_zip(cwd / 'a.docx', {'docProps/core.xml': DOCX_CORE.replace('<dc:description></dc:description>', '')})
roundtrip(cwd, 'a.docx')
assert '<dc:title>T</dc:title><dc:description>+proj #lofi R&amp;B C:\\1</dc:description></cp:coreProperties>' in core_xml(cwd, 'a.docx')
def test_self_closing_description_is_replaced(cwd):
make_zip(cwd / 'a.docx', {'docProps/core.xml': DOCX_CORE.replace('<dc:description></dc:description>', '<dc:description/>')})
roundtrip(cwd, 'a.docx')
assert core_xml(cwd, 'a.docx').count('dc:description') == 2
def test_inserted_description_declares_its_namespace(cwd):
import xml.etree.ElementTree as ET
core = '<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties"></cp:coreProperties>'
make_zip(cwd / 'a.docx', {'docProps/core.xml': core})
OrvaniTag().add('a.docx', '#lofi')
OrvaniTag().write_metadata('a.docx', 'office')
root = ET.fromstring(core_xml(cwd, 'a.docx'))
assert root.find('{http://purl.org/dc/elements/1.1/}description').text == '#lofi'
def test_docx_without_core_properties_is_refused(cwd):
from orvanitag.errors import UnsupportedFile
make_zip(cwd / 'a.docx', {'[Content_Types].xml': '<Types/>'})
before = (cwd / 'a.docx').read_bytes()
OrvaniTag().add('a.docx', '#lofi')
with pytest.raises(UnsupportedFile):
OrvaniTag().write_metadata('a.docx', 'office')
assert (cwd / 'a.docx').read_bytes() == before