-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deprecations fixture #19
Open
brantlk
wants to merge
11
commits into
testing-cabal:master
Choose a base branch
from
brantlk:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c378a3d
Deprecations fixture
55c3636
Import warnings in README
df856f0
Use context for Deprecations fixture in example
7939779
Cleanups
659dfcc
Merge remote-tracking branch 'remotes/upstream/master' into Deprecations
0220697
Revert unnecessary change
0d0f027
Use catch_warnings
fc7337d
Rename expect_deprecations_here to ignore_deprecations_here
46e10c8
Undo unrelated change
f7e6113
Add test for multiple instances
f4eacf2
Add note about modifying global state
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ AUTHORS | |
ChangeLog | ||
.eggs | ||
build | ||
.tox |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# Copyright 2015 IBM Corp. | ||
# | ||
# 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 contextlib | ||
import re | ||
import warnings # conflicts with the local warnings module so absolute_import | ||
|
||
import fixtures | ||
|
||
|
||
class Deprecations(fixtures.Fixture): | ||
"""Prevent calls to deprecated function. | ||
|
||
This fixture can be added to a testcase to ensure that the code under test | ||
isn't calling deprecated function. It sets Python's `warnings` module for | ||
the module under test to "error" so that DeprecationWarning will be | ||
raised. | ||
|
||
You might want your application to not use any deprecated function. | ||
Deprecated function is going to be removed and sometimes is being removed | ||
because it's buggy and you shouldn't be using it. | ||
|
||
It can be difficult to tell just through code reviews that new code is | ||
calling deprecated function. This fixture can be used to protect you from | ||
developers proposing changes that use deprecated function. | ||
|
||
It can also be useful to be able to test if your application is still using | ||
some function that's been newly deprecated. | ||
|
||
:param str module: The name of the module. Deprecated function called from | ||
this module will be errors. | ||
|
||
""" | ||
|
||
def __init__(self, module): | ||
super(Deprecations, self).__init__() | ||
self._module = module | ||
|
||
def _setUp(self): | ||
module_regex = '^%s' % re.escape(self._module + '.') | ||
warnings.filterwarnings('error', category=DeprecationWarning, | ||
module=module_regex) | ||
self.addCleanup(warnings.resetwarnings) | ||
|
||
def expect_deprecations(self): | ||
"""Indicate that this test expects to call deprecated function. | ||
|
||
If you've got a test that you expect to call deprecated function and | ||
you don't want it to fail call this at the start of the test. | ||
|
||
""" | ||
warnings.resetwarnings() | ||
|
||
@contextlib.contextmanager | ||
def expect_deprecations_here(self): | ||
"""This section of code expects to call deprecated function. | ||
|
||
If you've got a test that part of it is testing deprecated function | ||
then wrap the part in this context manager:: | ||
|
||
with self.deprecations.expect_deprecations_here(): | ||
call_deprecated_function() | ||
|
||
""" | ||
warnings.resetwarnings() | ||
|
||
yield | ||
|
||
module_regex = '^%s' % re.escape(self._module + '.') | ||
warnings.filterwarnings('error', category=DeprecationWarning, | ||
module=module_regex) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Copyright 2015 IBM Corp. | ||
# | ||
# 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. | ||
|
||
import warnings | ||
|
||
import testtools | ||
|
||
from fixtures import Deprecations | ||
|
||
|
||
MODULE = 'fixtures' | ||
|
||
|
||
class TestDeprecations(testtools.TestCase): | ||
def test_enabled_raises(self): | ||
# When the Deprecations fixture is active, calling deprecated function | ||
# is an error. | ||
self.useFixture(Deprecations(MODULE)) | ||
self.assertRaises( | ||
DeprecationWarning, | ||
lambda: warnings.warn('message ignored', DeprecationWarning)) | ||
|
||
def test_expect_deprecations(self): | ||
# When expect_deprecations in a test, deprecations are not an error. | ||
deprecations = self.useFixture(Deprecations(MODULE)) | ||
deprecations.expect_deprecations() | ||
warnings.warn('message ignored', DeprecationWarning) | ||
|
||
def test_expect_deprecations_here(self): | ||
# While in the expect_deprecations_here() context, deprecations are not | ||
# errors, and afterwards deprecations are errors. | ||
deprecations = self.useFixture(Deprecations(MODULE)) | ||
with deprecations.expect_deprecations_here(): | ||
warnings.warn('message ignored', DeprecationWarning) | ||
self.assertRaises( | ||
DeprecationWarning, | ||
lambda: warnings.warn('message ignored', DeprecationWarning)) | ||
|
||
def test_other_module(self): | ||
# When the Deprecations fixture is active, deprecations from other | ||
# modules are ignored. | ||
self.useFixture(Deprecations('different_module')) | ||
warnings.warn('message ignored', DeprecationWarning) | ||
|
||
def test_null_case(self): | ||
# When the Deprecations fixture isn't used then deprecations are not | ||
# errors. This shows that python works as expected. | ||
warnings.warn('message ignored', DeprecationWarning) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'll need to import warnings here.
You can make this a little cleaner by writing the test using with (we no longer support 2.5)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
imported warnings and now local tox -e py27 works. Also switched to using context for the setUp/cleanUp.