Exemplo n.º 1
0
    def test_namespace_package_path(self):
        class DS(object):
            def __init__(self, path, namespace_packages=None):
                self.location = path
                self._namespace_packages = namespace_packages

            def has_metadata(self, key):
                if key == 'namespace_packages.txt':
                    return self._namespace_packages is not None

                raise ValueError("invalid lookup key")

            def get_metadata(self, key):
                if key == 'namespace_packages.txt':
                    if self._namespace_packages is None:
                        raise ValueError("no file")

                    return self._namespace_packages

                raise ValueError("invalid lookup key")

        class WS(object):
            def __init__(self, path=None):
                pass

            def __iter__(self):
                yield DS("/pkg/pkg1")
                yield DS("/pkg/pkg2", "foo\n")
                yield DS("/pkg/pkg3", "bar.baz\n")
                yield DS("/pkg/pkg4", "foobar\nfoo\n")

        saved_ws = pkg_resources.WorkingSet
        try:
            pkg_resources.WorkingSet = WS

            self.assertEqual(
                modulegraph._namespace_package_path("sys", ["appdir/pkg"]),
                ["appdir/pkg"])
            self.assertEqual(
                modulegraph._namespace_package_path("foo", ["appdir/pkg"]), [
                    "appdir/pkg",
                    os.path.join("/pkg/pkg2", "foo"),
                    os.path.join("/pkg/pkg4", "foo")
                ])
            self.assertEqual(
                modulegraph._namespace_package_path("bar.baz", ["appdir/pkg"]),
                ["appdir/pkg",
                 os.path.join("/pkg/pkg3", "bar", "baz")])

        finally:
            pkg_resources.WorkingSet = saved_ws
Exemplo n.º 2
0
    def test_namespace_package_path(self):
        class DS (object):
            def __init__(self, path, namespace_packages=None):
                self.location = path
                self._namespace_packages = namespace_packages

            def has_metadata(self, key):
                if key == 'namespace_packages.txt':
                    return self._namespace_packages is not None

                raise ValueError("invalid lookup key")

            def get_metadata(self, key):
                if key == 'namespace_packages.txt':
                    if self._namespace_packages is None:
                        raise ValueError("no file")

                    return self._namespace_packages

                raise ValueError("invalid lookup key")

        class WS (object):
            def __init__(self, path=None):
                pass

            def __iter__(self):
                yield DS("/pkg/pkg1")
                yield DS("/pkg/pkg2", "foo\n")
                yield DS("/pkg/pkg3", "bar.baz\n")
                yield DS("/pkg/pkg4", "foobar\nfoo\n")

        saved_ws = pkg_resources.WorkingSet
        try:
            pkg_resources.WorkingSet = WS

            self.assertEqual(modulegraph._namespace_package_path("sys", ["appdir/pkg"]), ["appdir/pkg"])
            self.assertEqual(modulegraph._namespace_package_path("foo", ["appdir/pkg"]), ["appdir/pkg", "/pkg/pkg2/foo", "/pkg/pkg4/foo"])
            self.assertEqual(modulegraph._namespace_package_path("bar.baz", ["appdir/pkg"]), ["appdir/pkg", "/pkg/pkg3/bar/baz"])

        finally:
            pkg_resources.WorkingSet = saved_ws