Permalink
Browse files

Added a method to find a config file.

  • Loading branch information...
1 parent 1113141 commit 630914288ae71202d465cf4ed4bb785f10a17037 @SeanOC committed Feb 20, 2011
Showing with 60 additions and 3 deletions.
  1. +26 −0 humus/core.py
  2. +2 −0 humus/exceptions.py
  3. +32 −3 tests/core_tests.py
View
@@ -1,6 +1,32 @@
+import os
+
+from humus.exceptions import ConfigError
class Syncer(object):
+ config_paths = [
+ './humus.ini',
+ '~/humus.ini',
+ '/etc/humus.ini',
+ '/etc/humus/humus.ini',
+ ]
+
def __init__(self):
super(Syncer, self).__init__()
+ def get_config_path(self):
+ '''
+ Find the config file for humus.
+ '''
+ found_path = None
+
+ for path in self.config_paths:
+ full_path = os.path.abspath(path)
+ if os.path.exists(full_path):
+ found_path = full_path
+ break
+
+ if found_path is None:
+ raise ConfigError('No configuration file could be found (tried %s).' % u', '.join(self.config_paths))
+
+ return full_path
View
@@ -0,0 +1,2 @@
+class ConfigError(Exception):
+ pass
View
@@ -1,9 +1,38 @@
from unittest import TestCase
+from mock import Mock, patch
+from nose.tools import raises
+
from humus.core import Syncer
+from humus.exceptions import ConfigError
-class CoreTests(TestCase):
- def test_basic(self):
+def get_exists_mock(expected_path):
+ mock = Mock()
+ mock.side_effect = lambda path: path == expected_path
+
+ return mock
+
+etc_dir_exists_mock = get_exists_mock('/etc/humus/humus.ini')
+
+class CoreTests(TestCase):
+ def get_syncer(self):
syncer = Syncer()
- assert True
+ return syncer
+
+ def test_init(self):
+ syncer = self.get_syncer()
+
+ @raises(ConfigError)
+ def test_find_config_missing(self):
+ syncer = self.get_syncer()
+ config_path = syncer.get_config_path()
+
+ @patch('os.path.exists', etc_dir_exists_mock)
+ def test_find_config_etc_dir(self, *args, **kwargs):
+ syncer = self.get_syncer()
+ expected = '/etc/humus/humus.ini'
+ result = syncer.get_config_path()
+
+ self.assertEquals(expected, result)
+

0 comments on commit 6309142

Please sign in to comment.