From cb1585be36fb9178b46e76d95d0e992e03dadd60 Mon Sep 17 00:00:00 2001 From: Fedora Python maintainers Date: Wed, 15 Jul 2020 15:22:59 +0200 Subject: 00132-add-rpmbuild-hooks-to-unittest.patch 00132 # Add non-standard hooks to unittest for use in the "check" phase below, when running selftests within the build: @unittest._skipInRpmBuild(reason) for tests that hang or fail intermittently within the build environment, and: @unittest._expectedFailureInRpmBuild for tests that always fail within the build environment The hooks only take effect if WITHIN_PYTHON_RPM_BUILD is set in the environment, which we set manually in the appropriate portion of the "check" phase below (and which potentially other python-* rpms could set, to reuse these unittest hooks in their own "check" phases) --- Lib/unittest/__init__.py | 3 ++- Lib/unittest/case.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/Lib/unittest/__init__.py b/Lib/unittest/__init__.py index a5d50af..f1068a5 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 a3f75af..96bd5a7 100644 --- a/Lib/unittest/case.py +++ b/Lib/unittest/case.py @@ -1,6 +1,7 @@ """Test case implementation""" import collections +import os import sys import functools import difflib @@ -95,6 +96,43 @@ def expectedFailure(func): return wrapper +# 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 _AssertRaisesContext(object): """A context manager used to implement TestCase.assertRaises* methods.""" -- cgit v0.12