Skip to content

Commit

Permalink
Allow numeric part configs to have a first_value
Browse files Browse the repository at this point in the history
  • Loading branch information
peritus committed Mar 27, 2014
1 parent 081ad6e commit 7382a6b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
22 changes: 18 additions & 4 deletions bumpversion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class VersionPartConfiguration(object):

class ConfiguredVersionPartConfiguration(object):

def __init__(self, values, optional_value=None):
def __init__(self, values, optional_value=None, first_value=None):

assert len(values) > 0

Expand All @@ -196,19 +196,29 @@ def __init__(self, values, optional_value=None):
if optional_value is None:
optional_value = values[0]

self.first_value = values[0]
self.optional_value = optional_value

if first_value is None:
first_value = values[0]

self.first_value = first_value

def bump(self, value):
return self._values[self._values.index(value)+1]

class NumericVersionPartConfiguration(VersionPartConfiguration):

optional_value = "0"
first_value = "0"

FIRST_NUMERIC = re.compile('([^\d]*)(\d+)(.*)')

def __init__(self, first_value=None):

if first_value is None:
first_value = "0"

self.first_value = first_value

@classmethod
def bump(cls, value):
part_prefix, numeric_version, part_suffix = cls.FIRST_NUMERIC.search(value).groups()
Expand Down Expand Up @@ -537,10 +547,14 @@ def main(original_args=None):
for section_name in config.sections():
if section_name.startswith("bumpversion:part"):
part_config = dict(config.items(section_name))

ThisVersionPartConfiguration = NumericVersionPartConfiguration

if 'values' in part_config:
part_config['values'] = list(filter(None, (x.strip() for x in part_config['values'].splitlines())))
ThisVersionPartConfiguration = ConfiguredVersionPartConfiguration

part_configs[section_name.split(":", 2)[2]] = ConfiguredVersionPartConfiguration(**part_config)
part_configs[section_name.split(":", 2)[2]] = ThisVersionPartConfiguration(**part_config)

else:
message = "Could not read config file at {}".format(known_args.config_file)
Expand Down
19 changes: 19 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1247,3 +1247,22 @@ def file_content():

main(['minor', '--verbose'])
assert '1.1dev' == file_content()

def test_part_first_value(tmpdir):

tmpdir.join("the_version.txt").write("0.9.4")
tmpdir.chdir()

tmpdir.join(".bumpversion.cfg").write(dedent("""
[bumpversion]
files = the_version.txt
current_version = 0.9.4
[bumpversion:part:minor]
first_value = 1
"""))

main(['major', '--verbose'])

assert '1.1.0' == tmpdir.join("the_version.txt").read()

0 comments on commit 7382a6b

Please sign in to comment.