def collect_unittest(paths):
    suite = unittest.TestSuite()
    loader = unittest.defaultTestLoader
    resolver = resolve.ModuleNameResolver()
    paths = utils.sequence(paths)

    for filepath in paths:
        try:
            name, package = resolver.resolve(filepath)
        except ImportError:
            # .py file not in the search path
            name = filepath_to_identifier(filepath)
            package = None

        try:
            if package:
                module = utils.import_module(name)
            else:
                module = imp.load_source(name, filepath)
        except ImportError:
            LOGGER.warn("ImportError occurred while loading module", exc_info=True)
        else:
            tests = loader.loadTestsFromModule(module)
            if tests.countTestCases():
                suite.addTest(tests)
                LOGGER.info("Found %d test(s) in module '%s'" % (tests.countTestCases(), module.__name__))
            else:
                LOGGER.warn("No tests found in module '%s'" % module.__name__)
    return suite
Beispiel #2
0
    def _find_module(self, modname, path):
        try:
            pathname, kind = self._cache_find_module[modname]
        except KeyError:
            name = modname
            i = modname.rfind('.')
            if i != -1:
                path, _kind = self._find_module(modname[:i], path)
                name = modname[i+1:]
                if _kind != imp.PKG_DIRECTORY or not name:
                    self._cache_find_module[modname] = (None, None)
                    raise ImportError, "No module named %s" % modname
            try:
                fp, pathname, description = imp.find_module(
                                    name,
                                    utils.sequence(path, copy=list))
                if fp:
                    fp.close()
                kind = description[2]
                self._cache_find_module[modname] = (pathname, kind)
            except ImportError:
                self._cache_find_module[modname] = (None, None)
                raise
        else:
            if not pathname:
                raise ImportError, "No module named %s" % modname

        return pathname, kind
Beispiel #3
0
    def __init__(self, filepath_or_list, search_path=None):
        from modipyd.resolve import normalize_path

        super(Monitor, self).__init__()
        self.search_path = search_path

        # paths will be used as dictionary key,
        # so make it normalized.
        paths = utils.sequence(filepath_or_list)
        self.paths = [normalize_path(i) for i in paths]
        assert not isinstance(self.paths, basestring)

        self.monitoring = False
        self.__descriptors = None
        self.__filenames = {}
        self.__failures = set()
Beispiel #4
0
    def __init__(self, path=None):
        """
        The ``path`` argument is module search path,
        if *path* is omitted or ``None``, ``sys.path`` is used.
        """
        super(ModuleNameResolver, self).__init__()

        # list of module search paths
        syspaths = (path or sys.path)
        syspaths = utils.sequence(syspaths)
        self.path = [normalize_path(d) for d in syspaths if os.path.isdir(d)]

        # caches
        self._cache_package     = {}
        self._cache_find_module = {}
        self._cache_resolve     = {}
Beispiel #5
0
 def test_sequence(self):
     self.assertEqual([], utils.sequence([]))
     self.assertEqual([1, 2, 3], utils.sequence([1, 2, 3]))
     self.assertEqual((1,), utils.sequence(1))
     self.assertEqual([1], utils.sequence(1, list))