Exemple #1
0
    def test_collector(self):
        os.chdir(self.get_code_path())
        source_code_collection = SourceCodeCollector(
            root_path=Path(self.get_code_path())).collect(Path("jig"))
        assert len(source_code_collection) == 10
        filenames = sorted([
            str(code.file.source_file_path.relative_path_from_root)
            for code in source_code_collection
        ])
        assert filenames == [
            "jig/__init__.py",
            "jig/collector/__init__.py",
            "jig/collector/application/__init__.py",
            "jig/collector/domain/__init__.py",
            "jig/collector/jig_ast/__init__.py",
            "jig/collector/jig_ast/class_def.py",
            "jig/collector/jig_ast/imports.py",
            "jig/visualizer/__init__.py",
            "jig/visualizer/application/__init__.py",
            "jig/visualizer/domain/__init__.py",
        ]

        jig_init_py = source_code_collection.get_by_relative_path(
            "jig/__init__.py")
        assert jig_init_py.file.content == ""
        assert jig_init_py.module_path == ModulePath.from_str("jig")
        assert jig_init_py.import_paths == ImportPathCollection([])
        assert jig_init_py.class_defs == []

        jig_collector_jig_ast_init = source_code_collection.get_by_relative_path(
            "jig/collector/jig_ast/__init__.py")
        assert jig_collector_jig_ast_init.module_path == ModulePath.from_str(
            "jig.collector.jig_ast")
        assert jig_collector_jig_ast_init.import_paths == ImportPathCollection([
            ImportPath.from_str("dataclasses"),
            ImportPath.from_str("typed_ast.ast3"),
            ImportPath.from_str("typing.List"),
            ImportPath.from_str("jig.collector.jig_ast.class_def.ClassDef"),
            ImportPath.from_str(
                "jig.collector.jig_ast.class_def.ClassDefVisitor"),
            ImportPath.from_str("jig.collector.jig_ast.imports.Import"),
            ImportPath.from_str("jig.collector.jig_ast.imports.ImportFrom"),
            ImportPath.from_str(
                "jig.collector.jig_ast.imports.ImportFromVisitor"),
            ImportPath.from_str("jig.collector.jig_ast.imports.ImportVisitor"),
        ])

        jig_collector_application = source_code_collection.get_by_relative_path(
            "jig/collector/application/__init__.py")
        assert jig_collector_application.module_path == ModulePath.from_str(
            "jig.collector.application")
        assert jig_collector_application.import_paths == ImportPathCollection([
            ImportPath.from_str("dataclasses"),
            ImportPath.from_str("os"),
            ImportPath.from_str("typing.List"),
            ImportPath.from_str("jig.collector.domain.FilePath"),
            ImportPath.from_str("jig.collector.domain.SourceCode"),
            ImportPath.from_str("jig.collector.domain.SourceFile"),
        ])
Exemple #2
0
    def _build_import_paths(
            cls, file: SourceFile,
            jig_source_code: JigSourceCode) -> ImportPathCollection:
        import_paths = ImportPathCollection()

        for import_ast in jig_source_code.imports:
            import_paths += ImportPathCollection.build_by_import_ast(
                import_ast)

        for import_from_ast in jig_source_code.import_froms:
            import_paths += ImportPathCollection.build_by_import_from_ast(
                file_path=file.source_file_path, import_from=import_from_ast)

        return import_paths
    def test_import_from_nested_module(self):
        import_from = parse_import_from("from .aaa.bbb import xxx")

        import_modules = ImportPathCollection.build_by_import_from_ast(
            file_path=self.SOURCE_FILE_PATH, import_from=import_from
        )

        assert import_modules == mod_collections("jig.collector.domain.aaa.bbb.xxx")
    def test_import_from_parent_path_with_module_name(self):
        import_from = parse_import_from("from ..jig_ast import submodule")

        import_modules = ImportPathCollection.build_by_import_from_ast(
            file_path=self.SOURCE_FILE_PATH, import_from=import_from
        )

        assert import_modules == mod_collections("jig.collector.jig_ast.submodule")
    def test_import_from_current_path(self):
        import_from = parse_import_from("from . import sibling")

        import_modules = ImportPathCollection.build_by_import_from_ast(
            file_path=self.SOURCE_FILE_PATH, import_from=import_from
        )

        assert import_modules == mod_collections("jig.collector.domain.sibling")
    def test_external_module(self):
        import_from = parse_import_from("from typed_ast import ast3 as ast")

        import_modules = ImportPathCollection.build_by_import_from_ast(
            file_path=self.SOURCE_FILE_PATH, import_from=import_from
        )

        assert import_modules == mod_collections("typed_ast.ast3")
    def test_builtin_module(self):
        import_from = parse_import_from("from os import path")

        import_modules = ImportPathCollection.build_by_import_from_ast(
            file_path=self.SOURCE_FILE_PATH, import_from=import_from
        )

        assert import_modules == mod_collections("os.path")
Exemple #8
0
    def build(
        cls,
        source_module_path: ModulePath,
        import_paths: Union[List[ImportPath], ImportPathCollection],
    ) -> "SourceCodeImportDependency":
        if not isinstance(import_paths, ImportPathCollection):
            import_paths = ImportPathCollection(import_paths)

        return cls(source_module_path=source_module_path, import_paths=import_paths)
    def test_import_from_parent_path(self):
        import_from = parse_import_from("from .. import jig_ast")

        import_modules = ImportPathCollection.build_by_import_from_ast(
            file_path=self.SOURCE_FILE_PATH, import_from=import_from
        )

        assert len(import_modules) == 1
        assert ImportPath.from_str("jig.collector.jig_ast") in import_modules
    def test_multiple_import_module(self):
        import_from = parse_import_from("from datetime import datetime, timezone")

        import_modules = ImportPathCollection.build_by_import_from_ast(
            file_path=self.SOURCE_FILE_PATH, import_from=import_from
        )

        assert import_modules == mod_collections(
            "datetime.datetime", "datetime.timezone"
        )
Exemple #11
0
    def test_build_import_dependency(self):
        source_file_path = SourceFilePath(root_path=Path("/root"),
                                          file_path=Path("/root/main.py"))
        assert str(source_file_path.module_path) == "main"

        code = SourceCode.build(file=SourceFile(
            source_file_path=source_file_path,
            size=100,
            content="from os import path, environ",
        ))

        assert code.build_import_dependency() == SourceCodeImportDependency(
            source_module_path=ModulePath.from_str("main"),
            import_paths=ImportPathCollection([
                ImportPath.from_str("os.path"),
                ImportPath.from_str("os.environ")
            ]),
        )
    def test_one_module(self):
        import_ast = parse_import("import os")

        import_modules = ImportPathCollection.build_by_import_ast(import_ast)
        assert import_modules == mod_collections("os")
def mod_collections(*modules) -> ImportPathCollection:
    return ImportPathCollection([ImportPath.from_str(module) for module in modules])
    def test_multiple_modules(self):
        import_ast = parse_import("import os, datetime.datetime")

        import_modules = ImportPathCollection.build_by_import_ast(import_ast)
        assert import_modules == mod_collections("os", "datetime.datetime")
    def test_nested_module(self):
        import_ast = parse_import("import datetime.datetime")

        import_modules = ImportPathCollection.build_by_import_ast(import_ast)
        assert import_modules == mod_collections("datetime.datetime")
Exemple #16
0
def collection(*args):
    modules = [ImportPath.from_str(p) for p in args]

    return ImportPathCollection(modules)