commit 243c1a28a8dc09969c63ea2b1adde80df7e284b5 Author: Philippe Makowski <pmakowski@espelida.com> Date: Sun Jul 3 16:40:24 2016 +0200 add-rpmbuild-hooks-to-unittest diff --git a/Lib/unittest/__init__.py b/Lib/unittest/__init__.py index 7f61a80..5ab6e33 100644 --- a/Lib/unittest/__init__.py +++ b/Lib/unittest/__init__.py @@ -57,7 +57,8 @@ __unittest = True from .result import TestResult from .case import (TestCase, FunctionTestCase, SkipTest, skip, skipIf, - skipUnless, expectedFailure) + skipUnless, expectedFailure, + _skipInRpmBuild, _expectedFailureInRpmBuild) from .suite import BaseTestSuite, TestSuite from .loader import (TestLoader, defaultTestLoader, makeSuite, getTestCaseNames, findTestCases) diff --git a/Lib/unittest/case.py b/Lib/unittest/case.py index 524a7b1..2ec6b35 100644 --- a/Lib/unittest/case.py +++ b/Lib/unittest/case.py @@ -3,6 +3,7 @@ import sys import functools import difflib +import os import logging import pprint import re @@ -133,6 +134,43 @@ class _BaseTestCaseContext: msg = self.test_case._formatMessage(self.msg, standardMsg) raise self.test_case.failureException(msg) +# Non-standard/downstream-only hooks for handling issues with specific test +# cases: + +def _skipInRpmBuild(reason): + """ + Non-standard/downstream-only decorator for marking a specific unit test + to be skipped when run within the %check of an rpmbuild. + + Specifically, this takes effect when WITHIN_PYTHON_RPM_BUILD is set within + the environment, and has no effect otherwise. + """ + if 'WITHIN_PYTHON_RPM_BUILD' in os.environ: + return skip(reason) + else: + return _id + +def _expectedFailureInRpmBuild(func): + """ + Non-standard/downstream-only decorator for marking a specific unit test + as expected to fail within the %check of an rpmbuild. + + Specifically, this takes effect when WITHIN_PYTHON_RPM_BUILD is set within + the environment, and has no effect otherwise. + """ + @functools.wraps(func) + def wrapper(*args, **kwargs): + if 'WITHIN_PYTHON_RPM_BUILD' in os.environ: + try: + func(*args, **kwargs) + except Exception: + raise _ExpectedFailure(sys.exc_info()) + raise _UnexpectedSuccess + else: + # Call directly: + func(*args, **kwargs) + return wrapper + class _AssertRaisesBaseContext(_BaseTestCaseContext): def __init__(self, expected, test_case, expected_regex=None):