Skip to content

Commit

Permalink
Don't use '-' as a separator in release counter (salesforce#95) (sale…
Browse files Browse the repository at this point in the history
…sforce#96)

(cherry picked from commit e8c2b65)
  • Loading branch information
simontoens authored Jan 23, 2021
1 parent 41e31e2 commit 9c5d800
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
38 changes: 29 additions & 9 deletions common/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
version related shared code.
"""

from . import code
from collections import namedtuple
import re


version_re = re.compile("(^.*version *= *[\"'])(.*?)([\"'].*)$", re.S)


Expand Down Expand Up @@ -42,6 +44,7 @@ def get_version_increment_strategy(build_pom_content, path):
raise Exception("Unknown version increment strategy: %s" % maven_art_up.version_increment_strategy)
return lambda version: version_update_handler(version, incr_strat)


def parse_build_pom_version(build_pom_content):
"""
Returns the value of maven_artifact.version.
Expand All @@ -61,8 +64,6 @@ def parse_build_pom_released_version(build_pom_released_content):
return parse_build_pom_version(build_pom_released_content)


REL_QUALIFIER_PREFIX = "-rel-"

def get_release_version(current_version, last_released_version=None, incremental_release=False):
"""
If incremental_release is False:
Expand Down Expand Up @@ -143,14 +144,33 @@ def version_update_handler(version, version_update_strategy):
return next_version


OLD_REL_QUALIFIER_PREFIX = "-rel-"
REL_QUALIFIER_PREFIX = "-rel"


def _incr_rel_qualifier(version):
i = version.rfind(REL_QUALIFIER_PREFIX)
if i == -1:
counter = 1
else:
counter = int(version[i+len(REL_QUALIFIER_PREFIX)]) + 1
version = version[:i]
return "%s%s%s" % (version, REL_QUALIFIER_PREFIX, counter)
start_rel_qual_i = None
end_rel_qual_i = None
current_counter_value = None
for qual in (OLD_REL_QUALIFIER_PREFIX, REL_QUALIFIER_PREFIX,):
start_rel_qual_i = version.rfind(qual)
if start_rel_qual_i == -1:
continue
start_counter_i = start_rel_qual_i + len(qual)
end_rel_qual_i = version.rfind("-", start_counter_i + 1)
if end_rel_qual_i == -1:
end_rel_qual_i = len(version)
counter_str = version[start_counter_i:end_rel_qual_i]
current_counter_value = int(counter_str)
break

if current_counter_value is None:
start_rel_qual_i = end_rel_qual_i = len(version)
current_counter_value = 0
return "%s%s%s%s" % (version[:start_rel_qual_i],
REL_QUALIFIER_PREFIX,
current_counter_value + 1,
version[end_rel_qual_i:])


def _parse_maven_artifact_update(build_pom_content, path):
Expand Down
25 changes: 22 additions & 3 deletions tests/versiontest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,27 @@ def test_get_release_version__semver_release(self):
self.assertEqual("1.2.3", version.get_release_version("1.2.3"))

def test_get_release_version__incremental_release(self):
self.assertEqual("1.2.3-rel-1", version.get_release_version("foo", last_released_version="1.2.3", incremental_release=True))
self.assertEqual("1.2.3-rel-2", version.get_release_version("foo", last_released_version="1.2.3-rel-1", incremental_release=True))
self.assertEqual("0.0.0-rel-1", version.get_release_version("foo", last_released_version=None, incremental_release=True))
self.assertEqual("1.2.3-rel1", version.get_release_version("foo", last_released_version="1.2.3", incremental_release=True))
self.assertEqual("1.2.3-rel2", version.get_release_version("foo", last_released_version="1.2.3-rel1", incremental_release=True))
self.assertEqual("0.0.0-rel1", version.get_release_version("foo", last_released_version=None, incremental_release=True))

def test_get_release_version__incremental_release__multiple_digits(self):
self.assertEqual("1.2.3-rel10", version.get_release_version("foo", last_released_version="1.2.3-rel9", incremental_release=True))
self.assertEqual("1.2.3-rel11", version.get_release_version("foo", last_released_version="1.2.3-rel10", incremental_release=True))
self.assertEqual("1.2.3-rel100", version.get_release_version("foo", last_released_version="1.2.3-rel99", incremental_release=True))

def test_get_release_version__incremental_release__last_rel_qualifier_uses_old_dash_number_syntax(self):
# we used to use rel-<num>, for example rel-1, rel-2 etc
# we switched this to rel<num> (so rel1, rel2 etc) so that '-' is only
# used as a separator between version qualifiers: 1.0.0-rel1-SNAPSHOT
self.assertEqual("1.2.3-rel2", version.get_release_version("foo", last_released_version="1.2.3-rel-1", incremental_release=True))
self.assertEqual("1.2.3-rel11", version.get_release_version("foo", last_released_version="1.2.3-rel-10", incremental_release=True))

def test_get_release_version__multiple_qualifiers(self):
self.assertEqual("1.2.3-rel2-foo22", version.get_release_version("foo", last_released_version="1.2.3-rel1-foo22", incremental_release=True))
self.assertEqual("1.2.3-rel10-foo22", version.get_release_version("foo", last_released_version="1.2.3-rel9-foo22", incremental_release=True))
self.assertEqual("1.2.3-rel2-foo22", version.get_release_version("foo", last_released_version="1.2.3-rel-1-foo22", incremental_release=True))
self.assertEqual("1.2.3-rel10-foo22", version.get_release_version("foo", last_released_version="1.2.3-rel-9-foo22", incremental_release=True))

def test_get_next_dev_version__semver_release(self):
build_pom_content = self._get_build_pom("major")
Expand Down Expand Up @@ -163,5 +181,6 @@ def _get_build_pom(self, version_increment_strategy):
"""
return build_pom % version_increment_strategy


if __name__ == '__main__':
unittest.main()

0 comments on commit 9c5d800

Please sign in to comment.