summaryrefslogtreecommitdiffstats
path: root/test/units/module_utils/common/warnings/test_warn.py
blob: 85998b8419d0fd83fd150961f5b8ab891660e522 (plain)
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
56
57
58
59
60
# -*- coding: utf-8 -*-
# (c) 2019 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 pytest

from ansible.module_utils.common import warnings

from ansible.module_utils.common.warnings import warn, get_warning_messages
from ansible.module_utils.six import PY3


@pytest.fixture
def warning_messages():
    return [
        'First warning',
        'Second warning',
        'Third warning',
    ]


def test_warn():
    warn('Warning message')
    assert warnings._global_warnings == ['Warning message']


def test_multiple_warningss(warning_messages):
    for w in warning_messages:
        warn(w)

    assert warning_messages == warnings._global_warnings


def test_get_warning_messages(warning_messages):
    for w in warning_messages:
        warn(w)

    accessor_warnings = get_warning_messages()
    assert isinstance(accessor_warnings, tuple)
    assert len(accessor_warnings) == 3


@pytest.mark.parametrize(
    'test_case',
    (
        1,
        True,
        [1],
        {'k1': 'v1'},
        (1, 2),
        6.62607004,
        b'bytestr' if PY3 else None,
        None,
    )
)
def test_warn_failure(test_case):
    with pytest.raises(TypeError, match='warn requires a string not a %s' % type(test_case)):
        warn(test_case)