From 41b9708538b8aebf1ffb1fd7b799876efe4406d3 Mon Sep 17 00:00:00 2001 From: Michael Joseph Date: Sat, 8 Nov 2014 19:30:47 +0200 Subject: [PATCH] Add and test loading project configuration --- changes/config.py | 35 +++++++++++++++++++++++++++++++++-- requirements/runtime.txt | 1 + tests/test_config.py | 25 +++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 tests/test_config.py diff --git a/changes/config.py b/changes/config.py index 6bc99ba..2109b39 100644 --- a/changes/config.py +++ b/changes/config.py @@ -1,16 +1,47 @@ +from os.path import exists, join + +import click +import yaml + +CONFIG_FILE = '.changes' +DEFAULTS = { + 'changelog': 'CHANGELOG.md', + 'readme': 'README.md', +} + class CLI(object): test_command = None pypi = None skip_changelog = None - def __init__(self, module_name, dry_run, debug, no_input, requirements, new_version, current_version, repo_url, version_prefix): + def __init__(self, module_name, dry_run, debug, no_input, requirements, + new_version, current_version, repo_url, version_prefix): self.module_name = module_name self.dry_run = dry_run self.debug = debug self.no_input = no_input self.requirements = requirements - self.new_version = version_prefix + new_version if version_prefix else new_version + self.new_version = ( + version_prefix + new_version + if version_prefix + else new_version + ) self.current_version = current_version self.repo_url = repo_url + +def project_config(context): + config = {} + config_path = join(context.module_name, CONFIG_FILE) + + # initialise config with defaults + if not exists(config_path): + config = DEFAULTS.copy() + + with click.open_file(config_path, 'w') as f: + config_yaml = yaml.dump(config, default_flow_style=False) + f.write(config_yaml) + + config = yaml.safe_load(click.open_file(config_path)) + return config or {} diff --git a/requirements/runtime.txt b/requirements/runtime.txt index ef7127a..129c744 100644 --- a/requirements/runtime.txt +++ b/requirements/runtime.txt @@ -1,3 +1,4 @@ +PyYAML < 4.0.0 plumbum < 1.5.0 click < 2.6.0 path.py < 5.0.0 diff --git a/tests/test_config.py b/tests/test_config.py new file mode 100644 index 0000000..31a14ce --- /dev/null +++ b/tests/test_config.py @@ -0,0 +1,25 @@ +from os.path import exists + +import click + +from changes import config +from . import context, setup, teardown # noqa + + +def test_no_config(): + assert not exists('test_app/.changes') + assert config.project_config(context) == config.DEFAULTS + assert exists('test_app/.changes') + + +def test_existing_config(): + existing_config = 'foo: bar\nbaz: buzz\n' + with click.open_file('test_app/.changes', 'w') as f: + f.write(existing_config) + assert config.project_config(context) == {'foo': 'bar', 'baz': 'buzz'} + + +def test_malformed_config_returns_none(): + with click.open_file('test_app/.changes', 'w') as f: + f.write('something\n\n-another thing\n') + assert config.project_config(context) == {}