2015-06-11 23:05:57 +02:00
|
|
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
|
|
|
|
2016-01-04 07:12:39 +01:00
|
|
|
# Copyright 2015-2016 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
|
2015-06-11 23:05:57 +02:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
|
|
|
"""Tests for qutebrowser.misc.guiprocess."""
|
|
|
|
|
2015-08-19 07:57:02 +02:00
|
|
|
import json
|
2015-08-09 11:49:27 +02:00
|
|
|
import logging
|
2015-06-11 23:05:57 +02:00
|
|
|
|
|
|
|
import pytest
|
2015-08-19 07:57:02 +02:00
|
|
|
from PyQt5.QtCore import QProcess, QIODevice
|
2015-06-11 23:05:57 +02:00
|
|
|
|
|
|
|
from qutebrowser.misc import guiprocess
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
2015-08-18 21:28:22 +02:00
|
|
|
def guiprocess_message_mock(message_mock):
|
|
|
|
message_mock.patch('qutebrowser.misc.guiprocess.message')
|
|
|
|
return message_mock
|
2015-06-11 23:05:57 +02:00
|
|
|
|
|
|
|
|
2016-08-22 07:40:24 +02:00
|
|
|
@pytest.fixture()
|
2015-06-11 23:05:57 +02:00
|
|
|
def proc(qtbot):
|
|
|
|
"""A fixture providing a GUIProcess and cleaning it up after the test."""
|
2016-09-14 20:52:32 +02:00
|
|
|
p = guiprocess.GUIProcess('testprocess')
|
2015-06-11 23:05:57 +02:00
|
|
|
yield p
|
|
|
|
if p._proc.state() == QProcess.Running:
|
2016-01-08 09:49:06 +01:00
|
|
|
with qtbot.waitSignal(p.finished, timeout=10000,
|
|
|
|
raising=False) as blocker:
|
2015-06-11 23:05:57 +02:00
|
|
|
p._proc.terminate()
|
|
|
|
if not blocker.signal_triggered:
|
|
|
|
p._proc.kill()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
def fake_proc(monkeypatch, stubs):
|
|
|
|
"""A fixture providing a GUIProcess with a mocked QProcess."""
|
2016-09-14 20:52:32 +02:00
|
|
|
p = guiprocess.GUIProcess('testprocess')
|
2015-06-11 23:05:57 +02:00
|
|
|
monkeypatch.setattr(p, '_proc', stubs.fake_qprocess())
|
|
|
|
return p
|
|
|
|
|
|
|
|
|
2015-09-09 19:25:34 +02:00
|
|
|
def test_start(proc, qtbot, guiprocess_message_mock, py_proc):
|
2015-06-11 23:05:57 +02:00
|
|
|
"""Test simply starting a process."""
|
2016-07-29 07:35:16 +02:00
|
|
|
with qtbot.waitSignals([proc.started, proc.finished], timeout=10000,
|
|
|
|
order='strict'):
|
2015-09-09 19:25:34 +02:00
|
|
|
argv = py_proc("import sys; print('test'); sys.exit(0)")
|
2015-06-11 23:05:57 +02:00
|
|
|
proc.start(*argv)
|
|
|
|
|
2015-08-18 21:28:22 +02:00
|
|
|
assert not guiprocess_message_mock.messages
|
|
|
|
assert bytes(proc._proc.readAll()).rstrip() == b'test'
|
|
|
|
|
|
|
|
|
2015-09-09 19:25:34 +02:00
|
|
|
def test_start_verbose(proc, qtbot, guiprocess_message_mock, py_proc):
|
2015-08-18 21:28:22 +02:00
|
|
|
"""Test starting a process verbosely."""
|
|
|
|
proc.verbose = True
|
|
|
|
|
2016-07-29 07:35:16 +02:00
|
|
|
with qtbot.waitSignals([proc.started, proc.finished], timeout=10000,
|
|
|
|
order='strict'):
|
2015-09-09 19:25:34 +02:00
|
|
|
argv = py_proc("import sys; print('test'); sys.exit(0)")
|
2015-08-18 21:28:22 +02:00
|
|
|
proc.start(*argv)
|
|
|
|
|
|
|
|
msgs = guiprocess_message_mock.messages
|
2015-08-19 09:44:31 +02:00
|
|
|
assert msgs[0].level == guiprocess_message_mock.Level.info
|
|
|
|
assert msgs[1].level == guiprocess_message_mock.Level.info
|
2015-08-18 21:28:22 +02:00
|
|
|
assert msgs[0].text.startswith("Executing:")
|
2015-08-19 07:57:02 +02:00
|
|
|
assert msgs[1].text == "Testprocess exited successfully."
|
2015-06-11 23:14:56 +02:00
|
|
|
assert bytes(proc._proc.readAll()).rstrip() == b'test'
|
2015-06-11 23:05:57 +02:00
|
|
|
|
|
|
|
|
2015-09-09 19:25:34 +02:00
|
|
|
def test_start_env(monkeypatch, qtbot, py_proc):
|
2015-08-19 07:57:02 +02:00
|
|
|
monkeypatch.setenv('QUTEBROWSER_TEST_1', '1')
|
|
|
|
env = {'QUTEBROWSER_TEST_2': '2'}
|
2016-09-14 20:52:32 +02:00
|
|
|
proc = guiprocess.GUIProcess('testprocess', additional_env=env)
|
2015-08-19 07:57:02 +02:00
|
|
|
|
2015-09-09 19:25:34 +02:00
|
|
|
argv = py_proc("""
|
2015-08-19 07:57:02 +02:00
|
|
|
import os
|
|
|
|
import json
|
2015-12-18 21:23:33 +01:00
|
|
|
import sys
|
|
|
|
|
2015-08-19 07:57:02 +02:00
|
|
|
env = dict(os.environ)
|
|
|
|
print(json.dumps(env))
|
|
|
|
sys.exit(0)
|
|
|
|
""")
|
|
|
|
|
2016-07-29 07:35:16 +02:00
|
|
|
with qtbot.waitSignals([proc.started, proc.finished], timeout=10000,
|
|
|
|
order='strict'):
|
2015-08-19 07:57:02 +02:00
|
|
|
proc.start(*argv)
|
|
|
|
|
|
|
|
data = bytes(proc._proc.readAll()).decode('utf-8')
|
|
|
|
ret_env = json.loads(data)
|
|
|
|
assert 'QUTEBROWSER_TEST_1' in ret_env
|
|
|
|
assert 'QUTEBROWSER_TEST_2' in ret_env
|
|
|
|
|
|
|
|
|
2016-07-29 07:10:06 +02:00
|
|
|
@pytest.mark.qt_log_ignore('QIODevice::read.*: WriteOnly device')
|
2015-09-09 19:25:34 +02:00
|
|
|
def test_start_mode(proc, qtbot, py_proc):
|
2015-08-19 07:57:02 +02:00
|
|
|
"""Test simply starting a process with mode parameter."""
|
2016-07-29 07:35:16 +02:00
|
|
|
with qtbot.waitSignals([proc.started, proc.finished], timeout=10000,
|
|
|
|
order='strict'):
|
2015-09-09 19:25:34 +02:00
|
|
|
argv = py_proc("import sys; print('test'); sys.exit(0)")
|
2015-08-19 07:57:02 +02:00
|
|
|
proc.start(*argv, mode=QIODevice.NotOpen)
|
|
|
|
|
|
|
|
assert not proc._proc.readAll()
|
|
|
|
|
|
|
|
|
|
|
|
def test_start_detached(fake_proc):
|
2015-06-11 23:05:57 +02:00
|
|
|
"""Test starting a detached process."""
|
2015-08-19 07:57:02 +02:00
|
|
|
argv = ['foo', 'bar']
|
2015-06-11 23:05:57 +02:00
|
|
|
fake_proc._proc.startDetached.return_value = (True, 0)
|
|
|
|
fake_proc.start_detached(*argv)
|
|
|
|
fake_proc._proc.startDetached.assert_called_with(*list(argv) + [None])
|
|
|
|
|
|
|
|
|
2015-08-19 07:57:02 +02:00
|
|
|
def test_start_detached_error(fake_proc, guiprocess_message_mock):
|
|
|
|
"""Test starting a detached process with ok=False."""
|
|
|
|
argv = ['foo', 'bar']
|
|
|
|
fake_proc._proc.startDetached.return_value = (False, 0)
|
|
|
|
fake_proc._proc.error.return_value = "Error message"
|
|
|
|
fake_proc.start_detached(*argv)
|
2015-08-19 09:44:31 +02:00
|
|
|
msg = guiprocess_message_mock.getmsg(guiprocess_message_mock.Level.error,
|
|
|
|
immediate=True)
|
2015-08-19 07:57:02 +02:00
|
|
|
assert msg.text == "Error while spawning testprocess: Error message."
|
|
|
|
|
|
|
|
|
2015-09-09 19:25:34 +02:00
|
|
|
def test_double_start(qtbot, proc, py_proc):
|
2015-06-11 23:05:57 +02:00
|
|
|
"""Test starting a GUIProcess twice."""
|
2016-01-08 09:49:06 +01:00
|
|
|
with qtbot.waitSignal(proc.started, timeout=10000):
|
2015-09-09 19:25:34 +02:00
|
|
|
argv = py_proc("import time; time.sleep(10)")
|
2015-06-11 23:05:57 +02:00
|
|
|
proc.start(*argv)
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
proc.start('', [])
|
|
|
|
|
|
|
|
|
2015-09-09 19:25:34 +02:00
|
|
|
def test_double_start_finished(qtbot, proc, py_proc):
|
2015-06-11 23:05:57 +02:00
|
|
|
"""Test starting a GUIProcess twice (with the first call finished)."""
|
2016-07-29 07:35:16 +02:00
|
|
|
with qtbot.waitSignals([proc.started, proc.finished], timeout=10000,
|
|
|
|
order='strict'):
|
2015-09-09 19:25:34 +02:00
|
|
|
argv = py_proc("import sys; sys.exit(0)")
|
2015-06-11 23:05:57 +02:00
|
|
|
proc.start(*argv)
|
2016-07-29 07:35:16 +02:00
|
|
|
with qtbot.waitSignals([proc.started, proc.finished], timeout=10000,
|
|
|
|
order='strict'):
|
2015-09-09 19:25:34 +02:00
|
|
|
argv = py_proc("import sys; sys.exit(0)")
|
2015-06-11 23:05:57 +02:00
|
|
|
proc.start(*argv)
|
|
|
|
|
|
|
|
|
2015-08-01 23:50:29 +02:00
|
|
|
def test_cmd_args(fake_proc):
|
2015-06-11 23:05:57 +02:00
|
|
|
"""Test the cmd and args attributes."""
|
|
|
|
cmd = 'does_not_exist'
|
|
|
|
args = ['arg1', 'arg2']
|
2015-08-01 23:50:29 +02:00
|
|
|
fake_proc.start(cmd, args)
|
|
|
|
assert (fake_proc.cmd, fake_proc.args) == (cmd, args)
|
2015-06-11 23:05:57 +02:00
|
|
|
|
|
|
|
|
2015-09-10 19:25:58 +02:00
|
|
|
def test_start_logging(fake_proc, caplog):
|
|
|
|
"""Make sure that starting logs the executed commandline."""
|
|
|
|
cmd = 'does_not_exist'
|
|
|
|
args = ['arg', 'arg with spaces']
|
2015-11-11 19:57:03 +01:00
|
|
|
with caplog.at_level(logging.DEBUG):
|
2015-09-10 19:25:58 +02:00
|
|
|
fake_proc.start(cmd, args)
|
2015-11-11 19:57:03 +01:00
|
|
|
msgs = [e.msg for e in caplog.records]
|
2015-09-10 19:25:58 +02:00
|
|
|
assert msgs == ["Starting process.",
|
|
|
|
"Executing: does_not_exist arg 'arg with spaces'"]
|
|
|
|
|
|
|
|
|
2015-08-18 21:28:22 +02:00
|
|
|
def test_error(qtbot, proc, caplog, guiprocess_message_mock):
|
2015-06-11 23:05:57 +02:00
|
|
|
"""Test the process emitting an error."""
|
2015-11-11 19:57:03 +01:00
|
|
|
with caplog.at_level(logging.ERROR, 'message'):
|
2016-01-08 09:49:06 +01:00
|
|
|
with qtbot.waitSignal(proc.error, timeout=5000):
|
2015-08-09 11:49:27 +02:00
|
|
|
proc.start('this_does_not_exist_either', [])
|
2015-06-11 23:05:57 +02:00
|
|
|
|
2015-08-19 09:44:31 +02:00
|
|
|
msg = guiprocess_message_mock.getmsg(guiprocess_message_mock.Level.error,
|
|
|
|
immediate=True)
|
2015-08-19 07:57:02 +02:00
|
|
|
expected_msg = ("Error while spawning testprocess: The process failed to "
|
|
|
|
"start.")
|
2015-08-18 21:28:22 +02:00
|
|
|
assert msg.text == expected_msg
|
|
|
|
|
2015-06-11 23:05:57 +02:00
|
|
|
|
2015-09-09 19:25:34 +02:00
|
|
|
def test_exit_unsuccessful(qtbot, proc, guiprocess_message_mock, py_proc):
|
2016-01-08 09:49:06 +01:00
|
|
|
with qtbot.waitSignal(proc.finished, timeout=10000):
|
2015-09-09 19:25:34 +02:00
|
|
|
proc.start(*py_proc('import sys; sys.exit(1)'))
|
2015-08-18 21:28:22 +02:00
|
|
|
|
2015-08-19 09:44:31 +02:00
|
|
|
msg = guiprocess_message_mock.getmsg(guiprocess_message_mock.Level.error)
|
2015-08-19 07:57:02 +02:00
|
|
|
assert msg.text == "Testprocess exited with status 1."
|
2015-12-18 21:23:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('stream', ['stdout', 'stderr'])
|
|
|
|
def test_exit_unsuccessful_output(qtbot, proc, caplog, py_proc, stream):
|
|
|
|
"""When a process fails, its output should be logged."""
|
2015-12-21 09:52:33 +01:00
|
|
|
with caplog.at_level(logging.ERROR):
|
2016-01-08 09:49:06 +01:00
|
|
|
with qtbot.waitSignal(proc.finished, timeout=10000):
|
2015-12-18 21:23:33 +01:00
|
|
|
proc.start(*py_proc("""
|
|
|
|
import sys
|
|
|
|
print("test", file=sys.{})
|
|
|
|
sys.exit(1)
|
|
|
|
""".format(stream)))
|
|
|
|
assert len(caplog.records) == 2
|
|
|
|
assert caplog.records[1].msg == 'Process {}:\ntest'.format(stream)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('stream', ['stdout', 'stderr'])
|
|
|
|
def test_exit_successful_output(qtbot, proc, py_proc, stream):
|
2016-05-27 12:07:00 +02:00
|
|
|
"""When a process succeeds, no output should be logged.
|
2015-12-18 21:23:33 +01:00
|
|
|
|
|
|
|
The test doesn't actually check the log as it'd fail because of the error
|
|
|
|
logging.
|
|
|
|
"""
|
2016-01-08 09:49:06 +01:00
|
|
|
with qtbot.waitSignal(proc.finished, timeout=10000):
|
2015-12-18 21:23:33 +01:00
|
|
|
proc.start(*py_proc("""
|
|
|
|
import sys
|
|
|
|
print("test", file=sys.{})
|
|
|
|
sys.exit(0)
|
|
|
|
""".format(stream)))
|