Skip to content

Commit

Permalink
Add interpolation when reading setup.cfg.
Browse files Browse the repository at this point in the history
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 c4urself#21.
  • Loading branch information
SethMMorton committed Aug 20, 2018
1 parent 4fe7784 commit f34eae9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
22 changes: 13 additions & 9 deletions bumpversion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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 = {}
Expand Down
31 changes: 31 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit f34eae9

Please sign in to comment.