feat: batch and combine Word and PDF exports transactionally
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
import threading
|
||||
import time
|
||||
import zipfile
|
||||
from pathlib import Path
|
||||
import pytest
|
||||
from PIL import Image
|
||||
from PySide6.QtCore import QSettings
|
||||
from mdhelper.batch_documents import BatchDocument, combine_documents, fingerprint, publish_outputs
|
||||
from mdhelper.batch_export_dialog import BatchExportDialog
|
||||
from mdhelper.docx_exporter import export_markdown_to_docx
|
||||
from mdhelper.export_jobs import ExportCancelled
|
||||
from mdhelper.export_settings import PageSettings
|
||||
from mdhelper.markdown_renderer import MarkdownRenderer
|
||||
from mdhelper import main_window, batch_documents
|
||||
|
||||
|
||||
def documents(tmp_path):
|
||||
first, second = tmp_path / 'first.md', tmp_path / 'sub' / 'second.md'
|
||||
second.parent.mkdir()
|
||||
Image.new('RGB', (16, 16), 'red').save(second.parent / '图.png')
|
||||
first.write_text('# Shared\n\n[chapter](sub/second.md)\n\n[other](sub/second.md#shared)\n\n[ref][same]\n\n[same]: sub/second.md#shared', encoding='utf-8')
|
||||
second.write_text('# Shared\n\n\n\n[ref][same]\n\n[same]: ../first.md#shared\n\n```\n[code](../first.md)\n```', encoding='utf-8')
|
||||
return [BatchDocument(path, path.read_text(encoding='utf-8')) for path in (first, second)]
|
||||
|
||||
|
||||
def test_combined_resources_duplicate_headings_and_real_docx(tmp_path):
|
||||
docs = documents(tmp_path)
|
||||
combined = combine_documents(docs, MarkdownRenderer(tmp_path))
|
||||
assert [h.slug for h in combined.headings] == ['mdh1', 'mdh1h1', 'mdh2', 'mdh2h1']
|
||||
assert 'href="#mdh2h1"' in combined.html_body and 'href="#mdh1h1"' in combined.html_body
|
||||
assert 'id="mdh2h1"' in combined.html_body and '(#mdh2)' in combined.word_source
|
||||
assert '[code](../first.md)' in combined.word_source
|
||||
output = tmp_path / 'combined.docx'
|
||||
export_markdown_to_docx(combined.word_source, output, engine='pandoc', source_path=docs[0].path,
|
||||
page_settings=PageSettings(header='Batch header', footer='Batch footer'))
|
||||
with zipfile.ZipFile(output) as archive:
|
||||
xml = archive.read('word/document.xml').decode()
|
||||
assert 'w:anchor="mdh2h1"' in xml and 'w:name="mdh2h1"' in xml
|
||||
assert any(name.startswith('word/media/') for name in archive.namelist())
|
||||
assert 'TOC ' in xml
|
||||
assert 'Batch header' in archive.read('word/header1.xml').decode()
|
||||
|
||||
|
||||
def test_publication_cancellation_race_and_rollback(tmp_path, monkeypatch):
|
||||
destinations = [tmp_path / f'{i}.out' for i in range(2)]
|
||||
staged = [tmp_path / f'{i}.stage' for i in range(2)]
|
||||
for path in destinations: path.write_bytes(b'old')
|
||||
for path in staged: path.write_bytes(b'new')
|
||||
outputs = dict(zip(destinations, staged)); expected = {p: fingerprint(p) for p in destinations}
|
||||
cancel = threading.Event(); cancel.set()
|
||||
with pytest.raises(ExportCancelled): publish_outputs(outputs, expected, tmp_path, cancel)
|
||||
destinations[0].write_bytes(b'external')
|
||||
with pytest.raises(OSError, match='发生变化'): publish_outputs(outputs, expected, tmp_path)
|
||||
destinations[0].write_bytes(b'old')
|
||||
real_write = batch_documents.atomic_write
|
||||
def fail(path, data):
|
||||
if path == destinations[1] and data == b'new': raise OSError('disk full')
|
||||
real_write(path, data)
|
||||
monkeypatch.setattr(batch_documents, 'atomic_write', fail)
|
||||
with pytest.raises(OSError, match='disk full'): publish_outputs(outputs, expected, tmp_path)
|
||||
assert all(path.read_bytes() == b'old' for path in destinations)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('kind,combined', [('docx', False), ('pdf', True), ('pdf', False)])
|
||||
def test_queue_generates_complete_outputs(qt_application, tmp_path, monkeypatch, kind, combined):
|
||||
docs = documents(tmp_path)
|
||||
settings = QSettings(str(tmp_path / 'settings.ini'), QSettings.Format.IniFormat)
|
||||
monkeypatch.setattr(main_window, 'QSettings', lambda *_: settings)
|
||||
errors = []
|
||||
monkeypatch.setattr(main_window.QMessageBox, 'warning', lambda *args: errors.append(str(args[2])))
|
||||
window = main_window.MainWindow(tmp_path)
|
||||
panel = BatchExportDialog(window)
|
||||
try:
|
||||
window.open_document(docs[0].path)
|
||||
output = [tmp_path / f'export-{i}.{kind}' for i in range(1 if combined else 2)]
|
||||
panel.begin(docs, output, kind, combined, window._export_snapshot(), PageSettings(header='Batch', include_toc=True))
|
||||
deadline = time.monotonic() + 60
|
||||
while panel.running and time.monotonic() < deadline:
|
||||
qt_application.processEvents(); time.sleep(.02)
|
||||
assert not panel.running, panel.status.text()
|
||||
assert not errors and all(path.exists() for path in output), (panel.status.text(), errors)
|
||||
if kind == 'pdf':
|
||||
from pypdf import PdfReader
|
||||
text = '\n'.join(page.extract_text() for path in output for page in PdfReader(path).pages)
|
||||
assert 'Shared' in text and 'Batch' in text
|
||||
assert sum(len(page.images) for path in output for page in PdfReader(path).pages) >= 1
|
||||
finally:
|
||||
panel.cancel(); window.close()
|
||||
Reference in New Issue
Block a user