tests: Add a fake_args fixture

This commit is contained in:
Florian Bruhin 2016-07-08 19:30:17 +02:00
parent 949172809d
commit 2136d00aa2
6 changed files with 36 additions and 29 deletions

View File

@ -29,6 +29,7 @@ import collections
import itertools
import textwrap
import unittest.mock
import types
import pytest
@ -383,3 +384,11 @@ def fake_save_manager():
objreg.register('save-manager', fake_save_manager)
yield fake_save_manager
objreg.delete('save-manager')
@pytest.yield_fixture
def fake_args():
ns = types.SimpleNamespace()
objreg.register('args', ns)
yield ns
objreg.delete('args')

View File

@ -69,13 +69,10 @@ class BaseDirStub:
self.basedir = None
@pytest.yield_fixture
def basedir():
@pytest.fixture
def basedir(fake_args):
"""Register a Fake basedir."""
args = BaseDirStub()
objreg.register('args', args)
yield
objreg.delete('args')
fake_args.basedir = None
class FakeDownloadItem(QObject):

View File

@ -22,7 +22,8 @@
import pytest
from qutebrowser.commands import cmdutils, cmdexc, argparser, command
from qutebrowser.utils import usertypes, typing
from qutebrowser.utils import usertypes, typing, objreg
from qutebrowser.browser import tab as tabmod
@pytest.fixture(autouse=True)

View File

@ -21,7 +21,6 @@
import os
import os.path
import configparser
import types
import argparse
import collections
import shutil
@ -339,14 +338,18 @@ class TestConfigInit:
"""Test initializing of the config."""
@pytest.yield_fixture(autouse=True)
def patch(self):
def patch(self, fake_args):
objreg.register('app', QObject())
objreg.register('save-manager', mock.MagicMock())
args = argparse.Namespace(relaxed_config=False)
objreg.register('args', args)
fake_args.relaxed_config = False
old_standarddir_args = standarddir._args
yield
objreg.global_registry.clear()
objreg.delete('app')
objreg.delete('save-manager')
# registered by config.init()
objreg.delete('config')
objreg.delete('key-config')
objreg.delete('state-config')
standarddir._args = old_standarddir_args
@pytest.fixture
@ -361,12 +364,14 @@ class TestConfigInit:
}
return env
def test_config_none(self, monkeypatch, env):
def test_config_none(self, monkeypatch, env, fake_args):
"""Test initializing with config path set to None."""
args = types.SimpleNamespace(confdir='', datadir='', cachedir='',
basedir=None)
fake_args.confdir = ''
fake_args.datadir = ''
fake_args.cachedir = ''
fake_args.basedir = None
for k, v in env.items():
monkeypatch.setenv(k, v)
standarddir.init(args)
standarddir.init(fake_args)
config.init()
assert not os.listdir(env['XDG_CONFIG_HOME'])

View File

@ -42,9 +42,6 @@ from qutebrowser.utils import objreg, qtutils
from helpers import stubs
Args = collections.namedtuple('Args', 'basedir')
pytestmark = pytest.mark.usefixtures('qapp')

View File

@ -19,7 +19,6 @@
"""Tests for qutebrowser.utils.error."""
import sys
import collections
import logging
import pytest
@ -31,9 +30,6 @@ from PyQt5.QtCore import pyqtSlot, QTimer
from PyQt5.QtWidgets import QMessageBox
Args = collections.namedtuple('Args', 'no_err_windows')
class Error(Exception):
pass
@ -47,14 +43,15 @@ class Error(Exception):
(ipc.Error, 'misc.ipc.Error', 'none'),
(Error, 'test_error.Error', 'none'),
])
def test_no_err_windows(caplog, exc, name, exc_text):
def test_no_err_windows(caplog, exc, name, exc_text, fake_args):
"""Test handle_fatal_exc with no_err_windows = True."""
fake_args.no_err_windows = True
try:
raise exc
except Exception as e:
with caplog.at_level(logging.ERROR):
error.handle_fatal_exc(e, Args(no_err_windows=True), 'title',
pre_text='pre', post_text='post')
error.handle_fatal_exc(e, fake_args, 'title', pre_text='pre',
post_text='post')
assert len(caplog.records) == 1
@ -82,7 +79,7 @@ def test_no_err_windows(caplog, exc, name, exc_text):
('foo', 'bar', 'foo: exception\n\nbar'),
('', 'bar', 'exception\n\nbar'),
], ids=repr)
def test_err_windows(qtbot, qapp, pre_text, post_text, expected):
def test_err_windows(qtbot, qapp, fake_args, pre_text, post_text, expected):
@pyqtSlot()
def err_window_check():
@ -97,6 +94,7 @@ def test_err_windows(qtbot, qapp, pre_text, post_text, expected):
finally:
w.close()
fake_args.no_err_windows = False
QTimer.singleShot(0, err_window_check)
error.handle_fatal_exc(ValueError("exception"), Args(no_err_windows=False),
'title', pre_text=pre_text, post_text=post_text)
error.handle_fatal_exc(ValueError("exception"), fake_args, 'title',
pre_text=pre_text, post_text=post_text)