mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 21:00:22 -07:00
Clean up imports to prevent issues with mutable data being used in modules (#35112)
* Clean up imports to prevent issues with mutable data being used in modules * Remove un-needed mock
This commit is contained in:
parent
bf1580a333
commit
f9c2c9570d
2 changed files with 9 additions and 37 deletions
|
@ -41,8 +41,8 @@ class AnsibleModuleImportError(ImportError):
|
|||
|
||||
@contextmanager
|
||||
def add_mocks(filename):
|
||||
gp = mock.patch('ansible.module_utils.basic.get_platform').start()
|
||||
gp.return_value = 'linux'
|
||||
# Used to clean up imports later
|
||||
pre_sys_modules = list(sys.modules.keys())
|
||||
|
||||
module_mock = mock.MagicMock()
|
||||
mocks = []
|
||||
|
@ -62,6 +62,13 @@ def add_mocks(filename):
|
|||
for m in mocks:
|
||||
m.stop()
|
||||
|
||||
# Clean up imports to prevent issues with mutable data being used in modules
|
||||
for k in list(sys.modules.keys()):
|
||||
# It's faster if we limit to items in ansible.module_utils
|
||||
# But if this causes problems later, we should remove it
|
||||
if k not in pre_sys_modules and k.startswith('ansible.module_utils.'):
|
||||
del sys.modules[k]
|
||||
|
||||
|
||||
def get_argument_spec(filename):
|
||||
with add_mocks(filename) as module_mock:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue