author Ehsan Akhgari <>
Tue, 06 Jan 2015 18:35:02 -0500
changeset 235915 86924ac6a94a9147bcae518239d7a33b5981062b
parent 156404 e4e085ffb2ddb84b49c07c31e872ebef22d9828f
child 330625 687f4ed7ef41c8a3cced558422bd046b724e69ad
permissions -rw-r--r--
Bug 1118486 - Part 1: Use `= delete` instead of MOZ_DELETE directly; r=Waldo Most of this patch (with the exception of dom/bindings/ was generated by the following bash script: #!/bin/bash function convert() { echo "Converting $1 to $2..." find . ! -wholename "*nsprpub*" \ ! -wholename "*security/nss*" \ ! -wholename "*/.hg*" \ ! -wholename "*/.git*" \ ! -wholename "obj-*" \ -type f \ \( -iname "*.cpp" \ -o -iname "*.h" \ -o -iname "*.cc" \ -o -iname "*.idl" \ -o -iname "*.ipdl" \ -o -iname "*.ipdlh" \ -o -iname "*.mm" \) | \ xargs -n 1 sed -i -e "s/\b$1\b/$2/g" } convert MOZ_DELETE '= delete'

# 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

from unittest import TextTestRunner as _TestRunner, TestResult as _TestResult
import unittest
import inspect
from StringIO import StringIO
import os

'''Helper to make python unit tests report the way that the Mozilla
unit test infrastructure expects tests to report.


import unittest
import mozunit

if __name__ == '__main__':

class _MozTestResult(_TestResult):
    def __init__(self, stream, descriptions):
        _TestResult.__init__(self) = stream
        self.descriptions = descriptions

    def getDescription(self, test):
        if self.descriptions:
            return test.shortDescription() or str(test)
            return str(test)

    def addSuccess(self, test):
        _TestResult.addSuccess(self, test)
        filename = inspect.getfile(test.__class__)
        testname = test._testMethodName"TEST-PASS | {0} | {1}".format(filename, testname))

    def addError(self, test, err):
        _TestResult.addError(self, test, err)
        self.printFail(test, err)

    def addFailure(self, test, err):
        _TestResult.addFailure(self, test, err)

    def printFail(self, test, err):
        exctype, value, tb = err
        # Skip test runner traceback levels
        while tb and self._is_relevant_tb_level(tb):
            tb = tb.tb_next
        if not tb:
        _f, _ln, _t = inspect.getframeinfo(tb)[:3]"TEST-UNEXPECTED-FAIL | {0} | line {1}, {2}: {3}" 
                            .format(_f, _ln, _t, value.message))

    def printErrorList(self):
        for test, err in self.errors:
  "ERROR: {0}".format(self.getDescription(test)))

class MozTestRunner(_TestRunner):
    def _makeResult(self):
        return _MozTestResult(, self.descriptions)
    def run(self, test):
        result = self._makeResult()
        return result

class MockedFile(StringIO):
    def __init__(self, context, filename, content = ''):
        self.context = context = filename
        StringIO.__init__(self, content)

    def close(self):
        self.context.files[] = self.getvalue()

    def __enter__(self):
        return self

    def __exit__(self, type, value, traceback):

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')
    self.assertRaises(Exception,'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,, 'r').read())
            file =, mode)
        if 'a' in mode:
  , os.SEEK_END)
        return file

    def __enter__(self):
        import __builtin__ =
        self._orig_path_exists = os.path.exists = self
        os.path.exists = self._wrapped_exists

    def __exit__(self, type, value, traceback):
        import __builtin__ =
        os.path.exists = self._orig_path_exists

    def _wrapped_exists(self, p):
        if p in self.files:
            return True

        abspath = os.path.abspath(p)
        if abspath in self.files:
            return True

        return self._orig_path_exists(p)

def main(*args):