Allow salt.utils.warn_until() to not issue the warning because we're only after the RuntimeError behaviour.

This commit is contained in:
Pedro Algarvio 2013-07-20 16:26:47 +01:00
parent 4a9532174e
commit 83004fd8f8
2 changed files with 32 additions and 5 deletions

View File

@ -1333,8 +1333,11 @@ def date_format(date=None, format="%Y-%m-%d"):
return date_cast(date).strftime(format)
def warn_until(version_info, message,
category=DeprecationWarning, stacklevel=None):
def warn_until(version_info,
message,
category=DeprecationWarning,
stacklevel=None,
_dont_call_warnings=False):
'''
Helper function to raise a warning, by default, a ``DeprecationWarning``,
until the provided ``version_info``, after which, a ``RuntimeError`` will
@ -1348,8 +1351,11 @@ def warn_until(version_info, message,
``DeprecationWarning``
:param stacklevel: There should be no need to set the value of
``stacklevel`` salt should be able to do the right thing
:param _dont_call_warnings: This parameter is used just to get the
functionality until the actual error is to be
issued. When we're only after the salt version
checks to raise a ``RuntimeError``.
'''
if not isinstance(version_info, tuple):
raise RuntimeError(
'The \'version_info\' argument should be passed as a tuple.'
@ -1360,7 +1366,7 @@ def warn_until(version_info, message,
stacklevel = 2
if salt.version.__version_info__ >= version_info:
caller = inspect.getframeinfo(sys._getframe(stacklevel-1))
caller = inspect.getframeinfo(sys._getframe(stacklevel - 1))
raise RuntimeError(
'The warning triggered on filename {filename!r}, line number '
'{lineno}, is supposed to be shown until salt {until_version!r} '
@ -1373,4 +1379,5 @@ def warn_until(version_info, message,
),
)
warnings.warn(message, category, stacklevel=stacklevel)
if _dont_call_warnings is False:
warnings.warn(message, category, stacklevel=stacklevel)

View File

@ -52,6 +52,14 @@ class WarnUntilTestCase(TestCase):
'Deprecation Message!', str(recorded_warnings[0].message)
)
# the deprecation warning is not issued because we passed
# _dont_call_warning
with warnings.catch_warnings(record=True) as recorded_warnings:
warn_until(
(0, 17), 'Foo', _dont_call_warnings=True
)
self.assertEqual(0, len(recorded_warnings))
# Let's set version info to (0, 17), a RuntimeError should be raised
salt_version_mock.__version_info__ = (0, 17)
with self.assertRaisesRegexp(
@ -62,6 +70,18 @@ class WarnUntilTestCase(TestCase):
r'remove the warning.'):
raise_warning()
# Even though we're calling warn_until, we pass _dont_call_warnings
# because we're only after the RuntimeError
with self.assertRaisesRegexp(
RuntimeError,
r'The warning triggered on filename \'(.*)warnings_test.py\', '
r'line number ([\d]+), is supposed to be shown until salt '
r'\'0.17\' is released. Salt version is now \'0.17\'. Please '
r'remove the warning.'):
warn_until(
(0, 17), 'Foo', _dont_call_warnings=True
)
if __name__ == '__main__':
from integration import run_tests