comps-wrapper: Report unknown package types

When there is a typo in the comps file, instead of crashing with a
non-descript KeyError we should raise a nice error with details about
the problem.

Signed-off-by: Lubomír Sedlář <lsedlar@redhat.com>
This commit is contained in:
Lubomír Sedlář 2017-06-01 09:59:41 +02:00
parent 118444a311
commit 8c237b78c2
3 changed files with 28 additions and 0 deletions

View File

@ -88,6 +88,11 @@ class CompsWrapper(object):
packages_by_type = collections.defaultdict(list)
for pkg in group.packages:
if pkg.type == libcomps.PACKAGE_TYPE_UNKNOWN:
raise RuntimeError(
'Failed to process comps file. Package %s in group %s has unknown type'
% (pkg.name, group.id))
packages_by_type[TYPE_MAPPING[pkg.type]].append(pkg)
for type_name in TYPE_MAPPING.values():

14
tests/fixtures/comps-typo.xml vendored Normal file
View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE comps PUBLIC "-//Red Hat, Inc.//DTD Comps info//EN" "comps.dtd">
<comps>
<group>
<id>core</id>
<name>Core</name>
<description>Smallest possible installation</description>
<default>true</default>
<uservisible>false</uservisible>
<packagelist>
<packagereq type="mndatory">dummy-bash</packagereq>
</packagelist>
</group>
</comps>

View File

@ -19,6 +19,7 @@ COMPS_FILE = os.path.join(FIXTURE_DIR, 'comps.xml')
COMPS_FORMATTED_FILE = os.path.join(FIXTURE_DIR, 'comps-formatted.xml')
COMPS_GROUP_FILE = os.path.join(FIXTURE_DIR, 'comps-group.xml')
COMPS_ENVIRONMENT_FILE = os.path.join(FIXTURE_DIR, 'comps-env.xml')
COMPS_FILE_WITH_TYPO = os.path.join(FIXTURE_DIR, 'comps-typo.xml')
class CompsWrapperTest(unittest.TestCase):
@ -77,3 +78,11 @@ class CompsWrapperTest(unittest.TestCase):
]
comps.filter_environments(groups)
self.assertEqual(groups, [{"name": "minimal", "display_order": 99, "groups": ["core"]}])
def test_report_typo_in_package_type(self):
comps = CompsWrapper(COMPS_FILE_WITH_TYPO)
with self.assertRaises(RuntimeError) as ctx:
comps.write_comps(target_file=self.file.name)
self.assertIn(
'Package dummy-bash in group core has unknown type',
str(ctx.exception))