Add a py_proc fixture to conftest.py.

This commit is contained in:
Florian Bruhin 2015-09-09 19:25:34 +02:00
parent 1d7c2b6b82
commit 37a2523bff
3 changed files with 26 additions and 24 deletions

View File

@ -26,6 +26,7 @@ import sys
import collections
import itertools
import logging
import textwrap
import pytest
@ -298,3 +299,11 @@ def cookiejar_and_cache(stubs):
yield
objreg.delete('cookie-jar')
objreg.delete('cache')
@pytest.fixture
def py_proc():
"""Get a python executable and args list which executes the given code."""
def func(code):
return (sys.executable, ['-c', textwrap.dedent(code.strip('\n'))])
return func

View File

@ -20,9 +20,7 @@
"""Tests for qutebrowser.misc.guiprocess."""
import os
import sys
import json
import textwrap
import logging
import pytest
@ -31,11 +29,6 @@ from PyQt5.QtCore import QProcess, QIODevice
from qutebrowser.misc import guiprocess
def _py_proc(code):
"""Get a python executable and args list which executes the given code."""
return (sys.executable, ['-c', textwrap.dedent(code.strip('\n'))])
@pytest.fixture(autouse=True)
def guiprocess_message_mock(message_mock):
message_mock.patch('qutebrowser.misc.guiprocess.message')
@ -66,11 +59,11 @@ def fake_proc(monkeypatch, stubs):
@pytest.mark.not_frozen
def test_start(proc, qtbot, guiprocess_message_mock):
def test_start(proc, qtbot, guiprocess_message_mock, py_proc):
"""Test simply starting a process."""
with qtbot.waitSignals([proc.started, proc.finished], raising=True,
timeout=10000):
argv = _py_proc("import sys; print('test'); sys.exit(0)")
argv = py_proc("import sys; print('test'); sys.exit(0)")
proc.start(*argv)
assert not guiprocess_message_mock.messages
@ -78,13 +71,13 @@ def test_start(proc, qtbot, guiprocess_message_mock):
@pytest.mark.not_frozen
def test_start_verbose(proc, qtbot, guiprocess_message_mock):
def test_start_verbose(proc, qtbot, guiprocess_message_mock, py_proc):
"""Test starting a process verbosely."""
proc.verbose = True
with qtbot.waitSignals([proc.started, proc.finished], raising=True,
timeout=10000):
argv = _py_proc("import sys; print('test'); sys.exit(0)")
argv = py_proc("import sys; print('test'); sys.exit(0)")
proc.start(*argv)
msgs = guiprocess_message_mock.messages
@ -98,12 +91,12 @@ def test_start_verbose(proc, qtbot, guiprocess_message_mock):
# WORKAROUND for https://github.com/pytest-dev/pytest-qt/issues/67
@pytest.mark.skipif(os.name == 'nt', reason="Test is flaky on Windows...")
@pytest.mark.not_frozen
def test_start_env(monkeypatch, qtbot):
def test_start_env(monkeypatch, qtbot, py_proc):
monkeypatch.setenv('QUTEBROWSER_TEST_1', '1')
env = {'QUTEBROWSER_TEST_2': '2'}
proc = guiprocess.GUIProcess(0, 'testprocess', additional_env=env)
argv = _py_proc("""
argv = py_proc("""
import os
import json
env = dict(os.environ)
@ -123,11 +116,11 @@ def test_start_env(monkeypatch, qtbot):
@pytest.mark.not_frozen
@pytest.mark.qt_log_ignore('QIODevice::read.*: WriteOnly device')
def test_start_mode(proc, qtbot):
def test_start_mode(proc, qtbot, py_proc):
"""Test simply starting a process with mode parameter."""
with qtbot.waitSignals([proc.started, proc.finished], raising=True,
timeout=10000):
argv = _py_proc("import sys; print('test'); sys.exit(0)")
argv = py_proc("import sys; print('test'); sys.exit(0)")
proc.start(*argv, mode=QIODevice.NotOpen)
assert not proc._proc.readAll()
@ -153,25 +146,25 @@ def test_start_detached_error(fake_proc, guiprocess_message_mock):
@pytest.mark.not_frozen
def test_double_start(qtbot, proc):
def test_double_start(qtbot, proc, py_proc):
"""Test starting a GUIProcess twice."""
with qtbot.waitSignal(proc.started, raising=True, timeout=10000):
argv = _py_proc("import time; time.sleep(10)")
argv = py_proc("import time; time.sleep(10)")
proc.start(*argv)
with pytest.raises(ValueError):
proc.start('', [])
@pytest.mark.not_frozen
def test_double_start_finished(qtbot, proc):
def test_double_start_finished(qtbot, proc, py_proc):
"""Test starting a GUIProcess twice (with the first call finished)."""
with qtbot.waitSignals([proc.started, proc.finished], raising=True,
timeout=10000):
argv = _py_proc("import sys; sys.exit(0)")
argv = py_proc("import sys; sys.exit(0)")
proc.start(*argv)
with qtbot.waitSignals([proc.started, proc.finished], raising=True,
timeout=10000):
argv = _py_proc("import sys; sys.exit(0)")
argv = py_proc("import sys; sys.exit(0)")
proc.start(*argv)
@ -197,9 +190,9 @@ def test_error(qtbot, proc, caplog, guiprocess_message_mock):
@pytest.mark.not_frozen
def test_exit_unsuccessful(qtbot, proc, guiprocess_message_mock):
def test_exit_unsuccessful(qtbot, proc, guiprocess_message_mock, py_proc):
with qtbot.waitSignal(proc.finished, raising=True, timeout=10000):
proc.start(*_py_proc('import sys; sys.exit(1)'))
proc.start(*py_proc('import sys; sys.exit(1)'))
msg = guiprocess_message_mock.getmsg(guiprocess_message_mock.Level.error)
assert msg.text == "Testprocess exited with status 1."

View File

@ -754,13 +754,13 @@ class TestPyQIODevice:
pyqiodev.seek(0, whence)
@pytest.mark.not_frozen
def test_qprocess(self):
def test_qprocess(self, py_proc):
"""Test PyQIODevice with a QProcess which is non-sequential.
This also verifies seek() and tell() behave as expected.
"""
proc = QProcess()
proc.start(sys.executable, ['-c', 'print("Hello World")'])
proc.start(*py_proc('print("Hello World")'))
dev = qtutils.PyQIODevice(proc)
assert not dev.closed
with pytest.raises(OSError) as excinfo: