Converted test_editor to pytest

This commit is contained in:
Bruno Oliveira 2015-04-04 13:24:26 -03:00
parent 12903a34f4
commit bd9c807fb1
3 changed files with 97 additions and 89 deletions

View File

@ -33,18 +33,3 @@ def disable_logger(name):
logging.getLogger(name).propagate = True
class MessageModule:
"""A drop-in replacement for qutebrowser.utils.message."""
def error(self, _win_id, message, _immediately=False):
"""Log an error to the message logger."""
logging.getLogger('message').error(message)
def warning(self, _win_id, message, _immediately=False):
"""Log a warning to the message logger."""
logging.getLogger('message').warning(message)
def info(self, _win_id, message, _immediately=True):
"""Log an info message to the message logger."""
logging.getLogger('message').info(message)

View File

@ -28,14 +28,12 @@ import logging
from unittest import mock
from PyQt5.QtCore import QProcess
import pytest
from qutebrowser.misc import editor
from qutebrowser.test import stubs, helpers
@mock.patch('qutebrowser.misc.editor.QProcess',
new_callable=stubs.FakeQProcess)
class ArgTests(unittest.TestCase):
class TestArg:
"""Test argument handling.
@ -43,103 +41,109 @@ class ArgTests(unittest.TestCase):
editor: The ExternalEditor instance to test.
"""
def setUp(self):
@pytest.yield_fixture(autouse=True)
def setup(self, mocker, stubs):
mocker.patch('qutebrowser.misc.editor.QProcess',
new_callable=stubs.FakeQProcess)
self.config = stubs.ConfigStub()
mocker.patch('qutebrowser.misc.editor.config', new=self.config)
self.editor = editor.ExternalEditor(0)
yield
self.editor._cleanup() # pylint: disable=protected-access
@mock.patch('qutebrowser.misc.editor.config', new=stubs.ConfigStub(
{'general': {'editor': ['bin'], 'editor-encoding': 'utf-8'}}))
def test_simple_start_args(self, _proc_mock):
def test_simple_start_args(self):
"""Test starting editor without arguments."""
self.config.data = {
'general': {'editor': ['bin'], 'editor-encoding': 'utf-8'}}
self.editor.edit("")
self.editor._proc.start.assert_called_with("bin", [])
@mock.patch('qutebrowser.misc.editor.config', new=stubs.ConfigStub(
{'general': {'editor': ['bin', 'foo', 'bar'],
'editor-encoding': 'utf-8'}}))
def test_start_args(self, _proc_mock):
def test_start_args(self):
"""Test starting editor with static arguments."""
self.config.data = {'general': {'editor': ['bin', 'foo', 'bar'],
'editor-encoding': 'utf-8'}}
self.editor.edit("")
self.editor._proc.start.assert_called_with("bin", ["foo", "bar"])
@mock.patch('qutebrowser.misc.editor.config', new=stubs.ConfigStub(
{'general': {'editor': ['bin', 'foo', '{}', 'bar'],
'editor-encoding': 'utf-8'}}))
def test_placeholder(self, _proc_mock):
def test_placeholder(self):
"""Test starting editor with placeholder argument."""
self.config.data = {'general': {'editor': ['bin', 'foo', '{}', 'bar'],
'editor-encoding': 'utf-8'}}
self.editor.edit("")
filename = self.editor._filename
self.editor._proc.start.assert_called_with(
"bin", ["foo", filename, "bar"])
@mock.patch('qutebrowser.misc.editor.config', new=stubs.ConfigStub(
{'general': {'editor': ['bin', 'foo{}bar'],
'editor-encoding': 'utf-8'}}))
def test_in_arg_placeholder(self, _proc_mock):
def test_in_arg_placeholder(self):
self.config.data = {'general': {'editor': ['bin', 'foo{}bar'],
'editor-encoding': 'utf-8'}}
"""Test starting editor with placeholder argument inside argument."""
self.editor.edit("")
self.editor._proc.start.assert_called_with("bin", ["foo{}bar"])
def tearDown(self):
self.editor._cleanup() # pylint: disable=protected-access
@mock.patch('qutebrowser.misc.editor.message', new=helpers.MessageModule())
@mock.patch('qutebrowser.misc.editor.QProcess',
new_callable=stubs.FakeQProcess)
@mock.patch('qutebrowser.misc.editor.config', new=stubs.ConfigStub(
{'general': {'editor': [''], 'editor-encoding': 'utf-8'}}))
class FileHandlingTests(unittest.TestCase):
class TestFileHandling(object):
"""Test creation/deletion of tempfile.
Attributes:
editor: The ExternalEditor instance to test.
"""
def setUp(self):
@pytest.fixture(autouse=True)
def setup(self, mocker, stubs):
mocker.patch('qutebrowser.misc.editor.message',
new=stubs.MessageModule())
mocker.patch('qutebrowser.misc.editor.QProcess',
new_callable=stubs.FakeQProcess)
mocker.patch('qutebrowser.misc.editor.config',
new=stubs.ConfigStub(
{'general': {'editor': [''],
'editor-encoding': 'utf-8'}}))
self.editor = editor.ExternalEditor(0)
def test_file_handling_closed_ok(self, _proc_mock):
def test_file_handling_closed_ok(self):
"""Test file handling when closing with an exitstatus == 0."""
self.editor.edit("")
filename = self.editor._filename
self.assertTrue(os.path.exists(filename))
assert os.path.exists(filename)
self.editor.on_proc_closed(0, QProcess.NormalExit)
self.assertFalse(os.path.exists(filename))
assert not os.path.exists(filename)
def test_file_handling_closed_error(self, _proc_mock):
def test_file_handling_closed_error(self, caplog):
"""Test file handling when closing with an exitstatus != 0."""
self.editor.edit("")
filename = self.editor._filename
self.assertTrue(os.path.exists(filename))
with self.assertLogs('message', logging.ERROR):
assert os.path.exists(filename)
with caplog.atLevel(logging.ERROR):
self.editor.on_proc_closed(1, QProcess.NormalExit)
self.assertFalse(os.path.exists(filename))
assert len(caplog.records()) == 2
assert not os.path.exists(filename)
def test_file_handling_closed_crash(self, _proc_mock):
def test_file_handling_closed_crash(self, caplog):
"""Test file handling when closing with a crash."""
self.editor.edit("")
filename = self.editor._filename
self.assertTrue(os.path.exists(filename))
with self.assertLogs('message', logging.ERROR):
assert os.path.exists(filename)
with caplog.atLevel(logging.ERROR):
self.editor.on_proc_error(QProcess.Crashed)
assert len(caplog.records()) == 2
self.editor.on_proc_closed(0, QProcess.CrashExit)
self.assertFalse(os.path.exists(filename))
assert not os.path.exists(filename)
@mock.patch('qutebrowser.misc.editor.QProcess',
new_callable=stubs.FakeQProcess)
@mock.patch('qutebrowser.misc.editor.config', new=stubs.ConfigStub(
{'general': {'editor': [''], 'editor-encoding': 'utf-8'}}))
class TextModifyTests(unittest.TestCase):
class TestModifyTests(object):
"""Tests to test if the text gets saved/loaded correctly.
Attributes:
editor: The ExternalEditor instance to test.
"""
def setUp(self):
@pytest.fixture(autouse=True)
def setup(self, mocker, stubs):
mocker.patch('qutebrowser.misc.editor.QProcess',
new_callable=stubs.FakeQProcess)
mocker.patch('qutebrowser.misc.editor.config', new=stubs.ConfigStub(
{'general': {'editor': [''], 'editor-encoding': 'utf-8'}}))
self.editor = editor.ExternalEditor(0)
self.editor.editing_finished = mock.Mock()
@ -164,67 +168,69 @@ class TextModifyTests(unittest.TestCase):
data = f.read()
return data
def test_empty_input(self, _proc_mock):
def test_empty_input(self):
"""Test if an empty input gets modified correctly."""
self.editor.edit("")
self.assertEqual(self._read(), "")
assert self._read() == ""
self._write("Hello")
self.editor.on_proc_closed(0, QProcess.NormalExit)
self.editor.editing_finished.emit.assert_called_with("Hello")
def test_simple_input(self, _proc_mock):
def test_simple_input(self):
"""Test if an empty input gets modified correctly."""
self.editor.edit("Hello")
self.assertEqual(self._read(), "Hello")
assert self._read() == "Hello"
self._write("World")
self.editor.on_proc_closed(0, QProcess.NormalExit)
self.editor.editing_finished.emit.assert_called_with("World")
def test_umlaut(self, _proc_mock):
def test_umlaut(self):
"""Test if umlauts works correctly."""
self.editor.edit("Hällö Wörld")
self.assertEqual(self._read(), "Hällö Wörld")
assert self._read() == "Hällö Wörld"
self._write("Überprüfung")
self.editor.on_proc_closed(0, QProcess.NormalExit)
self.editor.editing_finished.emit.assert_called_with("Überprüfung")
def test_unicode(self, _proc_mock):
def test_unicode(self):
"""Test if other UTF8 chars work correctly."""
self.editor.edit("\u2603") # Unicode snowman
self.assertEqual(self._read(), "\u2603")
assert self._read() == "\u2603"
self._write("\u2601") # Cloud
self.editor.on_proc_closed(0, QProcess.NormalExit)
self.editor.editing_finished.emit.assert_called_with("\u2601")
@mock.patch('qutebrowser.misc.editor.QProcess',
new_callable=stubs.FakeQProcess)
@mock.patch('qutebrowser.misc.editor.message', new=helpers.MessageModule())
@mock.patch('qutebrowser.misc.editor.config', new=stubs.ConfigStub(
{'general': {'editor': [''], 'editor-encoding': 'utf-8'}}))
class ErrorMessageTests(unittest.TestCase):
class TestErrorMessage:
"""Test if statusbar error messages get emitted correctly.
Attributes:
editor: The ExternalEditor instance to test.
"""
def setUp(self):
@pytest.yield_fixture(autouse=True)
def setup(self, mocker, stubs):
mocker.patch('qutebrowser.misc.editor.QProcess',
new_callable=stubs.FakeQProcess)
mocker.patch('qutebrowser.misc.editor.message',
new=stubs.MessageModule())
mocker.patch('qutebrowser.misc.editor.config', new=stubs.ConfigStub(
{'general': {'editor': [''], 'editor-encoding': 'utf-8'}}))
self.editor = editor.ExternalEditor(0)
yield
self.editor._cleanup() # pylint: disable=protected-access
def test_proc_error(self, _proc_mock):
def test_proc_error(self, caplog):
"""Test on_proc_error."""
self.editor.edit("")
with self.assertLogs('message', logging.ERROR):
with caplog.atLevel(logging.ERROR, 'message'):
self.editor.on_proc_error(QProcess.Crashed)
assert len(caplog.records()) == 2
def test_proc_return(self, _proc_mock):
def test_proc_return(self, caplog):
"""Test on_proc_finished with a bad exit status."""
self.editor.edit("")
with self.assertLogs('message', logging.ERROR):
with caplog.atLevel(logging.ERROR, 'message'):
self.editor.on_proc_closed(1, QProcess.NormalExit)
assert len(caplog.records()) == 3
if __name__ == '__main__':
unittest.main()

View File

@ -20,6 +20,7 @@
# pylint: disable=invalid-name
"""Fake objects/stubs."""
import logging
from unittest import mock
@ -37,8 +38,8 @@ class ConfigStub:
data: The config data to return.
"""
def __init__(self, data):
self.data = data
def __init__(self, data=None):
self.data = data or {}
def section(self, name):
"""Get a section from the config.
@ -268,3 +269,19 @@ class FakeTimer(QObject):
def isActive(self):
return self._started
class MessageModule:
"""A drop-in replacement for qutebrowser.utils.message."""
def error(self, _win_id, message, _immediately=False):
"""Log an error to the message logger."""
logging.getLogger('message').error(message)
def warning(self, _win_id, message, _immediately=False):
"""Log a warning to the message logger."""
logging.getLogger('message').warning(message)
def info(self, _win_id, message, _immediately=True):
"""Log an info message to the message logger."""
logging.getLogger('message').info(message)