2012-05-21 15:12:37 +04:00
|
|
|
# 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/.
|
|
|
|
|
2009-10-27 15:33:38 +03:00
|
|
|
from unittest import TextTestRunner as _TestRunner, TestResult as _TestResult
|
2012-07-25 09:44:08 +04:00
|
|
|
import unittest
|
2009-10-27 15:33:38 +03:00
|
|
|
import inspect
|
2012-07-25 09:44:08 +04:00
|
|
|
from StringIO import StringIO
|
|
|
|
import os
|
2009-10-27 15:33:38 +03:00
|
|
|
|
|
|
|
'''Helper to make python unit tests report the way that the Mozilla
|
|
|
|
unit test infrastructure expects tests to report.
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
|
|
|
|
import unittest
|
2012-07-25 09:44:08 +04:00
|
|
|
import mozunit
|
2009-10-27 15:33:38 +03:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2012-07-25 09:44:08 +04:00
|
|
|
mozunit.main()
|
2009-10-27 15:33:38 +03:00
|
|
|
'''
|
|
|
|
|
|
|
|
class _MozTestResult(_TestResult):
|
|
|
|
def __init__(self, stream, descriptions):
|
|
|
|
_TestResult.__init__(self)
|
|
|
|
self.stream = stream
|
|
|
|
self.descriptions = descriptions
|
|
|
|
|
|
|
|
def getDescription(self, test):
|
|
|
|
if self.descriptions:
|
|
|
|
return test.shortDescription() or str(test)
|
|
|
|
else:
|
|
|
|
return str(test)
|
|
|
|
|
2016-02-12 06:21:05 +03:00
|
|
|
def printStatus(self, status, test, message=''):
|
2016-02-12 06:29:51 +03:00
|
|
|
line = "{status} | {file} | {klass}.{test}{sep}{message}".format(
|
2016-02-12 06:21:05 +03:00
|
|
|
status=status,
|
|
|
|
file=inspect.getfile(test.__class__),
|
2016-02-12 06:29:51 +03:00
|
|
|
klass=test.__class__.__name__,
|
2016-02-12 06:21:05 +03:00
|
|
|
test=test._testMethodName,
|
|
|
|
sep=', ' if message else '',
|
|
|
|
message=message,
|
|
|
|
)
|
|
|
|
self.stream.writeln(line)
|
|
|
|
|
2009-10-27 15:33:38 +03:00
|
|
|
def addSuccess(self, test):
|
|
|
|
_TestResult.addSuccess(self, test)
|
2016-02-12 06:21:05 +03:00
|
|
|
self.printStatus('TEST-PASS', test)
|
|
|
|
|
|
|
|
def addSkip(self, test, reason):
|
|
|
|
_TestResult.addSkip(self, test, reason)
|
|
|
|
self.printStatus('TEST-SKIP', test)
|
|
|
|
|
|
|
|
def addExpectedFailure(self, test, err):
|
|
|
|
_TestResult.addExpectedFailure(self, test, err)
|
|
|
|
self.printStatus('TEST-KNOWN-FAIL', test)
|
|
|
|
|
|
|
|
def addUnexpectedSuccess(self, test):
|
|
|
|
_TestResult.addUnexpectedSuccess(self, test)
|
|
|
|
self.printStatus('TEST-UNEXPECTED-PASS', test)
|
2009-10-27 15:33:38 +03:00
|
|
|
|
|
|
|
def addError(self, test, err):
|
|
|
|
_TestResult.addError(self, test, err)
|
|
|
|
self.printFail(test, err)
|
2016-02-12 06:16:17 +03:00
|
|
|
self.stream.writeln("ERROR: {0}".format(self.getDescription(test)))
|
|
|
|
self.stream.writeln(self.errors[-1][1])
|
2009-10-27 15:33:38 +03:00
|
|
|
|
|
|
|
def addFailure(self, test, err):
|
|
|
|
_TestResult.addFailure(self, test, err)
|
|
|
|
self.printFail(test,err)
|
2016-02-12 06:16:17 +03:00
|
|
|
self.stream.writeln("FAIL: {0}".format(self.getDescription(test)))
|
|
|
|
self.stream.writeln(self.failures[-1][1])
|
2009-10-27 15:33:38 +03:00
|
|
|
|
|
|
|
def printFail(self, test, err):
|
|
|
|
exctype, value, tb = err
|
2016-05-28 01:19:54 +03:00
|
|
|
message = value.message.splitlines()[0] if value.message else 'NO MESSAGE'
|
2009-10-27 15:33:38 +03:00
|
|
|
# Skip test runner traceback levels
|
|
|
|
while tb and self._is_relevant_tb_level(tb):
|
|
|
|
tb = tb.tb_next
|
2016-05-24 18:32:39 +03:00
|
|
|
if tb:
|
|
|
|
_, ln, _ = inspect.getframeinfo(tb)[:3]
|
|
|
|
message = 'line {0}: {1}'.format(ln, message)
|
|
|
|
self.printStatus("TEST-UNEXPECTED-FAIL", test, message)
|
2009-10-27 15:33:38 +03:00
|
|
|
|
|
|
|
|
|
|
|
class MozTestRunner(_TestRunner):
|
|
|
|
def _makeResult(self):
|
|
|
|
return _MozTestResult(self.stream, self.descriptions)
|
|
|
|
def run(self, test):
|
|
|
|
result = self._makeResult()
|
|
|
|
test(result)
|
|
|
|
return result
|
2012-07-25 09:44:08 +04:00
|
|
|
|
2012-07-25 09:44:08 +04:00
|
|
|
class MockedFile(StringIO):
|
|
|
|
def __init__(self, context, filename, content = ''):
|
|
|
|
self.context = context
|
|
|
|
self.name = filename
|
|
|
|
StringIO.__init__(self, content)
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
self.context.files[self.name] = self.getvalue()
|
|
|
|
StringIO.close(self)
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, type, value, traceback):
|
|
|
|
self.close()
|
|
|
|
|
|
|
|
class MockedOpen(object):
|
|
|
|
'''
|
|
|
|
Context manager diverting the open builtin such that opening files
|
|
|
|
can open "virtual" file instances given when creating a MockedOpen.
|
|
|
|
|
|
|
|
with MockedOpen({'foo': 'foo', 'bar': 'bar'}):
|
|
|
|
f = open('foo', 'r')
|
|
|
|
|
|
|
|
will thus open the virtual file instance for the file 'foo' to f.
|
|
|
|
|
|
|
|
MockedOpen also masks writes, so that creating or replacing files
|
|
|
|
doesn't touch the file system, while subsequently opening the file
|
|
|
|
will return the recorded content.
|
|
|
|
|
|
|
|
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():
|
|
|
|
self.files[os.path.abspath(name)] = content
|
|
|
|
|
|
|
|
def __call__(self, name, mode = 'r'):
|
|
|
|
absname = os.path.abspath(name)
|
|
|
|
if 'w' in mode:
|
|
|
|
file = MockedFile(self, absname)
|
|
|
|
elif absname in self.files:
|
|
|
|
file = MockedFile(self, absname, self.files[absname])
|
|
|
|
elif 'a' in mode:
|
|
|
|
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
|
2013-11-13 00:30:34 +04:00
|
|
|
self._orig_path_exists = os.path.exists
|
2016-06-05 21:34:31 +03:00
|
|
|
self._orig_path_isdir = os.path.isdir
|
|
|
|
self._orig_path_isfile = os.path.isfile
|
2012-07-25 09:44:08 +04:00
|
|
|
__builtin__.open = self
|
2013-11-13 00:30:34 +04:00
|
|
|
os.path.exists = self._wrapped_exists
|
2016-06-05 21:34:31 +03:00
|
|
|
os.path.isdir = self._wrapped_isdir
|
|
|
|
os.path.isfile = self._wrapped_isfile
|
2012-07-25 09:44:08 +04:00
|
|
|
|
|
|
|
def __exit__(self, type, value, traceback):
|
|
|
|
import __builtin__
|
|
|
|
__builtin__.open = self.open
|
2013-11-13 00:30:34 +04:00
|
|
|
os.path.exists = self._orig_path_exists
|
2016-06-05 21:34:31 +03:00
|
|
|
os.path.isdir = self._orig_path_isdir
|
|
|
|
os.path.isfile = self._orig_path_isfile
|
2013-11-13 00:30:34 +04:00
|
|
|
|
|
|
|
def _wrapped_exists(self, p):
|
2016-06-05 21:34:31 +03:00
|
|
|
return (self._wrapped_isfile(p) or
|
|
|
|
self._wrapped_isdir(p) or
|
|
|
|
self._orig_path_exists(p))
|
|
|
|
|
|
|
|
def _wrapped_isfile(self, p):
|
2013-11-13 00:30:34 +04:00
|
|
|
if p in self.files:
|
|
|
|
return True
|
|
|
|
|
|
|
|
abspath = os.path.abspath(p)
|
|
|
|
if abspath in self.files:
|
|
|
|
return True
|
|
|
|
|
2016-06-05 21:34:31 +03:00
|
|
|
return self._orig_path_isfile(p)
|
|
|
|
|
|
|
|
def _wrapped_isdir(self, p):
|
|
|
|
p = p if p.endswith(('/', '\\')) else p + os.sep
|
|
|
|
if any(f.startswith(p) for f in self.files):
|
|
|
|
return True
|
|
|
|
|
|
|
|
abspath = os.path.abspath(p) + os.sep
|
|
|
|
if any(f.startswith(abspath) for f in self.files):
|
|
|
|
return True
|
|
|
|
|
2013-11-13 00:30:34 +04:00
|
|
|
return self._orig_path_exists(p)
|
2012-07-25 09:44:08 +04:00
|
|
|
|
2016-05-09 21:56:24 +03:00
|
|
|
def main(*args, **kwargs):
|
|
|
|
unittest.main(testRunner=MozTestRunner(), *args, **kwargs)
|