示例#1
0
    def test_create_edge(self):
        fpath = "input_file.py"
        im = ImportManager()
        im.set_pkg("")
        node1 = "node1"
        node2 = "node2"

        im.create_node(node1)
        im.create_node(node2)

        # current module not set
        with self.assertRaises(ImportManagerError):
            im.create_edge(node2)

        im.set_current_mod(node1, fpath)
        im.create_edge(node2)

        self.assertEqual(im.get_imports(node1), set([node2]))

        # only non empty strings allowed
        with self.assertRaises(ImportManagerError):
            im.create_edge("")

        with self.assertRaises(ImportManagerError):
            im.create_edge(1)
示例#2
0
    def test_handle_import(self):
        # test builtin modules
        fpath = "input_file.py"
        im = ImportManager()
        im.set_pkg("")
        im.create_node("mod1")
        im.set_current_mod("mod1", fpath)

        self.assertEqual(im.handle_import("sys", 0), None)
        self.assertEqual(im.handle_import("sys", 10), None)

        self.assertEqual(im.get_imports("mod1"), set(["sys"]))

        # test parent package
        class MockImport:
            def __init__(self, name):
                self.__file__ = name

        with mock.patch("importlib.import_module",
                        return_value=MockImport(
                            os.path.abspath("mod2.py"))) as mock_import:
            modname = im.handle_import("mod2", 0)
            self.assertEqual(modname, "mod2")
            mock_import.assert_called_with("mod2", package="")

        with mock.patch("importlib.import_module",
                        return_value=MockImport(
                            os.path.abspath("mod2.py"))) as mock_import:
            im.set_current_mod("mod1.mod3", fpath)
            modname = im.handle_import("mod2", 1)
            self.assertEqual(modname, "mod2")
            mock_import.assert_called_once_with(".mod2", package="mod1")
示例#3
0
    def test_custom_loader(self):
        fpath = "input_file.py"
        im = ImportManager()
        im.set_pkg("")
        old_sys_path = copy.deepcopy(sys.path)
        im.set_current_mod("node1", fpath)
        im.create_node("node1")

        # an import happens and the loader is called
        loader = get_custom_loader(im)("node2", "filepath")

        # verify that edges and nodes have been added
        self.assertEqual(im.get_imports("node1"), set(["node2"]))
        self.assertEqual(im.get_filepath("node2"), os.path.abspath("filepath"))

        loader = get_custom_loader(im)("node2", "filepath")
        self.assertEqual(im.get_imports("node1"), set(["node2"]))
        self.assertEqual(im.get_filepath("node2"), os.path.abspath("filepath"))

        self.assertEqual(loader.get_filename("filepath"), "filepath")
        self.assertEqual(loader.get_data("filepath"), "")
示例#4
0
    def test_create_node(self):
        fpath = "input_file.py"
        im = ImportManager()
        im.set_pkg("")

        name = "node1"
        im.create_node(name)
        self.assertEqual(im.get_filepath(name), "")
        self.assertEqual(im.get_imports(name), set())

        # if a node already exists it can't be added again
        with self.assertRaises(ImportManagerError):
            im.create_node(name)

        # no empty node names
        with self.assertRaises(ImportManagerError):
            im.create_node("")

        with self.assertRaises(ImportManagerError):
            im.create_node(1)