lrossett / centos / centpkg

Forked from centos/centpkg 4 years ago
Clone
c0555c
import os
c0555c
import sys
5becc4
import warnings
c0555c
c0555c
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)),
c0555c
                                '../src'))
5becc4
484791
class _AssertWarnsContext(object):
484791
    def __init__(self, warningtype, testcase, msg=''):
484791
        self.warningtype = warningtype
484791
        warnings.filterwarnings('error')
484791
        self.failureException = testcase.failureException
484791
484791
    def __enter__(self):
484791
        return self
5becc4
484791
    def __exit__(self, exc_type, exc_value, tb):
484791
        if exc_type is None:
484791
            try:
484791
                exc_name = self.warningtype.__name__
484791
            except AttributeError:
484791
                exc_name = str(self.warningtype)
484791
            raise self.failureException(
484791
                "{0} not raised".format(exc_name))
5becc4
484791
        if not issubclass(exc_type, self.warningtype):
484791
            raise self.failureException('"%s" does not match "%s"' %
484791
                    (self.warningtype.__name__, str(exc_type.__name__)))
5becc4
484791
        return True
5becc4
484791
class CatchWarningsMixin(object):
484791
    def assertWarns(self, wrnClass, callableObj=None, *args, **kwargs):
484791
        context = _AssertWarnsContext(wrnClass, self)
484791
        if callableObj is None:
484791
            return context
484791
        with context:
484791
            callableObj(*args, **kwargs)