summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Brown <ben.brown@codethink.co.uk>2014-01-08 12:10:58 (GMT)
committerBen Brown <ben.brown@codethink.co.uk>2014-01-22 16:41:52 (GMT)
commit825981a102422c01b054a93c7de00a680b5ce77f (patch)
tree2be383a4b3ba4e6420327b564de30d4cfcb3e42c
parent7265acd1440dc898d3ebe9de443a470c3d845b50 (diff)
downloadmorph-825981a102422c01b054a93c7de00a680b5ce77f.tar.gz
Add validation for chunk refs in strata to be (non-empty) strings
-rw-r--r--morphlib/morphloader.py33
-rw-r--r--morphlib/morphloader_tests.py36
2 files changed, 69 insertions, 0 deletions
diff --git a/morphlib/morphloader.py b/morphlib/morphloader.py
index 637544b..32b5b40 100644
--- a/morphlib/morphloader.py
+++ b/morphlib/morphloader.py
@@ -120,6 +120,27 @@ class DuplicateChunkError(morphlib.Error):
'in stratum %(stratum_name)s' % locals())
+class EmptyRefError(morphlib.Error):
+
+ def __init__(self, ref_location, morphology):
+ self.ref_location = ref_location
+ self.morphology = morphology
+ morphlib.Error.__init__(
+ self, 'Empty ref found for %(ref_location)s '\
+ 'in %(morphology)s' % locals())
+
+
+class ChunkSpecRefNotStringError(morphlib.Error):
+
+ def __init__(self, ref_value, chunk_name, stratum_name):
+ self.ref_value = ref_value
+ self.chunk_name = chunk_name
+ self.stratum_name = stratum_name
+ morphlib.Error.__init__(
+ self, 'Ref %(ref_value)s for %(chunk_name)s '\
+ 'in stratum %(stratum_name)s is not a string' % locals())
+
+
class SystemStrataNotListError(morphlib.Error):
def __init__(self, system_name, strata_type):
@@ -130,6 +151,7 @@ class SystemStrataNotListError(morphlib.Error):
self, 'System %(system_name)s has the wrong type for its strata: '\
'%(typename)s, expected list' % locals())
+
class DuplicateStratumError(morphlib.Error):
def __init__(self, system_name, stratum_name):
@@ -366,6 +388,17 @@ class MorphologyLoader(object):
raise DuplicateChunkError(morph['name'], name)
names.add(name)
+ # All chunk refs must be strings.
+ for spec in morph['chunks']:
+ if 'ref' in spec:
+ ref = spec['ref']
+ if ref == None:
+ raise EmptyRefError(
+ spec.get('alias', spec['name']), morph.filename)
+ elif not isinstance(ref, basestring):
+ raise ChunkSpecRefNotStringError(
+ ref, spec.get('alias', spec['name']), morph.filename)
+
# Require build-dependencies for the stratum itself, unless
# it has chunks built in bootstrap mode.
if 'build-depends' not in morph:
diff --git a/morphlib/morphloader_tests.py b/morphlib/morphloader_tests.py
index c2fbc5e..3992334 100644
--- a/morphlib/morphloader_tests.py
+++ b/morphlib/morphloader_tests.py
@@ -187,6 +187,42 @@ build-system: dummy
self.assertRaises(
morphlib.morphloader.InvalidFieldError, self.loader.validate, m)
+ def test_validate_requires_chunk_refs_in_stratum_to_be_strings(self):
+ m = morphlib.morph3.Morphology({
+ 'kind': 'stratum',
+ 'name': 'foo',
+ 'build-depends': [],
+ 'chunks': [
+ {
+ 'name': 'chunk',
+ 'repo': 'test:repo',
+ 'ref': 1,
+ 'build-depends': []
+ }
+ ]
+ })
+ with self.assertRaises(
+ morphlib.morphloader.ChunkSpecRefNotStringError):
+ self.loader.validate(m)
+
+ def test_fails_to_validate_stratum_with_empty_refs_for_a_chunk(self):
+ m = morphlib.morph3.Morphology({
+ 'kind': 'stratum',
+ 'name': 'foo',
+ 'build-depends': [],
+ 'chunks' : [
+ {
+ 'name': 'chunk',
+ 'repo': 'test:repo',
+ 'ref': None,
+ 'build-depends': []
+ }
+ ]
+ })
+ with self.assertRaises(
+ morphlib.morphloader.EmptyRefError):
+ self.loader.validate(m)
+
def test_fails_to_validate_system_with_obsolete_system_kind_field(self):
m = morphlib.morph3.Morphology({
'kind': 'system',