Repository: pimutils/khal

Issue:
Testsuite fails when test collections actually exist
From https://aur.archlinux.org/packages/khal/#comment-560248

	platform linux -- Python 3.5.2, pytest-2.9.2, py-1.4.31, pluggy-0.3.1
	rootdir: /tmp/yaourt-tmp-nicolas/aur-khal/src/khal-0.8.3, inifile:
	plugins: localserver-0.3.5, hypothesis-3.4.2, subtesthack-0.1.1
	collected 178 items

	tests/aux_test.py .....................
	tests/backend_test.py ..........................
	tests/cal_display_test.py ...xxx
	tests/cli_test.py .....x.............
	tests/controller_test.py ....
	tests/event_test.py ..............................
	tests/khalendar_aux_test.py ................................
	tests/khalendar_test.py ......................
	tests/settings_test.py F.F.....
	tests/terminal_test.py ...
	tests/vtimezone_test.py ...
	tests/ui/test_calendarwidget.py ...
	tests/ui/test_widgets.py .

	============================================ FAILURES =============================================
	_________________________________ TestSettings.test_simple_config _________________________________

	self = <tests.settings_test.TestSettings object at 0x7f6799a26240>

	def test_simple_config(self):
	config = get_config(PATH + 'simple.conf')
	comp_config = {
	'calendars': {
	'home': {'path': os.path.expanduser('~/.calendars/home/'),
	'readonly': False, 'color': None, 'type': 'calendar'},
	'work': {'path': os.path.expanduser('~/.calendars/work/'),
	'readonly': False, 'color': None, 'type': 'calendar'},
	},
	'sqlite': {'path': os.path.expanduser('~/.local/share/khal/khal.db')},
	'locale': {
	'local_timezone': pytz.timezone('Europe/Berlin'),
	'default_timezone': pytz.timezone('Europe/Berlin'),
	'timeformat': '%H:%M',
	'dateformat': '%d.%m.',
	'longdateformat': '%d.%m.%Y',
	'datetimeformat': '%d.%m. %H:%M',
	'longdatetimeformat': '%d.%m.%Y %H:%M',
	'firstweekday': 0,
	'encoding': 'utf-8',
	'unicode_symbols': True,
	'weeknumbers': False,
	},
	'default': {
	'default_command': 'calendar',
	'default_calendar': None,
	'show_all_days': False,
	'print_new': 'False',
	'days': 2,
	'highlight_event_days': False
	}
	}
	for key in comp_config:
	> assert config[key] == comp_config[key]
	E assert {'home': {'pa...: 'calendar'}} == {'home': {'col...: 'calendar'}}
	E Differing items:
	E {'work': {'path': '/home/nicolas/.calendars/work/', 'color': '#E6C800FF', 'readonly': False, 'type': 'calendar'}} != {'work': {'color': None, 'path': '/home/nicolas/.calendars/work/', 'readonly': False, 'type': 'calendar'}}
	E {'home': {'path': '/home/nicolas/.calendars/home/', 'color': '#882F00FF', 'readonly': False, 'type': 'calendar'}} != {'home': {'color': None, 'path': '/home/nicolas/.calendars/home/', 'readonly': False, 'type': 'calendar'}}
	E Use -v to get the full diff

	tests/settings_test.py:50: AssertionError
	_____________________________________ TestSettings.test_small _____________________________________

	self = <tests.settings_test.TestSettings object at 0x7f6799a6cb00>

	def test_small(self):
	config = get_config(PATH + 'small.conf')
	comp_config = {
	'calendars': {
	'home': {'path': os.path.expanduser('~/.calendars/home/'),
	'color': 'dark green', 'readonly': False,
	'type': 'calendar'},
	'work': {'path': os.path.expanduser('~/.calendars/work/'),
	'readonly': True, 'color': None,
	'type': 'calendar'}},
	'sqlite': {'path': os.path.expanduser('~/.local/share/khal/khal.db')},
	'locale': {
	'local_timezone': get_localzone(),
	'default_timezone': get_localzone(),
	'timeformat': '%H:%M',
	'dateformat': '%d.%m.',
	'longdateformat': '%d.%m.%Y',
	'datetimeformat': '%d.%m. %H:%M',
	'longdatetimeformat': '%d.%m.%Y %H:%M',
	'firstweekday': 0,
	'encoding': 'utf-8',
	'unicode_symbols': True,
	'weeknumbers': False,
	},
	'default': {
	'default_calendar': None,
	'default_command': 'calendar',
	'print_new': 'False',
	'show_all_days': False,
	'days': 2,
	'highlight_event_days': False
	}
	}
	for key in comp_config:
	> assert config[key] == comp_config[key]
	E assert {'home': {'pa...: 'calendar'}} == {'home': {'col...: 'calendar'}}
	E Omitting 1 identical items, use -v to show
	E Differing items:
	E {'work': {'path': '/home/nicolas/.calendars/work/', 'readonly': True, 'color': '#E6C800FF', 'type': 'calendar'}} != {'work': {'color': None, 'path': '/home/nicolas/.calendars/work/', 'readonly': True, 'type': 'calendar'}}
	E Use -v to get the full diff

	tests/settings_test.py:90: AssertionError
	========================= 2 failed, 172 passed, 4 xfailed in 5.65 seconds =========================
	==> ERROR: A failure occurred in check().
	Aborting...
	==> ERROR: Makepkg was unable to build khal.
	==> Restart building khal ? [y/N]


