Exemple #1
0
    def test_pkgutil_namespace(self):
        mg = modulegraph2.ModuleGraph()
        mg.add_module("mynamespace.pkg1_mod")
        mg.add_module("mynamespace.pkg2_mod")

        n = mg.find_node("mynamespace.pkg1_mod")
        self.assertIsInstance(n, modulegraph2.SourceModule)

        n = mg.find_node("mynamespace.pkg2_mod")
        self.assertIsInstance(n, modulegraph2.SourceModule)

        n = mg.find_node("mynamespace")
        self.assertIsInstance(n, modulegraph2.Package)

        self.assertCountEqual(
            n.search_path,
            [INPUT_DIR / subdir / "mynamespace" for subdir in ("pkg1", "pkg2")],
        )

        n = mg.find_node("pkgutil")
        self.assertIsInstance(n, modulegraph2.SourceModule)

        try:
            mg.edge_data("mynamespace", "pkgutil")
        except KeyError:
            self.fail("No edge between mynamespace and pkgutil")
    def test_six_incorrect(self):
        try:
            moved_copyreg = graphbuilder.SIX_MOVES_TO.pop("copyreg")

            mg = modulegraph2.ModuleGraph()
            mg.add_module("using_six_incorrectly")

            self.assert_has_node(mg, "using_six_incorrectly", modulegraph2.SourceModule)
            self.assert_has_node(mg, "six", modulegraph2.SourceModule)
            self.assert_has_node(mg, "six.moves", modulegraph2.Package)
            self.assert_has_node(mg, "six.moves.copyreg", modulegraph2.MissingModule)

            self.assert_has_edge(
                mg,
                "using_six_incorrectly",
                "six.moves",
                {modulegraph2.DependencyInfo(False, True, False, None)},
            )
            self.assert_has_edge(
                mg,
                "using_six_incorrectly",
                "six.moves.copyreg",
                {modulegraph2.DependencyInfo(False, True, True, None)},
            )

        finally:
            graphbuilder.SIX_MOVES_TO["copyreg"] = moved_copyreg
    def test_moved_to_missing(self):
        mg = modulegraph2.ModuleGraph()
        mg.add_module("using_six_moved_to_missing")

        self.assert_has_node(
            mg, "using_six_moved_to_missing", modulegraph2.SourceModule
        )
        self.assert_has_node(mg, "six", modulegraph2.SourceModule)
        self.assert_has_node(mg, "six.moves", modulegraph2.Package)
        self.assert_has_node(mg, "six.moves.winreg", modulegraph2.AliasNode)
        self.assert_has_node(mg, "winreg", modulegraph2.MissingModule)

        self.assert_has_edge(
            mg,
            "using_six_moved_to_missing",
            "six.moves",
            {modulegraph2.DependencyInfo(False, True, False, None)},
        )
        self.assert_has_edge(
            mg,
            "using_six_moved_to_missing",
            "six.moves.winreg",
            {modulegraph2.DependencyInfo(False, True, True, None)},
        )
        self.assert_has_edge(
            mg,
            "six.moves.winreg",
            "winreg",
            {modulegraph2.DependencyInfo(False, True, False, None)},
        )
    def test_format_to_file(self, print_graph):
        td = tempfile.mkdtemp()

        try:
            args = argparse.Namespace()
            args.output_file = os.path.join(td, "filename.dot")
            args.output_format = main.OutputFormat.GRAPHVIZ

            mg = modulegraph2.ModuleGraph()

            rv = main.format_graph(args, mg)
            self.assertIs(rv, None)

            print_graph.assert_called_once()
            self.assertTrue(
                isinstance(print_graph.mock_calls[-1][1][0], io.TextIOWrapper))
            self.assertEqual(print_graph.mock_calls[-1][1][0].name,
                             args.output_file)

            self.assertEqual(print_graph.mock_calls[-1][1][1],
                             args.output_format)

            self.assertIs(print_graph.mock_calls[-1][1][2], mg)

        finally:
            os.unlink(os.path.join(td, "filename.dot"))
            os.rmdir(td)
