Skip to content

Commit

Permalink
tests for config parser
Browse files Browse the repository at this point in the history
  • Loading branch information
geier committed May 3, 2014
1 parent a414202 commit 2f4eec9
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ python:
- 2.7
# command to install dependencies, e.g. pip install -r requirements.txt --use-mirrors
install:
- pip install -r requirements.txt --use-mirrors
- pip install -r requirements.txt
- pip install .
- pip install pytest-capturelog
# command to run tests, e.g. python setup.py test
script: py.test tests/
12 changes: 4 additions & 8 deletions khal/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,6 @@ def __delattr__(self, name):

class Section(object):

READERS = {bool: SafeConfigParser.getboolean,
float: SafeConfigParser.getfloat,
int: SafeConfigParser.getint,
str: SafeConfigParser.get}

def __init__(self, parser, group):
self._parser = parser
self._group = group
Expand Down Expand Up @@ -224,7 +219,7 @@ def __init__(self, parser):
Section.__init__(self, parser, 'calendars')
self._schema = [
('path', None, os.path.expanduser),
('readonly', False, None),
('readonly', False, self._parse_bool_string),
('color', '', None)
]

Expand Down Expand Up @@ -258,7 +253,7 @@ def __init__(self, parser):
('longdateformat', None, None),
('datetimeformat', None, None),
('longdatetimeformat', None, None),
('firstweekday', 0, lambda x: x),
('firstweekday', 0, int),
('encoding', 'utf-8', None),
('unicode_symbols', True, self._parse_bool_string),
]
Expand All @@ -268,7 +263,7 @@ class DefaultSection(Section):
def __init__(self, parser):
Section.__init__(self, parser, 'default')
self._schema = [
('debug', False, None),
('debug', False, self._parse_bool_string),
('default_command', 'calendar', self._parse_commands),
]

Expand Down Expand Up @@ -464,6 +459,7 @@ def main_khal():
logger.setLevel(logging.DEBUG)

conf = ConfigParser().parse_config(arguments['-c'])

if conf is None:
sys.exit('Invalid config file, exiting.')

Expand Down
122 changes: 122 additions & 0 deletions tests/cli_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import pytz

from khal.cli import ConfigParser
from khal.cli import Namespace

str_calendars_good = """
[Calendar home]
path = /home/user/somewhere
color = dark blue
[Calendar work]
path = /home/user/somewhereelse
color = dark green
readonly = 0
[Calendar workagain]
path = /home/user/here
readonly = True
"""

str_sqlite_good = """
[sqlite]
path = /home/user/.khal/khal.db
"""

str_locale_good = """
[locale]
local_timezone: Europe/Berlin
default_timezone: America/New_York
timeformat: %H:%M
dateformat: %d.%m.
longdateformat: %d.%m.%Y
datetimeformat: %d.%m. %H:%M
longdatetimeformat: %d.%m.%Y %H:%M
firstweekday: 0
"""

str_default_good = """
[default]
default_command: calendar
debug: 0
"""


goodlocale = Namespace(
{'dateformat': '%d.%m.',
'local_timezone': pytz.timezone('Europe/Berlin'),
'unicode_symbols': True,
'longdateformat': '%d.%m.%Y',
'longdatetimeformat': '%d.%m.%Y %H:%M',
'default_timezone': pytz.timezone('America/New_York'),
'encoding': 'utf-8',
'timeformat': '%H:%M',
'datetimeformat': '%d.%m. %H:%M',
'firstweekday': 0
}
)

gooddefault = Namespace(
{'default_command': 'calendar',
'debug': 0
}
)

goodsqlite = Namespace(
{'path': '/home/user/.khal/khal.db'
}
)

goodcalendars = [
Namespace({
'name': 'home',
'path': '/home/user/somewhere',
'color': 'dark blue',
'readonly': False
}),
Namespace({
'name': 'work',
'path': '/home/user/somewhereelse',
'color': 'dark green',
'readonly': False
}),

Namespace({
'name': 'workagain',
'path': '/home/user/here',
'color': '',
'readonly': True
})
]


class TestConfigParser(object):
def test_easy(self, tmpdir):
goodconf = Namespace(
{'locale': goodlocale,
'sqlite': goodsqlite,
'default': gooddefault,
'calendars': goodcalendars
}
)

basic_config = (str_calendars_good +
str_locale_good +
str_sqlite_good +
str_default_good)
tmpdir.join('config').write(basic_config)
conf_parser = ConfigParser()
config = conf_parser.parse_config(str(tmpdir) + '/config')
assert config == goodconf

def test_no_cal(self, tmpdir, caplog):
no_cal_config = (str_locale_good +
str_sqlite_good +
str_default_good)
tmpdir.join('config').write(no_cal_config)
conf_parser = ConfigParser()
config = conf_parser.parse_config(str(tmpdir) + '/config')
assert "Missing required section 'calendars'" in caplog.text()
assert config is None
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ deps =
urwid
pyxdg
vdirsyncer
pytest-capturelog
commands =
py.test {posargs}

0 comments on commit 2f4eec9

Please sign in to comment.