author David Keeler <>
Fri, 15 Sep 2017 14:47:54 -0700
changeset 432366 0893b7f1f14a830395dad12d2f0b5136589289f7
parent 427514 3f58f4dc8303cb08e66907b3d403e8d2a1cf3234
child 436725 5e80ad94e2a5e14943b5f096a759c33291e88c8c
permissions -rw-r--r--
Bug 1369561 - Address misc. SnprintfLiteral correctness nits. r=jld, r=froydnj, a=ritu

# 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

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

import pytest

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


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 printStatus(self, status, test, message=''):
        line = "{status} | {file} | {klass}.{test}{sep}{message}".format(
            sep=', ' if message else '',

    def addSuccess(self, test):
        _TestResult.addSuccess(self, test)
        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)

    def addError(self, test, err):
        _TestResult.addError(self, test, err)
        self.printFail(test, err)"ERROR: {0}".format(self.getDescription(test)))[-1][1])

    def addFailure(self, test, err):
        _TestResult.addFailure(self, test, err)
        self.printFail(test, err)"FAIL: {0}".format(self.getDescription(test)))[-1][1])

    def printFail(self, test, err):
        exctype, value, tb = err
        message = value.message.splitlines()[0] if value.message else 'NO MESSAGE'
        # 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)

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):

def normcase(path):
    Normalize the case of `path`.

    Don't use `os.path.normcase` because that also normalizes forward slashes
    to backslashes on Windows.
    if sys.platform.startswith('win'):
        return path.lower()
    return path

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[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])
        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._orig_path_isdir = os.path.isdir
        self._orig_path_isfile = os.path.isfile = 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__ =
        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

    def _wrapped_isfile(self, p):
        p = normcase(p)
        if p in self.files:
            return True

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

        return self._orig_path_isfile(p)

    def _wrapped_isdir(self, p):
        p = normcase(p)
        p = p if p.endswith(('/', '\\')) else p + os.sep
        if any(f.startswith(p) for f in self.files):
            return True

        abspath = normcase(os.path.abspath(p) + os.sep)
        if any(f.startswith(abspath) for f in self.files):
            return True

        return self._orig_path_exists(p)

def main(*args, **kwargs):
    runwith = kwargs.pop('runwith', 'pytest')

    if runwith == 'unittest':
        unittest.main(testRunner=MozTestRunner(), *args, **kwargs)
        args = list(args)
        if os.environ.get('MACH_STDOUT_ISATTY') and not any(a.startswith('--color') for a in args):

        module = __import__('__main__')
            '-p', 'mozlog.pytest_mozlog.plugin',