mirror of https://github.com/ansible/ansible.git
Add mechanism for storing warnings and deprecations outside of AnsibleModule (#58993)
* Move warn() and deprecate() methods out of basic.py * Use _global_warnings and _global_deprications and create accessor functions - This lays the foundation for future functions being moved outside of AnsibleModule that need an interface to warnings and deprecations without modifying them. * Add unit tests for new warn and deprecate functionspull/66875/head
parent
cc2376b782
commit
3461c682c3
@ -0,0 +1,2 @@
|
||||
minor_changes:
|
||||
- add mechanism for storing warnings and deprecations globally and not attached to an ``AnsibleModule`` object (https://github.com/ansible/ansible/pull/58993)
|
@ -0,0 +1,35 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright (c) 2019 Ansible Project
|
||||
# Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause)
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
__metaclass__ = type
|
||||
|
||||
from ansible.module_utils.six import string_types
|
||||
|
||||
_global_warnings = []
|
||||
_global_deprecations = []
|
||||
|
||||
|
||||
def warn(warning):
|
||||
if isinstance(warning, string_types):
|
||||
_global_warnings.append(warning)
|
||||
else:
|
||||
raise TypeError("warn requires a string not a %s" % type(warning))
|
||||
|
||||
|
||||
def deprecate(msg, version=None):
|
||||
if isinstance(msg, string_types):
|
||||
_global_deprecations.append({'msg': msg, 'version': version})
|
||||
else:
|
||||
raise TypeError("deprecate requires a string not a %s" % type(msg))
|
||||
|
||||
|
||||
def get_warning_messages():
|
||||
"""Return a tuple of warning messages accumulated over this run"""
|
||||
return tuple(_global_warnings)
|
||||
|
||||
|
||||
def get_deprecation_messages():
|
||||
"""Return a tuple of deprecations accumulated over this run"""
|
||||
return tuple(_global_deprecations)
|
@ -0,0 +1,66 @@
|
||||
# -*- 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 absolute_import, division, print_function
|
||||
__metaclass__ = type
|
||||
|
||||
import pytest
|
||||
|
||||
import ansible.module_utils.common.warnings as warnings
|
||||
|
||||
from ansible.module_utils.common.warnings import deprecate, get_deprecation_messages
|
||||
from ansible.module_utils.six import PY3
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def deprecation_messages():
|
||||
return [
|
||||
{'msg': 'First deprecation', 'version': None},
|
||||
{'msg': 'Second deprecation', 'version': '2.14'},
|
||||
{'msg': 'Third deprecation', 'version': '2.9'},
|
||||
]
|
||||
|
||||
|
||||
def test_deprecate_message_only():
|
||||
deprecate('Deprecation message')
|
||||
assert warnings._global_deprecations == [{'msg': 'Deprecation message', 'version': None}]
|
||||
|
||||
|
||||
def test_deprecate_with_version():
|
||||
deprecate(msg='Deprecation message', version='2.14')
|
||||
assert warnings._global_deprecations == [{'msg': 'Deprecation message', 'version': '2.14'}]
|
||||
|
||||
|
||||
def test_multiple_deprecations(deprecation_messages):
|
||||
for d in deprecation_messages:
|
||||
deprecate(**d)
|
||||
|
||||
assert deprecation_messages == warnings._global_deprecations
|
||||
|
||||
|
||||
def test_get_deprecation_messages(deprecation_messages):
|
||||
for d in deprecation_messages:
|
||||
deprecate(**d)
|
||||
|
||||
accessor_deprecations = get_deprecation_messages()
|
||||
assert isinstance(accessor_deprecations, tuple)
|
||||
assert len(accessor_deprecations) == 3
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'test_case',
|
||||
(
|
||||
1,
|
||||
True,
|
||||
[1],
|
||||
{'k1': 'v1'},
|
||||
(1, 2),
|
||||
6.62607004,
|
||||
b'bytestr' if PY3 else None,
|
||||
None,
|
||||
)
|
||||
)
|
||||
def test_deprecate_failure(test_case):
|
||||
with pytest.raises(TypeError, match='deprecate requires a string not a %s' % type(test_case)):
|
||||
deprecate(test_case)
|
@ -0,0 +1,61 @@
|
||||
# -*- 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 absolute_import, division, print_function
|
||||
__metaclass__ = type
|
||||
|
||||
import pytest
|
||||
|
||||
import ansible.module_utils.common.warnings as 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)
|
Loading…
Reference in New Issue