forked from pimutils/khal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings_test.py
304 lines (275 loc) · 9.68 KB
/
settings_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import datetime as dt
import os.path
import pytest
from tzlocal import get_localzone as _get_localzone
from khal.settings import get_config
from khal.settings.exceptions import CannotParseConfigFileError, InvalidSettingsError
from khal.settings.utils import (
config_checks,
get_all_vdirs,
get_color_from_vdir,
get_unique_name,
is_color,
)
try:
# Available from configobj 5.1.0
from configobj.validate import VdtValueError
except ModuleNotFoundError:
from validate import VdtValueError
from .utils import LOCALE_BERLIN
PATH = __file__.rsplit('/', 1)[0] + '/configs/'
def get_localzone():
# this reproduces the code in settings.util for the time being
import pytz
return pytz.timezone(str(_get_localzone()))
class TestSettings:
def test_simple_config(self):
config = get_config(
PATH + 'simple.conf',
_get_color_from_vdir=lambda x: None,
_get_vdir_type=lambda x: 'calendar',
)
comp_config = {
'calendars': {
'home': {'path': os.path.expanduser('~/.calendars/home/'),
'readonly': False, 'color': None, 'priority': 10, 'type': 'calendar'},
'work': {'path': os.path.expanduser('~/.calendars/work/'),
'readonly': False, 'color': None, 'priority': 10, 'type': 'calendar'},
},
'sqlite': {'path': os.path.expanduser('~/.local/share/khal/khal.db')},
'locale': LOCALE_BERLIN,
'default': {
'default_calendar': None,
'print_new': 'False',
'highlight_event_days': False,
'timedelta': dt.timedelta(days=2),
'default_event_duration': dt.timedelta(days=1),
'default_dayevent_duration': dt.timedelta(hours=1),
'show_all_days': False,
}
}
for key in comp_config:
assert config[key] == comp_config[key]
def test_nocalendars(self):
with pytest.raises(InvalidSettingsError):
get_config(PATH + 'nocalendars.conf')
def test_one_level_calendar(self):
with pytest.raises(InvalidSettingsError):
get_config(PATH + 'one_level_calendars.conf')
def test_small(self):
config = get_config(
PATH + 'small.conf',
_get_color_from_vdir=lambda x: None,
_get_vdir_type=lambda x: 'calendar',
)
comp_config = {
'calendars': {
'home': {'path': os.path.expanduser('~/.calendars/home/'),
'color': 'dark green', 'readonly': False, 'priority': 20,
'type': 'calendar'},
'work': {'path': os.path.expanduser('~/.calendars/work/'),
'readonly': True, 'color': None, 'priority': 10,
'type': 'calendar'}},
'sqlite': {'path': os.path.expanduser('~/.local/share/khal/khal.db')},
'locale': {
'local_timezone': get_localzone(),
'default_timezone': get_localzone(),
'timeformat': '%X',
'dateformat': '%x',
'longdateformat': '%x',
'datetimeformat': '%c',
'longdatetimeformat': '%c',
'firstweekday': 0,
'unicode_symbols': True,
'weeknumbers': False,
},
'default': {
'default_calendar': None,
'print_new': 'False',
'highlight_event_days': False,
'timedelta': dt.timedelta(days=2),
'default_event_duration': dt.timedelta(days=1),
'default_dayevent_duration': dt.timedelta(hours=1),
'show_all_days': False
}
}
for key in comp_config:
assert config[key] == comp_config[key]
def test_old_config(self, tmpdir):
old_config = """
[Calendar home]
path: ~/.khal/calendars/home/
color: dark blue
[sqlite]
path: ~/.khal/khal.db
[locale]
timeformat: %H:%M
dateformat: %d.%m.
longdateformat: %d.%m.%Y
[default]
"""
conf_path = str(tmpdir.join('old.conf'))
with open(conf_path, 'w+') as conf:
conf.write(old_config)
with pytest.raises(CannotParseConfigFileError):
get_config(conf_path)
def test_extra_sections(self, tmpdir):
config = """
[calendars]
[[home]]
path = ~/.khal/calendars/home/
color = dark blue
unknown = 42
[unknownsection]
foo = bar
"""
conf_path = str(tmpdir.join('old.conf'))
with open(conf_path, 'w+') as conf:
conf.write(config)
get_config(conf_path)
# FIXME test for log entries
def test_default_calendar_readonly(self, tmpdir):
config = """
[calendars]
[[home]]
path = ~/.khal/calendars/home/
color = dark blue
readonly = True
[default]
default_calendar = home
"""
conf_path = str(tmpdir.join('old.conf'))
with open(conf_path, 'w+') as conf:
conf.write(config)
with pytest.raises(InvalidSettingsError):
config_checks(get_config(conf_path))
def test_broken_color(metavdirs):
path = metavdirs
newvdir = path + '/cal5/'
os.makedirs(newvdir)
with open(newvdir + 'color', 'w') as metafile:
metafile.write('xxx')
assert get_color_from_vdir(newvdir) is None
def test_discover(metavdirs):
test_vdirs = {
'/cal1/public', '/cal1/private', '/cal2/public',
'/cal3/home', '/cal3/public', '/cal3/work',
'/cal4/cfgcolor', '/cal4/dircolor', '/cal4/cfgcolor_again',
'/cal4/cfgcolor_once_more',
'/singlecollection',
}
path = metavdirs
assert test_vdirs == {vdir[len(path):] for vdir in get_all_vdirs(path + '/**/*/')}
assert test_vdirs == {vdir[len(path):] for vdir in get_all_vdirs(path + '/**/')}
assert test_vdirs == {vdir[len(path):] for vdir in get_all_vdirs(path + '/**/*')}
def test_get_unique_name(metavdirs):
path = metavdirs
vdirs = list(get_all_vdirs(path + '/**/'))
names = []
for vdir in sorted(vdirs):
names.append(get_unique_name(vdir, names))
assert sorted(names) == sorted([
'my private calendar', 'my calendar', 'public', 'home', 'public1',
'work', 'cfgcolor', 'cfgcolor_again', 'cfgcolor_once_more', 'dircolor',
'singlecollection',
])
def test_config_checks(metavdirs):
path = metavdirs
config = {
'calendars': {
'default': {'path': path + '/cal[1-3]/*', 'type': 'discover'},
'calendars_color': {'path': path + '/cal4/*', 'type': 'discover', 'color': 'dark blue'},
},
'sqlite': {'path': '/tmp'},
'locale': {'default_timezone': 'Europe/Berlin', 'local_timezone': 'Europe/Berlin'},
'default': {'default_calendar': None},
}
config_checks(config)
# cut off the part of the path that changes on each run
for cal in config['calendars']:
config['calendars'][cal]['path'] = config['calendars'][cal]['path'][len(metavdirs):]
test_config = {
'calendars': {
'home': {
'color': None,
'path': '/cal3/home',
'readonly': False,
'type': 'calendar',
'priority': 10,
},
'my calendar': {
'color': 'dark blue',
'path': '/cal1/public',
'readonly': False,
'type': 'calendar',
'priority': 10,
},
'my private calendar': {
'color': '#FF00FF',
'path': '/cal1/private',
'readonly': False,
'type': 'calendar',
'priority': 10,
},
'public1': {
'color': None,
'path': '/cal3/public',
'readonly': False,
'type': 'calendar',
'priority': 10,
},
'public': {
'color': None,
'path': '/cal2/public',
'readonly': False,
'type': 'calendar',
'priority': 10,
},
'work': {
'color': None,
'path': '/cal3/work',
'readonly': False,
'type': 'calendar',
'priority': 10,
},
'cfgcolor': {
'color': 'dark blue',
'path': '/cal4/cfgcolor',
'readonly': False,
'type': 'calendar',
'priority': 10,
},
'dircolor': {
'color': 'dark blue',
'path': '/cal4/dircolor',
'readonly': False,
'type': 'calendar',
'priority': 10,
},
'cfgcolor_again': {
'color': 'dark blue',
'path': '/cal4/cfgcolor_again',
'readonly': False,
'type': 'calendar',
'priority': 10,
},
'cfgcolor_once_more': {
'color': 'dark blue',
'path': '/cal4/cfgcolor_once_more',
'readonly': False,
'type': 'calendar',
'priority': 10,
},
},
'default': {'default_calendar': None},
'locale': {'default_timezone': 'Europe/Berlin', 'local_timezone': 'Europe/Berlin'},
'sqlite': {'path': '/tmp'},
}
assert config['calendars'] == test_config['calendars']
assert config == test_config
def test_is_color():
assert is_color('dark blue') == 'dark blue'
assert is_color('#123456') == '#123456'
assert is_color('123') == '123'
with pytest.raises(VdtValueError):
assert is_color('red') == 'red'