From fca2f088ea5e5ef83e5b9c24103bed6fbd1bacaf Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Fri, 12 Feb 2016 14:29:17 -0600 Subject: [PATCH] Verify that new arguments have a correct version added. Fixes #16 --- ansible_testing/modules.py | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/ansible_testing/modules.py b/ansible_testing/modules.py index 8bd2ea7cd90..8438a450db5 100644 --- a/ansible_testing/modules.py +++ b/ansible_testing/modules.py @@ -350,6 +350,41 @@ class ModuleValidator(Validator): self.errors.append('version_added should be %s. Currently %s' % (should_be, version_added)) + def _check_for_new_args(self, doc): + if self._is_new_module(): + return + + existing = module_loader.find_plugin(self.name) + existing_doc, _, _ = get_docstring(existing, verbose=True) + existing_options = existing_doc.get('options', {}) + + options = doc.get('options', {}) + + strict_ansible_version = StrictVersion(ansible_version) + should_be = '.'.join(ansible_version.split('.')[:2]) + + for option, details in options.iteritems(): + new = not bool(existing_options.get(option)) + if not new: + continue + + try: + version_added = StrictVersion( + str(details.get('version_added', '0.0')) + ) + except ValueError: + version_added = details.get('version_added', '0.0') + self.errors.append('version_added for new option (%s) ' + 'is not a valid version number: %r' % + (option, version_added)) + continue + + if (version_added < strict_ansible_version or + strict_ansible_version < version_added): + self.errors.append('version_added for new option (%s) should ' + 'be %s. Currently %s' % + (option, should_be, version_added)) + def validate(self): super(ModuleValidator, self).validate() @@ -378,7 +413,7 @@ class ModuleValidator(Validator): if self._python_module(): sys_stdout = sys.stdout sys_stderr = sys.stderr - sys.stdout = sys.stderr = StringIO() + sys.stdout = sys.stderr = buf = StringIO() setattr(sys.stdout, 'encoding', sys_stdout.encoding) setattr(sys.stderr, 'encoding', sys_stderr.encoding) try: @@ -397,6 +432,7 @@ class ModuleValidator(Validator): self.errors.append('Invalid or no DOCUMENTATION provided') else: self._check_version_added(doc) + self._check_for_new_args(doc) if not bool(examples): self.errors.append('No EXAMPLES provided') if not bool(ret):