100% test coverage for misc.editor.

This commit is contained in:
Florian Bruhin 2015-08-19 09:34:44 +02:00
parent aa367fa004
commit 685bbaae6d
3 changed files with 36 additions and 2 deletions

View File

@ -53,6 +53,9 @@ class ExternalEditor(QObject):
def _cleanup(self):
"""Clean up temporary files after the editor closed."""
if self._oshandle is None or self._filename is None:
# Could not create initial file.
return
try:
os.close(self._oshandle)
os.remove(self._filename)
@ -78,7 +81,7 @@ class ExternalEditor(QObject):
encoding = config.get('general', 'editor-encoding')
try:
with open(self._filename, 'r', encoding=encoding) as f:
text = ''.join(f.readlines())
text = ''.join(f.readlines()) # pragma: no branch
except OSError as e:
# NOTE: Do not replace this with "raise CommandError" as it's
# executed async.
@ -109,7 +112,7 @@ class ExternalEditor(QObject):
if text:
encoding = config.get('general', 'editor-encoding')
with open(self._filename, 'w', encoding=encoding) as f:
f.write(text)
f.write(text) # pragma: no branch
except OSError as e:
message.error(self._win_id, "Failed to create initial file: "
"{}".format(e))

View File

@ -50,6 +50,7 @@ PERFECT_FILES = [
'qutebrowser/misc/msgbox.py',
'qutebrowser/misc/checkpyver.py',
'qutebrowser/misc/guiprocess.py',
'qutebrowser/misc/editor.py',
'qutebrowser/mainwindow/statusbar/keystring.py',
'qutebrowser/mainwindow/statusbar/percentage.py',

View File

@ -26,6 +26,7 @@ from unittest import mock
from PyQt5.QtCore import QProcess
import pytest
from helpers import messagemock
from qutebrowser.misc import editor as editormod
@ -82,6 +83,7 @@ class TestFileHandling:
editor.edit("")
filename = editor._filename
assert os.path.exists(filename)
assert os.path.basename(filename).startswith('qutebrowser-editor-')
editor._proc.finished.emit(0, QProcess.NormalExit)
assert not os.path.exists(filename)
@ -106,6 +108,34 @@ class TestFileHandling:
editor._proc.finished.emit(0, QProcess.CrashExit)
assert not os.path.exists(filename)
def test_unreadable(self, message_mock, editor):
"""Test file handling when closing with an unreadable file."""
editor.edit("")
filename = editor._filename
assert os.path.exists(filename)
os.chmod(filename, 0o077)
editor._proc.finished.emit(0, QProcess.NormalExit)
assert not os.path.exists(filename)
msg = message_mock.getmsg()
assert msg.level == messagemock.Level.error
assert msg.text.startswith("Failed to read back edited file: ")
def test_unwritable(self, monkeypatch, message_mock, editor, tmpdir):
"""Test file handling when the initial file is not writable."""
tmpdir.chmod(0)
monkeypatch.setattr('qutebrowser.misc.editor.tempfile.tempdir',
str(tmpdir))
editor.edit("")
msg = message_mock.getmsg()
assert msg.level == messagemock.Level.error
assert msg.text.startswith("Failed to create initial file: ")
assert editor._proc is None
def test_double_edit(self, editor):
editor.edit("")
with pytest.raises(ValueError):
editor.edit("")
@pytest.mark.parametrize('initial_text, edited_text', [
('', 'Hello'),