From f34eae962858cfd6a03449b73599115c04076355 Mon Sep 17 00:00:00 2001 From: Seth M Morton Date: Sun, 19 Aug 2018 22:39:28 -0700 Subject: [PATCH] Add interpolation when reading setup.cfg. Setuptools uses the default ConfigParser object when reading setup.cfg which does interpolation. To be consistent with that format bumpversion must do the same. This will close issue #21. --- bumpversion/__init__.py | 22 +++++++++++++--------- tests/test_cli.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/bumpversion/__init__.py b/bumpversion/__init__.py index 57972aa..c1e805f 100644 --- a/bumpversion/__init__.py +++ b/bumpversion/__init__.py @@ -3,9 +3,9 @@ from __future__ import unicode_literals try: - from configparser import RawConfigParser, NoOptionError + from configparser import RawConfigParser, ConfigParser, NoOptionError except ImportError: - from ConfigParser import RawConfigParser, NoOptionError + from ConfigParser import RawConfigParser, SafeConfigParser as ConfigParser, NoOptionError try: from StringIO import StringIO @@ -605,13 +605,6 @@ def main(original_args=None): if 'current_version' in vcs_info: defaults['current_version'] = vcs_info['current_version'] - config = RawConfigParser('') - - # don't transform keys to lowercase (which would be the default) - config.optionxform = lambda option: option - - config.add_section('bumpversion') - explicit_config = hasattr(known_args, 'config_file') if explicit_config: @@ -622,6 +615,17 @@ def main(original_args=None): else: config_file = '.bumpversion.cfg' + # setup.cfg supports interpolation - for compatibility we must do the same. + if os.path.basename(config_file) == 'setup.cfg': + config = ConfigParser('') + else: + config = RawConfigParser('') + + # don't transform keys to lowercase (which would be the default) + config.optionxform = lambda option: option + + config.add_section('bumpversion') + config_file_exists = os.path.exists(config_file) part_configs = {} diff --git a/tests/test_cli.py b/tests/test_cli.py index 7b798d3..2495fa9 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -10,6 +10,7 @@ import argparse import subprocess +from datetime import datetime from os import curdir, makedirs, chdir, environ from os.path import join, curdir, dirname from shlex import split as shlex_split @@ -1936,6 +1937,36 @@ def test_regression_new_version_cli_in_files(tmpdir, capsys): assert "__version__ = '0.9.3'" == tmpdir.join("myp___init__.py").read() assert "current_version = 0.9.3" in tmpdir.join(".bumpversion.cfg").read() +def test_correct_interpolation_for_setup_cfg_files(tmpdir, configfile): + ''' + Reported here: https://github.com/c4urself/bump2version/issues/21 + ''' + tmpdir.chdir() + tmpdir.join("file.py").write("XX-XX-XXXX v. X.X.X") + tmpdir.chdir() + + if configfile == "setup.cfg": + tmpdir.join(configfile).write(dedent(""" + [bumpversion] + current_version = 0.7.2 + files = file.py + search = XX-XX-XXXX v. X.X.X + replace = {now:%%m-%%d-%%Y} v. {new_version} + """).strip()) + else: + tmpdir.join(configfile).write(dedent(""" + [bumpversion] + current_version = 0.7.2 + files = file.py + search = XX-XX-XXXX v. X.X.X + replace = {now:%m-%d-%Y} v. {new_version} + """).strip()) + + main(["major"]) + + assert datetime.now().strftime('%m-%d-%Y') + ' v. 1.0.0' == tmpdir.join("file.py").read() + assert "current_version = 1.0.0" in tmpdir.join(configfile).read() + class TestSplitArgsInOptionalAndPositional: def test_all_optional(self):