--- a/config/mozunit.py
+++ b/config/mozunit.py
@@ -1,19 +1,22 @@
# 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
from unittest import TextTestRunner as _TestRunner, TestResult as _TestResult
import unittest
import inspect
-from StringIO import StringIO
import os
import sys
+import six
+from six import StringIO
+
'''Helper to make python unit tests report the way that the Mozilla
unit test infrastructure expects tests to report.
Usage:
import unittest
import mozunit
@@ -69,17 +72,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)
@@ -135,17 +140,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 six.iteritems(files):
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:
file = MockedFile(self, absname, self.files[absname])
@@ -153,29 +158,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) or
self._orig_path_exists(p))