From 4b320ce0e494213bf110d4db68e1e97cb283f416 Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Wed, 22 Feb 2017 15:16:35 -0600 Subject: [PATCH] Add checks for from module_utils import * (#21800) --- test/sanity/validate-modules/README.rst | 4 ++++ test/sanity/validate-modules/validate-modules | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/test/sanity/validate-modules/README.rst b/test/sanity/validate-modules/README.rst index b94173d28f..427f621894 100644 --- a/test/sanity/validate-modules/README.rst +++ b/test/sanity/validate-modules/README.rst @@ -76,6 +76,8 @@ Errors +---------+--------------------------------------------------------------------------------------------------------------------------------------------+ | 207 | ``REPLACER_WINDOWS`` not found in module | +---------+--------------------------------------------------------------------------------------------------------------------------------------------+ +| 208 | ``module_utils`` imports should import specific components, not ``*`` | ++---------+--------------------------------------------------------------------------------------------------------------------------------------------+ +---------+--------------------------------------------------------------------------------------------------------------------------------------------+ | **3xx** | **Documentation** | +---------+--------------------------------------------------------------------------------------------------------------------------------------------+ @@ -140,6 +142,8 @@ Warnings +=========+============================================================================================================================================+ | **2xx** | **Imports** | +---------+--------------------------------------------------------------------------------------------------------------------------------------------+ +| 208 | ``module_utils`` imports should import specific components for legacy module, not ``*`` | ++---------+--------------------------------------------------------------------------------------------------------------------------------------------+ | 291 | Try/Except ``HAS_`` expression missing | +---------+--------------------------------------------------------------------------------------------------------------------------------------------+ | 292 | Did not find ``ansible.module_utils.basic`` import | diff --git a/test/sanity/validate-modules/validate-modules b/test/sanity/validate-modules/validate-modules index e5c0d3bf01..0cf61c8100 100755 --- a/test/sanity/validate-modules/validate-modules +++ b/test/sanity/validate-modules/validate-modules @@ -364,6 +364,19 @@ class ModuleValidator(Validator): linenos.append(child.lineno) for name in child.names: + if ('module_utils' in getattr(child, 'module', '') and + isinstance(name, ast.alias) and + name.name == '*'): + msg = ( + 208, + ('module_utils imports should import specific ' + 'components, not "*". line %d' % child.lineno) + ) + if self._is_new_module(): + self.errors.append(msg) + else: + self.warnings.append(msg) + if (isinstance(name, ast.alias) and name.name == 'basic'): found_basic = True