From e102ad02c396868d6f6c514f42a641e78f7fca9b Mon Sep 17 00:00:00 2001 From: Chris Lamb Date: Wed, 28 Oct 2009 16:23:38 +0000 Subject: [PATCH] Add validate_changes_format utility. Signed-off-by: Chris Lamb --- daklib/formats.py | 13 +++++++++++++ tests/test_formats.py | 29 ++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/daklib/formats.py b/daklib/formats.py index e8ab1d77..aaad2715 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 8a2b1ad5..1ae6860a 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') -- 2.39.5