Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/modules/conftest.py

raw
1 # Copyright (c) 2017 Ansible Project
2 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5 from __future__ import annotations
6
7 import contextlib as _contextlib
8 import json
9 from collections.abc import MutableMapping
10
11 import pytest
12 from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import (
13 set_module_args as _set_module_args,
14 )
15
16 from ansible_collections.community.general.plugins.module_utils import _deps as deps
17
18
19 def _fix_ansible_args(args):
20 if isinstance(args, str):
21 # This should be deprecated!
22 return json.loads(args)
23
24 if isinstance(args, MutableMapping):
25 return args
26
27 raise Exception("Malformed data to the patch_ansible_module pytest fixture")
28
29
30 @pytest.fixture
31 def patch_ansible_module(request):
32 args = _fix_ansible_args(request.param)
33 with _set_module_args(args):
34 yield
35
36
37 @pytest.fixture
38 def patch_ansible_module_uthelper(request):
39 @_contextlib.contextmanager
40 def _patch(args):
41 args = _fix_ansible_args(args)
42 with _set_module_args(args):
43 yield
44
45 return _patch
46
47
48 @pytest.fixture(autouse=True)
49 def deps_cleanup():
50 deps.clear()