From a1f2d14c05980d16d7eb4d75e898b899043f9653 Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Mon, 15 Dec 2014 20:36:02 +0100 Subject: [PATCH] Refactor parse_config to use fileobject --- tests/test_cli.py | 21 +++++++++------------ vdirsyncer/cli.py | 8 ++++---- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index 3b0deaa..8475937 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -6,6 +6,7 @@ :copyright: (c) 2014 Markus Unterwaditzer & contributors :license: MIT, see LICENSE for more details. ''' +import io from textwrap import dedent from click.testing import CliRunner @@ -13,13 +14,10 @@ from click.testing import CliRunner import vdirsyncer.cli as cli -def test_load_config(tmpdir, monkeypatch): - f = tmpdir.join('test.cfg') - status_path = '{}/status/'.format(str(tmpdir)) - contacts_path = '{}/contacts/'.format(str(tmpdir)) - f.write(dedent(''' +def test_load_config(monkeypatch): + f = io.StringIO(dedent(u''' [general] - status_path = {status} + status_path = /tmp/status/ [pair bob] a = bob_a @@ -29,7 +27,7 @@ def test_load_config(tmpdir, monkeypatch): [storage bob_a] type = filesystem - path = {contacts} + path = /tmp/contacts/ fileext = .vcf yesno = off number = 42 @@ -39,16 +37,15 @@ def test_load_config(tmpdir, monkeypatch): [bogus] lol = true - ''').strip().format(status=status_path, contacts=contacts_path)) + ''')) - fname = str(tmpdir) + '/test.cfg' errors = [] monkeypatch.setattr('vdirsyncer.cli.cli_logger.error', errors.append) - general, pairs, storages = cli.load_config(fname, pair_options=('bam',)) - assert general == {'status_path': status_path} + general, pairs, storages = cli.load_config(f, pair_options=('bam',)) + assert general == {'status_path': '/tmp/status/'} assert pairs == {'bob': ('bob_a', 'bob_b', {'bam': True}, {'foo': 'bar'})} assert storages == { - 'bob_a': {'type': 'filesystem', 'path': contacts_path, 'fileext': + 'bob_a': {'type': 'filesystem', 'path': '/tmp/contacts/', 'fileext': '.vcf', 'yesno': False, 'number': 42, 'instance_name': 'bob_a'}, 'bob_b': {'type': 'carddav', 'instance_name': 'bob_b'} diff --git a/vdirsyncer/cli.py b/vdirsyncer/cli.py index e1f9843..b3e5b5e 100644 --- a/vdirsyncer/cli.py +++ b/vdirsyncer/cli.py @@ -75,10 +75,9 @@ def validate_general_section(general_config): raise CliError('Invalid general section.') -def load_config(fname, pair_options=('collections', 'conflict_resolution')): +def load_config(f, pair_options=('collections', 'conflict_resolution')): c = RawConfigParser() - with open(fname) as f: - c.readfp(f) + c.readfp(f) get_options = lambda s: dict(parse_options(c.items(s), section=s)) @@ -283,7 +282,8 @@ def _create_app(): expand_path('~/.config/')) fname = os.path.join(xdg_config_dir, 'vdirsyncer/config') try: - ctx.obj['config'] = load_config(fname) + with open(fname) as f: + ctx.obj['config'] = load_config(f) except Exception as e: raise CliError('Error during reading config {}: {}' .format(fname, e))