From a6695ea1bee1d2815a41baa8733273216568ed00 Mon Sep 17 00:00:00 2001 From: Ryan Roden-Corrent Date: Thu, 14 Jul 2016 12:24:44 -0400 Subject: [PATCH] Prevent test_adblock from creating real config dir. Don't create ~/.config/qute_test by mocking out standdarddir.config for all tests in this module. This adds config_tmpdir to fixtures.py and moves temp_datadir from test_adblock to fixtures.py as it will be needed more broadly. --- tests/helpers/fixtures.py | 27 +++++++++++++++++++++++++++ tests/unit/browser/test_adblock.py | 11 ++--------- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/tests/helpers/fixtures.py b/tests/helpers/fixtures.py index 464d0c520..eea527779 100644 --- a/tests/helpers/fixtures.py +++ b/tests/helpers/fixtures.py @@ -30,6 +30,7 @@ import itertools import textwrap import unittest.mock import types +import os import pytest @@ -405,3 +406,29 @@ 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 diff --git a/tests/unit/browser/test_adblock.py b/tests/unit/browser/test_adblock.py index be801de73..b300c8a26 100644 --- a/tests/unit/browser/test_adblock.py +++ b/tests/unit/browser/test_adblock.py @@ -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')