Skip to contentSkip to conversation

prserv: increment 9 to 10 correctly

PATCH New Dan McGregor 1 participant 1 message Sep 9, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 prserv: increment 9 to 10 correctly

From: Dan McGregor <[email protected]>

Previously, incrementing "0.9" would result in "0.1.0", which generally gets recognised as a lower version number. Even more surprising, incrementing "0.99" returned "0.1.0.0".

This is due to the behaviour of the list function on a string object; it adds each character as an element in a new list, causing the new string '10' to become the list [ '1', '0' ].

Instead of converting a string to a list, add the string to a new list, and concatenate it with the existing list slice. And provide test cases for "0.9" -> "0.10" and related edge cases.

---
 lib/prserv/__init__.py | 2 +-
 lib/prserv/tests.py    | 2 ++
 2 files changed, 3 insertions(+), 1 deletion(-)
lib/prserv/__init__.py +1 -1
index a817b03c1e1..ffc5a40a287 100644
@@ -34,7 +34,7 @@ def increase_revision(ver):
          logger.critical("Unable to increase revision value %s: %s" % (ver, e))
          raise e
 
-    return ".".join(fields[0:-1] + list(str(val + 1)))
+    return ".".join(fields[0:-1] + [ str(val + 1) ])
 
 def _revision_greater_or_equal(rev1, rev2):
     """Compares x.y.z revision numbers, using integer comparison
lib/prserv/tests.py +2 -0
index 8765b129f28..df0c0030039 100644
@@ -84,6 +84,8 @@ class FunctionTests(unittest.TestCase):
         self.assertEqual(increase_revision("1.0"), "1.1")
         self.assertEqual(increase_revision("1.1.1"), "1.1.2")
         self.assertEqual(increase_revision("1.1.1.3"), "1.1.1.4")
+        self.assertEqual(increase_revision("9"), "10")
+        self.assertEqual(increase_revision("1.9"), "1.10")
         self.assertRaises(ValueError, increase_revision, "1.a")
         self.assertRaises(ValueError, increase_revision, "1.")
         self.assertRaises(ValueError, increase_revision, "")
Signature · 1 line
2.46.0