]> git.decadent.org.uk Git - dak.git/commitdiff
Add validate_changes_format utility.
authorChris Lamb <lamby@debian.org>
Wed, 28 Oct 2009 16:23:38 +0000 (16:23 +0000)
committerChris Lamb <lamby@debian.org>
Wed, 28 Oct 2009 16:30:45 +0000 (16:30 +0000)
Signed-off-by: Chris Lamb <lamby@debian.org>
daklib/formats.py
tests/test_formats.py

index e8ab1d77d205f85db14ee80ea2cd5173e7ce8b3f..aaad2715aac119bd40766d1cef333f9cf2df36df 100644 (file)
@@ -30,3 +30,16 @@ def parse_format(txt):
         format = format[:2]
 
     return format
+
+def validate_changes_format(format, field):
+    """
+    Validate a tuple-representation of a .changes Format: field. Raises
+    UnknownFormatError if the field is invalid, otherwise return type is
+    undefined.
+    """
+
+    if (format < (1, 5) or format > (1, 8)):
+        raise UnknownFormatError, repr(format)
+
+    if field != 'files' and format < (1, 8):
+        raise UnknownFormatError, repr(format)
index 8a2b1ad56f27d98064fac0b368139f714e1426f8..1ae6860aa60659ba890e9f00c31b1c02a9945ac9 100755 (executable)
@@ -5,7 +5,7 @@ import unittest
 import os, sys
 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
 
-from daklib.formats import parse_format
+from daklib.formats import parse_format, validate_changes_format
 from daklib.dak_exceptions import UnknownFormatError
 
 class ParseFormatTestCase(unittest.TestCase):
@@ -29,3 +29,30 @@ class ParseFormatTestCase(unittest.TestCase):
     def textText(self):
         self.assertParse('1.2 (three)', (1, 2, 'three'))
         self.assertParseFail('0.0 ()')
+
+class ValidateChangesFormat(unittest.TestCase):
+    def assertValid(self, changes, field='files'):
+        validate_changes_format(changes, field)
+
+    def assertInvalid(self, *args, **kwargs):
+        self.assertRaises(
+            UnknownFormatError,
+            lambda: self.assertValid(*args, **kwargs)
+        )
+
+    ##
+
+    def testBinary(self):
+        self.assertValid((1, 5))
+        self.assertValid((1, 8))
+        self.assertInvalid((1, 0))
+
+    def testRange(self):
+        self.assertInvalid((1, 3))
+        self.assertValid((1, 5))
+        self.assertValid((1, 8))
+        self.assertInvalid((1, 9))
+
+    def testFilesField(self):
+        self.assertInvalid((1, 7), field='notfiles')
+        self.assertValid((1, 8), field='notfiles')