Skip to content

Commit

Permalink
Add and test loading project configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljoseph committed Nov 8, 2014
1 parent 8e75c7c commit 41b9708
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
35 changes: 33 additions & 2 deletions changes/config.py
Original file line number Diff line number Diff line change
@@ -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 {}
1 change: 1 addition & 0 deletions requirements/runtime.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
PyYAML < 4.0.0
plumbum < 1.5.0
click < 2.6.0
path.py < 5.0.0
Expand Down
25 changes: 25 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -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) == {}

0 comments on commit 41b9708

Please sign in to comment.