This commit is contained in:
Florian Bruhin 2014-06-02 18:01:56 +02:00
parent ce7595639a
commit fb7f1b381e
3 changed files with 33 additions and 28 deletions

View File

@ -64,7 +64,8 @@ class _BlockingFIFOReader(QObject):
# See http://www.outflux.net/blog/archives/2008/03/09/using-select-on-a-fifo/ # See http://www.outflux.net/blog/archives/2008/03/09/using-select-on-a-fifo/
# We also use os.open and os.fdopen rather than built-in open so we can # We also use os.open and os.fdopen rather than built-in open so we can
# add O_NONBLOCK. # add O_NONBLOCK.
fd = os.open(self.filepath, os.O_RDWR | os.O_NONBLOCK) fd = os.open(self.filepath, os.O_RDWR |
os.O_NONBLOCK) # pylint: disable=no-member
self.fifo = os.fdopen(fd, 'r') self.fifo = os.fdopen(fd, 'r')
while True: while True:
logger.debug("thread loop") logger.debug("thread loop")
@ -191,7 +192,7 @@ class _POSIXUserscriptRunner(_BaseUserscriptRunner):
# raise an exception anyways when the path doesn't exist, it shouldn't # raise an exception anyways when the path doesn't exist, it shouldn't
# be a big issue. # be a big issue.
self.filepath = tempfile.mktemp(prefix='userscript-', dir=rundir) self.filepath = tempfile.mktemp(prefix='userscript-', dir=rundir)
os.mkfifo(self.filepath) os.mkfifo(self.filepath) # pylint: disable=no-member
self.reader = _BlockingFIFOReader(self.filepath) self.reader = _BlockingFIFOReader(self.filepath)
self.thread = QThread() self.thread = QThread()

View File

