mirror of
				https://github.com/f-droid/fdroidserver.git
				synced 2025-11-04 06:30:27 +03:00 
			
		
		
		
	🪨 version string conversion: error handling+tests
This commit is contained in:
		
							parent
							
								
									3ee91d1777
								
							
						
					
					
						commit
						c288317530
					
				
					 2 changed files with 32 additions and 4 deletions
				
			
		| 
						 | 
				
			
			@ -524,6 +524,9 @@ def insert_obbs(repodir, apps, apks):
 | 
			
		|||
                break
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
VERSION_STRING_RE = re.compile(r'^([0-9]+)\.([0-9]+)\.([0-9]+)$')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def version_string_to_int(version):
 | 
			
		||||
    """
 | 
			
		||||
    Convert sermver version designation to version code.
 | 
			
		||||
| 
						 | 
				
			
			@ -532,10 +535,12 @@ def version_string_to_int(version):
 | 
			
		|||
    consisting of numeric characters (0-9) and periods to a number. The
 | 
			
		||||
    exponents are chosen such that it still fits in the 64bit JSON/Android range.
 | 
			
		||||
    """
 | 
			
		||||
    version = version.split('.')
 | 
			
		||||
    major = int(version.pop(0)) if version else 0
 | 
			
		||||
    minor = int(version.pop(0)) if version else 0
 | 
			
		||||
    patch = int(version.pop(0)) if version else 0
 | 
			
		||||
    m = VERSION_STRING_RE.match(version)
 | 
			
		||||
    if not m:
 | 
			
		||||
        raise ValueError(f"invalid version string '{version}'")
 | 
			
		||||
    major = int(m.group(1))
 | 
			
		||||
    minor = int(m.group(2))
 | 
			
		||||
    patch = int(m.group(3))
 | 
			
		||||
    return major * 10**12 + minor * 10**6 + patch
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1923,6 +1923,28 @@ class UpdateTest(unittest.TestCase):
 | 
			
		|||
        )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class TestUpdateVersionStringToInt(unittest.TestCase):
 | 
			
		||||
 | 
			
		||||
    def test_version_string_to_int(self):
 | 
			
		||||
        self.assertEqual(fdroidserver.update.version_string_to_int("1.2.3"), 1000002000003)
 | 
			
		||||
        self.assertEqual(fdroidserver.update.version_string_to_int("0.0.0003"), 3)
 | 
			
		||||
        self.assertEqual(fdroidserver.update.version_string_to_int("0.0.0"), 0)
 | 
			
		||||
        self.assertEqual(fdroidserver.update.version_string_to_int("4321.321.21"), 4321000321000021)
 | 
			
		||||
        self.assertEqual(fdroidserver.update.version_string_to_int("18446744.073709.551615"), 18446744073709551615)
 | 
			
		||||
 | 
			
		||||
    def test_version_string_to_int_value_errors(self):
 | 
			
		||||
        with self.assertRaises(ValueError):
 | 
			
		||||
            fdroidserver.update.version_string_to_int("1.2.3a")
 | 
			
		||||
        with self.assertRaises(ValueError):
 | 
			
		||||
            fdroidserver.update.version_string_to_int("asdfasdf")
 | 
			
		||||
        with self.assertRaises(ValueError):
 | 
			
		||||
            fdroidserver.update.version_string_to_int("1.2.-3")
 | 
			
		||||
        with self.assertRaises(ValueError):
 | 
			
		||||
            fdroidserver.update.version_string_to_int("-1.2.-3")
 | 
			
		||||
        with self.assertRaises(ValueError):
 | 
			
		||||
            fdroidserver.update.version_string_to_int("0.0.0x3")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
if __name__ == "__main__":
 | 
			
		||||
    os.chdir(os.path.dirname(__file__))
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -1938,4 +1960,5 @@ if __name__ == "__main__":
 | 
			
		|||
 | 
			
		||||
    newSuite = unittest.TestSuite()
 | 
			
		||||
    newSuite.addTest(unittest.makeSuite(UpdateTest))
 | 
			
		||||
    newSuite.addTest(unittest.makeSuite(TestUpdateVersionStringToInt))
 | 
			
		||||
    unittest.main(failfast=False)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue