1import sys, os, string, glob
2from os.path import basename, dirname, splitext, join, expanduser
3from fnmatch import fnmatch
4import unittest
5import dejagnu
6
7from distutils.command.install_lib import install_lib
8from distutils.errors import DistutilsOptionError
9
10
11def recursiveGlob(root, pathPattern):
12    """
13    Recursively look for files matching 'pathPattern'. Return a list
14    of matching files/directories.
15    """
16    result = []
17
18    for rootpath, dirnames, filenames in os.walk(root):
19        for fn in filenames:
20            if fnmatch(fn, pathPattern):
21                result.append(join(rootpath, fn))
22    return result
23
24
25def importExternalTestCases(pathPattern="test_*.py", root=".", package=None):
26    """
27    Import all unittests in the PyObjC tree starting at 'root'
28    """
29
30    testFiles = recursiveGlob(root, pathPattern)
31    testModules = map(lambda x:x[len(root)+1:-3].replace('/', '.'), testFiles)
32    if package is not None:
33        testModules = [(package + '.' + m) for m in testModules]
34
35    suites = []
36
37    for modName in testModules:
38        try:
39            module = __import__(modName)
40        except ImportError, msg:
41            print "SKIP %s: %s"%(modName, msg)
42            continue
43
44        if '.' in modName:
45            for elem in modName.split('.')[1:]:
46                module = getattr(module, elem)
47
48        s = unittest.defaultTestLoader.loadTestsFromModule(module)
49        suites.append(s)
50
51    return unittest.TestSuite(suites)
52
53
54
55def makeTestSuite():
56    import __main__
57    topdir = dirname(__main__.__file__)
58    if sys.version_info[0] == 3:
59        del sys.path[1]
60        deja_topdir = dirname(dirname(topdir))
61    else:
62        deja_topdir = topdir
63    deja_suite = dejagnu.testSuiteForDirectory(join(deja_topdir,
64        'libffi-src/tests/testsuite/libffi.call'))
65
66    plain_suite = importExternalTestCases("test_*.py",
67        join(topdir, 'PyObjCTest'), package='PyObjCTest')
68
69    version_suite = importExternalTestCases("test%d_*.py"%(sys.version_info[0],),
70        join(topdir, 'PyObjCTest'), package='PyObjCTest')
71
72    suite = unittest.TestSuite((plain_suite, version_suite, deja_suite))
73
74    # the libffi tests don't work unless we use our own
75    # copy of libffi.
76    import __main__
77    if __main__.USE_SYSTEM_FFI:
78        return unittest.TestSuite((plain_suite, version_suite))
79    return suite
80