feat: compare and merge external save conflicts safely
This commit is contained in:
@@ -137,7 +137,9 @@ def test_save_as_changes_draft_ownership_only_after_success(editing, monkeypatch
|
||||
assert original.read_text() == "# Original"
|
||||
|
||||
|
||||
def test_save_recreates_externally_deleted_file(editing):
|
||||
def test_save_recreates_externally_deleted_file(editing, monkeypatch):
|
||||
from mdhelper import save_conflict
|
||||
monkeypatch.setattr(save_conflict, "resolve_save_conflict", lambda parent, path, source, *args, **kwargs: source)
|
||||
window, editor, path = editing
|
||||
editor.replace_source_undoable("recovered")
|
||||
path.unlink()
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
import pytest
|
||||
from PySide6.QtCore import QSettings, QTimer, Qt
|
||||
from PySide6.QtWidgets import QApplication, QDialog, QListWidget
|
||||
from mdhelper import main_window, save_conflict
|
||||
|
||||
|
||||
def test_chunk_choices_preserve_insertions_deletions_and_final_newline():
|
||||
comparison = save_conflict.ConflictComparison('A\nlocal\nB\nremove\nC', 'A\nremote\nB\nC\nadded\n')
|
||||
assert comparison.merge() == 'A\nlocal\nB\nremove\nC'
|
||||
assert comparison.merge(range(len(comparison.changes))) == 'A\nremote\nB\nC\nadded\n'
|
||||
assert comparison.merge([0]).startswith('A\nremote\nB\nremove')
|
||||
assert save_conflict.ConflictComparison('', 'new').merge([0]) == 'new'
|
||||
|
||||
|
||||
def test_comparison_dialog_uses_selected_disk_chunk(qt_application, tmp_path):
|
||||
def choose():
|
||||
dialog = QApplication.activeModalWidget()
|
||||
dialog.findChild(QListWidget).item(0).setCheckState(Qt.CheckState.Checked)
|
||||
dialog.accept()
|
||||
QTimer.singleShot(30, choose)
|
||||
assert save_conflict.resolve_save_conflict(None, tmp_path / 'note.md', 'a\nlocal\n', 'a\nremote\n', 'a\nbase\n') == 'a\nremote\n'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('decision', ['cancel', 'merge', 'race', 'deleted'])
|
||||
def test_editor_conflict_protects_both_versions(qt_application, tmp_path, monkeypatch, decision):
|
||||
settings = QSettings(str(tmp_path / 'settings.ini'), QSettings.Format.IniFormat)
|
||||
monkeypatch.setattr(main_window, 'QSettings', lambda *_: settings)
|
||||
monkeypatch.setenv('MDHELPER_HISTORY_DIR', str(tmp_path / 'history'))
|
||||
monkeypatch.setattr(main_window.QMessageBox, 'warning', lambda *_: None)
|
||||
path = tmp_path / 'note.md'; path.write_text('base')
|
||||
window = main_window.MainWindow(tmp_path)
|
||||
try:
|
||||
window.open_document(path); window.edit_current_markdown()
|
||||
editor = window.tabs.currentWidget(); editor.replace_source_undoable('local')
|
||||
if decision == 'deleted': path.unlink()
|
||||
else: path.write_text('external modification')
|
||||
def choose(*args, **kwargs):
|
||||
if decision == 'cancel': return None
|
||||
if decision == 'race': path.write_text('newer external change')
|
||||
return 'merged result'
|
||||
monkeypatch.setattr(save_conflict, 'resolve_save_conflict', choose)
|
||||
saved = window._save_editor_tab(editor, path, editor.source_text(), editor.session.signature)
|
||||
assert saved == (decision in {'merge', 'deleted'})
|
||||
if saved:
|
||||
assert path.read_text() == editor.source_text() == editor.session.saved_source == 'merged result'
|
||||
assert not editor.has_unsaved_changes()
|
||||
else:
|
||||
assert editor.source_text() == 'local'
|
||||
assert path.read_text() == ('newer external change' if decision == 'race' else 'external modification')
|
||||
editor.mark_saved(path, editor.source_text(), window._file_signature(path))
|
||||
finally:
|
||||
window.close()
|
||||
Reference in New Issue
Block a user