[dak/master 3/5] Add validate_changes_format utility.
Signed-off-by: Chris Lamb <lamby@debian.org>
---
daklib/formats.py | 13 +++++++++++++
tests/test_formats.py | 29 ++++++++++++++++++++++++++++-
2 files changed, 41 insertions(+), 1 deletions(-)
diff --git a/daklib/formats.py b/daklib/formats.py
index e8ab1d7..aaad271 100644
--- a/daklib/formats.py
+++ b/daklib/formats.py
@@ -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)
diff --git a/tests/test_formats.py b/tests/test_formats.py
index 8a2b1ad..1ae6860 100755
--- a/tests/test_formats.py
+++ b/tests/test_formats.py
@@ -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')
--
1.6.3.3
Reply to: