Source

pyobjc / pyobjc-core / PyObjCTest / loader.py

from __future__ import absolute_import, print_function
import sys, os, string, glob
from os.path import basename, dirname, splitext, join, expanduser
from fnmatch import fnmatch
import unittest

from . import dejagnu

from distutils.command.install_lib import install_lib
from distutils.errors import DistutilsOptionError


def recursiveGlob(root, pathPattern):
    """
    Recursively look for files matching 'pathPattern'. Return a list
    of matching files/directories.
    """
    result = []

    for rootpath, dirnames, filenames in os.walk(root):
        for fn in filenames:
            if fnmatch(fn, pathPattern):
                result.append(join(rootpath, fn))
    return result


def importExternalTestCases(pathPattern="test_*.py", root=".", package=None):
    """
    Import all unittests in the PyObjC tree starting at 'root'
    """

    testFiles = recursiveGlob(root, pathPattern)
    testModules = [ x[len(root)+1:-3].replace('/', '.') for x in  testFiles ]
    if package is not None:
        testModules = [(package + '.' + m) for m in testModules]

    suites = []

    for modName in testModules:
        try:
            module = __import__(modName)
        except ImportError as msg:
            print("SKIP %s: %s"%(modName, msg))
            continue

        if '.' in modName:
            for elem in modName.split('.')[1:]:
                module = getattr(module, elem)

        s = unittest.defaultTestLoader.loadTestsFromModule(module)
        suites.append(s)

    return unittest.TestSuite(suites)



def makeTestSuite():
    import __main__
    topdir = dirname(__main__.__file__)
    if sys.version_info[0] == 3:
        del sys.path[1]
        deja_topdir = dirname(dirname(topdir))
    else:
        deja_topdir = topdir
    deja_suite = dejagnu.testSuiteForDirectory(join(deja_topdir,
        'libffi-src/tests/testsuite/libffi.call'))

    plain_suite = importExternalTestCases("test_*.py",
        join(topdir, 'PyObjCTest'), package='PyObjCTest')

    version_suite = importExternalTestCases("test%d_*.py"%(sys.version_info[0],),
        join(topdir, 'PyObjCTest'), package='PyObjCTest')

    suite = unittest.TestSuite((plain_suite, version_suite, deja_suite))

    # the libffi tests don't work unless we use our own
    # copy of libffi.
    import __main__
    if __main__.USE_SYSTEM_FFI:
        return unittest.TestSuite((plain_suite, version_suite))
    return suite
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.