Merge branch 'cut_test_clutter' of https://github.com/rcorre/qutebrowser into rcorre-cut_test_clutter
This commit is contained in:
commit
c7eec246d3
@ -30,6 +30,7 @@ import itertools
|
||||
import textwrap
|
||||
import unittest.mock
|
||||
import types
|
||||
import os
|
||||
|
||||
import pytest
|
||||
|
||||
@ -405,3 +406,40 @@ def mode_manager(win_registry, config_stub, qapp):
|
||||
objreg.register('mode-manager', mm, scope='window', window=0)
|
||||
yield mm
|
||||
objreg.delete('mode-manager', scope='window', window=0)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def config_tmpdir(monkeypatch, tmpdir):
|
||||
"""Set tmpdir/config as the configdir.
|
||||
|
||||
Use this to avoid creating a 'real' config dir (~/.config/qute_test).
|
||||
"""
|
||||
tmpdir = tmpdir / 'config'
|
||||
path = str(tmpdir)
|
||||
os.mkdir(path)
|
||||
monkeypatch.setattr('qutebrowser.utils.standarddir.config', lambda: path)
|
||||
return tmpdir
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def data_tmpdir(monkeypatch, tmpdir):
|
||||
"""Set tmpdir/data as the datadir.
|
||||
|
||||
Use this to avoid creating a 'real' data dir (~/.local/share/qute_test).
|
||||
"""
|
||||
tmpdir = tmpdir / 'data'
|
||||
path = str(tmpdir)
|
||||
os.mkdir(path)
|
||||
monkeypatch.setattr('qutebrowser.utils.standarddir.data', lambda: path)
|
||||
return tmpdir
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def redirect_xdg_data(data_tmpdir, monkeypatch):
|
||||
"""Set XDG_DATA_HOME to a temp location.
|
||||
|
||||
While data_tmpdir covers most cases by redirecting standarddir.data(), this
|
||||
is not enough for places Qt references the data dir internally. For these,
|
||||
we need to set the environment variable to redirect data access.
|
||||
"""
|
||||
monkeypatch.setenv('XDG_DATA_HOME', str(data_tmpdir))
|
||||
|
@ -30,7 +30,7 @@ from qutebrowser.browser import adblock
|
||||
from qutebrowser.utils import objreg
|
||||
from qutebrowser.commands import cmdexc
|
||||
|
||||
pytestmark = pytest.mark.usefixtures('qapp')
|
||||
pytestmark = pytest.mark.usefixtures('qapp', 'config_tmpdir')
|
||||
|
||||
# TODO See ../utils/test_standarddirutils for OSError and caplog assertion
|
||||
|
||||
@ -54,13 +54,6 @@ URLS_TO_CHECK = ('http://localhost',
|
||||
'http://qutebrowser.org')
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def data_tmpdir(monkeypatch, tmpdir):
|
||||
"""Set tmpdir as datadir."""
|
||||
tmpdir = str(tmpdir)
|
||||
monkeypatch.setattr('qutebrowser.utils.standarddir.data', lambda: tmpdir)
|
||||
|
||||
|
||||
class BaseDirStub:
|
||||
|
||||
"""Mock for objreg.get('args') called in adblock.HostBlocker.read_hosts."""
|
||||
@ -348,7 +341,7 @@ def test_blocking_with_whitelist(config_stub, basedir, download_stub,
|
||||
# by creating a file named blocked-hosts,
|
||||
# Exclude localhost from it, since localhost is in HostBlocker.WHITELISTED
|
||||
filtered_blocked_hosts = BLOCKLIST_HOSTS[1:]
|
||||
blocklist = create_blocklist(tmpdir,
|
||||
blocklist = create_blocklist(data_tmpdir,
|
||||
blocked_hosts=filtered_blocked_hosts,
|
||||
name='blocked-hosts',
|
||||
line_format='one_per_line')
|
||||
|
@ -25,6 +25,8 @@ from qutebrowser.browser import browsertab
|
||||
from qutebrowser.keyinput import modeman
|
||||
from qutebrowser.utils import objreg
|
||||
|
||||
pytestmark = pytest.mark.usefixtures('redirect_xdg_data')
|
||||
|
||||
try:
|
||||
from PyQt5.QtWebKitWidgets import QWebView
|
||||
|
||||
|
@ -24,6 +24,8 @@ import pytest
|
||||
from qutebrowser.browser.webkit import cookies
|
||||
from qutebrowser.misc import lineparser
|
||||
|
||||
pytestmark = pytest.mark.usefixtures('data_tmpdir')
|
||||
|
||||
CONFIG_ALL_COOKIES = {'content': {'cookies-accept': 'all'}}
|
||||
CONFIG_NEVER_COOKIES = {'content': {'cookies-accept': 'never'}}
|
||||
CONFIG_COOKIES_ENABLED = {'content': {'cookies-store': True}}
|
||||
|
@ -64,6 +64,7 @@ def test_element_js_webkit(webview, js_enabled, expected):
|
||||
assert result == expected
|
||||
|
||||
|
||||
@pytest.mark.usefixtures('redirect_xdg_data')
|
||||
@pytest.mark.parametrize('js_enabled, expected', [(True, 2.0), (False, 2.0)])
|
||||
def test_simple_js_webengine(qtbot, webengineview, js_enabled, expected):
|
||||
"""With QtWebEngine, runJavaScript works even when JS is off."""
|
||||
|
@ -295,6 +295,7 @@ class TestKeyConfigParser:
|
||||
assert new == new_expected
|
||||
|
||||
|
||||
@pytest.mark.usefixtures('config_tmpdir')
|
||||
@pytest.mark.integration
|
||||
class TestDefaultConfig:
|
||||
|
||||
|
@ -1282,6 +1282,7 @@ def unrequired_class(**kwargs):
|
||||
|
||||
|
||||
@pytest.mark.usefixtures('qapp')
|
||||
@pytest.mark.usefixtures('config_tmpdir')
|
||||
class TestFileAndUserStyleSheet:
|
||||
|
||||
"""Test File/UserStyleSheet."""
|
||||
|
@ -42,7 +42,7 @@ def gen_classes():
|
||||
yield member
|
||||
|
||||
|
||||
@pytest.mark.usefixtures('qapp')
|
||||
@pytest.mark.usefixtures('qapp', 'config_tmpdir')
|
||||
@pytest.mark.parametrize('klass', gen_classes())
|
||||
@hypothesis.given(strategies.text())
|
||||
@hypothesis.example('\x00')
|
||||
|
@ -52,15 +52,15 @@ def no_cachedir_tag(monkeypatch):
|
||||
lambda: None)
|
||||
|
||||
|
||||
@pytest.yield_fixture(autouse=True)
|
||||
@pytest.mark.usefixtures('no_cachedir_tag')
|
||||
def reset_standarddir():
|
||||
@pytest.yield_fixture
|
||||
def reset_standarddir(no_cachedir_tag):
|
||||
"""Clean up standarddir arguments before and after each test."""
|
||||
standarddir.init(None)
|
||||
yield
|
||||
standarddir.init(None)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures('reset_standarddir')
|
||||
@pytest.mark.parametrize('data_subdir, config_subdir, expected', [
|
||||
('foo', 'foo', 'foo/data'),
|
||||
('foo', 'bar', 'foo'),
|
||||
@ -80,6 +80,7 @@ def test_get_fake_windows_equal_dir(data_subdir, config_subdir, expected,
|
||||
assert standarddir.data() == expected
|
||||
|
||||
|
||||
@pytest.mark.usefixtures('reset_standarddir')
|
||||
class TestWritableLocation:
|
||||
|
||||
"""Tests for _writable_location."""
|
||||
@ -100,7 +101,7 @@ class TestWritableLocation:
|
||||
assert '\\' in loc
|
||||
|
||||
|
||||
@pytest.mark.usefixtures('no_cachedir_tag')
|
||||
@pytest.mark.usefixtures('reset_standarddir')
|
||||
class TestStandardDir:
|
||||
|
||||
"""Tests for standarddir."""
|
||||
@ -159,7 +160,7 @@ class TestStandardDir:
|
||||
DirArgTest = collections.namedtuple('DirArgTest', 'arg, expected')
|
||||
|
||||
|
||||
@pytest.mark.usefixtures('no_cachedir_tag')
|
||||
@pytest.mark.usefixtures('reset_standarddir')
|
||||
class TestArguments:
|
||||
|
||||
"""Tests with confdir/cachedir/datadir arguments."""
|
||||
@ -195,8 +196,10 @@ class TestArguments:
|
||||
standarddir.init(args)
|
||||
assert standarddir.data() == testcase.expected
|
||||
|
||||
def test_confdir_none(self):
|
||||
def test_confdir_none(self, mocker):
|
||||
"""Test --confdir with None given."""
|
||||
# patch makedirs to a noop so we don't really create a directory
|
||||
mocker.patch('qutebrowser.utils.standarddir.os.makedirs')
|
||||
args = types.SimpleNamespace(confdir=None, cachedir=None, datadir=None,
|
||||
basedir=None)
|
||||
standarddir.init(args)
|
||||
@ -294,6 +297,7 @@ class TestCreatingDir:
|
||||
if os.name == 'posix':
|
||||
assert basedir.stat().mode & 0o777 == 0o700
|
||||
|
||||
@pytest.mark.usefixtures('reset_standarddir')
|
||||
@pytest.mark.parametrize('typ', DIR_TYPES)
|
||||
def test_exists_race_condition(self, mocker, tmpdir, typ):
|
||||
"""Make sure there can't be a TOCTOU issue when creating the file.
|
||||
@ -315,6 +319,7 @@ class TestCreatingDir:
|
||||
func()
|
||||
|
||||
|
||||
@pytest.mark.usefixtures('reset_standarddir')
|
||||
class TestSystemData:
|
||||
|
||||
"""Test system data path."""
|
||||
@ -326,12 +331,18 @@ class TestSystemData:
|
||||
assert standarddir.system_data() == "/usr/share/qutebrowser"
|
||||
|
||||
@pytest.mark.linux
|
||||
def test_system_datadir_not_exist_linux(self, monkeypatch):
|
||||
def test_system_datadir_not_exist_linux(self, monkeypatch, tmpdir,
|
||||
fake_args):
|
||||
"""Test that system-wide path isn't used on linux if path not exist."""
|
||||
fake_args.basedir = str(tmpdir)
|
||||
standarddir.init(fake_args)
|
||||
monkeypatch.setattr(os.path, 'exists', lambda path: False)
|
||||
assert standarddir.system_data() == standarddir.data()
|
||||
|
||||
def test_system_datadir_unsupportedos(self, monkeypatch):
|
||||
def test_system_datadir_unsupportedos(self, monkeypatch, tmpdir,
|
||||
fake_args):
|
||||
"""Test that system-wide path is not used on non-Linux OS."""
|
||||
fake_args.basedir = str(tmpdir)
|
||||
standarddir.init(fake_args)
|
||||
monkeypatch.setattr('sys.platform', "potato")
|
||||
assert standarddir.system_data() == standarddir.data()
|
||||
|
Loading…
Reference in New Issue
Block a user