Skip to content

Commit

Permalink
Merge pull request #1066 from dmitriy-serdyuk/empty-config
Browse files Browse the repository at this point in the history
Allow empty config file
  • Loading branch information
rizar committed Apr 25, 2016
2 parents a79b336 + 1d2a1e8 commit 122a786
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
3 changes: 2 additions & 1 deletion blocks/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ def load_yaml(self):
yaml_file = os.path.expanduser('~/.blocksrc')
if os.path.isfile(yaml_file) and os.path.getsize(yaml_file):
with open(yaml_file) as f:
for key, value in yaml.safe_load(f).items():
config_dict = yaml.safe_load(f) or {}
for key, value in config_dict.items():
if key not in self.config:
raise ValueError("Unrecognized config in YAML: {}"
.format(key))
Expand Down
48 changes: 29 additions & 19 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,32 @@
from blocks.config import Configuration, ConfigurationError


def test_config():
_environ = dict(os.environ)
try:

with tempfile.NamedTemporaryFile(mode='w', delete=False) as f:
f.write('data_path: yaml_path')
filename = f.name
os.environ['BLOCKS_CONFIG'] = filename
if 'BLOCKS_DATA_PATH' in os.environ:
del os.environ['BLOCKS_DATA_PATH']
config = Configuration()
config.add_config('data_path', str, env_var='BLOCKS_DATA_PATH')
config.add_config('config_with_default', int, default='1',
env_var='BLOCKS_CONFIG_TEST')
config.add_config('config_without_default', str)
config.load_yaml()
def load_config(contents):
with tempfile.NamedTemporaryFile(mode='w', delete=False) as f:
f.write(contents)
filename = f.name
os.environ['BLOCKS_CONFIG'] = filename
if 'BLOCKS_DATA_PATH' in os.environ:
del os.environ['BLOCKS_DATA_PATH']
config = Configuration()
config.add_config('data_path', str, env_var='BLOCKS_DATA_PATH')
config.add_config('config_with_default', int, default='1',
env_var='BLOCKS_CONFIG_TEST')
config.add_config('config_without_default', str)
config.load_yaml()
return config


class TestConfig(object):
def setUp(self):
self._environ = dict(os.environ)

def tearDown(self):
os.environ.clear()
os.environ.update(self._environ)

def test_config(self):
config = load_config('data_path: yaml_path')
assert config.data_path == 'yaml_path'
os.environ['BLOCKS_DATA_PATH'] = 'env_path'
assert config.data_path == 'env_path'
Expand All @@ -36,6 +46,6 @@ def test_config():
assert config.data_path == 'manual_path'
config.new_config = 'new_config'
assert config.new_config == 'new_config'
finally:
os.environ.clear()
os.environ.update(_environ)

def test_empty_config(self):
load_config('')

0 comments on commit 122a786

Please sign in to comment.