Merge branch 'rcorre-cut_test_clutter'

This commit is contained in:
Florian Bruhin 2016-07-20 16:19:49 +02:00
commit aabee4828e
11 changed files with 69 additions and 18 deletions

View File

@ -9,6 +9,7 @@ load-plugins=qute_pylint.config,
qute_pylint.settrace, qute_pylint.settrace,
pylint.extensions.bad_builtin, pylint.extensions.bad_builtin,
pylint.extensions.docstyle pylint.extensions.docstyle
persistent=n
[MESSAGES CONTROL] [MESSAGES CONTROL]
enable=all enable=all

View File

@ -30,6 +30,7 @@ import itertools
import textwrap import textwrap
import unittest.mock import unittest.mock
import types import types
import os
import pytest import pytest
@ -405,3 +406,40 @@ def mode_manager(win_registry, config_stub, qapp):
objreg.register('mode-manager', mm, scope='window', window=0) objreg.register('mode-manager', mm, scope='window', window=0)
yield mm yield mm
objreg.delete('mode-manager', scope='window', window=0) 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).
"""
confdir = tmpdir / 'config'
path = str(confdir)
os.mkdir(path)
monkeypatch.setattr('qutebrowser.utils.standarddir.config', lambda: path)
return confdir
@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).
"""
datadir = tmpdir / 'data'
path = str(datadir)
os.mkdir(path)
monkeypatch.setattr('qutebrowser.utils.standarddir.data', lambda: path)
return datadir
@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))

View File

