fix: retain conflicting saves and preserve list content in Word
This commit is contained in:
@@ -53,9 +53,9 @@ def test_publication_cancellation_race_and_rollback(tmp_path, monkeypatch):
|
||||
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):
|
||||
def fail(path, data, **kwargs):
|
||||
if path == destinations[1] and data == b'new': raise OSError('disk full')
|
||||
real_write(path, data)
|
||||
real_write(path, data, **kwargs)
|
||||
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)
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
"""Commit-boundary conflicts and preservation of list content in Word."""
|
||||
import zipfile
|
||||
from lxml import etree as ET
|
||||
import pytest
|
||||
from mdhelper import file_versions as files, batch_documents as batch, rename_references as rename
|
||||
from mdhelper.docx_exporter import export_markdown_to_docx
|
||||
from mdhelper.markdown_renderer import MarkdownRenderer
|
||||
from mdhelper.docx_style import W, NS
|
||||
from test_style_regressions import parts
|
||||
|
||||
TABLE_LIST = '1. | HeadOne | HeadTwo |\n |---|---|\n | CellOne | CellTwo |\n\n ContinuedText\n\n2. LastText'
|
||||
HEADING_LIST = '1. ### FirstHeading\n\n ContinuedText\n\n2. ### LastHeading'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('operation', ['batch', 'rename'])
|
||||
@pytest.mark.parametrize('boundary', ['flush', 'replace'])
|
||||
def test_transactions_retain_external_save_at_commit(tmp_path, monkeypatch, operation, boundary):
|
||||
target = tmp_path/'ref.md'
|
||||
target.write_bytes(b'[Link](old.md)')
|
||||
stage = tmp_path/'stage'; stage.write_bytes(b'Exported')
|
||||
source = tmp_path/'old.md'; source.write_bytes(b'Source')
|
||||
plan = rename.plan_rename(tmp_path, source, tmp_path/'new.md')
|
||||
expected = {target: files.fingerprint(target)}
|
||||
replace, fsync = files._replace_with_backup, files.os.fsync
|
||||
external = b'External save at final commit boundary'
|
||||
injected = False
|
||||
def race_replace(temporary, path, backup):
|
||||
nonlocal injected
|
||||
if path == target and not injected:
|
||||
injected = True; target.write_bytes(external)
|
||||
replace(temporary, path, backup)
|
||||
def race_flush(descriptor):
|
||||
nonlocal injected
|
||||
fsync(descriptor)
|
||||
if not injected:
|
||||
injected = True; target.write_bytes(external)
|
||||
monkeypatch.setattr(files, '_replace_with_backup', race_replace) if boundary == 'replace' else monkeypatch.setattr(files.os, 'fsync', race_flush)
|
||||
with pytest.raises(OSError):
|
||||
if operation == 'batch': batch.publish_outputs({target: stage}, expected, tmp_path)
|
||||
else: rename.execute_rename(plan, tmp_path/'backups')
|
||||
assert target.read_bytes() == external
|
||||
assert source.exists() and not (tmp_path/'new.md').exists()
|
||||
|
||||
|
||||
def test_creation_does_not_overwrite_file_created_at_commit(tmp_path, monkeypatch):
|
||||
path = tmp_path/'new.out'
|
||||
link = files.os.link
|
||||
def race(src, dst):
|
||||
path.write_bytes(b'External creation')
|
||||
link(src, dst)
|
||||
monkeypatch.setattr(files.os, 'link', race)
|
||||
with pytest.raises(FileExistsError): files.atomic_write(path, b'Export', expected=None)
|
||||
assert path.read_bytes() == b'External creation'
|
||||
|
||||
|
||||
def test_second_conflict_during_recovery_keeps_both_external_versions(tmp_path, monkeypatch):
|
||||
path = tmp_path/'existing.out'; path.write_bytes(b'Original')
|
||||
expected = files.fingerprint(path)
|
||||
replace = files._replace_with_backup
|
||||
count = 0
|
||||
def racing_replace(temporary, target, backup):
|
||||
nonlocal count
|
||||
count += 1; path.write_bytes(f'External {count}'.encode())
|
||||
replace(temporary, target, backup)
|
||||
monkeypatch.setattr(files, '_replace_with_backup', racing_replace)
|
||||
with pytest.raises(OSError, match='恢复副本'): files.atomic_write(path, b'Export', expected=expected)
|
||||
recovered = [p.read_bytes() for p in tmp_path.iterdir()]
|
||||
assert b'External 1' in recovered and b'External 2' in recovered
|
||||
|
||||
|
||||
@pytest.mark.parametrize('engine', ['builtin', 'python-docx', 'pandoc'])
|
||||
def test_table_first_list_preserves_cells_and_numbered_empty_paragraph(tmp_path, engine):
|
||||
path = export_markdown_to_docx(TABLE_LIST, tmp_path/'table.docx', engine=engine)
|
||||
doc, _ = parts(path)
|
||||
body = doc.find(W+'body'); table = body.find(W+'tbl')
|
||||
assert [''.join(c.itertext()) for c in table.findall('w:tr/w:tc', NS)] == ['HeadOne','HeadTwo','CellOne','CellTwo']
|
||||
before = table.getprevious()
|
||||
assert before.tag == W+'p' and before.find('w:pPr/w:numPr', NS) is not None
|
||||
continued = next(p for p in doc.iter(W+'p') if ''.join(p.itertext()) == 'ContinuedText')
|
||||
num = continued.find('w:pPr/w:numPr/w:numId', NS)
|
||||
if num is not None:
|
||||
with zipfile.ZipFile(path) as package: numbering = ET.fromstring(package.read('word/numbering.xml'))
|
||||
abstract = numbering.find(f"w:num[@w:numId='{num.get(W+'val')}']/w:abstractNumId", NS).get(W+'val')
|
||||
marker = numbering.find(f"w:abstractNum[@w:abstractNumId='{abstract}']/w:lvl/w:lvlText", NS).get(W+'val')
|
||||
assert not marker.strip()
|
||||
assert '\u200b' not in ''.join(doc.itertext())
|
||||
|
||||
|
||||
@pytest.mark.parametrize('prefix', ['1. ', '- ', '> 1. ', '1. > '])
|
||||
def test_combined_heading_preserves_container_prefix(tmp_path, prefix):
|
||||
source = prefix + '### Heading\n'
|
||||
combined = batch.combine_documents([batch.BatchDocument(tmp_path/'one.md', source)], MarkdownRenderer(tmp_path))
|
||||
assert prefix + '#### Heading {#mdh1h1}' in combined.word_source
|
||||
|
||||
|
||||
def test_combined_word_keeps_heading_numbering(tmp_path):
|
||||
combined = batch.combine_documents([batch.BatchDocument(tmp_path/'one.md', HEADING_LIST)], MarkdownRenderer(tmp_path))
|
||||
doc, _ = parts(export_markdown_to_docx(combined.word_source, tmp_path/'combined.docx', engine='pandoc'))
|
||||
headings = [p for p in doc.iter(W+'p') if ''.join(p.itertext()) in ('FirstHeading', 'LastHeading')]
|
||||
assert len(headings) == 2
|
||||
assert all(p.find('w:pPr/w:numPr', NS) is not None for p in headings)
|
||||
@@ -15,8 +15,8 @@ def test_batch_preserves_external_changes_between_writes(tmp_path, monkeypatch,
|
||||
if changed != 'created': second.write_bytes(b'original')
|
||||
expected = {p: batch.fingerprint(p) for p in (first, second)}
|
||||
write = batch.atomic_write
|
||||
def concurrent_write(path, data):
|
||||
write(path, data)
|
||||
def concurrent_write(path, data, **kwargs):
|
||||
write(path, data, **kwargs)
|
||||
if path == first and data == b'exported':
|
||||
second.write_bytes(b'external second')
|
||||
if changed == 'rollback': first.write_bytes(b'external first')
|
||||
@@ -35,8 +35,8 @@ def test_rename_preserves_external_edits_during_transaction(tmp_path, monkeypatc
|
||||
ref.write_text('[ref](old.md)', encoding='utf8')
|
||||
plan = rename.plan_rename(tmp_path, source, target)
|
||||
write = rename.atomic_write
|
||||
def concurrent_write(path, data):
|
||||
write(path, data)
|
||||
def concurrent_write(path, data, **kwargs):
|
||||
write(path, data, **kwargs)
|
||||
if (when == 'backup' and path.name == 'manifest.json') or (when != 'backup' and path == target):
|
||||
ref.write_bytes(b'external ref')
|
||||
if when == 'rollback': target.write_bytes(b'external source')
|
||||
|
||||
@@ -32,9 +32,9 @@ def test_changed_disk_aborts_and_partial_write_rolls_back(tmp_path, monkeypatch)
|
||||
old, new, ref = fixture_documents(tmp_path)
|
||||
plan = renaming.plan_rename(tmp_path, old, new)
|
||||
write = renaming.atomic_write
|
||||
def fail(path, data):
|
||||
def fail(path, data, **kwargs):
|
||||
if path == ref: raise OSError('simulated permission error')
|
||||
write(path, data)
|
||||
write(path, data, **kwargs)
|
||||
monkeypatch.setattr(renaming, 'atomic_write', fail)
|
||||
with pytest.raises(OSError, match='文件已恢复'): renaming.execute_rename(plan, tmp_path / 'backups')
|
||||
assert not new.exists()
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
param([string]$Python = (Join-Path $PSScriptRoot '..\mdhelper_env\mdhelper_env\Scripts\python.exe'))
|
||||
$ErrorActionPreference='Stop'
|
||||
Push-Location (Join-Path $PSScriptRoot '..')
|
||||
$word=$null
|
||||
try {
|
||||
$folder = (@'
|
||||
import sys,tempfile
|
||||
from pathlib import Path
|
||||
sys.path.insert(0,str(Path('tests').resolve()))
|
||||
from test_content_revision_057 import *
|
||||
folder=Path(tempfile.mkdtemp(prefix='MDHelper-057-Word-'))
|
||||
for engine in ('builtin','python-docx','pandoc'):
|
||||
export_markdown_to_docx(TABLE_LIST,folder/f'{engine}-table.docx',engine=engine)
|
||||
combined=batch.combine_documents([batch.BatchDocument(folder/'chapter.md',HEADING_LIST)],MarkdownRenderer(folder))
|
||||
export_markdown_to_docx(combined.word_source,folder/'pandoc-combined.docx',engine='pandoc')
|
||||
print(folder)
|
||||
'@ | & $Python -B -).Trim()
|
||||
if ($LASTEXITCODE -ne 0) {throw 'Fixture generation failed'}
|
||||
$word=New-Object -ComObject Word.Application
|
||||
$word.Visible=$false; $word.DisplayAlerts=0
|
||||
foreach($engine in @('builtin','python-docx','pandoc')) {
|
||||
$doc=$word.Documents.Open((Join-Path $folder "$engine-table.docx"),$false,$true,$false)
|
||||
try {
|
||||
foreach($text in @('HeadOne','HeadTwo','CellOne','CellTwo')) {
|
||||
$r=$doc.Content.Duplicate
|
||||
if(-not $r.Find.Execute($text)){throw "$engine lost table text $text"}
|
||||
}
|
||||
if($doc.Paragraphs.Item(1).Range.ListFormat.ListString.Trim() -ne '1.'){throw "$engine table marker missing"}
|
||||
$r=$doc.Content.Duplicate; [void]$r.Find.Execute('ContinuedText')
|
||||
if($r.ListFormat.ListString.Trim() -ne ''){throw "$engine continuation gained number"}
|
||||
$r=$doc.Content.Duplicate; [void]$r.Find.Execute('LastText')
|
||||
if($r.ListFormat.ListString.Trim() -ne '2.'){throw "$engine numbering restarted"}
|
||||
Write-Output "$engine table content and numbering PASS"
|
||||
} finally {$doc.Close(0)}
|
||||
}
|
||||
$doc=$word.Documents.Open((Join-Path $folder 'pandoc-combined.docx'),$false,$true,$false)
|
||||
try {
|
||||
foreach($pair in @(@('FirstHeading','1.'),@('ContinuedText',''),@('LastHeading','2.'))) {
|
||||
$r=$doc.Content.Duplicate;[void]$r.Find.Execute($pair[0])
|
||||
if($r.ListFormat.ListString.Trim() -ne $pair[1]){throw "combined heading marker wrong: $($pair[0])"}
|
||||
}
|
||||
Write-Output 'combined headings PASS'
|
||||
} finally {$doc.Close(0)}
|
||||
Write-Output "Fixtures: $folder"
|
||||
} finally {
|
||||
if($word){$word.Quit();[void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($word)}
|
||||
Pop-Location
|
||||
}
|
||||
Reference in New Issue
Block a user