Discussion:

Patch:
diff --git a/khal/settings/settings.py b/khal/settings/settings.py
index b9fde9d..254e424 100644
--- a/khal/settings/settings.py
+++ b/khal/settings/settings.py
@@ -31,7 +31,7 @@ from .exceptions import InvalidSettingsError, CannotParseConfigFileError, NoConf
 from khal import __productname__
 from ..log import logger
 from .utils import is_timezone, weeknumber_option, config_checks, \
-    expand_path, expand_db_path, is_color
+    expand_path, expand_db_path, is_color, get_vdir_type, get_color_from_vdir
 
 SPECPATH = os.path.join(os.path.dirname(__file__), 'khal.spec')
 
@@ -66,12 +66,17 @@ def find_configuration_file():
     return None
 
 
-def get_config(config_path=None):
+def get_config(
+        config_path=None,
+        _get_color_from_vdir=get_color_from_vdir,
+        _get_vdir_type=get_vdir_type):
     """reads the config file, validates it and return a config dict
 
     :param config_path: path to a custom config file, if none is given the
                         default locations will be searched
     :type config_path: str
+    :param _get_color_from_vdir: override get_color_from_vdir for testing purposes
+    :param _get_vdir_type: override get_vdir_type for testing purposes
     :returns: configuration
     :rtype: dict
     """
@@ -124,7 +129,7 @@ def get_config(config_path=None):
     if abort or not results:
         raise InvalidSettingsError()
 
-    config_checks(user_config)
+    config_checks(user_config, _get_color_from_vdir, _get_vdir_type)
 
     extras = get_extra_values(user_config)
     for section, value in extras:
diff --git a/khal/settings/utils.py b/khal/settings/utils.py
index d7a714c..0470ea4 100644
--- a/khal/settings/utils.py
+++ b/khal/settings/utils.py
@@ -152,7 +152,10 @@ def get_vdir_type(_):
     return 'calendar'
 
 
-def config_checks(config):
+def config_checks(
+        config,
+        _get_color_from_vdir=get_color_from_vdir,
+        _get_vdir_type=get_vdir_type):
     """do some tests on the config we cannot do with configobj's validator"""
     if len(config['calendars'].keys()) < 1:
         logger.fatal('Found no calendar section in the config file')
@@ -173,8 +176,8 @@ def config_checks(config):
             config['calendars'].pop(calendar)
     for vdir in sorted(vdirs):
         calendar = {'path': vdir,
-                    'color': get_color_from_vdir(vdir),
-                    'type': get_vdir_type(vdir),
+                    'color': _get_color_from_vdir(vdir),
+                    'type': _get_vdir_type(vdir),
                     'readonly': False
                     }
         name = get_unique_name(vdir, config['calendars'].keys())
@@ -186,4 +189,4 @@ def config_checks(config):
             config['calendars'][calendar]['readonly'] = True
         if config['calendars'][calendar]['color'] == 'auto':
             config['calendars'][calendar]['color'] = \
-                get_color_from_vdir(config['calendars'][calendar]['path'])
+                _get_color_from_vdir(config['calendars'][calendar]['path'])


Test patch:
diff --git a/tests/settings_test.py b/tests/settings_test.py
index 9b18bfc..51156b6 100644
--- a/tests/settings_test.py
+++ b/tests/settings_test.py
@@ -15,7 +15,11 @@ PATH = __file__.rsplit('/', 1)[0] + '/configs/'
 
 class TestSettings(object):
     def test_simple_config(self):
-        config = get_config(PATH + 'simple.conf')
+        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/'),
@@ -53,7 +57,11 @@ class TestSettings(object):
             get_config(PATH + 'nocalendars.conf')
 
     def test_small(self):
-        config = get_config(PATH + 'small.conf')
+        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/'),