Debian Codegraph @

test/units/module_utils/conftest.py

raw
1 # Copyright (c) 2017 Ansible Project
2 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3
4 from __future__ import annotations
5
6 import sys
7
8 import pytest
9
10 import ansible.module_utils.basic
11
12 from ansible.module_utils.testing import patch_module_args
13 from ..mock.module import module_env_mocker # expose shared fixture in this part of the unit test tree
14
15 assert module_env_mocker is not None # avoid unused imports
16
17
18 @pytest.fixture
19 def stdin(request):
20 old_argv = sys.argv
21 sys.argv = ['ansible_unittest']
22
23 try:
24 args = request.param.copy()
25 except AttributeError:
26 args = {}
27
28 args.setdefault('_ansible_remote_tmp', '/tmp')
29 args.setdefault('_ansible_keep_remote_files', False)
30 args.setdefault('_ansible_tracebacks_for', [])
31 args.setdefault('_ansible_inject_invocation', True)
32
33 with patch_module_args(args):
34 yield
35
36 sys.argv = old_argv
37
38
39 @pytest.fixture
40 def am(stdin, request):
41 old_argv = sys.argv
42 sys.argv = ['ansible_unittest']
43
44 argspec = {}
45 if hasattr(request, 'param'):
46 if isinstance(request.param, dict):
47 argspec = request.param
48
49 am = ansible.module_utils.basic.AnsibleModule(
50 argument_spec=argspec,
51 )
52 am._name = 'ansible_unittest'
53
54 yield am
55
56 sys.argv = old_argv