83 lines
4.1 KiB
Python
83 lines
4.1 KiB
Python
"""Ordinary saves must protect the exact disk version reviewed by the editor."""
|
|
from pathlib import Path
|
|
import pytest
|
|
from PySide6.QtCore import QSettings
|
|
from PySide6.QtWidgets import QMessageBox
|
|
from mdhelper import file_versions as files, main_window
|
|
from mdhelper.document_session import DocumentStore
|
|
|
|
|
|
@pytest.mark.parametrize('boundary', ['store', 'flush', 'replace'])
|
|
def test_editor_save_conflict_preserves_external_file_and_unsaved_buffer(qt_application, tmp_path, monkeypatch, boundary):
|
|
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(QMessageBox, 'warning', lambda *args: QMessageBox.StandardButton.Cancel)
|
|
path = tmp_path/'note.md'; path.write_bytes(b'Original')
|
|
window = main_window.MainWindow(tmp_path)
|
|
try:
|
|
window.open_document(path); window.edit_current_markdown()
|
|
editor = window.tabs.currentWidget()
|
|
editor.replace_source_undoable('Editor contents')
|
|
external = b'External saved at commit'
|
|
injected = False
|
|
def change():
|
|
nonlocal injected
|
|
if not injected:
|
|
injected = True; path.write_bytes(external)
|
|
if boundary == 'store':
|
|
save = DocumentStore.save
|
|
def race(target, source, **kwargs):
|
|
change(); return save(target, source, **kwargs)
|
|
monkeypatch.setattr(DocumentStore, 'save', staticmethod(race))
|
|
elif boundary == 'flush':
|
|
fsync = files.os.fsync
|
|
def race(descriptor):
|
|
fsync(descriptor); change()
|
|
monkeypatch.setattr(files.os, 'fsync', race)
|
|
else:
|
|
replace = files._replace_with_backup
|
|
def race(temporary, target, backup):
|
|
if target == path: change()
|
|
replace(temporary, target, backup)
|
|
monkeypatch.setattr(files, '_replace_with_backup', race)
|
|
assert not window._save_editor_tab(editor, path, editor.source_text(), editor.file_signature)
|
|
assert injected and path.read_bytes() == external
|
|
assert editor.has_unsaved_changes() and editor.source_text() == 'Editor contents'
|
|
assert editor.session.saved_source == 'Original'
|
|
assert path not in window.internal_save_paths
|
|
if boundary == 'replace':
|
|
assert external in [p.read_bytes() for p in tmp_path.glob('.*.conflict-*.bak')]
|
|
finally:
|
|
monkeypatch.setattr(QMessageBox, 'question', lambda *args: QMessageBox.StandardButton.Discard)
|
|
window.close()
|
|
|
|
|
|
def test_new_editor_save_cannot_overwrite_concurrent_creation(tmp_path, monkeypatch):
|
|
path = tmp_path/'new.md'
|
|
rename = files.os.rename
|
|
def race(source, target):
|
|
if Path(target) == path: path.write_bytes(b'External creation')
|
|
rename(source, target)
|
|
monkeypatch.setattr(files.os, 'rename', race)
|
|
with pytest.raises(FileExistsError): files.VersionStore(tmp_path/'history').save(path, 'Editor')
|
|
assert path.read_bytes() == b'External creation'
|
|
|
|
|
|
def test_save_as_checks_destination_snapshot_before_image_relocation(qt_application, tmp_path, monkeypatch):
|
|
settings = QSettings(str(tmp_path/'settings.ini'), QSettings.Format.IniFormat)
|
|
monkeypatch.setattr(main_window, 'QSettings', lambda *_: settings)
|
|
monkeypatch.setattr(QMessageBox, 'warning', lambda *args: QMessageBox.StandardButton.Cancel)
|
|
source = tmp_path/'original.md'; source.write_text('Original')
|
|
target = tmp_path/'other'/'new.md'; target.parent.mkdir()
|
|
def relocate(text, original, destination):
|
|
destination.write_text('External creation'); return text
|
|
monkeypatch.setattr(DocumentStore, 'prepare_save', staticmethod(relocate))
|
|
window = main_window.MainWindow(tmp_path)
|
|
try:
|
|
window.open_document(source); window.edit_current_markdown()
|
|
editor = window.tabs.currentWidget()
|
|
assert not window._save_editor_tab(editor, target, 'Editor', editor.file_signature)
|
|
assert target.read_text() == 'External creation' and editor.session.path == source
|
|
finally: window.close()
|