File: conftest.py

package info (click to toggle)
ansible-core 2.19.0~beta6-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 32,628 kB
  • sloc: python: 180,313; cs: 4,929; sh: 4,601; xml: 34; makefile: 21
file content (55 lines) | stat: -rw-r--r-- 1,273 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Copyright (c) 2017 Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import annotations

import sys

import pytest

import ansible.module_utils.basic

from ansible.module_utils.testing import patch_module_args
from ..mock.module import module_env_mocker  # expose shared fixture in this part of the unit test tree

assert module_env_mocker is not None  # avoid unused imports


@pytest.fixture
def stdin(request):
    old_argv = sys.argv
    sys.argv = ['ansible_unittest']

    try:
        args = request.param.copy()
    except AttributeError:
        args = {}

    args.setdefault('_ansible_remote_tmp', '/tmp')
    args.setdefault('_ansible_keep_remote_files', False)
    args.setdefault('_ansible_tracebacks_for', [])

    with patch_module_args(args):
        yield

    sys.argv = old_argv


@pytest.fixture
def am(stdin, request):
    old_argv = sys.argv
    sys.argv = ['ansible_unittest']

    argspec = {}
    if hasattr(request, 'param'):
        if isinstance(request.param, dict):
            argspec = request.param

    am = ansible.module_utils.basic.AnsibleModule(
        argument_spec=argspec,
    )
    am._name = 'ansible_unittest'

    yield am

    sys.argv = old_argv