test_editor: Fix handling of statusbar messages.

This commit is contained in:
Florian Bruhin 2015-03-04 20:25:57 +01:00
parent 40af99bacc
commit e431f09fab
2 changed files with 28 additions and 10 deletions

View File

@ -73,3 +73,17 @@ def fake_keyevent(key, modifiers=0, text=''):
evtmock.modifiers.return_value = modifiers evtmock.modifiers.return_value = modifiers
evtmock.text.return_value = text evtmock.text.return_value = text
return evtmock return evtmock
class MessageModule:
"""A drop-in replacement for qutebrowser.utils.message."""
def error(self, _win_id, message, _immediately=False):
logging.getLogger('message').error(message)
def warning(self, win_id, message, immediately=False):
logging.getLogger('message').warning(message)
def info(self, win_id, message, immediately=True):
logging.getLogger('message').info(message)

View File

@ -24,12 +24,13 @@
import os import os
import os.path import os.path
import unittest import unittest
import logging
from unittest import mock from unittest import mock
from PyQt5.QtCore import QProcess from PyQt5.QtCore import QProcess
from qutebrowser.misc import editor from qutebrowser.misc import editor
from qutebrowser.test import stubs from qutebrowser.test import stubs, helpers
@mock.patch('qutebrowser.misc.editor.QProcess', @mock.patch('qutebrowser.misc.editor.QProcess',
@ -82,6 +83,7 @@ class ArgTests(unittest.TestCase):
self.editor._cleanup() # pylint: disable=protected-access self.editor._cleanup() # pylint: disable=protected-access
@mock.patch('qutebrowser.misc.editor.message', new=helpers.MessageModule())
@mock.patch('qutebrowser.misc.editor.QProcess', @mock.patch('qutebrowser.misc.editor.QProcess',
new_callable=stubs.FakeQProcess) new_callable=stubs.FakeQProcess)
@mock.patch('qutebrowser.misc.editor.config', new=stubs.ConfigStub( @mock.patch('qutebrowser.misc.editor.config', new=stubs.ConfigStub(
@ -110,6 +112,7 @@ class FileHandlingTests(unittest.TestCase):
self.editor.edit("") self.editor.edit("")
filename = self.editor._filename filename = self.editor._filename
self.assertTrue(os.path.exists(filename)) self.assertTrue(os.path.exists(filename))
with self.assertLogs('message', logging.ERROR):
self.editor.on_proc_closed(1, QProcess.NormalExit) self.editor.on_proc_closed(1, QProcess.NormalExit)
self.assertFalse(os.path.exists(filename)) self.assertFalse(os.path.exists(filename))
@ -118,6 +121,7 @@ class FileHandlingTests(unittest.TestCase):
self.editor.edit("") self.editor.edit("")
filename = self.editor._filename filename = self.editor._filename
self.assertTrue(os.path.exists(filename)) self.assertTrue(os.path.exists(filename))
with self.assertLogs('message', logging.ERROR):
self.editor.on_proc_error(QProcess.Crashed) self.editor.on_proc_error(QProcess.Crashed)
self.editor.on_proc_closed(0, QProcess.CrashExit) self.editor.on_proc_closed(0, QProcess.CrashExit)
self.assertFalse(os.path.exists(filename)) self.assertFalse(os.path.exists(filename))
@ -195,7 +199,7 @@ class TextModifyTests(unittest.TestCase):
@mock.patch('qutebrowser.misc.editor.QProcess', @mock.patch('qutebrowser.misc.editor.QProcess',
new_callable=stubs.FakeQProcess) new_callable=stubs.FakeQProcess)
@mock.patch('qutebrowser.misc.editor.message', autospec=True) @mock.patch('qutebrowser.misc.editor.message', new=helpers.MessageModule())
@mock.patch('qutebrowser.misc.editor.config', new=stubs.ConfigStub( @mock.patch('qutebrowser.misc.editor.config', new=stubs.ConfigStub(
{'general': {'editor': [''], 'editor-encoding': 'utf-8'}})) {'general': {'editor': [''], 'editor-encoding': 'utf-8'}}))
class ErrorMessageTests(unittest.TestCase): class ErrorMessageTests(unittest.TestCase):
@ -211,17 +215,17 @@ class ErrorMessageTests(unittest.TestCase):
def setUp(self): def setUp(self):
self.editor = editor.ExternalEditor(0) self.editor = editor.ExternalEditor(0)
def test_proc_error(self, msg_mock, _proc_mock): def test_proc_error(self, _proc_mock):
"""Test on_proc_error.""" """Test on_proc_error."""
self.editor.edit("") self.editor.edit("")
with self.assertLogs('message', logging.ERROR):
self.editor.on_proc_error(QProcess.Crashed) self.editor.on_proc_error(QProcess.Crashed)
self.assertTrue(msg_mock.error.called)
def test_proc_return(self, msg_mock, _proc_mock): def test_proc_return(self, _proc_mock):
"""Test on_proc_finished with a bad exit status.""" """Test on_proc_finished with a bad exit status."""
self.editor.edit("") self.editor.edit("")
with self.assertLogs('message', logging.ERROR):
self.editor.on_proc_closed(1, QProcess.NormalExit) self.editor.on_proc_closed(1, QProcess.NormalExit)
self.assertTrue(msg_mock.error.called)
if __name__ == '__main__': if __name__ == '__main__':