From 7382a6b60efcfeb09c80d227574969cbb4577237 Mon Sep 17 00:00:00 2001 From: Filip Noetzel Date: Thu, 27 Mar 2014 13:45:50 +0100 Subject: [PATCH] Allow numeric part configs to have a first_value --- bumpversion/__init__.py | 22 ++++++++++++++++++---- tests.py | 19 +++++++++++++++++++ 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/bumpversion/__init__.py b/bumpversion/__init__.py index 1544308..e9f4ce4 100644 --- a/bumpversion/__init__.py +++ b/bumpversion/__init__.py @@ -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 @@ -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() @@ -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) diff --git a/tests.py b/tests.py index 208061b..ded3dd8 100644 --- a/tests.py +++ b/tests.py @@ -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() +