Fix test_editor for edit-on-write behavior.

Now that the editor fires editing_finished on every write, the unit
tests had to be updated.

- Add qtbot to the editor fixture to resolve `QtWarningMsg:
  QSocketNotifier: Can only be used with threads started with QThread`
- Use removePaths instead of disconnect to stop the watcher from
  signalling. This avoids an error when the editor is forcibly cleaned
  up by the tests without the signal ever being connected, but otherwise
  has the same behavior as disconnecting the singal.
- wait for a signal on write instead of proc closed
- wait for _watcher.fileChanged in test_unreadable to ensure the write
  event is fired before the test exits.
This commit is contained in:
Ryan Roden-Corrent 2017-12-29 16:08:59 -05:00
parent 8a9b98c2dc
commit 23eb6a6c53
2 changed files with 12 additions and 9 deletions

View File

@ -55,7 +55,7 @@ class ExternalEditor(QObject):
def _cleanup(self):
"""Clean up temporary files after the editor closed."""
assert self._remove_file is not None
self._watcher.fileChanged.disconnect()
self._watcher.removePaths(self._watcher.files())
if self._filename is None or not self._remove_file:
# Could not create initial file.
return

View File

@ -37,7 +37,7 @@ def patch_things(config_stub, monkeypatch, stubs):
@pytest.fixture
def editor(caplog):
def editor(caplog, qtbot):
ed = editormod.ExternalEditor()
yield ed
with caplog.at_level(logging.ERROR):
@ -118,18 +118,21 @@ class TestFileHandling:
os.remove(filename)
def test_unreadable(self, message_mock, editor, caplog):
def test_unreadable(self, message_mock, editor, caplog, qtbot):
"""Test file handling when closing with an unreadable file."""
editor.edit("")
filename = editor._filename
assert os.path.exists(filename)
os.chmod(filename, 0o077)
os.chmod(filename, 0o277)
if os.access(filename, os.R_OK):
# Docker container or similar
pytest.skip("File was still readable")
with caplog.at_level(logging.ERROR):
editor._proc.finished.emit(0, QProcess.NormalExit)
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)
assert msg.text.startswith("Failed to read back edited file: ")
@ -170,11 +173,11 @@ def test_modify(qtbot, editor, initial_text, edited_text):
with open(editor._filename, 'r', encoding='utf-8') as f:
assert f.read() == initial_text
with open(editor._filename, 'w', encoding='utf-8') as f:
f.write(edited_text)
with qtbot.wait_signal(editor.editing_finished) as blocker:
editor._proc.finished.emit(0, QProcess.NormalExit)
with open(editor._filename, 'w', encoding='utf-8') as f:
f.write(edited_text)
editor._proc.finished.emit(0, QProcess.NormalExit)
assert blocker.args == [edited_text]