Add ability to disable warnings being emitted

Provide a helpful fixture that can be used
to disable warnings being output, for testing, or other
purporses.

Closes-Bug: #1503918

Change-Id: I2e1fd6f427ff6ee6d0b11b86ac2ff1b75dc0548c
This commit is contained in:
Joshua Harlow 2015-10-08 09:11:49 -07:00 committed by Joshua Harlow
parent c788514836
commit 5fc9662d6c
6 changed files with 62 additions and 0 deletions

View File

@ -30,6 +30,7 @@ except AttributeError:
# and see https://docs.python.org/2/reference/executionmodel.html (and likely # and see https://docs.python.org/2/reference/executionmodel.html (and likely
# others)... # others)...
_BUILTIN_MODULES = ('builtins', '__builtin__', '__builtins__', 'exceptions') _BUILTIN_MODULES = ('builtins', '__builtin__', '__builtins__', 'exceptions')
_enabled = True
def deprecation(message, stacklevel=None, category=None): def deprecation(message, stacklevel=None, category=None):
@ -49,6 +50,8 @@ def deprecation(message, stacklevel=None, category=None):
avoid doing by always giving at *least* N + 1 release for users to address avoid doing by always giving at *least* N + 1 release for users to address
the deprecation warnings). the deprecation warnings).
""" """
if not _enabled:
return
if category is None: if category is None:
category = DeprecationWarning category = DeprecationWarning
if stacklevel is None: if stacklevel is None:

View File

View File

@ -0,0 +1,39 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2015 Yahoo! Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from __future__ import absolute_import
import fixtures
from debtcollector import _utils
class DisableFixture(fixtures.Fixture):
"""Fixture that disables debtcollector triggered warnings.
This does **not** disable warnings calls emitted by other libraries.
This can be used like::
from debtcollector.fixtures import disable
with disable.DisableFixture():
<some code that calls into depreciated code>
"""
def _setUp(self):
self.addCleanup(setattr, _utils, "_enabled", True)
_utils._enabled = False

View File

@ -15,6 +15,7 @@
import warnings import warnings
import debtcollector import debtcollector
from debtcollector.fixtures import disable
from debtcollector import moves from debtcollector import moves
from debtcollector import removals from debtcollector import removals
from debtcollector import renames from debtcollector import renames
@ -225,6 +226,19 @@ class MovedPropertyTest(test_base.TestCase):
self.assertEqual(0, len(capture)) self.assertEqual(0, len(capture))
class DisabledTest(test_base.TestCase):
def test_basics(self):
dog = WoofWoof()
c = KittyKat()
with warnings.catch_warnings(record=True) as capture:
warnings.simplefilter("always")
with disable.DisableFixture():
self.assertTrue(yellowish_sun())
self.assertEqual('woof', dog.berk)
self.assertEqual('supermeow', c.meow())
self.assertEqual(0, len(capture))
class MovedFunctionTest(test_base.TestCase): class MovedFunctionTest(test_base.TestCase):
def test_basics(self): def test_basics(self):
self.assertTrue(yellowish_sun()) self.assertTrue(yellowish_sun())

View File

@ -31,3 +31,8 @@ Removals
-------- --------
.. automodule:: debtcollector.removals .. automodule:: debtcollector.removals
Fixtures
--------
.. automodule:: debtcollector.fixtures.disable

View File

@ -13,4 +13,5 @@ oslotest>=1.10.0 # Apache-2.0
testrepository>=0.0.18 testrepository>=0.0.18
testscenarios>=0.4 testscenarios>=0.4
testtools>=1.4.0 testtools>=1.4.0
fixtures>=1.3.1
doc8 # Apache-2.0 doc8 # Apache-2.0