Refactor test_config

This commit is contained in:
Markus Unterwaditzer 2016-05-02 16:41:03 +02:00
parent e4c88ce84b
commit 900ffceca9

View file

@ -3,24 +3,41 @@ from textwrap import dedent
import pytest import pytest
import vdirsyncer.cli.config # noqa
import vdirsyncer.cli.utils # noqa import vdirsyncer.cli.utils # noqa
from vdirsyncer import cli, exceptions from vdirsyncer import cli, exceptions
from vdirsyncer.cli.config import parse_config_value, \
read_config as _read_config
invalid = object()
@pytest.fixture @pytest.fixture
def read_config(tmpdir): def read_config(tmpdir, monkeypatch):
def inner(cfg): def inner(cfg):
errors = []
monkeypatch.setattr('vdirsyncer.cli.cli_logger.error', errors.append)
f = io.StringIO(dedent(cfg.format(base=str(tmpdir)))) f = io.StringIO(dedent(cfg.format(base=str(tmpdir))))
return _read_config(f) rv = vdirsyncer.cli.config.read_config(f)
monkeypatch.undo()
return errors, rv
return inner return inner
def test_read_config(read_config, monkeypatch): @pytest.fixture
errors = [] def parse_config_value(capsys):
monkeypatch.setattr('vdirsyncer.cli.cli_logger.error', errors.append) def inner(s):
general, pairs, storages = read_config(u''' try:
rv = vdirsyncer.cli.config.parse_config_value(s)
except ValueError:
return invalid
else:
warnings = capsys.readouterr()[1]
return rv, len(warnings.splitlines())
return inner
def test_read_config(read_config):
errors, (general, pairs, storages) = read_config(u'''
[general] [general]
status_path = /tmp/status/ status_path = /tmp/status/
@ -60,10 +77,7 @@ def test_read_config(read_config, monkeypatch):
assert 'bogus' in errors[0] assert 'bogus' in errors[0]
def test_missing_collections_param(read_config, monkeypatch): def test_missing_collections_param(read_config):
errorlog = []
monkeypatch.setattr('vdirsyncer.cli.cli_logger.error', errorlog.append)
with pytest.raises(exceptions.UserError) as excinfo: with pytest.raises(exceptions.UserError) as excinfo:
read_config(u''' read_config(u'''
[general] [general]
@ -81,7 +95,6 @@ def test_missing_collections_param(read_config, monkeypatch):
''') ''')
assert 'collections parameter missing' in str(excinfo.value) assert 'collections parameter missing' in str(excinfo.value)
assert not errorlog
def test_storage_instance_from_config(monkeypatch): def test_storage_instance_from_config(monkeypatch):
@ -131,31 +144,20 @@ def test_wrong_general_section(read_config):
] ]
def test_invalid_storage_name(): def test_invalid_storage_name(read_config):
f = io.StringIO(dedent(u''' with pytest.raises(exceptions.UserError) as excinfo:
read_config(u'''
[general] [general]
status_path = {base}/status/ status_path = {base}/status/
[storage foo.bar] [storage foo.bar]
''')) ''')
with pytest.raises(exceptions.UserError) as excinfo:
_read_config(f)
assert 'invalid characters' in str(excinfo.value).lower() assert 'invalid characters' in str(excinfo.value).lower()
def test_parse_config_value(capsys): def test_parse_config_value(parse_config_value):
invalid = object() x = parse_config_value
def x(s):
try:
rv = parse_config_value(s)
except ValueError:
return invalid
else:
warnings = capsys.readouterr()[1]
return rv, len(warnings.splitlines())
assert x('123 # comment!') is invalid assert x('123 # comment!') is invalid
@ -175,8 +177,9 @@ def test_parse_config_value(capsys):
assert x('""') == ('', 0) assert x('""') == ('', 0)
def test_invalid_collections_arg(): def test_invalid_collections_arg(read_config):
f = io.StringIO(dedent(u''' with pytest.raises(exceptions.UserError) as excinfo:
read_config(u'''
[general] [general]
status_path = /tmp/status/ status_path = /tmp/status/
@ -194,9 +197,6 @@ def test_invalid_collections_arg():
type = filesystem type = filesystem
path = /tmp/bar/ path = /tmp/bar/
fileext = .txt fileext = .txt
''')) ''')
with pytest.raises(exceptions.UserError) as excinfo:
_read_config(f)
assert 'Expected string' in str(excinfo.value) assert 'Expected string' in str(excinfo.value)