Exemple #5
0
    def test_toplevel_without_hook(self):
        with prefixed_sys_path(INPUT_DIR / "toplevel"):

            mg = modulegraph2.ModuleGraph(use_builtin_hooks=False)
            mg.add_module("example")

            self.assert_has_node(mg, "example", modulegraph2.SourceModule)
            self.assert_has_node(mg, "_example", modulegraph2.ExtensionModule)

            self.assert_has_edge(mg, "example", "_example", None)
    def test_format_to_invalid_file(self):
        with captured_output() as (stdout, stderr):
            args = argparse.Namespace()
            args.output_file = os.path.join("nosuchdir/filename.dot")
            args.output_format = main.OutputFormat.GRAPHVIZ

            mg = modulegraph2.ModuleGraph()

            self.assertRaises(SystemExit, main.format_graph, args, mg)

        self.assertIn("No such file or directory", stderr.getvalue())
    def test_format_to_stdout(self, print_graph):
        args = argparse.Namespace()
        args.output_file = None
        args.output_format = main.OutputFormat.HTML

        mg = modulegraph2.ModuleGraph()

        rv = main.format_graph(args, mg)
        self.assertIs(rv, None)

        print_graph.assert_called_once_with(sys.stdout, args.output_format, mg)
Exemple #8
0
    def test_missing_but_not_swig(self):
        with prefixed_sys_path(INPUT_DIR / "not_swig"):

            mg = modulegraph2.ModuleGraph()
            mg.add_module("example")

            self.assert_has_node(mg, "example", modulegraph2.SourceModule)

            # Note that this is a toplevel module, not in the package:
            self.assert_has_node(mg, "_example", modulegraph2.MissingModule)

            self.assert_has_edge(mg, "example", "_example", None)
Exemple #9
0
    def test_package_init_without_hook(self):
        with prefixed_sys_path(INPUT_DIR / "package"):

            mg = modulegraph2.ModuleGraph(use_builtin_hooks=False)
            mg.add_module("using")

            self.assert_has_node(mg, "using", modulegraph2.SourceModule)
            self.assert_has_node(mg, "example", modulegraph2.Package)

            # Note that this is a toplevel module, not in the package:
            self.assert_has_node(mg, "_example", modulegraph2.MissingModule)

            self.assert_has_edge(mg, "example", "_example", None)
Exemple #10
0
    def test_package_init(self):
        with prefixed_sys_path(INPUT_DIR / "package"):

            mg = modulegraph2.ModuleGraph()
            mg.add_module("using")

            self.assert_has_node(mg, "using", modulegraph2.SourceModule)
            self.assert_has_node(mg, "example", modulegraph2.Package)

            # Note that this is a toplevel module, not in the package:
            self.assert_has_node(mg, "_example", modulegraph2.ExtensionModule)

            self.assert_has_edge(mg, "example", "_example", None)
