2015-10-10 17:20:20 +02:00
|
|
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
|
|
|
|
|
|
|
# Copyright 2015 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
|
|
|
|
#
|
|
|
|
# This file is part of qutebrowser.
|
|
|
|
#
|
|
|
|
# qutebrowser is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# qutebrowser is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
"""Base class for a subprocess run for tests.."""
|
|
|
|
|
2015-10-22 22:23:55 +02:00
|
|
|
import re
|
|
|
|
import fnmatch
|
|
|
|
|
2015-10-10 17:20:20 +02:00
|
|
|
import pytestqt.plugin # pylint: disable=import-error
|
2015-10-22 22:23:55 +02:00
|
|
|
from PyQt5.QtCore import pyqtSlot, pyqtSignal, QProcess, QObject, QElapsedTimer
|
|
|
|
from PyQt5.QtTest import QSignalSpy
|
2015-10-10 17:20:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
class InvalidLine(Exception):
|
|
|
|
|
|
|
|
"""Raised when the process prints a line which is not parsable."""
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2015-10-10 18:17:07 +02:00
|
|
|
class ProcessExited(Exception):
|
|
|
|
|
|
|
|
"""Raised when the child process did exit."""
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2015-10-22 22:23:55 +02:00
|
|
|
class WaitForTimeout(Exception):
|
|
|
|
|
|
|
|
"""Raised when wait_for didn't get the expected message."""
|
|
|
|
|
|
|
|
|
2015-10-10 17:20:20 +02:00
|
|
|
class Process(QObject):
|
|
|
|
|
|
|
|
"""Abstraction over a running test subprocess process.
|
|
|
|
|
|
|
|
Reads the log from its stdout and parses it.
|
|
|
|
|
|
|
|
Signals:
|
2015-10-10 19:21:12 +02:00
|
|
|
ready: Emitted when the server finished starting up.
|
2015-10-10 17:20:20 +02:00
|
|
|
new_data: Emitted when a new line was parsed.
|
|
|
|
"""
|
|
|
|
|
2015-10-10 19:21:12 +02:00
|
|
|
ready = pyqtSignal()
|
2015-10-10 17:20:20 +02:00
|
|
|
new_data = pyqtSignal(object)
|
|
|
|
|
|
|
|
def __init__(self, parent=None):
|
|
|
|
super().__init__(parent)
|
|
|
|
self._invalid = False
|
|
|
|
self._data = []
|
|
|
|
self.proc = QProcess()
|
|
|
|
self.proc.setReadChannel(QProcess.StandardError)
|
|
|
|
|
|
|
|
def _parse_line(self, line):
|
|
|
|
"""Parse the given line from the log.
|
|
|
|
|
|
|
|
Return:
|
|
|
|
A self.ParseResult member.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def _executable_args(self):
|
2015-10-10 18:16:47 +02:00
|
|
|
"""Get the executable and arguments to pass to it as a tuple."""
|
2015-10-10 17:20:20 +02:00
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def _get_data(self):
|
|
|
|
"""Get the parsed data for this test.
|
|
|
|
|
|
|
|
Also waits for 0.5s to make sure any new data is received.
|
|
|
|
|
|
|
|
Subprocesses are expected to alias this to a public method with a
|
|
|
|
better name.
|
|
|
|
"""
|
|
|
|
self.proc.waitForReadyRead(500)
|
|
|
|
self.read_log()
|
|
|
|
return self._data
|
|
|
|
|
2015-10-10 19:21:12 +02:00
|
|
|
def _wait_signal(self, signal, timeout=5000, raising=True):
|
|
|
|
"""Wait for a signal to be emitted.
|
|
|
|
|
|
|
|
Should be used in a contextmanager.
|
|
|
|
"""
|
|
|
|
blocker = pytestqt.plugin.SignalBlocker(
|
|
|
|
timeout=timeout, raising=raising)
|
|
|
|
blocker.connect(signal)
|
|
|
|
return blocker
|
|
|
|
|
2015-10-10 17:20:20 +02:00
|
|
|
@pyqtSlot()
|
|
|
|
def read_log(self):
|
|
|
|
"""Read the log from the process' stdout."""
|
2015-10-27 07:08:17 +01:00
|
|
|
if not hasattr(self, 'proc'):
|
|
|
|
# I have no idea how this happens, but it does...
|
|
|
|
return
|
2015-10-10 17:20:20 +02:00
|
|
|
while self.proc.canReadLine():
|
|
|
|
line = self.proc.readLine()
|
2015-10-14 20:48:12 +02:00
|
|
|
line = bytes(line).decode('utf-8', errors='ignore').rstrip('\r\n')
|
2015-10-10 17:20:20 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
parsed = self._parse_line(line)
|
|
|
|
except InvalidLine:
|
|
|
|
self._invalid = True
|
|
|
|
print("INVALID: {}".format(line))
|
|
|
|
continue
|
|
|
|
|
|
|
|
if parsed is not None:
|
|
|
|
self._data.append(parsed)
|
|
|
|
self.new_data.emit(parsed)
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
"""Start the process and wait until it started."""
|
2015-10-14 07:45:08 +02:00
|
|
|
with self._wait_signal(self.ready, timeout=20000):
|
2015-10-10 17:20:20 +02:00
|
|
|
self._start()
|
|
|
|
|
|
|
|
def _start(self):
|
|
|
|
"""Actually start the process."""
|
2015-10-10 18:16:47 +02:00
|
|
|
executable, args = self._executable_args()
|
|
|
|
self.proc.start(executable, args)
|
2015-10-10 17:20:20 +02:00
|
|
|
ok = self.proc.waitForStarted()
|
|
|
|
assert ok
|
2015-10-10 19:21:12 +02:00
|
|
|
assert self.is_running()
|
2015-10-10 17:20:20 +02:00
|
|
|
self.proc.readyRead.connect(self.read_log)
|
|
|
|
|
|
|
|
def after_test(self):
|
|
|
|
"""Clean up data after each test.
|
|
|
|
|
|
|
|
Also checks self._invalid so the test counts as failed if there were
|
|
|
|
unexpected output lines earlier.
|
|
|
|
"""
|
|
|
|
self._data.clear()
|
|
|
|
if self._invalid:
|
|
|
|
raise InvalidLine
|
|
|
|
|
2015-10-27 07:11:23 +01:00
|
|
|
def terminate(self):
|
2015-10-10 17:20:20 +02:00
|
|
|
"""Clean up and shut down the process."""
|
2015-10-27 07:14:40 +01:00
|
|
|
if not self.is_running():
|
|
|
|
raise ProcessExited
|
2015-10-10 17:20:20 +02:00
|
|
|
self.proc.terminate()
|
|
|
|
self.proc.waitForFinished()
|
2015-10-10 19:21:12 +02:00
|
|
|
|
|
|
|
def is_running(self):
|
|
|
|
"""Check if the process is currently running."""
|
|
|
|
return self.proc.state() == QProcess.Running
|
2015-10-22 22:23:55 +02:00
|
|
|
|
|
|
|
def wait_for(self, timeout=5000, **kwargs):
|
|
|
|
"""Wait until a given value is found in the data.
|
|
|
|
|
|
|
|
Keyword arguments to this function get interpreted as attributes of the
|
|
|
|
searched data. Every given argument is treated as a pattern which
|
|
|
|
the attribute has to match against.
|
|
|
|
|
|
|
|
If a string is passed, the patterns are treated as a fnmatch glob
|
|
|
|
pattern. Alternatively, a compiled regex can be passed to match against
|
|
|
|
that.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# FIXME make this a context manager which inserts a marker in
|
|
|
|
# self._data in __enter__ and checks if the signal already did arrive
|
|
|
|
# after marker in __exit__, and if not, waits?
|
|
|
|
|
|
|
|
regex_type = type(re.compile(''))
|
|
|
|
|
|
|
|
spy = QSignalSpy(self.new_data)
|
|
|
|
elapsed_timer = QElapsedTimer()
|
|
|
|
elapsed_timer.start()
|
|
|
|
|
|
|
|
while True:
|
|
|
|
got_signal = spy.wait(timeout)
|
|
|
|
if not got_signal or elapsed_timer.hasExpired(timeout):
|
|
|
|
raise WaitForTimeout
|
|
|
|
|
|
|
|
for args in spy:
|
|
|
|
assert len(args) == 1
|
|
|
|
line = args[0]
|
|
|
|
|
|
|
|
matches = []
|
|
|
|
|
|
|
|
for key, expected in kwargs.items():
|
|
|
|
value = getattr(line, key)
|
|
|
|
|
|
|
|
if isinstance(expected, regex_type):
|
|
|
|
matches.append(expected.match(value))
|
|
|
|
else:
|
|
|
|
matches.append(fnmatch.fnmatchcase(value, expected))
|
|
|
|
|
|
|
|
if all(matches):
|
|
|
|
return
|