1from pathlib import Path
2import re
3import runpy
4import shutil
5from tempfile import TemporaryDirectory
6import unittest
7import xml.etree.ElementTree as ET
8
9from native_xml import ns
10
11ROOT = Path(__file__).resolve().parent.parent
12FIXTURE = ROOT / 'corpus/attachment-edit'
13compare = runpy.run_path(str(ROOT / 'tools/verify-document.py'))['compare']
14
15
16class AttachmentEditTest(unittest.TestCase):
17 def check(self, variant):
18 with TemporaryDirectory() as temporary:
19 read = Path(temporary) / 'read'
20 shutil.copytree(FIXTURE / variant / 'cold/read', read)
21 compare(FIXTURE / variant / 'candidate', read)
22 page, = (ET.parse(path).getroot() for path in sorted((FIXTURE / variant / 'cold/read').glob('page-*.xml')))
23 inserted, = page.iter('{%s}InsertedFile' % ns['one'])
24 self.assertEqual(inserted.get('preferredName'), 'notes 🦀.txt')
25 texts = [re.sub(r'<[^>]*>', '', t.text or '') for t in page.findall('.//one:OE/one:T', ns)]
26 self.assertEqual(texts, ['Before the file', 'After the file'])
27
28 def test_a_rust_inserted_attachment_renders_natively_with_its_payload(self):
29 self.check('plain')
30
31 def test_a_rust_inserted_attachment_with_an_icon_preview_renders_natively(self):
32 self.check('icon')
33
34 def test_a_stored_attachment_renamed_by_rust_shows_its_new_name_and_source(self):
35 with TemporaryDirectory() as temporary:
36 read = Path(temporary) / 'read'
37 shutil.copytree(FIXTURE / 'renamed/cold/read', read)
38 compare(FIXTURE / 'renamed/candidate', read)
39 page, = (ET.parse(path).getroot() for path in sorted((FIXTURE / 'renamed/cold/read').glob('page-*.xml')))
40 inserted, = page.iter('{%s}InsertedFile' % ns['one'])
41 self.assertEqual((inserted.get('preferredName'), inserted.get('pathSource')), ('renamed 🦀.txt', 'C:\\inputs\\renamed 🦀.txt'))
42
43
44if __name__ == '__main__':
45 unittest.main()