Bug 1438253 - Add Python 3 support to mozunit. r?gps draft
authorDave Hunt <dhunt@mozilla.com>
Wed, 14 Feb 2018 18:50:52 +0000
changeset 754963 2b7f52c991b028135c57130721bcb7dfa67981ea
parent 754948 375d162649d2fba6cf967b1062163cb87790dd87
push id99074
push userbmo:dave.hunt@gmail.com
push dateWed, 14 Feb 2018 18:51:44 +0000
reviewersgps
bugs1438253
milestone60.0a1
Bug 1438253 - Add Python 3 support to mozunit. r?gps MozReview-Commit-ID: AJEb9Wcm2T4
config/mozunit.py
--- a/config/mozunit.py
+++ b/config/mozunit.py
@@ -1,20 +1,23 @@
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
+from __future__ import absolute_import
 import inspect
 import os
 import sys
 import unittest
-from StringIO import StringIO
 from unittest import TextTestRunner as _TestRunner, TestResult as _TestResult
 
 import pytest
+import six
+
+StringIO = six.StringIO
 
 '''Helper to make python unit tests report the way that the Mozilla
 unit test infrastructure expects tests to report.
 
 Usage:
 
 import mozunit
 
@@ -71,17 +74,19 @@ class _MozTestResult(_TestResult):
     def addFailure(self, test, err):
         _TestResult.addFailure(self, test, err)
         self.printFail(test, err)
         self.stream.writeln("FAIL: {0}".format(self.getDescription(test)))
         self.stream.writeln(self.failures[-1][1])
 
     def printFail(self, test, err):
         exctype, value, tb = err
-        message = value.message.splitlines()[0] if value.message else 'NO MESSAGE'
+        message = value or 'NO MESSAGE'
+        if hasattr(value, 'message'):
+            message = value.message.splitlines()[0]
         # Skip test runner traceback levels
         while tb and self._is_relevant_tb_level(tb):
             tb = tb.tb_next
         if tb:
             _, ln, _ = inspect.getframeinfo(tb)[:3]
             message = 'line {0}: {1}'.format(ln, message)
         self.printStatus("TEST-UNEXPECTED-FAIL", test, message)
 
@@ -144,17 +149,17 @@ class MockedOpen(object):
 
     with MockedOpen():
         f = open('foo', 'w')
         f.write('foo')
     self.assertRaises(Exception,f.open('foo', 'r'))
     '''
     def __init__(self, files={}):
         self.files = {}
-        for name, content in files.iteritems():
+        for name, content in files.items():
             self.files[normcase(os.path.abspath(name))] = content
 
     def __call__(self, name, mode='r'):
         absname = normcase(os.path.abspath(name))
         if 'w' in mode:
             file = MockedFile(self, absname)
         elif absname in self.files:
             content = self.files[absname]
@@ -165,29 +170,29 @@ class MockedOpen(object):
             file = MockedFile(self, absname, self.open(name, 'r').read())
         else:
             file = self.open(name, mode)
         if 'a' in mode:
             file.seek(0, os.SEEK_END)
         return file
 
     def __enter__(self):
-        import __builtin__
-        self.open = __builtin__.open
+        import six.moves.builtins
+        self.open = six.moves.builtins.open
         self._orig_path_exists = os.path.exists
         self._orig_path_isdir = os.path.isdir
         self._orig_path_isfile = os.path.isfile
-        __builtin__.open = self
+        six.moves.builtins.open = self
         os.path.exists = self._wrapped_exists
         os.path.isdir = self._wrapped_isdir
         os.path.isfile = self._wrapped_isfile
 
     def __exit__(self, type, value, traceback):
-        import __builtin__
-        __builtin__.open = self.open
+        import six.moves.builtins
+        six.moves.builtins.open = self.open
         os.path.exists = self._orig_path_exists
         os.path.isdir = self._orig_path_isdir
         os.path.isfile = self._orig_path_isfile
 
     def _wrapped_exists(self, p):
         return (self._wrapped_isfile(p) or
                 self._wrapped_isdir(p))