Give an explicit error message when unsupported view type used

When specifying a view type that isn't supported by any modules (such as
"list-view" instead of "list"), this ensures that the user is guided
towards something correct.

This changes the error message in this case from:

AttributeError: 'NoneType' object has no attribute 'name'

to:

jenkins_jobs.errors.JenkinsJobsException: Unrecognized view type: list-view (supported types are: list, pipeline)

Change-Id: I0ee800db1c9c8aeecffcf11f1e86c03ba0590da8
This commit is contained in:
Daniel Watkins 2017-08-29 16:39:41 +01:00
parent 41c54bba00
commit e6d6c276d1
3 changed files with 25 additions and 0 deletions

View File

@ -123,6 +123,12 @@ class XmlViewGenerator(object):
self._gen_xml(xml, data)
view = XmlJob(xml, data['name'])
return view
names = [
ep.name for ep in pkg_resources.iter_entry_points(
group='jenkins_jobs.views')]
raise errors.JenkinsJobsException(
'Unrecognized view type: {} (supported types are: {})'.format(
kind, ', '.join(names)))
def _gen_xml(self, xml, data):
for module in self.registry.modules:

View File

@ -0,0 +1,3 @@
- view:
name: invalid-view-type
view-type: invalid

View File

@ -42,6 +42,22 @@ class TestXmlJobGeneratorExceptions(base.BaseTestCase):
xml_generator.generateXML, job_data)
self.assertIn("Unrecognized project type:", str(e))
def test_invalid_view(self):
self.conf_filename = None
config = self._get_config()
yp = parser.YamlParser(config)
yp.parse(os.path.join(self.fixtures_path, "invalid_view.yaml"))
reg = registry.ModuleRegistry(config)
_, view_data = yp.expandYaml(reg)
# Generate the XML tree
xml_generator = xml_config.XmlViewGenerator(reg)
e = self.assertRaises(errors.JenkinsJobsException,
xml_generator.generateXML, view_data)
self.assertIn("Unrecognized view type:", str(e))
def test_incorrect_template_params(self):
self.conf_filename = None
config = self._get_config()