From 5fc9662d6c6568a0b8402959e95a6618c5c744cc Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Thu, 8 Oct 2015 09:11:49 -0700 Subject: [PATCH] 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 --- debtcollector/_utils.py | 3 ++ debtcollector/fixtures/__init__.py | 0 debtcollector/fixtures/disable.py | 39 +++++++++++++++++++++++++ debtcollector/tests/test_deprecation.py | 14 +++++++++ doc/source/api.rst | 5 ++++ test-requirements.txt | 1 + 6 files changed, 62 insertions(+) create mode 100644 debtcollector/fixtures/__init__.py create mode 100644 debtcollector/fixtures/disable.py diff --git a/debtcollector/_utils.py b/debtcollector/_utils.py index 0086970..2a65553 100644 --- a/debtcollector/_utils.py +++ b/debtcollector/_utils.py @@ -30,6 +30,7 @@ except AttributeError: # and see https://docs.python.org/2/reference/executionmodel.html (and likely # others)... _BUILTIN_MODULES = ('builtins', '__builtin__', '__builtins__', 'exceptions') +_enabled = True 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 the deprecation warnings). """ + if not _enabled: + return if category is None: category = DeprecationWarning if stacklevel is None: diff --git a/debtcollector/fixtures/__init__.py b/debtcollector/fixtures/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/debtcollector/fixtures/disable.py b/debtcollector/fixtures/disable.py new file mode 100644 index 0000000..431b4f8 --- /dev/null +++ b/debtcollector/fixtures/disable.py @@ -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(): + + """ + + def _setUp(self): + self.addCleanup(setattr, _utils, "_enabled", True) + _utils._enabled = False diff --git a/debtcollector/tests/test_deprecation.py b/debtcollector/tests/test_deprecation.py index 120fc6c..2a4f03b 100644 --- a/debtcollector/tests/test_deprecation.py +++ b/debtcollector/tests/test_deprecation.py @@ -15,6 +15,7 @@ import warnings import debtcollector +from debtcollector.fixtures import disable from debtcollector import moves from debtcollector import removals from debtcollector import renames @@ -225,6 +226,19 @@ class MovedPropertyTest(test_base.TestCase): 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): def test_basics(self): self.assertTrue(yellowish_sun()) diff --git a/doc/source/api.rst b/doc/source/api.rst index 4469ff7..10761a9 100644 --- a/doc/source/api.rst +++ b/doc/source/api.rst @@ -31,3 +31,8 @@ Removals -------- .. automodule:: debtcollector.removals + +Fixtures +-------- + +.. automodule:: debtcollector.fixtures.disable diff --git a/test-requirements.txt b/test-requirements.txt index d3cad4e..7b339e7 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -13,4 +13,5 @@ oslotest>=1.10.0 # Apache-2.0 testrepository>=0.0.18 testscenarios>=0.4 testtools>=1.4.0 +fixtures>=1.3.1 doc8 # Apache-2.0