diff options
-rw-r--r-- | morphlib/morphology.py | 84 | ||||
-rw-r--r-- | morphlib/morphology_tests.py | 328 |
2 files changed, 13 insertions, 399 deletions
diff --git a/morphlib/morphology.py b/morphlib/morphology.py index 903df472..359c8f3b 100644 --- a/morphlib/morphology.py +++ b/morphlib/morphology.py @@ -18,11 +18,6 @@ import json import logging -class SchemaError(Exception): - - pass - - class Morphology(object): '''Represent a morphology: description of how to build binaries.''' @@ -36,86 +31,12 @@ class Morphology(object): logging.debug('Loading morphology %s' % self._fp.name) self._dict = json.load(self._fp) - if 'name' not in self._dict: - raise self._error('must contain "name"') - - if not self.name: - raise self._error('"name" must not be empty') - - if 'kind' not in self._dict: - raise self._error('must contain "kind"') - - if self.kind == 'chunk': - self._validate_chunk() - elif self.kind == 'stratum': - self._validate_stratum() + if self.kind == 'stratum': for source in self.sources.itervalues(): source['repo'] = self._join_with_baseurl(source['repo']) - elif self.kind == 'system': - pass - else: - raise self._error('kind must be chunk or stratum, not %s' % - self.kind) self.filename = self._fp.name - def _validate_chunk(self): - valid_toplevel_keys = ['name', 'kind', 'configure-commands', - 'build-commands', 'test-commands', - 'install-commands'] - - cmdlists = [ - (self.configure_commands, 'configure-commands'), - (self.build_commands, 'build-commands'), - (self.test_commands, 'test-commands'), - (self.install_commands, 'install-commands'), - ] - for value, name in cmdlists: - if type(value) != list: - raise self._error('"%s" must be a list' % name) - for x in value: - if type(x) != unicode: - raise self._error('"%s" must contain strings' % name) - - for key in self._dict.keys(): - if key not in valid_toplevel_keys: - raise self._error('unknown key "%s"' % key) - - def _validate_stratum(self): - valid_toplevel_keys = ['name', 'kind', 'sources'] - - if 'sources' not in self._dict: - raise self._error('stratum must contain "sources"') - - if type(self.sources) != dict: - raise self._error('"sources" must be a dict') - - if len(self.sources) == 0: - raise self._error('"sources" must not be empty') - - for name, source in self.sources.iteritems(): - if type(source) != dict: - raise self._error('"sources" must contain dicts') - if 'repo' not in source: - raise self._error('sources must have "repo"') - if type(source['repo']) != unicode: - raise self._error('"repo" must be a string') - if not source['repo']: - raise self._error('"repo" must be a non-empty string') - if 'ref' not in source: - raise self._error('sources must have "ref"') - if type(source['ref']) != unicode: - raise self._error('"ref" must be a string') - if not source['ref']: - raise self._error('"ref" must be a non-empty string') - for key in source: - if key not in ('repo', 'ref'): - raise self._error('unknown key "%s" in sources' % key) - - for key in self._dict.keys(): - if key not in valid_toplevel_keys: - raise self._error('unknown key "%s"' % key) - @property def name(self): return self._dict['name'] @@ -159,6 +80,3 @@ class Morphology(object): else: return url - def _error(self, msg): - return SchemaError('Morphology %s: %s' % (self._fp.name, msg)) - diff --git a/morphlib/morphology_tests.py b/morphlib/morphology_tests.py index 2982bb27..243fd89f 100644 --- a/morphlib/morphology_tests.py +++ b/morphlib/morphology_tests.py @@ -30,199 +30,8 @@ class MockFile(StringIO.StringIO): class MorphologyTests(unittest.TestCase): - def assertRaisesSchemaError(self, morph_dict): - f = MockFile(json.dumps(morph_dict)) - self.assertRaises(morphlib.morphology.SchemaError, - morphlib.morphology.Morphology, f) - - def test_raises_exception_for_empty_file(self): - self.assertRaises(ValueError, - morphlib.morphology.Morphology, - MockFile()) - - def test_raises_exception_for_file_without_kind_field(self): - self.assertRaisesSchemaError({}) - - def test_raises_exception_for_chunk_with_unknown_keys_only(self): - self.assertRaisesSchemaError({ 'x': 'y' }) - - def test_raises_exception_if_name_only(self): - self.assertRaisesSchemaError({ 'name': 'hello' }) - - def test_raises_exception_if_name_is_empty(self): - self.assertRaisesSchemaError({ 'name': '', 'kind': 'chunk', - 'sources': { 'repo': 'x', 'ref': 'y' }}) - - def test_raises_exception_if_kind_only(self): - self.assertRaisesSchemaError({ 'kind': 'chunk' }) - - def test_raises_exception_for_kind_that_has_unknown_kind(self): - self.assertRaisesSchemaError({ 'name': 'hello', 'kind': 'x' }) - - def test_raises_exception_for_chunk_with_nondict_source(self): - self.assertRaisesSchemaError({ - 'name': 'hello', - 'kind': 'chunk', - 'source': [], - }) - - def test_raises_exception_for_chunk_with_empty_source(self): - self.assertRaisesSchemaError({ - 'name': 'hello', - 'kind': 'chunk', - 'source': {}, - }) - - def test_raises_exception_for_chunk_without_repo_in_source(self): - self.assertRaisesSchemaError({ - 'name': 'hello', - 'kind': 'chunk', - 'source': { - 'x': 'y' - }, - }) - - def test_raises_exception_for_chunk_with_empty_repo_in_source(self): - self.assertRaisesSchemaError({ - 'name': 'hello', - 'kind': 'chunk', - 'source': { - 'repo': '', - 'ref': 'master' - }, - }) - - def test_raises_exception_for_chunk_without_ref_in_source(self): - self.assertRaisesSchemaError({ - 'name': 'hello', - 'kind': 'chunk', - 'source': { - 'repo': 'foo', - }, - }) - - def test_raises_exception_for_chunk_with_empty_ref_in_source(self): - self.assertRaisesSchemaError({ - 'name': 'hello', - 'kind': 'chunk', - 'source': { - 'repo': 'foo', - 'ref': '' - }, - }) - - def test_raises_exception_for_chunk_with_unknown_keys_in_source(self): - self.assertRaisesSchemaError({ - 'name': 'hello', - 'kind': 'chunk', - 'source': { - 'repo': 'foo', - 'ref': 'master', - 'x': 'y' - }, - }) - - def test_raises_exception_for_chunk_with_unknown_keys(self): - self.assertRaisesSchemaError({ - 'name': 'hello', - 'kind': 'chunk', - 'source': { - 'repo': 'foo', - 'ref': 'master' - }, - 'x': 'y' - }) - - def test_raises_exception_for_nonlist_configure_commands(self): - self.assertRaisesSchemaError({ - 'name': 'hello', - 'kind': 'chunk', - 'source': { - 'repo': 'foo', - 'ref': 'master' - }, - 'configure-commands': 0, - }) - - def test_raises_exception_for_list_of_nonstring_configure_commands(self): - self.assertRaisesSchemaError({ - 'name': 'hello', - 'kind': 'chunk', - 'source': { - 'repo': 'foo', - 'ref': 'master' - }, - 'configure-commands': [0], - }) - - def test_raises_exception_for_nonlist_build_commands(self): - self.assertRaisesSchemaError({ - 'name': 'hello', - 'kind': 'chunk', - 'source': { - 'repo': 'foo', - 'ref': 'master' - }, - 'build-commands': 0, - }) - - def test_raises_exception_for_list_of_nonstring_build_commands(self): - self.assertRaisesSchemaError({ - 'name': 'hello', - 'kind': 'chunk', - 'source': { - 'repo': 'foo', - 'ref': 'master' - }, - 'build-commands': [0], - }) - - def test_raises_exception_for_nonlist_test_commands(self): - self.assertRaisesSchemaError({ - 'name': 'hello', - 'kind': 'chunk', - 'source': { - 'repo': 'foo', - 'ref': 'master' - }, - 'test-commands': 0, - }) - - def test_raises_exception_for_list_of_nonstring_test_commands(self): - self.assertRaisesSchemaError({ - 'name': 'hello', - 'kind': 'chunk', - 'source': { - 'repo': 'foo', - 'ref': 'master' - }, - 'build-commands': [0], - }) - - def test_raises_exception_for_nonlist_install_commands(self): - self.assertRaisesSchemaError({ - 'name': 'hello', - 'kind': 'chunk', - 'source': { - 'repo': 'foo', - 'ref': 'master' - }, - 'install-commands': 0, - }) - - def test_raises_exception_for_list_of_nonstring_install_commands(self): - self.assertRaisesSchemaError({ - 'name': 'hello', - 'kind': 'chunk', - 'source': { - 'repo': 'foo', - 'ref': 'master' - }, - 'install-commands': [0], - }) - def test_accepts_valid_chunk_morphology(self): - chunk = morphlib.morphology.Morphology( + morph = morphlib.morphology.Morphology( MockFile(''' { "name": "hello", @@ -232,130 +41,13 @@ class MorphologyTests(unittest.TestCase): "test-commands": ["make check"], "install-commands": ["make install"] }''')) - self.assertEqual(chunk.kind, 'chunk') - self.assertEqual(chunk.filename, 'mockfile') - - def test_raises_exception_for_stratum_without_sources(self): - self.assertRaisesSchemaError({ 'name': 'hello', 'kind': 'stratum' }) - - def test_raises_exception_for_stratum_with_nondict_sources(self): - self.assertRaisesSchemaError({ - 'name': 'hello', - 'kind': 'stratum', - 'sources': [], - }) - - def test_raises_exception_for_stratum_with_empty_sources(self): - self.assertRaisesSchemaError({ - 'name': 'hello', - 'kind': 'stratum', - 'sources': {}, - }) - - def test_raises_exception_for_stratum_with_bad_children_in_sources(self): - self.assertRaisesSchemaError({ - 'name': 'hello', - 'kind': 'stratum', - 'sources': { - 'foo': 0, - }, - }) - - def test_raises_exception_for_stratum_without_repo_in_sources(self): - self.assertRaisesSchemaError({ - 'name': 'hello', - 'kind': 'stratum', - 'sources': { - 'foo': { - 'ref': 'master' - } - }, - }) - - def test_raises_exception_for_stratum_with_empty_repo_in_sources(self): - self.assertRaisesSchemaError({ - 'name': 'hello', - 'kind': 'stratum', - 'sources': { - 'foo': { - 'repo': '', - 'ref': 'master' - } - }, - }) - - def test_raises_exception_for_stratum_with_nonstring_repo_in_sources(self): - self.assertRaisesSchemaError({ - 'name': 'hello', - 'kind': 'stratum', - 'sources': { - 'foo': { - 'repo': 0, - 'ref': 'master' - } - }, - }) - - def test_raises_exception_for_stratum_without_ref_in_sources(self): - self.assertRaisesSchemaError({ - 'name': 'hello', - 'kind': 'stratum', - 'sources': { - 'foo': { - 'repo': 'foo', - } - }, - }) - - def test_raises_exception_for_stratum_with_empty_ref_in_sources(self): - self.assertRaisesSchemaError({ - 'name': 'hello', - 'kind': 'stratum', - 'sources': { - 'foo': { - 'repo': 'foo', - 'ref': '' - } - }, - }) - - def test_raises_exception_for_stratum_with_nonstring_ref_in_sources(self): - self.assertRaisesSchemaError({ - 'name': 'hello', - 'kind': 'stratum', - 'sources': { - 'foo': { - 'repo': 'foo', - 'ref': 0 - } - }, - }) - - def test_raises_exception_for_stratum_with_unknown_keys_in_sources(self): - self.assertRaisesSchemaError({ - 'name': 'hello', - 'kind': 'stratum', - 'sources': { - 'foo': { - 'repo': 'foo', - 'ref': 'master', - 'x': 'y' - } - }, - }) - - def test_raises_exception_for_stratum_with_unknown_keys(self): - self.assertRaisesSchemaError({ - 'name': 'hello', - 'kind': 'stratum', - 'sources': { - 'foo': { - 'repo': 'foo', - 'ref': 'master' - } - }, - 'x': 'y' - }) + self.assertEqual(morph.name, 'hello') + self.assertEqual(morph.kind, 'chunk') + self.assertEqual(morph.filename, 'mockfile') + self.assertEqual(morph.configure_commands, ['./configure']) + self.assertEqual(morph.build_commands, ['make']) + self.assertEqual(morph.test_commands, ['make check']) + self.assertEqual(morph.install_commands, ['make install']) def test_accepts_valid_stratum_morphology(self): morph = morphlib.morphology.Morphology( @@ -373,6 +65,10 @@ class MorphologyTests(unittest.TestCase): }''')) self.assertEqual(morph.kind, 'stratum') self.assertEqual(morph.filename, 'mockfile') + self.assertEqual(morph.sources, + { + 'foo': { 'repo': 'foo/', 'ref': 'ref' }, + }) def test_accepts_valid_system_morphology(self): morph = morphlib.morphology.Morphology( |