@ -30,7 +30,7 @@ from qutebrowser.browser import adblock
from qutebrowser.utils import objreg from qutebrowser.utils import objreg
from qutebrowser.commands import cmdexc 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 # TODO See ../utils/test_standarddirutils for OSError and caplog assertion
@ -54,13 +54,6 @@ URLS_TO_CHECK = ('http://localhost',
'http://qutebrowser.org') '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: class BaseDirStub:
"""Mock for objreg.get('args') called in adblock.HostBlocker.read_hosts.""" """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, # by creating a file named blocked-hosts,
# Exclude localhost from it, since localhost is in HostBlocker.WHITELISTED # Exclude localhost from it, since localhost is in HostBlocker.WHITELISTED
filtered_blocked_hosts = BLOCKLIST_HOSTS[1:] filtered_blocked_hosts = BLOCKLIST_HOSTS[1:]
blocklist = create_blocklist(tmpdir, blocklist = create_blocklist(data_tmpdir,
blocked_hosts=filtered_blocked_hosts, blocked_hosts=filtered_blocked_hosts,
name='blocked-hosts', name='blocked-hosts',
line_format='one_per_line') line_format='one_per_line')

View File

@ -25,6 +25,8 @@ from qutebrowser.browser import browsertab
from qutebrowser.keyinput import modeman from qutebrowser.keyinput import modeman
from qutebrowser.utils import objreg from qutebrowser.utils import objreg
pytestmark = pytest.mark.usefixtures('redirect_xdg_data')
try: try:
from PyQt5.QtWebKitWidgets import QWebView from PyQt5.QtWebKitWidgets import QWebView

View File

@ -24,6 +24,8 @@ import pytest
from qutebrowser.browser.webkit import cookies from qutebrowser.browser.webkit import cookies
from qutebrowser.misc import lineparser from qutebrowser.misc import lineparser
pytestmark = pytest.mark.usefixtures('data_tmpdir')
CONFIG_ALL_COOKIES = {'content': {'cookies-accept': 'all'}} CONFIG_ALL_COOKIES = {'content': {'cookies-accept': 'all'}}
CONFIG_NEVER_COOKIES = {'content': {'cookies-accept': 'never'}} CONFIG_NEVER_COOKIES = {'content': {'cookies-accept': 'never'}}
CONFIG_COOKIES_ENABLED = {'content': {'cookies-store': True}} CONFIG_COOKIES_ENABLED = {'content': {'cookies-store': True}}

View File

@ -64,6 +64,7 @@ def test_element_js_webkit(webview, js_enabled, expected):
assert result == expected assert result == expected
@pytest.mark.usefixtures('redirect_xdg_data')
@pytest.mark.parametrize('js_enabled, expected', [(True, 2.0), (False, 2.0)]) @pytest.mark.parametrize('js_enabled, expected', [(True, 2.0), (False, 2.0)])
def test_simple_js_webengine(qtbot, webengineview, js_enabled, expected): def test_simple_js_webengine(qtbot, webengineview, js_enabled, expected):
"""With QtWebEngine, runJavaScript works even when JS is off.""" """With QtWebEngine, runJavaScript works even when JS is off."""

View File

@ -295,6 +295,7 @@ class TestKeyConfigParser:
assert new == new_expected assert new == new_expected
@pytest.mark.usefixtures('config_tmpdir')
@pytest.mark.integration @pytest.mark.integration
class TestDefaultConfig: class TestDefaultConfig:

View File

@ -1282,6 +1282,7 @@ def unrequired_class(**kwargs):
@pytest.mark.usefixtures('qapp') @pytest.mark.usefixtures('qapp')
@pytest.mark.usefixtures('config_tmpdir')
class TestFileAndUserStyleSheet: class TestFileAndUserStyleSheet:
"""Test File/UserStyleSheet.""" """Test File/UserStyleSheet."""

View File

@ -42,7 +42,7 @@ def gen_classes():
yield member yield member
@pytest.mark.usefixtures('qapp') @pytest.mark.usefixtures('qapp', 'config_tmpdir')
@pytest.mark.parametrize('klass', gen_classes()) @pytest.mark.parametrize('klass', gen_classes())
@hypothesis.given(strategies.text()) @hypothesis.given(strategies.text())
@hypothesis.example('\x00') @hypothesis.example('\x00')

View File

@ -52,15 +52,15 @@ def no_cachedir_tag(monkeypatch):
lambda: None) lambda: None)
@pytest.yield_fixture(autouse=True) @pytest.yield_fixture
@pytest.mark.usefixtures('no_cachedir_tag') def reset_standarddir(no_cachedir_tag):
def reset_standarddir():
"""Clean up standarddir arguments before and after each test.""" """Clean up standarddir arguments before and after each test."""
standarddir.init(None) standarddir.init(None)
yield yield
standarddir.init(None) standarddir.init(None)
@pytest.mark.usefixtures('reset_standarddir')
@pytest.mark.parametrize('data_subdir, config_subdir, expected', [ @pytest.mark.parametrize('data_subdir, config_subdir, expected', [
('foo', 'foo', 'foo/data'), ('foo', 'foo', 'foo/data'),
('foo', 'bar', 'foo'), ('foo', 'bar', 'foo'),
@ -80,6 +80,7 @@ def test_get_fake_windows_equal_dir(data_subdir, config_subdir, expected,
assert standarddir.data() == expected assert standarddir.data() == expected
@pytest.mark.usefixtures('reset_standarddir')
class TestWritableLocation: class TestWritableLocation:
"""Tests for _writable_location.""" """Tests for _writable_location."""
@ -100,7 +101,7 @@ class TestWritableLocation:
assert '\\' in loc assert '\\' in loc
@pytest.mark.usefixtures('no_cachedir_tag') @pytest.mark.usefixtures('reset_standarddir')
class TestStandardDir: class TestStandardDir:
"""Tests for standarddir.""" """Tests for standarddir."""
@ -159,7 +160,7 @@ class TestStandardDir:
DirArgTest = collections.namedtuple('DirArgTest', 'arg, expected') DirArgTest = collections.namedtuple('DirArgTest', 'arg, expected')
@pytest.mark.usefixtures('no_cachedir_tag') @pytest.mark.usefixtures('reset_standarddir')
class TestArguments: class TestArguments:
"""Tests with confdir/cachedir/datadir arguments.""" """Tests with confdir/cachedir/datadir arguments."""
@ -195,8 +196,10 @@ class TestArguments:
standarddir.init(args) standarddir.init(args)
assert standarddir.data() == testcase.expected assert standarddir.data() == testcase.expected
def test_confdir_none(self): def test_confdir_none(self, mocker):
"""Test --confdir with None given.""" """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, args = types.SimpleNamespace(confdir=None, cachedir=None, datadir=None,
basedir=None) basedir=None)
standarddir.init(args) standarddir.init(args)
@ -294,6 +297,7 @@ class TestCreatingDir:
if os.name == 'posix': if os.name == 'posix':
assert basedir.stat().mode & 0o777 == 0o700 assert basedir.stat().mode & 0o777 == 0o700
@pytest.mark.usefixtures('reset_standarddir')
@pytest.mark.parametrize('typ', DIR_TYPES) @pytest.mark.parametrize('typ', DIR_TYPES)
def test_exists_race_condition(self, mocker, tmpdir, typ): def test_exists_race_condition(self, mocker, tmpdir, typ):
"""Make sure there can't be a TOCTOU issue when creating the file. """Make sure there can't be a TOCTOU issue when creating the file.
@ -315,6 +319,7 @@ class TestCreatingDir:
func() func()
@pytest.mark.usefixtures('reset_standarddir')
class TestSystemData: class TestSystemData:
"""Test system data path.""" """Test system data path."""
@ -326,12 +331,18 @@ class TestSystemData:
assert standarddir.system_data() == "/usr/share/qutebrowser" assert standarddir.system_data() == "/usr/share/qutebrowser"
@pytest.mark.linux @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.""" """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) monkeypatch.setattr(os.path, 'exists', lambda path: False)
assert standarddir.system_data() == standarddir.data() 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.""" """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") monkeypatch.setattr('sys.platform', "potato")
assert standarddir.system_data() == standarddir.data() assert standarddir.system_data() == standarddir.data()

View File

@ -5,6 +5,7 @@
[tox] [tox]
envlist = py34,py35-cov,misc,vulture,flake8,pylint,pyroma,check-manifest envlist = py34,py35-cov,misc,vulture,flake8,pylint,pyroma,check-manifest
distshare = {toxworkdir}
[testenv] [testenv]
# https://bitbucket.org/hpk42/tox/issue/246/ - only needed for Windows though # https://bitbucket.org/hpk42/tox/issue/246/ - only needed for Windows though