Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

also load configuration from setup.cfg #57

Merged
merged 3 commits into from
Dec 28, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions bumpversion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,8 +607,9 @@ def main(original_args=None):
parser1 = argparse.ArgumentParser(add_help=False)

parser1.add_argument(
'--config-file', default='.bumpversion.cfg', metavar='FILE',
help='Config file to read most of the variables from', required=False)
'--config-file', metavar='FILE',
default=argparse.SUPPRESS, required=False,
help='Config file to read most of the variables from (default: .bumpversion.cfg)')

parser1.add_argument(
'--verbose', action='count', default=0,
Expand Down Expand Up @@ -662,18 +663,28 @@ def main(original_args=None):
config = RawConfigParser('')
config.add_section('bumpversion')

config_file_exists = os.path.exists(known_args.config_file)
explicit_config = hasattr(known_args, 'config_file')

if explicit_config:
config_file = known_args.config_file
elif not os.path.exists('.bumpversion.cfg') and \
os.path.exists('setup.cfg'):
config_file = 'setup.cfg'
else:
config_file = '.bumpversion.cfg'

config_file_exists = os.path.exists(config_file)

part_configs = {}

files = []

if config_file_exists:

logger.info("Reading config file {}:".format(known_args.config_file))
logger.info(io.open(known_args.config_file, 'rt', encoding='utf-8').read())
logger.info("Reading config file {}:".format(config_file))
logger.info(io.open(config_file, 'rt', encoding='utf-8').read())

config.readfp(io.open(known_args.config_file, 'rt', encoding='utf-8'))
config.readfp(io.open(config_file, 'rt', encoding='utf-8'))

log_config = StringIO()
config.write(log_config)
Expand Down Expand Up @@ -745,8 +756,8 @@ def main(original_args=None):
files.append(ConfiguredFile(filename, VersionConfig(**section_config)))

else:
message = "Could not read config file at {}".format(known_args.config_file)
if known_args.config_file != parser1.get_default('config_file'):
message = "Could not read config file at {}".format(config_file)
if explicit_config:
raise argparse.ArgumentTypeError(message)
else:
logger.info(message)
Expand Down Expand Up @@ -916,14 +927,14 @@ def main(original_args=None):

logger.info("{} to config file {}:".format(
"Would write" if not write_to_config_file else "Writing",
known_args.config_file,
config_file,
))

config.write(new_config)
logger.info(new_config.getvalue())

if write_to_config_file:
with io.open(known_args.config_file, 'wb') as f:
with io.open(config_file, 'wb') as f:
f.write(new_config.getvalue().encode('utf-8'))

except UnicodeEncodeError:
Expand All @@ -933,7 +944,7 @@ def main(original_args=None):
)

if config_file_exists:
commit_files.append(known_args.config_file)
commit_files.append(config_file)

if not vcs:
return
Expand Down
26 changes: 24 additions & 2 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
reason="hg is not installed"
)

@pytest.fixture(params=['.bumpversion.cfg', 'setup.cfg'])
def configfile(request):
return request.param


try:
bumpversion.RawConfigParser(empty_lines_in_values=False)
using_old_configparser = False
Expand Down Expand Up @@ -197,9 +202,9 @@ def test_config_file(tmpdir):
assert "0.9.35" == tmpdir.join("file1").read()


def test_default_config_file(tmpdir):
def test_default_config_files(tmpdir, configfile):
tmpdir.join("file2").write("0.10.2")
tmpdir.join(".bumpversion.cfg").write("""[bumpversion]
tmpdir.join(configfile).write("""[bumpversion]
current_version: 0.10.2
new_version: 0.10.3
files: file2""")
Expand All @@ -210,6 +215,23 @@ def test_default_config_file(tmpdir):
assert "0.10.3" == tmpdir.join("file2").read()


def test_multiple_config_files(tmpdir):
tmpdir.join("file2").write("0.10.2")
tmpdir.join("setup.cfg").write("""[bumpversion]
current_version: 0.10.2
new_version: 0.10.3
files: file2""")
tmpdir.join(".bumpversion.cfg").write("""[bumpversion]
current_version: 0.10.2
new_version: 0.10.4
files: file2""")

tmpdir.chdir()
main(['patch'])

assert "0.10.4" == tmpdir.join("file2").read()


def test_config_file_is_updated(tmpdir):
tmpdir.join("file3").write("0.0.13")
tmpdir.join(".bumpversion.cfg").write("""[bumpversion]
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ deps=
pytest
mock
commands=
py.test tests.py
py.test [] tests.py

[testenv:py27-configparser]
basepython= python2.7
Expand Down