From e6d6c276d154afa4908c83ad90d1f8a36ffdbd92 Mon Sep 17 00:00:00 2001 From: Daniel Watkins Date: Tue, 29 Aug 2017 16:39:41 +0100 Subject: [PATCH] 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 --- jenkins_jobs/xml_config.py | 6 ++++++ tests/xml_config/exceptions/invalid_view.yaml | 3 +++ tests/xml_config/test_xml_config.py | 16 ++++++++++++++++ 3 files changed, 25 insertions(+) create mode 100644 tests/xml_config/exceptions/invalid_view.yaml diff --git a/jenkins_jobs/xml_config.py b/jenkins_jobs/xml_config.py index f145e7d76..52c37f3c1 100644 --- a/jenkins_jobs/xml_config.py +++ b/jenkins_jobs/xml_config.py @@ -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: diff --git a/tests/xml_config/exceptions/invalid_view.yaml b/tests/xml_config/exceptions/invalid_view.yaml new file mode 100644 index 000000000..6393f8e83 --- /dev/null +++ b/tests/xml_config/exceptions/invalid_view.yaml @@ -0,0 +1,3 @@ +- view: + name: invalid-view-type + view-type: invalid diff --git a/tests/xml_config/test_xml_config.py b/tests/xml_config/test_xml_config.py index c3016fa6a..e50fc958f 100644 --- a/tests/xml_config/test_xml_config.py +++ b/tests/xml_config/test_xml_config.py @@ -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()