Avoid using pytest.config
It's going to be removed in some future pytest release.
This commit is contained in:
parent
dbb89b1073
commit
12ba642547
@ -44,9 +44,9 @@ from end2end.fixtures import testprocess
|
|||||||
instance_counter = itertools.count()
|
instance_counter = itertools.count()
|
||||||
|
|
||||||
|
|
||||||
def is_ignored_qt_message(message):
|
def is_ignored_qt_message(pytestconfig, message):
|
||||||
"""Check if the message is listed in qt_log_ignore."""
|
"""Check if the message is listed in qt_log_ignore."""
|
||||||
regexes = pytest.config.getini('qt_log_ignore')
|
regexes = pytestconfig.getini('qt_log_ignore')
|
||||||
for regex in regexes:
|
for regex in regexes:
|
||||||
if re.search(regex, message):
|
if re.search(regex, message):
|
||||||
return True
|
return True
|
||||||
@ -207,7 +207,7 @@ class LogLine(testprocess.Line):
|
|||||||
expected: Whether the message was expected or not.
|
expected: Whether the message was expected or not.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, data):
|
def __init__(self, pytestconfig, data):
|
||||||
super().__init__(data)
|
super().__init__(data)
|
||||||
try:
|
try:
|
||||||
line = json.loads(data)
|
line = json.loads(data)
|
||||||
@ -229,7 +229,7 @@ class LogLine(testprocess.Line):
|
|||||||
self.traceback = line.get('traceback')
|
self.traceback = line.get('traceback')
|
||||||
self.message = line['message']
|
self.message = line['message']
|
||||||
|
|
||||||
self.expected = is_ignored_qt_message(self.message)
|
self.expected = is_ignored_qt_message(pytestconfig, self.message)
|
||||||
self.use_color = False
|
self.use_color = False
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
@ -299,7 +299,7 @@ class QuteProc(testprocess.Process):
|
|||||||
'message']
|
'message']
|
||||||
|
|
||||||
def __init__(self, request, *, parent=None):
|
def __init__(self, request, *, parent=None):
|
||||||
super().__init__(parent)
|
super().__init__(request.config, parent)
|
||||||
self._ipc_socket = None
|
self._ipc_socket = None
|
||||||
self.basedir = None
|
self.basedir = None
|
||||||
self._focus_ready = False
|
self._focus_ready = False
|
||||||
@ -372,11 +372,11 @@ class QuteProc(testprocess.Process):
|
|||||||
|
|
||||||
def _parse_line(self, line):
|
def _parse_line(self, line):
|
||||||
try:
|
try:
|
||||||
log_line = LogLine(line)
|
log_line = LogLine(self._pytestconfig, line)
|
||||||
except testprocess.InvalidLine:
|
except testprocess.InvalidLine:
|
||||||
if not line.strip():
|
if not line.strip():
|
||||||
return None
|
return None
|
||||||
elif (is_ignored_qt_message(line) or
|
elif (is_ignored_qt_message(self._pytestconfig, line) or
|
||||||
is_ignored_lowlevel_message(line) or
|
is_ignored_lowlevel_message(line) or
|
||||||
is_ignored_chromium_message(line) or
|
is_ignored_chromium_message(line) or
|
||||||
self.request.node.get_marker('no_invalid_lines')):
|
self.request.node.get_marker('no_invalid_lines')):
|
||||||
|
@ -222,8 +222,8 @@ def test_quteprocess_quitting(qtbot, quteproc_process):
|
|||||||
{'category': 'py.warnings'},
|
{'category': 'py.warnings'},
|
||||||
id='resourcewarning'),
|
id='resourcewarning'),
|
||||||
])
|
])
|
||||||
def test_log_line_parse(data, attrs):
|
def test_log_line_parse(pytestconfig, data, attrs):
|
||||||
line = quteprocess.LogLine(data)
|
line = quteprocess.LogLine(pytestconfig, data)
|
||||||
for name, expected in attrs.items():
|
for name, expected in attrs.items():
|
||||||
actual = getattr(line, name)
|
actual = getattr(line, name)
|
||||||
assert actual == expected, name
|
assert actual == expected, name
|
||||||
@ -283,9 +283,10 @@ def test_log_line_parse(data, attrs):
|
|||||||
'\033[36mfoo bar:qux:10\033[0m \033[37mquux\033[0m',
|
'\033[36mfoo bar:qux:10\033[0m \033[37mquux\033[0m',
|
||||||
id='expected error colorized'),
|
id='expected error colorized'),
|
||||||
])
|
])
|
||||||
def test_log_line_formatted(data, colorized, expect_error, expected):
|
def test_log_line_formatted(pytestconfig,
|
||||||
|
data, colorized, expect_error, expected):
|
||||||
line = json.dumps(data)
|
line = json.dumps(data)
|
||||||
record = quteprocess.LogLine(line)
|
record = quteprocess.LogLine(pytestconfig, line)
|
||||||
record.expected = expect_error
|
record.expected = expect_error
|
||||||
ts = datetime.datetime.fromtimestamp(data['created']).strftime('%H:%M:%S')
|
ts = datetime.datetime.fromtimestamp(data['created']).strftime('%H:%M:%S')
|
||||||
ts += '.{:03.0f}'.format(data['msecs'])
|
ts += '.{:03.0f}'.format(data['msecs'])
|
||||||
@ -293,9 +294,9 @@ def test_log_line_formatted(data, colorized, expect_error, expected):
|
|||||||
assert record.formatted_str(colorized=colorized) == expected
|
assert record.formatted_str(colorized=colorized) == expected
|
||||||
|
|
||||||
|
|
||||||
def test_log_line_no_match():
|
def test_log_line_no_match(pytestconfig):
|
||||||
with pytest.raises(testprocess.InvalidLine):
|
with pytest.raises(testprocess.InvalidLine):
|
||||||
quteprocess.LogLine("Hello World!")
|
quteprocess.LogLine(pytestconfig, "Hello World!")
|
||||||
|
|
||||||
|
|
||||||
class TestClickElementByText:
|
class TestClickElementByText:
|
||||||
|
@ -51,8 +51,8 @@ class PythonProcess(testprocess.Process):
|
|||||||
|
|
||||||
"""A testprocess which runs the given Python code."""
|
"""A testprocess which runs the given Python code."""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, pytestconfig):
|
||||||
super().__init__()
|
super().__init__(pytestconfig)
|
||||||
self.proc.setReadChannel(QProcess.StandardOutput)
|
self.proc.setReadChannel(QProcess.StandardOutput)
|
||||||
self.code = None
|
self.code = None
|
||||||
|
|
||||||
@ -103,22 +103,22 @@ class NoReadyPythonProcess(PythonProcess):
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def pyproc():
|
def pyproc(pytestconfig):
|
||||||
proc = PythonProcess()
|
proc = PythonProcess(pytestconfig)
|
||||||
yield proc
|
yield proc
|
||||||
proc.terminate()
|
proc.terminate()
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def quit_pyproc():
|
def quit_pyproc(pytestconfig):
|
||||||
proc = QuitPythonProcess()
|
proc = QuitPythonProcess(pytestconfig)
|
||||||
yield proc
|
yield proc
|
||||||
proc.terminate()
|
proc.terminate()
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def noready_pyproc():
|
def noready_pyproc(pytestconfig):
|
||||||
proc = NoReadyPythonProcess()
|
proc = NoReadyPythonProcess(pytestconfig)
|
||||||
yield proc
|
yield proc
|
||||||
proc.terminate()
|
proc.terminate()
|
||||||
|
|
||||||
|
@ -73,12 +73,11 @@ class Line:
|
|||||||
waited_for = attr.ib(False)
|
waited_for = attr.ib(False)
|
||||||
|
|
||||||
|
|
||||||
def _render_log(data, threshold=100):
|
def _render_log(data, *, verbose, threshold=100):
|
||||||
"""Shorten the given log without -v and convert to a string."""
|
"""Shorten the given log without -v and convert to a string."""
|
||||||
data = [str(d) for d in data]
|
data = [str(d) for d in data]
|
||||||
is_exception = any('Traceback (most recent call last):' in line or
|
is_exception = any('Traceback (most recent call last):' in line or
|
||||||
'Uncaught exception' in line for line in data)
|
'Uncaught exception' in line for line in data)
|
||||||
verbose = pytest.config.getoption('--verbose')
|
|
||||||
if len(data) > threshold and not verbose and not is_exception:
|
if len(data) > threshold and not verbose and not is_exception:
|
||||||
msg = '[{} lines suppressed, use -v to show]'.format(
|
msg = '[{} lines suppressed, use -v to show]'.format(
|
||||||
len(data) - threshold)
|
len(data) - threshold)
|
||||||
@ -105,15 +104,17 @@ def pytest_runtest_makereport(item, call):
|
|||||||
# is actually a tuple. This is handled similarily in pytest-qt too.
|
# is actually a tuple. This is handled similarily in pytest-qt too.
|
||||||
return
|
return
|
||||||
|
|
||||||
if pytest.config.getoption('--capture') == 'no':
|
if item.config.getoption('--capture') == 'no':
|
||||||
# Already printed live
|
# Already printed live
|
||||||
return
|
return
|
||||||
|
|
||||||
|
verbose = item.config.getoption('--verbose')
|
||||||
if quteproc_log is not None:
|
if quteproc_log is not None:
|
||||||
report.longrepr.addsection("qutebrowser output",
|
report.longrepr.addsection("qutebrowser output",
|
||||||
_render_log(quteproc_log))
|
_render_log(quteproc_log, verbose=verbose))
|
||||||
if server_log is not None:
|
if server_log is not None:
|
||||||
report.longrepr.addsection("server output", _render_log(server_log))
|
report.longrepr.addsection("server output",
|
||||||
|
_render_log(server_log, verbose=verbose))
|
||||||
|
|
||||||
|
|
||||||
class Process(QObject):
|
class Process(QObject):
|
||||||
@ -128,6 +129,7 @@ class Process(QObject):
|
|||||||
_started: Whether the process was ever started.
|
_started: Whether the process was ever started.
|
||||||
proc: The QProcess for the underlying process.
|
proc: The QProcess for the underlying process.
|
||||||
exit_expected: Whether the process is expected to quit.
|
exit_expected: Whether the process is expected to quit.
|
||||||
|
pytestconfig: The pytestconfig fixture.
|
||||||
|
|
||||||
Signals:
|
Signals:
|
||||||
ready: Emitted when the server finished starting up.
|
ready: Emitted when the server finished starting up.
|
||||||
@ -138,8 +140,9 @@ class Process(QObject):
|
|||||||
new_data = pyqtSignal(object)
|
new_data = pyqtSignal(object)
|
||||||
KEYS = ['data']
|
KEYS = ['data']
|
||||||
|
|
||||||
def __init__(self, parent=None):
|
def __init__(self, pytestconfig, parent=None):
|
||||||
super().__init__(parent)
|
super().__init__(parent)
|
||||||
|
self._pytestconfig = pytestconfig
|
||||||
self.captured_log = []
|
self.captured_log = []
|
||||||
self._started = False
|
self._started = False
|
||||||
self._invalid = []
|
self._invalid = []
|
||||||
@ -150,7 +153,7 @@ class Process(QObject):
|
|||||||
|
|
||||||
def _log(self, line):
|
def _log(self, line):
|
||||||
"""Add the given line to the captured log output."""
|
"""Add the given line to the captured log output."""
|
||||||
if pytest.config.getoption('--capture') == 'no':
|
if self._pytestconfig.getoption('--capture') == 'no':
|
||||||
print(line)
|
print(line)
|
||||||
self.captured_log.append(line)
|
self.captured_log.append(line)
|
||||||
|
|
||||||
@ -225,6 +228,8 @@ class Process(QObject):
|
|||||||
"""Start the process and wait until it started."""
|
"""Start the process and wait until it started."""
|
||||||
self._start(args, env=env)
|
self._start(args, env=env)
|
||||||
self._started = True
|
self._started = True
|
||||||
|
verbose = self._pytestconfig.getoption('--verbose')
|
||||||
|
|
||||||
timeout = 60 if 'CI' in os.environ else 20
|
timeout = 60 if 'CI' in os.environ else 20
|
||||||
for _ in range(timeout):
|
for _ in range(timeout):
|
||||||
with self._wait_signal(self.ready, timeout=1000,
|
with self._wait_signal(self.ready, timeout=1000,
|
||||||
@ -236,14 +241,15 @@ class Process(QObject):
|
|||||||
return
|
return
|
||||||
# _start ensures it actually started, but it might quit shortly
|
# _start ensures it actually started, but it might quit shortly
|
||||||
# afterwards
|
# afterwards
|
||||||
raise ProcessExited('\n' + _render_log(self.captured_log))
|
raise ProcessExited('\n' + _render_log(self.captured_log,
|
||||||
|
verbose=verbose))
|
||||||
|
|
||||||
if blocker.signal_triggered:
|
if blocker.signal_triggered:
|
||||||
self._after_start()
|
self._after_start()
|
||||||
return
|
return
|
||||||
|
|
||||||
raise WaitForTimeout("Timed out while waiting for process start.\n" +
|
raise WaitForTimeout("Timed out while waiting for process start.\n" +
|
||||||
_render_log(self.captured_log))
|
_render_log(self.captured_log, verbose=verbose))
|
||||||
|
|
||||||
def _start(self, args, env):
|
def _start(self, args, env):
|
||||||
"""Actually start the process."""
|
"""Actually start the process."""
|
||||||
|
@ -137,8 +137,8 @@ class WebserverProcess(testprocess.Process):
|
|||||||
|
|
||||||
KEYS = ['verb', 'path']
|
KEYS = ['verb', 'path']
|
||||||
|
|
||||||
def __init__(self, script, parent=None):
|
def __init__(self, pytestconfig, script, parent=None):
|
||||||
super().__init__(parent)
|
super().__init__(pytestconfig, parent)
|
||||||
self._script = script
|
self._script = script
|
||||||
self.port = utils.random_port()
|
self.port = utils.random_port()
|
||||||
self.new_data.connect(self.new_request)
|
self.new_data.connect(self.new_request)
|
||||||
@ -174,9 +174,9 @@ class WebserverProcess(testprocess.Process):
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope='session', autouse=True)
|
@pytest.fixture(scope='session', autouse=True)
|
||||||
def server(qapp):
|
def server(qapp, pytestconfig):
|
||||||
"""Fixture for an server object which ensures clean setup/teardown."""
|
"""Fixture for an server object which ensures clean setup/teardown."""
|
||||||
server = WebserverProcess('webserver_sub')
|
server = WebserverProcess(pytestconfig, 'webserver_sub')
|
||||||
server.start()
|
server.start()
|
||||||
yield server
|
yield server
|
||||||
server.terminate()
|
server.terminate()
|
||||||
@ -198,7 +198,7 @@ def ssl_server(request, qapp):
|
|||||||
This needs to be explicitly used in a test, and overwrites the server log
|
This needs to be explicitly used in a test, and overwrites the server log
|
||||||
used in that test.
|
used in that test.
|
||||||
"""
|
"""
|
||||||
server = WebserverProcess('webserver_sub_ssl')
|
server = WebserverProcess(request.config, 'webserver_sub_ssl')
|
||||||
request.node._server_log = server.captured_log
|
request.node._server_log = server.captured_log
|
||||||
server.start()
|
server.start()
|
||||||
yield server
|
yield server
|
||||||
|
Loading…
Reference in New Issue
Block a user