@ -15,18 +15,19 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>. # along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
# pylint: disable=maybe-no-member
"""Tests for qutebrowser.utils.editor.""" """Tests for qutebrowser.utils.editor."""
import os import os
import os.path import os.path
import logging
import unittest import unittest
from unittest import TestCase from unittest import TestCase
from unittest.mock import Mock from unittest.mock import Mock
from PyQt5.QtCore import QProcess from PyQt5.QtCore import QProcess
import qutebrowser.utils.editor as editor import qutebrowser.utils.editor as editorutils
class ConfigStub: class ConfigStub:
@ -41,6 +42,7 @@ class ConfigStub:
self.editor = editor self.editor = editor
def get(self, sect, opt): def get(self, sect, opt):
"""Get the configured value for sect/opt."""
if sect == 'general' and opt == 'editor': if sect == 'general' and opt == 'editor':
return self.editor return self.editor
else: else:
@ -64,7 +66,7 @@ class FakeQProcess:
ReadError = QProcess.ReadError ReadError = QProcess.ReadError
UnknownError = QProcess.UnknownError UnknownError = QProcess.UnknownError
def __init__(self, parent=None): def __init__(self, parent=None): # pylint: disable=unused-argument
self.finished = Mock() self.finished = Mock()
self.error = Mock() self.error = Mock()
self.start = Mock() self.start = Mock()
@ -72,9 +74,9 @@ class FakeQProcess:
def setUpModule(): def setUpModule():
"""Mock some things imported in the editor module.""" """Mock some things imported in the editor module."""
editor.message = Mock() editorutils.message = Mock()
editor.logger = Mock() editorutils.logger = Mock()
editor.QProcess = FakeQProcess editorutils.QProcess = FakeQProcess
class ArgTests(TestCase): class ArgTests(TestCase):
@ -86,37 +88,36 @@ class ArgTests(TestCase):
""" """
def setUp(self): def setUp(self):
self.editor = editor.ExternalEditor() self.editor = editorutils.ExternalEditor()
def test_simple_start_args(self): def test_simple_start_args(self):
"""Test starting editor without arguments.""" """Test starting editor without arguments."""
editor.config = ConfigStub(editor=["executable"]) editorutils.config = ConfigStub(editor=["bin"])
self.editor.edit("") self.editor.edit("")
self.editor.proc.start.assert_called_with("executable", []) self.editor.proc.start.assert_called_with("bin", [])
def test_start_args(self): def test_start_args(self):
"""Test starting editor with static arguments.""" """Test starting editor with static arguments."""
editor.config = ConfigStub(editor=["executable", "foo", "bar"]) editorutils.config = ConfigStub(editor=["bin", "foo", "bar"])
self.editor.edit("") self.editor.edit("")
self.editor.proc.start.assert_called_with("executable", ["foo", "bar"]) self.editor.proc.start.assert_called_with("bin", ["foo", "bar"])
def test_placeholder(self): def test_placeholder(self):
"""Test starting editor with placeholder argument.""" """Test starting editor with placeholder argument."""
editor.config = ConfigStub(editor=["executable", "foo", "{}", "bar"]) editorutils.config = ConfigStub(editor=["bin", "foo", "{}", "bar"])
self.editor.edit("") self.editor.edit("")
filename = self.editor.filename filename = self.editor.filename
self.editor.proc.start.assert_called_with( self.editor.proc.start.assert_called_with("bin",
"executable", ["foo", filename, "bar"]) ["foo", filename, "bar"])
def test_in_arg_placeholder(self): def test_in_arg_placeholder(self):
"""Test starting editor with placeholder argument inside argument.""" """Test starting editor with placeholder argument inside argument."""
editor.config = ConfigStub(editor=["executable", "foo{}bar"]) editorutils.config = ConfigStub(editor=["bin", "foo{}bar"])
self.editor.edit("") self.editor.edit("")
filename = self.editor.filename self.editor.proc.start.assert_called_with("bin", ["foo{}bar"])
self.editor.proc.start.assert_called_with("executable", ["foo{}bar"])
def tearDown(self): def tearDown(self):
self.editor._cleanup() self.editor._cleanup() # pylint: disable=protected-access
class FileHandlingTests(TestCase): class FileHandlingTests(TestCase):
@ -128,8 +129,8 @@ class FileHandlingTests(TestCase):
""" """
def setUp(self): def setUp(self):
self.editor = editor.ExternalEditor() self.editor = editorutils.ExternalEditor()
editor.config = ConfigStub(editor=[""]) editorutils.config = ConfigStub(editor=[""])
def test_file_handling_closed_ok(self): def test_file_handling_closed_ok(self):
"""Test file handling when closing with an exitstatus == 0.""" """Test file handling when closing with an exitstatus == 0."""
@ -166,9 +167,9 @@ class TextModifyTests(TestCase):
""" """
def setUp(self): def setUp(self):
self.editor = editor.ExternalEditor() self.editor = editorutils.ExternalEditor()
self.editor.editing_finished = Mock() self.editor.editing_finished = Mock()
editor.config = ConfigStub(editor=[""]) editorutils.config = ConfigStub(editor=[""])
def _write(self, text): def _write(self, text):
"""Write a text to the file opened in the fake editor. """Write a text to the file opened in the fake editor.
@ -233,20 +234,20 @@ class ErrorMessageTests(TestCase):
""" """
def setUp(self): def setUp(self):
self.editor = editor.ExternalEditor() self.editor = editorutils.ExternalEditor()
editor.config = ConfigStub(editor=[""]) editorutils.config = ConfigStub(editor=[""])
def test_proc_error(self): def test_proc_error(self):
"""Test on_proc_error.""" """Test on_proc_error."""
self.editor.edit("") self.editor.edit("")
self.editor.on_proc_error(QProcess.Crashed) self.editor.on_proc_error(QProcess.Crashed)
self.assertTrue(editor.message.error.called) self.assertTrue(editorutils.message.error.called)
def test_proc_return(self): def test_proc_return(self):
"""Test on_proc_finished with a bad exit status.""" """Test on_proc_finished with a bad exit status."""
self.editor.edit("") self.editor.edit("")
self.editor.on_proc_closed(1, QProcess.NormalExit) self.editor.on_proc_closed(1, QProcess.NormalExit)
self.assertTrue(editor.message.error.called) self.assertTrue(editorutils.message.error.called)
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -15,6 +15,8 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>. # along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
# pylint: disable=missing-docstring
"""Tests for qutebrowser.utils.readline.""" """Tests for qutebrowser.utils.readline."""
import inspect import inspect
@ -32,6 +34,7 @@ class FakeQApplication:
"""Stub to insert as QApplication module.""" """Stub to insert as QApplication module."""
def __init__(self, focus): def __init__(self, focus):
# pylint: disable=invalid-name
self.focusWidget = Mock(return_value=focus) self.focusWidget = Mock(return_value=focus)
self.instance = Mock(return_value=self) self.instance = Mock(return_value=self)