Skip to content

Commit

Permalink
Added implementation and tests for configuration loading of console
Browse files Browse the repository at this point in the history
  • Loading branch information
prashnts committed Sep 17, 2016
1 parent f7c6939 commit 4d19ea0
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 5 deletions.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ include README.md
include requirements.txt
include CHANGELOG.md
include LICENSE
include hues/.hues.yml
5 changes: 5 additions & 0 deletions hues/.hues.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
colors:
default: white
themes:
powerline:
right_sep:
53 changes: 50 additions & 3 deletions hues/console.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,54 @@
# Unicorns
'''Helper module for all the goodness.'''
import os
import sys
import yaml

from .huestr import Hues

class Console(object):
def __init__(self):
pass
CONFIG_FNAME = '.hues.yml'


class InvalidConfiguration(Exception):
'''Raise when configuration is invalid.'''


class _Console(object):
def __init__(self, stdout=sys.stdout, stderr=sys.stderr):
self.stdout = stdout
self.stderr = stderr
self.config = self._load_config()

@staticmethod
def _load_config():
'''Find and load configuration params.
Config files are loaded in the following order:
- Beginning from current working dir, all the way to the root.
- User home (~).
- Module dir (defaults).
'''
def _load(cdir, recurse=False):
confl = os.path.join(cdir, CONFIG_FNAME)
try:
with open(confl, 'r') as fp:
conf = yaml.safe_load(fp)
if type(conf) is not dict:
raise InvalidConfiguration('Configuration at %s is not a dictionary.' % confl)
return conf
except OSError:
parent = os.path.dirname(cdir)
if recurse and parent != cdir:
return _load(parent, recurse=True)
else:
return dict()
except yaml.YAMLError:
raise InvalidConfiguration('Configuration at %s is an invalid YAML file.' % confl)

conf = _load(os.path.dirname(__file__))

home_conf = _load(os.path.expanduser('~'))
local_conf = _load(os.path.abspath(os.curdir), recurse=True)

conf.update(home_conf)
conf.update(local_conf)
return conf
13 changes: 13 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
codeclimate-test-reporter==0.1.2
coverage==4.2
extras==1.0.0
fixtures==3.0.0
linecache2==1.0.0
mox3==0.18.0
pbr==1.10.0
py==1.4.31
pyfakefs==2.7
pytest==3.0.2
pytest-cov==2.3.1
pytest-runner==2.9
python-mimeparse==1.5.2
PyYAML==3.12
requests==2.11.1
six==1.10.0
testtools==2.2.0
traceback2==1.4.0
unittest2==1.1.0
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
vtp = re.search(rex, fp.read(), re.M).groups()
__version__ = '.'.join(vtp)

install_requires = []
install_requires = ['PyYAML',]
setup_requires = ['pytest-runner',]
test_requirements = ['pytest', 'coverage',]
test_requirements = ['pytest', 'coverage', 'pyfakefs']


setup(
Expand Down
61 changes: 61 additions & 0 deletions tests/test_console.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import os
import pyfakefs.fake_filesystem_unittest as fake_fs_unittest

CONFIG_FNAME = '.hues.yml'
usr_conf = os.path.join(os.path.expanduser('~'), CONFIG_FNAME)
mod_conf = os.path.join(os.path.dirname(__file__), '..', 'hues', CONFIG_FNAME)

with open(mod_conf, 'r') as fp:
default_conf = fp.read()

home_conf = '''
colors:
default: red
'''
local_conf = '''
colors:
default: green
'''
invalid_conf = '''Invalid Conf, but valid YAML.'''
invalid_yaml = '''Nested: Dicts: Are: Invalid!'''

from hues.console import _Console, InvalidConfiguration


class Test_Console(fake_fs_unittest.TestCase):
def setUp(self):
self.setUpPyfakefs()
self.fs.CreateFile(mod_conf, contents=default_conf)
self.fs.CreateFile(usr_conf, contents=home_conf)
self.fs.CreateFile('/var/foo/.hues.yml', contents=local_conf)
self.fs.CreateFile('/var/invalid/.hues.yml', contents=invalid_conf)
self.fs.CreateFile('/var/invalidyml/.hues.yml', contents=invalid_yaml)
self.fs.CreateDirectory('/var/foo/bar/baz')
self.fs.CreateDirectory('/var/doom/baz')

def test_home_config(self):
os.chdir('/var/doom/baz')
cs = _Console()
assert cs.config['colors']['default'] == 'red'

def test_local_config(self):
os.chdir('/var/foo')
cs = _Console()
assert cs.config['colors']['default'] == 'green'

def test_local_nested_config(self):
os.chdir('/var/foo/bar/baz')
cs = _Console()
assert cs.config['colors']['default'] == 'green'

def test_invalid_config(self):
os.chdir('/var/invalid')
with self.assertRaises(InvalidConfiguration) as e:
cs = _Console()
assert 'not a dictionary' in str(e.exception)

def test_invalid_yaml(self):
os.chdir('/var/invalidyml')
with self.assertRaises(InvalidConfiguration) as e:
cs = _Console()
assert 'invalid YAML' in str(e.exception)

0 comments on commit 4d19ea0

Please sign in to comment.