mirror of
https://git.centos.org/centos/centpkg.git
synced 2025-02-23 08:12:55 +00:00
moving the mixins to their own module to hide them in stackframes
Reference: https://mail.python.org/pipermail/python-list/2012-October/632386.html
This commit is contained in:
parent
cf167f1c37
commit
ddb17b05cb
3 changed files with 36 additions and 34 deletions
|
@ -1,38 +1,6 @@
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import warnings
|
|
||||||
|
|
||||||
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)),
|
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)),
|
||||||
'../src'))
|
'../src'))
|
||||||
|
|
||||||
class _AssertWarnsContext(object):
|
|
||||||
def __init__(self, warningtype, testcase, msg=''):
|
|
||||||
self.warningtype = warningtype
|
|
||||||
warnings.filterwarnings('error')
|
|
||||||
self.failureException = testcase.failureException
|
|
||||||
|
|
||||||
def __enter__(self):
|
|
||||||
return self
|
|
||||||
|
|
||||||
def __exit__(self, exc_type, exc_value, tb):
|
|
||||||
if exc_type is None:
|
|
||||||
try:
|
|
||||||
exc_name = self.warningtype.__name__
|
|
||||||
except AttributeError:
|
|
||||||
exc_name = str(self.warningtype)
|
|
||||||
raise self.failureException(
|
|
||||||
"{0} not raised".format(exc_name))
|
|
||||||
|
|
||||||
if not issubclass(exc_type, self.warningtype):
|
|
||||||
raise self.failureException('"%s" does not match "%s"' %
|
|
||||||
(self.warningtype.__name__, str(exc_type.__name__)))
|
|
||||||
|
|
||||||
return True
|
|
||||||
|
|
||||||
class CatchWarningsMixin(object):
|
|
||||||
def assertWarns(self, wrnClass, callableObj=None, *args, **kwargs):
|
|
||||||
context = _AssertWarnsContext(wrnClass, self)
|
|
||||||
if callableObj is None:
|
|
||||||
return context
|
|
||||||
with context:
|
|
||||||
callableObj(*args, **kwargs)
|
|
||||||
|
|
35
tests/mixins.py
Normal file
35
tests/mixins.py
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
import warnings
|
||||||
|
|
||||||
|
__unittest=True
|
||||||
|
|
||||||
|
class _AssertWarnsContext(object):
|
||||||
|
def __init__(self, warningtype, testcase, msg=''):
|
||||||
|
self.warningtype = warningtype
|
||||||
|
warnings.filterwarnings('error')
|
||||||
|
self.failureException = testcase.failureException
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, exc_type, exc_value, tb):
|
||||||
|
if exc_type is None:
|
||||||
|
try:
|
||||||
|
exc_name = self.warningtype.__name__
|
||||||
|
except AttributeError:
|
||||||
|
exc_name = str(self.warningtype)
|
||||||
|
raise self.failureException(
|
||||||
|
"{0} not raised".format(exc_name))
|
||||||
|
|
||||||
|
if not issubclass(exc_type, self.warningtype):
|
||||||
|
raise self.failureException('"%s" does not match "%s"' %
|
||||||
|
(self.warningtype.__name__, str(exc_type.__name__)))
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
class CatchWarningsMixin(object):
|
||||||
|
def assertWarns(self, wrnClass, callableObj=None, *args, **kwargs):
|
||||||
|
context = _AssertWarnsContext(wrnClass, self)
|
||||||
|
if callableObj is None:
|
||||||
|
return context
|
||||||
|
with context:
|
||||||
|
callableObj(*args, **kwargs)
|
|
@ -1,7 +1,6 @@
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
|
from mixins import CatchWarningsMixin
|
||||||
from . import CatchWarningsMixin
|
|
||||||
from centpkg import DistGitDirectory
|
from centpkg import DistGitDirectory
|
||||||
|
|
||||||
class TestDistGitNothing(unittest.TestCase):
|
class TestDistGitNothing(unittest.TestCase):
|
||||||
|
|
Loading…
Add table
Reference in a new issue