qutebrowser/tests/end2end/test_invocations.py

264 lines
9.4 KiB
Python
Raw Normal View History

# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
# Copyright 2016 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/>.
"""Test starting qutebrowser with special arguments/environments."""
import socket
import sys
import logging
2016-09-28 14:38:15 +02:00
import re
import pytest
2016-09-28 14:38:15 +02:00
from PyQt5.QtCore import QProcess
2016-09-29 06:36:31 +02:00
from end2end.fixtures import quteprocess, testprocess
2017-02-28 08:22:06 +01:00
from qutebrowser.utils import qtutils
2016-09-28 14:38:15 +02:00
def _base_args(config):
"""Get the arguments to pass with every invocation."""
args = ['--debug', '--json-logging', '--no-err-windows']
if config.webengine:
args += ['--backend', 'webengine']
else:
args += ['--backend', 'webkit']
args.append('about:blank')
return args
@pytest.fixture
def temp_basedir_env(tmpdir, short_tmpdir):
"""Return a dict of environment variables that fakes --temp-basedir.
We can't run --basedir or --temp-basedir for some tests, so we mess with
XDG_*_DIR to get things relocated.
"""
data_dir = tmpdir / 'data'
config_dir = tmpdir / 'config'
runtime_dir = short_tmpdir / 'rt'
cache_dir = tmpdir / 'cache'
runtime_dir.ensure(dir=True)
runtime_dir.chmod(0o700)
(data_dir / 'qutebrowser' / 'state').write_text(
'[general]\nquickstart-done = 1', encoding='utf-8', ensure=True)
env = {
'XDG_DATA_HOME': str(data_dir),
'XDG_CONFIG_HOME': str(config_dir),
'XDG_RUNTIME_DIR': str(runtime_dir),
'XDG_CACHE_HOME': str(cache_dir),
}
return env
@pytest.mark.linux
def test_ascii_locale(request, httpbin, tmpdir, quteproc_new):
"""Test downloads with LC_ALL=C set.
2017-02-05 00:13:11 +01:00
https://github.com/qutebrowser/qutebrowser/issues/908
https://github.com/qutebrowser/qutebrowser/issues/1726
"""
if request.config.webengine:
pytest.skip("Downloads are not implemented with QtWebEngine yet")
args = ['--temp-basedir'] + _base_args(request.config)
quteproc_new.start(args, env={'LC_ALL': 'C'})
quteproc_new.set_setting('storage', 'download-directory', str(tmpdir))
# Test a normal download
quteproc_new.set_setting('storage', 'prompt-download-directory', 'false')
url = 'http://localhost:{port}/data/downloads/ä-issue908.bin'.format(
port=httpbin.port)
quteproc_new.send_cmd(':download {}'.format(url))
quteproc_new.wait_for(category='downloads',
message='Download ?-issue908.bin finished')
# Test :prompt-open-download
quteproc_new.set_setting('storage', 'prompt-download-directory', 'true')
quteproc_new.send_cmd(':download {}'.format(url))
quteproc_new.send_cmd(':prompt-open-download "{}" -c pass'
.format(sys.executable))
quteproc_new.wait_for(category='downloads',
message='Download ä-issue908.bin finished')
2017-01-04 17:56:56 +01:00
quteproc_new.wait_for(category='misc',
message='Opening * with [*python*]')
assert len(tmpdir.listdir()) == 1
assert (tmpdir / '?-issue908.bin').exists()
@pytest.mark.linux
def test_misconfigured_user_dirs(request, httpbin, temp_basedir_env,
tmpdir, quteproc_new):
2016-09-10 17:32:04 +02:00
"""Test downloads with a misconfigured XDG_DOWNLOAD_DIR.
2017-02-05 00:13:11 +01:00
https://github.com/qutebrowser/qutebrowser/issues/866
https://github.com/qutebrowser/qutebrowser/issues/1269
"""
if request.config.webengine:
pytest.skip("Downloads are not implemented with QtWebEngine yet")
home = tmpdir / 'home'
home.ensure(dir=True)
temp_basedir_env['HOME'] = str(home)
assert temp_basedir_env['XDG_CONFIG_HOME'] == tmpdir / 'config'
(tmpdir / 'config' / 'user-dirs.dirs').write('XDG_DOWNLOAD_DIR="relative"',
ensure=True)
quteproc_new.start(_base_args(request.config), env=temp_basedir_env)
quteproc_new.set_setting('storage', 'prompt-download-directory', 'false')
url = 'http://localhost:{port}/data/downloads/download.bin'.format(
port=httpbin.port)
quteproc_new.send_cmd(':download {}'.format(url))
line = quteproc_new.wait_for(
loglevel=logging.ERROR, category='message',
message='XDG_DOWNLOAD_DIR points to a relative path - please check '
'your ~/.config/user-dirs.dirs. The download is saved in your '
'home directory.')
line.expected = True
quteproc_new.wait_for(category='downloads',
message='Download download.bin finished')
assert (home / 'download.bin').exists()
def test_no_loglines(request, quteproc_new):
"""Test qute:log with --loglines=0."""
if request.config.webengine:
pytest.skip("qute:log is not implemented with QtWebEngine yet")
quteproc_new.start(args=['--temp-basedir', '--loglines=0'] +
_base_args(request.config))
quteproc_new.open_path('qute:log')
assert quteproc_new.get_content() == 'Log output was disabled.'
2016-08-10 09:05:04 +02:00
@pytest.mark.not_frozen
2016-08-10 09:05:04 +02:00
@pytest.mark.parametrize('level', ['1', '2'])
def test_optimize(request, quteproc_new, capfd, level):
quteproc_new.start(args=['--temp-basedir'] + _base_args(request.config),
2016-08-10 09:05:04 +02:00
env={'PYTHONOPTIMIZE': level})
if level == '2':
msg = ("Running on optimize level higher than 1, unexpected behavior "
"may occur.")
line = quteproc_new.wait_for(message=msg)
line.expected = True
# Waiting for quit to make sure no other warning is emitted
2016-08-10 09:05:04 +02:00
quteproc_new.send_cmd(':quit')
quteproc_new.wait_for_quit()
2016-09-28 14:38:15 +02:00
def test_version(request):
"""Test invocation with --version argument."""
args = ['--version'] + _base_args(request.config)
# can't use quteproc_new here because it's confused by
# early process termination
2016-09-29 06:36:31 +02:00
proc = quteprocess.QuteProc(request)
2016-09-28 14:38:15 +02:00
proc.proc.setProcessChannelMode(QProcess.SeparateChannels)
try:
proc.start(args)
proc.wait_for_quit()
2016-09-29 06:36:31 +02:00
except testprocess.ProcessExited:
2016-09-28 14:38:15 +02:00
assert proc.proc.exitStatus() == QProcess.NormalExit
else:
2016-09-29 06:37:04 +02:00
pytest.fail("Process did not exit!")
2016-09-28 14:38:15 +02:00
output = bytes(proc.proc.readAllStandardOutput()).decode('utf-8')
assert re.search(r'^qutebrowser\s+v\d+(\.\d+)', output) is not None
2017-02-27 14:01:58 +01:00
2017-02-28 08:22:06 +01:00
@pytest.mark.skipif(not qtutils.version_check('5.3'),
reason="Does not work on Qt 5.2")
2017-02-27 14:01:58 +01:00
def test_qt_arg(request, quteproc_new, tmpdir):
"""Test --qt-arg."""
2017-02-27 15:12:38 +01:00
args = (['--temp-basedir', '--qt-arg', 'stylesheet',
str(tmpdir / 'does-not-exist')] + _base_args(request.config))
2017-02-27 14:01:58 +01:00
quteproc_new.start(args)
msg = 'QCss::Parser - Failed to load file "*does-not-exist"'
line = quteproc_new.wait_for(message=msg)
line.expected = True
quteproc_new.send_cmd(':quit')
quteproc_new.wait_for_quit()
def test_webengine_inspector(request, quteproc_new):
if not request.config.webengine:
pytest.skip()
args = (['--temp-basedir', '--enable-webengine-inspector'] +
_base_args(request.config))
quteproc_new.start(args)
line = quteproc_new.wait_for(
message='Remote debugging server started successfully. Try pointing a '
'Chromium-based browser to http://127.0.0.1:*')
port = int(line.message.split(':')[-1])
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1', port))
s.close()
@pytest.mark.linux
def test_webengine_download_suffix(request, quteproc_new, tmpdir):
"""Make sure QtWebEngine does not add a suffix to downloads."""
if not request.config.webengine:
pytest.skip()
download_dir = tmpdir / 'downloads'
download_dir.ensure(dir=True)
(tmpdir / 'user-dirs.dirs').write(
'XDG_DOWNLOAD_DIR={}'.format(download_dir))
env = {'XDG_CONFIG_HOME': str(tmpdir)}
args = (['--temp-basedir'] + _base_args(request.config))
quteproc_new.start(args, env=env)
quteproc_new.set_setting('storage', 'prompt-download-directory', 'false')
2017-03-02 08:29:43 +01:00
quteproc_new.set_setting('storage', 'download-directory',
str(download_dir))
quteproc_new.open_path('data/downloads/download.bin', wait=False)
quteproc_new.wait_for(category='downloads', message='Download * finished')
quteproc_new.open_path('data/downloads/download.bin', wait=False)
quteproc_new.wait_for(message='Entering mode KeyMode.yesno *')
quteproc_new.send_cmd(':prompt-accept yes')
quteproc_new.wait_for(category='downloads', message='Download * finished')
files = download_dir.listdir()
assert len(files) == 1
assert files[0].basename == 'download.bin'
def test_command_on_start(request, quteproc_new):
"""Make sure passing a command on start works.
See https://github.com/qutebrowser/qutebrowser/issues/2408
"""
args = (['--temp-basedir'] + _base_args(request.config) +
[':quickmark-add https://www.example.com/ example'])
quteproc_new.start(args)
quteproc_new.send_cmd(':quit')
quteproc_new.wait_for_quit()