def _check_basic(self, input_module_path, input_symbol, input_statement): """Checks all but to_str(), returning the imported_symbol instance.""" statement = ast.parse(input_statement).body[0] symbol = ImportedSymbol.get_symbol_from_statement(statement) msg = f'Expected symbol name "{input_symbol}", found "{symbol}"' self.assertEqual(symbol, input_symbol, msg) module_path = ImportedSymbol.get_module_path_from_statement(statement) msg = f'Expected module path "{input_module_path}", ' f'found "{module_path}"' self.assertEqual(module_path, input_module_path, msg) imported_symbol = ImportedSymbol(module_path, symbol) self.assertEqual(imported_symbol, ImportedSymbol.from_statement(statement)) return imported_symbol
def add_imported_symbol(self, statement): """ Keeps track of symbol names and importable entities """ for index, name in enumerate(statement.names): final_name = self._get_name_from_alias_statement(name) imported_symbol = ImportedSymbol.from_statement( statement, os.path.abspath(self.path), index) self.imported_symbols[final_name] = imported_symbol
def test_compound_dont_know_if_symbol_is_module(self): imported_symbol = ImportedSymbol("avocado.utils", "BaseClass", __file__) self.assertTrue(imported_symbol.is_importable(False))
def test_non_existing_module(self): imported_symbol = ImportedSymbol("avocado.utils", "non_existing_symbol", __file__) self.assertFalse(imported_symbol.is_importable(True))
def test_single(self): self.assertEqual( ImportedSymbol._split_last_module_path_component('os'), ('', 'os'))
def test_compound(self): imported_symbol = ImportedSymbol("avocado.utils", "software_manager", __file__) self.assertTrue(imported_symbol.is_importable(True))
def test_no_relative_import_from(self): statement = ast.parse("from os import path").body[0] self.assertEqual(ImportedSymbol._get_relative_prefix(statement), "")
def test_relative_import_from_same(self): statement = ast.parse("from .utils import function").body[0] self.assertEqual(ImportedSymbol._get_relative_prefix(statement), ".")
def test_same_from_statement(self): statement = ast.parse("from .test_bar import symbol").body[0] importer = "/abs/fs/location/of/selftests/unit/test_foo.py" symbol = ImportedSymbol.from_statement(statement, importer) self.assertEqual(symbol.get_relative_module_fs_path(), "/abs/fs/location/of/selftests/unit")
def test_relative_compound(self): res = ImportedSymbol._split_last_module_path_component("...a.b.c.d") self.assertEqual(res, ("a.b.c", "d"))
def test_incorrect_statement_type(self): statement = ast.parse("pass").body[0] with self.assertRaises(ValueError): _ = ImportedSymbol.get_symbol_from_statement(statement)
def test_upper(self): imported_symbol = ImportedSymbol("..module", "symbol", "/abs/fs/location/test.py") self.assertEqual(imported_symbol.get_relative_module_fs_path(), "/abs/fs")
def test_symbol_noalias(self): statement = ast.parse("from os import path").body[0] imported_symbol = ImportedSymbol.from_statement(statement) self.assertEqual(imported_symbol.symbol, "path") self.assertEqual(imported_symbol.symbol_name, "path")
def test_compound(self): self.assertEqual( ImportedSymbol._split_last_module_path_component("os.path"), ("os", "path"))
def test_module_noalias(self): statement = ast.parse("import os").body[0] imported_symbol = ImportedSymbol.from_statement(statement) self.assertEqual(imported_symbol.module_path, "os") self.assertEqual(imported_symbol.module_name, "os")
def test_non_existing_symbol(self): imported_symbol = ImportedSymbol("non_existing_module", "", __file__) self.assertFalse(imported_symbol.is_importable())
def test_relative_simple(self): self.assertEqual( ImportedSymbol._split_last_module_path_component("..utils"), ("", "utils"))
def test_non_existing_module_path(self): imported_symbol = ImportedSymbol("avocado.utils.non_existing", "", __file__) self.assertFalse(imported_symbol.is_importable())
def test_compound(self): statement = ast.parse("from path import parent3").body[0] importer = "/abs/fs/location/of/imports.py" symbol = ImportedSymbol.from_statement(statement, importer) self.assertEqual(symbol.get_parent_fs_path(), "/abs/fs/location/of/path")
def test_no_relative_import(self): statement = ast.parse("import os").body[0] self.assertEqual(ImportedSymbol._get_relative_prefix(statement), "")
def test_single(self): self.assertEqual( ImportedSymbol._split_last_module_path_component("os"), ("", "os"))
def test_relative_import_from_upper(self): statement = ast.parse("from ..selftests import utils").body[0] self.assertEqual(ImportedSymbol._get_relative_prefix(statement), "..")
def test_relative_simple(self): self.assertEqual( ImportedSymbol._split_last_module_path_component('..utils'), ('', 'utils'))
def test_compound_levels(self): statement = ast.parse("from .path.parent8 import Class8").body[0] importer = "/abs/fs/location/of/imports.py" symbol = ImportedSymbol.from_statement(statement, importer) self.assertEqual(symbol.get_parent_fs_path(), "/abs/fs/location/of")
def test_relative_compound(self): res = ImportedSymbol._split_last_module_path_component('...a.b.c.d') self.assertEqual(res, ('a.b.c', 'd'))
def test_single(self): imported_symbol = ImportedSymbol("avocado", "", __file__) self.assertTrue(imported_symbol.is_importable())
def test_compound(self): self.assertEqual( ImportedSymbol._split_last_module_path_component('os.path'), ('os', 'path'))