65 lines
3.3 KiB
Python
65 lines
3.3 KiB
Python
from pathlib import Path
|
|
import pytest
|
|
from PySide6.QtCore import QSettings
|
|
from mdhelper import main_window, rename_references as renaming
|
|
|
|
|
|
def fixture_documents(tmp_path):
|
|
old, new, ref = tmp_path / 'old.md', tmp_path / 'new name.md', tmp_path / 'ref.md'
|
|
old.write_text('# Title\n[self](old.md#title)', encoding='utf-8')
|
|
ref.write_text('[go][id]\n\n[id]: old.md#title "tip"\n\n`[literal](old.md)`\n\n<a href="old.md?q=1#title">HTML</a>', encoding='utf-8')
|
|
return old, new, ref
|
|
|
|
|
|
def test_reference_rewrite_preserves_code_titles_fragments_and_backups(tmp_path):
|
|
old, new, ref = fixture_documents(tmp_path)
|
|
originals = {p: p.read_bytes() for p in (old, ref)}
|
|
plan = renaming.plan_rename(tmp_path, old, new)
|
|
backup = renaming.execute_rename(plan, tmp_path / 'backups')
|
|
assert not old.exists() and '[self](new%20name.md#title)' in new.read_text()
|
|
content = ref.read_text()
|
|
assert '[go](new%20name.md#title "tip")' in content
|
|
assert '`[literal](old.md)`' in content and 'new%20name.md?q=1#title' in content
|
|
assert {p.read_bytes() for p in backup.glob('*.bak')} == set(originals.values())
|
|
|
|
|
|
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)
|
|
ref.write_text('external')
|
|
with pytest.raises(OSError, match='已变化'): renaming.execute_rename(plan, tmp_path / 'backups')
|
|
assert old.exists() and not new.exists() and ref.read_text() == 'external'
|
|
old, new, ref = fixture_documents(tmp_path)
|
|
plan = renaming.plan_rename(tmp_path, old, new)
|
|
write = renaming.atomic_write
|
|
def fail(path, data):
|
|
if path == ref: raise OSError('simulated permission error')
|
|
write(path, data)
|
|
monkeypatch.setattr(renaming, 'atomic_write', fail)
|
|
with pytest.raises(OSError, match='文件已恢复'): renaming.execute_rename(plan, tmp_path / 'backups')
|
|
assert not new.exists()
|
|
for path, data in plan.originals.items(): assert path.read_bytes() == data
|
|
|
|
|
|
def test_gui_rename_keeps_buffers_tabs_and_navigation(qt_application, tmp_path, monkeypatch):
|
|
old, new, ref = fixture_documents(tmp_path)
|
|
settings = QSettings(str(tmp_path / 'settings.ini'), QSettings.Format.IniFormat)
|
|
monkeypatch.setattr(main_window, 'QSettings', lambda *_: settings)
|
|
monkeypatch.setattr(renaming, 'confirm_rename', lambda *_: True)
|
|
monkeypatch.setenv('LOCALAPPDATA', str(tmp_path / 'user-data'))
|
|
window = main_window.MainWindow(tmp_path)
|
|
try:
|
|
window.open_document(old); window.edit_current_markdown()
|
|
editor = window.tabs.currentWidget()
|
|
editor.replace_source_undoable(editor.source_text() + '\nUnsaved content')
|
|
window.bookmarks = [{'path': str(old), 'fragment': 'title', 'label': 'Title'}]
|
|
assert window._rename_with_references(old, new)
|
|
assert editor.session.path == new and not editor.has_unsaved_changes()
|
|
assert 'Unsaved content' in new.read_text() and 'new%20name.md' in ref.read_text()
|
|
assert window.current_file == new and window.bookmarks[0]['path'] == str(new)
|
|
assert old not in window.recent_files and new in window.recent_files
|
|
states, _ = window.workspaces.load(tmp_path)
|
|
assert states[0].path == str(new)
|
|
finally:
|
|
window.close()
|