Blob Blame History Raw
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Fedora Python maintainers <python-devel@lists.fedoraproject.org>
Date: Wed, 22 Jul 2020 16:47:25 +0200
Subject: [PATCH] 00132-add-rpmbuild-hooks-to-unittest.patch

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 a5d50af78f2..f1068a5c549 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 8a9f1c0a9d7..0d53542d994 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
@@ -130,6 +131,43 @@ class _BaseTestCaseContext:
         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, callable_obj=None,