Fix up editor backup patch.

- Use qutebrowser-editor-backup as the backup file prefix
- Consistently use message.error instead of cmdexc
- Improve test coverage for the backup function
- Fix lint errors in the unit test code
This commit is contained in:
Ryan Roden-Corrent 2018-03-13 07:31:48 -04:00
parent 38bb3673db
commit 27966c94a6
3 changed files with 28 additions and 8 deletions

View File

@ -1668,8 +1668,8 @@ class CommandDispatcher:
message.error('Edited element vanished')
ed.backup()
except webelem.Error as e:
message.error(str(e))
ed.backup()
raise cmdexc.CommandError(str(e))
@cmdutils.register(instance='command-dispatcher', maxsplit=0,
scope='window')

View File

@ -113,7 +113,7 @@ class ExternalEditor(QObject):
if self._filename is not None:
raise ValueError("Already editing a file!")
try:
self._filename = self._create_tempfile(text)
self._filename = self._create_tempfile(text, 'qutebrowser-editor-')
except OSError as e:
message.error("Failed to create initial file: {}".format(e))
return
@ -128,19 +128,20 @@ class ExternalEditor(QObject):
if not self._content:
return
try:
fname = self._create_tempfile(self._content)
fname = self._create_tempfile(self._content,
'qutebrowser-editor-backup-')
message.info('Editor backup at {}'.format(fname))
except OSError as e:
message.error('Failed to create editor backup: {}'.format(e))
def _create_tempfile(self, text):
def _create_tempfile(self, text, prefix):
# Close while the external process is running, as otherwise systems
# with exclusive write access (e.g. Windows) may fail to update
# the file from the external editor, see
# https://github.com/qutebrowser/qutebrowser/issues/1767
with tempfile.NamedTemporaryFile(
# pylint: disable=bad-continuation
mode='w', prefix='qutebrowser-editor-',
mode='w', prefix=prefix,
encoding=config.val.editor.encoding,
delete=False) as fobj:
# pylint: enable=bad-continuation

View File

@ -160,7 +160,7 @@ class TestFileHandling:
def test_backup(self, qtbot, message_mock):
editor = editormod.ExternalEditor(watch=True)
editor.edit('foo')
with qtbot.wait_signal(editor.file_updated) as blocker:
with qtbot.wait_signal(editor.file_updated):
_update_file(editor._filename, 'bar')
editor.backup()
@ -170,12 +170,31 @@ class TestFileHandling:
assert msg.text.startswith(prefix)
fname = msg.text[len(prefix):]
with qtbot.wait_signal(editor.editing_finished) as blocker:
with qtbot.wait_signal(editor.editing_finished):
editor._proc.finished.emit(0, QProcess.NormalExit)
with open(fname, 'r') as f:
with open(fname, 'r', encoding='utf-8') as f:
assert f.read() == 'bar'
def test_backup_no_content(self, qtbot, message_mock):
editor = editormod.ExternalEditor(watch=True)
editor.edit('foo')
editor.backup()
# content has not changed, so no backup should be created
assert not message_mock.messages
def test_backup(self, qtbot, message_mock, mocker):
editor = editormod.ExternalEditor(watch=True)
editor.edit('foo')
with qtbot.wait_signal(editor.file_updated):
_update_file(editor._filename, 'bar')
mocker.patch('tempfile.NamedTemporaryFile', side_effect=OSError)
editor.backup()
msg = message_mock.getmsg(usertypes.MessageLevel.error)
assert msg.text.startswith('Failed to create editor backup:')
@pytest.mark.parametrize('initial_text, edited_text', [
('', 'Hello'),