Debian Codegraph @

test/units/module_utils/common/warnings/test_deprecate.py

raw
1 # -*- coding: utf-8 -*-
2 # (c) 2019 Ansible Project
3 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
4
5 # pylint: disable=ansible-deprecated-no-version # arg-splatting prevents pylint from understanding how deprecate is being called
6
7 from __future__ import annotations
8
9 import typing as t
10
11 import pytest
12
13 from ansible.module_utils._internal import _traceback, _messages, _deprecator
14 from ansible.module_utils.basic import AnsibleModule
15 from ansible.module_utils.common import warnings
16 from ansible.module_utils.common.warnings import deprecate
17 from ansible.module_utils.testing import patch_module_args
18 from units.mock.module import ModuleEnvMocker
19
20 pytestmark = pytest.mark.usefixtures("as_target", "module_env_mocker")
21
22
23 def test_deprecate() -> None:
24 deprecate('Deprecation message')
25 assert warnings.get_deprecation_messages() == (dict(msg='Deprecation message', collection_name=None, version=None),)
26 assert warnings.get_deprecations() == [_messages.DeprecationSummary(
27 event=_messages.Event(msg='Deprecation message'),
28 deprecator=_deprecator.INDETERMINATE_DEPRECATOR,
29 )]
30
31
32 def test_deprecate_via_module() -> None:
33 with patch_module_args():
34 am = AnsibleModule(argument_spec={})
35
36 am.deprecate('Deprecation message')
37
38 assert warnings.get_deprecation_messages() == (dict(msg='Deprecation message', collection_name=None, version=None),)
39 assert warnings.get_deprecations() == [_messages.DeprecationSummary(
40 event=_messages.Event(msg='Deprecation message'),
41 deprecator=_deprecator.INDETERMINATE_DEPRECATOR,
42 )]
43
44
45 def test_dedupe_with_traceback(module_env_mocker: ModuleEnvMocker) -> None:
46 module_env_mocker.set_traceback_config([_traceback.TracebackEvent.DEPRECATED])
47 deprecate_args: dict[str, t.Any] = dict(msg="same", version="1.2.3", collection_name="blar.blar")
48
49 # DeprecationSummary dataclass object hash is the dedupe key; presence of differing tracebacks or SourceContexts affects de-dupe
50
51 for _i in range(0, 10):
52 # same location, same traceback- should be collapsed to one message
53 deprecate(**deprecate_args)
54
55 assert len(warnings.get_deprecation_messages()) == 1
56 assert len(warnings.get_deprecations()) == 1
57
58 for _i in range(0, 10):
59 deprecate(**deprecate_args) # with tracebacks on, we should have a different source location than the first loop, but still de-dupe
60
61 assert len(warnings.get_deprecation_messages()) == 2
62 assert len(warnings.get_deprecations()) == 2
63
64
65 @pytest.mark.parametrize(
66 'test_case',
67 (
68 1,
69 True,
70 [1],
71 {'k1': 'v1'},
72 (1, 2),
73 6.62607004,
74 b'bytestr',
75 None,
76 )
77 )
78 def test_deprecate_failure(test_case):
79 with pytest.raises(TypeError, match=f"must be <class 'str'> instead of {type(test_case)}"):
80 deprecate(test_case) # pylint: disable=ansible-deprecated-no-version