Bug 1595814 - work around marionette dependency on private classes in Python2 unittest/case.py, r=whimboo. draft
authorBob Clary <bclary@bclary.com>
Tue, 19 May 2020 14:02:11 -0700
changeset 2913086 0959979c79d636ea462372a16a4d6861cd896993
parent 2913085 6191dcf6f130dd30fe25b93836231b0d122bc3e9
child 2913087 874f079b181aac0377b53e9b882ab6aa54df8c8b
push id542231
push userbclary@mozilla.com
push dateSun, 24 May 2020 06:08:15 +0000
treeherdertry@5bd349510b42 [default view] [failures only]
reviewerswhimboo
bugs1595814
milestone78.0a1
Bug 1595814 - work around marionette dependency on private classes in Python2 unittest/case.py, r=whimboo. Differential Revision: https://phabricator.services.mozilla.com/D76494
testing/marionette/harness/marionette_harness/marionette_test/testcases.py
--- a/testing/marionette/harness/marionette_harness/marionette_test/testcases.py
+++ b/testing/marionette/harness/marionette_harness/marionette_test/testcases.py
@@ -9,29 +9,60 @@ import os
 import re
 import sys
 import time
 import types
 import unittest
 import warnings
 import weakref
 
-from unittest.case import (
-    _ExpectedFailure,
-    _UnexpectedSuccess,
-    SkipTest,
-)
+from unittest.case import SkipTest
 
 from marionette_driver.errors import (
     TimeoutException,
     UnresponsiveInstanceException
 )
 from mozlog import get_default_logger
 
 
+# ExpectedFailure and UnexpectedSuccess are adapted from the Python 2
+# private classes _ExpectedFailure and _UnexpectedSuccess in
+# unittest/case.py which are no longer available in Python 3. Since
+# these errors can be thrown during execution under Python 2 we must
+# support them until Marionette completes its transition from Python 2
+# to Python 3.
+class ExpectedFailure(Exception):
+    """
+    Raise this when a test is expected to fail.
+
+    This is an implementation detail.
+    """
+
+    def __init__(self, exc_info):
+        super(ExpectedFailure, self).__init__()
+        self.exc_info = exc_info
+
+
+class UnexpectedSuccess(Exception):
+    """
+    The test was supposed to fail, but it didn't!
+    """
+    pass
+
+
+try:
+    from unittest.case import (
+        _ExpectedFailure,
+        _UnexpectedSuccess,
+    )
+except ImportError:
+    _ExpectedFailure = ExpectedFailure
+    _UnexpectedSuccess = UnexpectedSuccess
+
+
 def _wraps_parameterized(func, func_suffix, args, kwargs):
     """Internal: Decorator used in class MetaParameterized."""
     def wrapper(self):
         return func(self, *args, **kwargs)
     wrapper.__name__ = func.__name__ + '_' + str(func_suffix)
     wrapper.__doc__ = '[{0}] {1}'.format(func_suffix, func.__doc__)
     return wrapper