Skip to content

Commit

Permalink
Make Version.from_string() stricter, matching from start of string
Browse files Browse the repository at this point in the history
  • Loading branch information
zas committed Mar 30, 2024
1 parent 1329c06 commit 005837e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion picard/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class VersionError(Exception):


class Version(namedtuple('VersionBase', 'major minor patch identifier revision')):
_version_re = re.compile(r"(\d+)(?:[._](\d+)(?:[._](\d+)[._]?(?:(dev|a|alpha|b|beta|rc|final)[._]?(\d+))?)?)?$")
_version_re = re.compile(r"^(\d+)(?:[._](\d+)(?:[._](\d+)[._]?(?:(dev|a|alpha|b|beta|rc|final)[._]?(\d+))?)?)?$")

_identifiers = {
'dev': 0,
Expand Down
18 changes: 11 additions & 7 deletions test/test_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,23 @@ def test_version_from_string_underscores(self):
l, s = (1, 1, 0, 'dev', 0), '1_1_0_dev_0'
self.assertEqual(l, Version.from_string(s))

def test_version_from_string_prefixed(self):
l, s = (1, 1, 0, 'dev', 0), 'anything_28_1_1_0_dev_0'
self.assertEqual(l, Version.from_string(s))
def test_version_from_string_prefixed_with_num(self):
self.assertRaises(VersionError, Version.from_string, '8_1_1_0_dev_0')

def test_version_from_string_suffixed_with_num(self):
self.assertRaises(VersionError, Version.from_string, '1_1_0_dev_0_8')

def test_version_from_string_prefixed_with_alpha(self):
self.assertRaises(VersionError, Version.from_string, 'a_1_1_0_dev_0')

def test_version_from_string_suffixed_with_alpha(self):
self.assertRaises(VersionError, Version.from_string, '1_1_0_dev_0_a')

def test_version_single_digit(self):
l, s = (2, 0, 0, 'final', 0), '2'
self.assertEqual(l, Version.from_string(s))
self.assertEqual(l, Version(2))

def test_version_from_string_prefixed_final(self):
l, s = (1, 1, 0, 'final', 0), 'anything_28_1_1_0'
self.assertEqual(l, Version.from_string(s))

def test_from_string_invalid_identifier(self):
self.assertRaises(VersionError, Version.from_string, '1.1.0dev')
self.assertRaises(VersionError, Version.from_string, '1.1.0devx')
Expand Down

0 comments on commit 005837e

Please sign in to comment.