Trigger editor signal on exit if content changed.

With the previous code, the editor could miss the final signal on a
save-and-exit. This is avoided by always running the file changed
handler on a successful exit, but only firing the signal if the content
actually changed (to avoid double-signalling).
This commit is contained in:
Ryan Roden-Corrent 2018-01-26 11:12:07 -05:00
parent 7c33ff4046
commit 530a1859a3
2 changed files with 28 additions and 6 deletions

View File

@ -51,6 +51,7 @@ class ExternalEditor(QObject):
self._proc = None
self._remove_file = None
self._watcher = QFileSystemWatcher(parent=self)
self._content = None
def _cleanup(self):
"""Clean up temporary files after the editor closed."""
@ -79,6 +80,8 @@ class ExternalEditor(QObject):
# No error/cleanup here, since we already handle this in
# on_proc_error.
return
# do a final read to make sure we don't miss the last signal
self._on_file_changed(self._filename)
self._cleanup()
@pyqtSlot(QProcess.ProcessError)
@ -128,8 +131,11 @@ class ExternalEditor(QObject):
message.error("Failed to read back edited file: {}".format(e))
return
log.procs.debug("Read back: {}".format(text))
if self._content != text:
self._content = text
self.file_updated.emit(text)
def edit_file(self, filename):
"""Edit the file with the given filename."""
self._filename = filename

View File

@ -129,9 +129,6 @@ class TestFileHandling:
pytest.skip("File was still readable")
with caplog.at_level(logging.ERROR):
with qtbot.wait_signal(editor._watcher.fileChanged):
with open(filename, 'w', encoding='utf-8') as f:
f.write('unreadable')
editor._proc.finished.emit(0, QProcess.NormalExit)
assert not os.path.exists(filename)
msg = message_mock.getmsg(usertypes.MessageLevel.error)
@ -177,11 +174,30 @@ def test_modify(qtbot, editor, initial_text, edited_text):
with open(editor._filename, 'w', encoding='utf-8') as f:
f.write(edited_text)
with qtbot.assert_not_emitted(editor.file_updated):
editor._proc.finished.emit(0, QProcess.NormalExit)
assert blocker.args == [edited_text]
def test_modify_multiple(qtbot, editor):
"""Test that multiple saves all trigger file_updated."""
editor.edit('foo')
with qtbot.wait_signal(editor.file_updated) as blocker:
with open(editor._filename, 'w', encoding='utf-8') as f:
f.write('bar')
assert blocker.args == ['bar']
with qtbot.wait_signal(editor.file_updated) as blocker:
with open(editor._filename, 'w', encoding='utf-8') as f:
f.write('baz')
assert blocker.args == ['baz']
with qtbot.assert_not_emitted(editor.file_updated):
editor._proc.finished.emit(0, QProcess.NormalExit)
@pytest.mark.parametrize('text, caret_position, result', [
('', 0, (1, 1)),
('a', 0, (1, 1)),