From 23eb6a6c534f397885e89f91fd48f641b807e10a Mon Sep 17 00:00:00 2001 From: Ryan Roden-Corrent Date: Fri, 29 Dec 2017 16:08:59 -0500 Subject: [PATCH] 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. --- qutebrowser/misc/editor.py | 2 +- tests/unit/misc/test_editor.py | 19 +++++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/qutebrowser/misc/editor.py b/qutebrowser/misc/editor.py index 62d4a2d6f..53067ca2c 100644 --- a/qutebrowser/misc/editor.py +++ b/qutebrowser/misc/editor.py @@ -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 diff --git a/tests/unit/misc/test_editor.py b/tests/unit/misc/test_editor.py index 7dea6e069..55425a1c4 100644 --- a/tests/unit/misc/test_editor.py +++ b/tests/unit/misc/test_editor.py @@ -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]