Exemple #11
0
        def test_adjustments(self):
            # Explicitly test adjustments for distutils because virtualenv environments
            # contain a stub distutils package that should not be present in the graph.
            mg = modulegraph2.ModuleGraph()
            mg.add_module("distutils.command.build_ext")

            node = mg.find_node("distutils")
            self.assertTrue(isinstance(node, modulegraph2.Package))
            self.assertTrue(
                isinstance(node.init_module, modulegraph2.SourceModule))

            self.assertEqual(
                node.init_module.filename,
                pathlib.Path(sys.real_prefix) / "lib" / PYLIB_DIR /
                "distutils" / "__init__.py",
            )
    def test_six(self):
        mg = modulegraph2.ModuleGraph()
        mg.add_module("using_six")

        self.assert_has_node(mg, "using_six", modulegraph2.SourceModule)
        self.assert_has_node(mg, "six", modulegraph2.SourceModule)
        self.assert_has_node(mg, "six.moves", modulegraph2.Package)
        self.assert_has_node(mg, "six.moves.html_parser", modulegraph2.AliasNode)
        self.assert_has_node(mg, "six.moves.urllib.error", modulegraph2.AliasNode)
        self.assert_has_node(mg, "html", modulegraph2.Package)
        self.assert_has_node(mg, "html.parser", modulegraph2.SourceModule)
        self.assert_has_node(mg, "importlib", modulegraph2.Package)
        self.assert_has_node(mg, "functools", modulegraph2.SourceModule)
        self.assert_has_node(mg, "urllib.error", modulegraph2.SourceModule)

        self.assert_has_edge(
            mg,
            "using_six",
            "six.moves",
            {modulegraph2.DependencyInfo(False, True, False, None)},
        )
        self.assert_has_edge(
            mg,
            "using_six",
            "six.moves.html_parser",
            {modulegraph2.DependencyInfo(False, True, True, None)},
        )
        self.assert_has_edge(
            mg,
            "using_six",
            "six.moves.urllib.error",
            {modulegraph2.DependencyInfo(False, True, False, None)},
        )
        self.assert_has_edge(
            mg,
            "using_six",
            "functools",  # six.moves.reduce
            {modulegraph2.DependencyInfo(False, True, False, None)},
        )

        self.assert_has_edge(
            mg,
            "six.moves.html_parser",
            "html.parser",
            {modulegraph2.DependencyInfo(False, True, False, None)},
        )
        self.assert_has_edge(
            mg,
            "using_six",
            "importlib",  # six.moves.reload_module
            {modulegraph2.DependencyInfo(False, True, False, None)},
        )
        self.assert_has_edge(
            mg,
            "six.moves.urllib.error",
            "urllib.error",
            {modulegraph2.DependencyInfo(False, True, False, None)},
        )

        self.assertRaises(
            KeyError, mg.edge_data, "using_six", "six.moves.reload_module"
        )

        node = mg.find_node("six.moves")
        self.assertFalse(node.has_data_files)
    def test_vendored_six(self):
        # Same as test_six, but now with six installed as sub package,
        # as used by a number of projects that have vendored six.
        return

        (INPUT_DIR / "site-packages" / "vendored").mkdir()
        with open(INPUT_DIR / "site-packages" / "vendored" / "__init__.py", "w") as fp:
            fp.write("''' init '''\n")

        (INPUT_DIR / "site-packages" / "six").rename(
            INPUT_DIR / "site-packages" / "vendored" / "six"
        )

        try:
            mg = modulegraph2.ModuleGraph()
            mg.add_module("using_vendored_six")

            self.assert_has_node(mg, "using_six", modulegraph2.SourceModule)
            self.assert_has_node(mg, "vendored.six", modulegraph2.SourceModule)
            self.assert_has_node(
                mg, "vendored.six.moves", modulegraph2.NamespacePackage
            )
            self.assert_has_node(
                mg, "vendored.six.moves.html_parser", modulegraph2.AliasNode
            )
            self.assertRaises(
                KeyError, mg.edge_data, "using_six", "vendored.six.moves.reload_module"
            )
            self.assert_has_node(
                mg, "vendored.six.moves.urllib.error", modulegraph2.AliasNode
            )
            self.assertRaises(
                KeyError, mg.edge_data, "using_six", "vendored.six.moves.reduce"
            )
            self.assert_has_node(mg, "html.parser", modulegraph2.Package)
            self.assert_has_node(mg, "importlib", modulegraph2.Package)
            self.assert_has_node(mg, "urllib.error", modulegraph2.Package)

            self.assert_has_edge(
                mg,
                "using_six",
                "vendored.six.moves",
                {modulegraph2.DependencyInfo(False, True, True, None)},
            )
            self.assert_has_edge(
                mg,
                "using_six",
                "vendored.six.moves.html_parser",
                {modulegraph2.DependencyInfo(False, True, True, None)},
            )
            self.assert_has_edge(
                mg,
                "using_six",
                "vendored.six.moves.reload_module",
                {modulegraph2.DependencyInfo(False, True, True, None)},
            )
            self.assert_has_edge(
                mg,
                "using_six",
                "vendored.six.moves.urllib.error",
                {modulegraph2.DependencyInfo(False, True, True, None)},
            )
            self.assert_has_edge(
                mg,
                "using_six",
                "vendored.six.moves.reduce",
                {modulegraph2.DependencyInfo(False, True, True, None)},
            )

            self.assert_has_edge(
                mg,
                "vendored.six.moves.html_parser",
                "html.parser",
                {modulegraph2.DependencyInfo(False, True, False, None)},
            )
            self.assert_has_edge(
                mg,
                "vendored.six.moves.reload_module",
                "importlib",
                {modulegraph2.DependencyInfo(False, True, False, None)},
            )
            self.assert_has_edge(
                mg,
                "vendored.six.moves.urllib.error",
                "urllib.error",
                {modulegraph2.DependencyInfo(False, True, False, None)},
            )
            self.assert_has_edge(
                mg,
                "vendored.six.moves.reduce",
                "functools",
                {modulegraph2.DependencyInfo(False, True, False, None)},
            )

        finally:
            (INPUT_DIR / "site-packages" / "vendored" / "six").rename(
                INPUT_DIR / "site-packages" / "six"
            )
 def setUp(self):
     self.mg = modulegraph2.ModuleGraph()
     self.mg.add_module("os")
     self.mg.add_module("faulthandler")
     self.mg.add_distribution("wheel")