def test_find_egg_module(self): mtype, mfile = modutils._module_file( [self.package], [resources.find('data/MyPyPa-0.1.0-py2.5.egg')]) self.assertEqual(mtype, modutils.PY_ZIPMODULE) self.assertEqual( mfile.split(os.sep)[-3:], ["data", "MyPyPa-0.1.0-py2.5.egg", self.package])
def tearDown(self): # Since we may have created a brainless manager, leading # to a new cache builtin module and proxy classes in the constants, # clear out the global manager cache. MANAGER.clear_cache() sys.path.pop(0) sys.path_importer_cache.pop(resources.find('data'), None)
def test_get_module_files_1(self): package = resources.find('data/find_test') modules = set(modutils.get_module_files(package, [])) expected = ['__init__.py', 'module.py', 'module2.py', 'noendingnewline.py', 'nonregr.py'] self.assertEqual(modules, {os.path.join(package, x) for x in expected})
def test_custom_path(self): datadir = resources.find('') if datadir.startswith(modutils.EXT_LIB_DIR): self.skipTest('known breakage of is_standard_module on installed package') self.assertTrue(modutils.is_standard_module('data.module', (datadir,))) self.assertTrue(modutils.is_standard_module('data.module', (os.path.abspath(datadir),)))
def test_find_egg_module(self): found_spec = spec.find_spec( [self.package], [resources.find('data/MyPyPa-0.1.0-py2.5.egg')]) self.assertEqual(found_spec.type, spec.ModuleType.PY_ZIPMODULE) self.assertEqual(found_spec.location.split(os.sep)[-3:], ["data", "MyPyPa-0.1.0-py2.5.egg", self.package])
def tearDown(self): # Since we may have created a brainless manager, leading # to a new cache builtin module and proxy classes in the constants, # clear out the global manager cache. MANAGER.clear_cache(self._builtins) MANAGER.always_load_extensions = False sys.path.pop(0) sys.path_importer_cache.pop(resources.find("data"), None)
def setUp(self): super(LinkerTest, self).setUp() self.project = MANAGER.project_from_files( [resources.find('data')], astroid_wrapper, black_list=['joined_strings.py']) self.linker = inspector.Linker(self.project) self.linker.visit(self.project)
def setUp(self): resources.SysPathSetup.setUp(self) self.project = MANAGER.project_from_files( [resources.find('data')], func_wrapper=_silent_no_wrap, black_list=['joined_strings.py']) self.linker = inspector.Linker(self.project) self.linker.visit(self.project)
def test_stream_api(self): path = resources.find('data/all.py') astroid = builder.AstroidBuilder().file_build(path, 'all') stream = astroid.stream() self.assertTrue(hasattr(stream, 'close')) with stream: with open(path, 'rb') as file_io: self.assertEqual(stream.read(), file_io.read())
def test_get_module_files_1(self): """given a directory return a list of all available python module's files, even in subdirectories """ package = resources.find('data/find_test') modules = set(modutils.get_module_files(package, [])) self.assertEqual( modules, {os.path.join(package, x) for x in ['__init__.py', 'module.py', 'module2.py', 'noendingnewline.py', 'nonregr.py']})
def test_get_all_files(self): """test that list_all returns all Python files from given location """ non_package = resources.find('data/notamodule') modules = modutils.get_module_files(non_package, [], list_all=True) self.assertEqual( modules, [os.path.join(non_package, 'file.py')], )
def test_custom_path(self): datadir = resources.find("") if any(datadir.startswith(p) for p in modutils.EXT_LIB_DIRS): self.skipTest("known breakage of is_standard_module on installed package") self.assertTrue(modutils.is_standard_module("data.module", (datadir,))) self.assertTrue( modutils.is_standard_module("data.module", (os.path.abspath(datadir),)) )
def _test_ast_from_old_namespace_package_protocol(self, root): origpath = sys.path[:] paths = [resources.find("data/path_{}_{}".format(root, index)) for index in range(1, 4)] sys.path.extend(paths) try: for name in ("foo", "bar", "baz"): module = self.manager.ast_from_module_name("package." + name) self.assertIsInstance(module, astroid.Module) finally: sys.path = origpath
def _test_ast_from_old_namespace_package_protocol(self, root): origpath = sys.path[:] paths = [resources.find('data/path_{}_{}'.format(root, index)) for index in range(1, 4)] sys.path.extend(paths) try: for name in ('foo', 'bar', 'baz'): module = self.manager.ast_from_module_name('package.' + name) self.assertIsInstance(module, astroid.Module) finally: sys.path = origpath
def test_import_2(self): data = '''from . import subpackage as pouet''' astroid = test_utils.build_module(data, 'package', 'data/package/__init__.py') sys.path.insert(0, resources.find('data')) try: m = astroid.import_module('', level=1) self.assertEqual(m.name, 'package') infered = list(astroid.igetattr('pouet')) self.assertEqual(len(infered), 1) self.assertEqual(infered[0].name, 'package.subpackage') finally: del sys.path[0]
def test_get_module_files_1(self): """given a directory return a list of all available python module's files, even in subdirectories """ package = resources.find('data/find_test') modules = set(modutils.get_module_files(package, [])) self.assertEqual( modules, { os.path.join(package, x) for x in [ '__init__.py', 'module.py', 'module2.py', 'noendingnewline.py', 'nonregr.py' ] })
def test_module_base_props(self): """test base properties and method of a astroid module""" module = self.module self.assertEqual(module.name, 'data.module') self.assertEqual(module.doc, "test module for astroid\n") self.assertEqual(module.fromlineno, 0) self.assertIsNone(module.parent) self.assertEqual(module.frame(), module) self.assertEqual(module.root(), module) self.assertEqual(module.file, os.path.abspath(resources.find('data/module.py'))) self.assertEqual(module.pure_python, 1) self.assertEqual(module.package, 0) self.assertFalse(module.is_statement) self.assertEqual(module.statement(), module) self.assertEqual(module.statement(), module)
def test_module_base_props(self): """test base properties and method of an astroid module""" module = self.module self.assertEqual(module.name, "data.module") self.assertEqual(module.doc, "test module for astroid\n") self.assertEqual(module.fromlineno, 0) self.assertIsNone(module.parent) self.assertEqual(module.frame(), module) self.assertEqual(module.root(), module) self.assertEqual(module.file, os.path.abspath(resources.find("data/module.py"))) self.assertEqual(module.pure_python, 1) self.assertEqual(module.package, 0) self.assertFalse(module.is_statement) self.assertEqual(module.statement(), module) self.assertEqual(module.statement(), module)
def test_implicit_namespace_package(self): data_dir = os.path.dirname(resources.find('data/namespace_pep_420')) contribute = os.path.join(data_dir, 'contribute_to_namespace') for value in (data_dir, contribute): sys.path.insert(0, value) try: module = self.manager.ast_from_module_name('namespace_pep_420.module') self.assertIsInstance(module, astroid.Module) self.assertEqual(module.name, 'namespace_pep_420.module') var = next(module.igetattr('var')) self.assertIsInstance(var, astroid.Const) self.assertEqual(var.value, 42) finally: for _ in range(2): sys.path.pop(0)
def test_special_attributes(self): self.assertEqual(len(self.module.getattr('__name__')), 1) self.assertIsInstance(self.module.getattr('__name__')[0], nodes.Const) self.assertEqual(self.module.getattr('__name__')[0].value, 'data.module') self.assertEqual(len(self.module.getattr('__doc__')), 1) self.assertIsInstance(self.module.getattr('__doc__')[0], nodes.Const) self.assertEqual(self.module.getattr('__doc__')[0].value, 'test module for astroid\n') self.assertEqual(len(self.module.getattr('__file__')), 1) self.assertIsInstance(self.module.getattr('__file__')[0], nodes.Const) self.assertEqual(self.module.getattr('__file__')[0].value, os.path.abspath(resources.find('data/module.py'))) self.assertEqual(len(self.module.getattr('__dict__')), 1) self.assertIsInstance(self.module.getattr('__dict__')[0], nodes.Dict) self.assertRaises(NotFoundError, self.module.getattr, '__path__') self.assertEqual(len(self.pack.getattr('__path__')), 1) self.assertIsInstance(self.pack.getattr('__path__')[0], nodes.List)
def test_file_stream_api(self): path = resources.find('data/all.py') astroid = builder.AstroidBuilder().file_build(path, 'all') if __pkginfo__.numversion >= (1, 6): # file_stream is slated for removal in astroid 1.6. with self.assertRaises(AttributeError): astroid.file_stream else: # Until astroid 1.6, Module.file_stream will emit # PendingDeprecationWarning in 1.4, DeprecationWarning # in 1.5 and finally it will be removed in 1.6, leaving # only Module.stream as the recommended way to retrieve # its file stream. with warnings.catch_warnings(record=True) as cm: warnings.simplefilter("always") self.assertIsNot(astroid.file_stream, astroid.file_stream) self.assertGreater(len(cm), 1) self.assertEqual(cm[0].category, PendingDeprecationWarning)
def _test_ast_from_zip(self, archive): origpath = sys.path[:] sys.modules.pop("mypypa", None) archive_path = resources.find(archive) sys.path.insert(0, archive_path) try: module = self.manager.ast_from_module_name("mypypa") self.assertEqual(module.name, "mypypa") end = os.path.join(archive, "mypypa") self.assertTrue(module.file.endswith(end), "%s doesn't endswith %s" % (module.file, end)) finally: # remove the module, else after importing egg, we don't get the zip if "mypypa" in self.manager.astroid_cache: del self.manager.astroid_cache["mypypa"] del self.manager._mod_file_cache[("mypypa", None)] if archive_path in sys.path_importer_cache: del sys.path_importer_cache[archive_path] sys.path = origpath
def _test_ast_from_zip(self, archive): origpath = sys.path[:] sys.modules.pop('mypypa', None) archive_path = resources.find(archive) sys.path.insert(0, archive_path) try: module = self.manager.ast_from_module_name('mypypa') self.assertEqual(module.name, 'mypypa') end = os.path.join(archive, 'mypypa') self.assertTrue(module.file.endswith(end), "%s doesn't endswith %s" % (module.file, end)) finally: # remove the module, else after importing egg, we don't get the zip if 'mypypa' in self.manager.astroid_cache: del self.manager.astroid_cache['mypypa'] del self.manager._mod_file_cache[('mypypa', None)] if archive_path in sys.path_importer_cache: del sys.path_importer_cache[archive_path] sys.path = origpath
def test_project_node(self): obj = self.manager.project_from_files([resources.find('data')], _silent_no_wrap, 'data') expected = [ 'data', 'data.SSL1', 'data.SSL1.Connection1', 'data.absimp', 'data.absimp.sidepackage', 'data.absimp.string', 'data.absimport', 'data.all', 'data.appl', 'data.appl.myConnection', 'data.clientmodule_test', 'data.descriptor_crash', 'data.email', 'data.find_test', 'data.find_test.module', 'data.find_test.module2', 'data.find_test.noendingnewline', 'data.find_test.nonregr', 'data.format', 'data.lmfp', 'data.lmfp.foo', 'data.module', 'data.module1abs', 'data.module1abs.core', 'data.module2', 'data.noendingnewline', 'data.nonregr', 'data.notall', 'data.package', 'data.package.absimport', 'data.package.hello', 'data.package.import_package_subpackage_module', 'data.package.subpackage', 'data.package.subpackage.module', 'data.recursion', 'data.suppliermodule_test', 'data.unicode_package', 'data.unicode_package.core'] self.assertListEqual(sorted(obj.keys()), expected)
def test_special_attributes(self): self.assertEqual(len(self.module.getattr('__name__')), 1) self.assertIsInstance(self.module.getattr('__name__')[0], nodes.Const) self.assertEqual( self.module.getattr('__name__')[0].value, 'data.module') self.assertEqual(len(self.module.getattr('__doc__')), 1) self.assertIsInstance(self.module.getattr('__doc__')[0], nodes.Const) self.assertEqual( self.module.getattr('__doc__')[0].value, 'test module for astroid\n') self.assertEqual(len(self.module.getattr('__file__')), 1) self.assertIsInstance(self.module.getattr('__file__')[0], nodes.Const) self.assertEqual( self.module.getattr('__file__')[0].value, os.path.abspath(resources.find('data/module.py'))) self.assertEqual(len(self.module.getattr('__dict__')), 1) self.assertIsInstance(self.module.getattr('__dict__')[0], nodes.Dict) self.assertRaises(NotFoundError, self.module.getattr, '__path__') self.assertEqual(len(self.pack.getattr('__path__')), 1) self.assertIsInstance(self.pack.getattr('__path__')[0], nodes.List)
def test_project_node(self): obj = self.manager.project_from_files([resources.find('data')], _silent_no_wrap, 'data') expected = [ 'data', 'data.SSL1', 'data.SSL1.Connection1', 'data.absimp', 'data.absimp.sidepackage', 'data.absimp.string', 'data.absimport', 'data.all', 'data.appl', 'data.appl.myConnection', 'data.clientmodule_test', 'data.descriptor_crash', 'data.email', 'data.find_test', 'data.find_test.module', 'data.find_test.module2', 'data.find_test.noendingnewline', 'data.find_test.nonregr', 'data.format', 'data.lmfp', 'data.lmfp.foo', 'data.module', 'data.module1abs', 'data.module1abs.core', 'data.module2', 'data.noendingnewline', 'data.nonregr', 'data.notall', 'data.package', 'data.package.absimport', 'data.package.hello', 'data.package.import_package_subpackage_module', 'data.package.subpackage', 'data.package.subpackage.module', 'data.recursion', 'data.suppliermodule_test', 'data.unicode_package', 'data.unicode_package.core' ] self.assertListEqual(sorted(obj.keys()), expected)
def test_from_directory(self): obj = self.manager.project_from_files([resources.find('data')], _silent_no_wrap, 'data') self.assertEqual(obj.name, 'data') self.assertEqual(obj.path, os.path.abspath(resources.find('data/__init__.py')))
def test_operator_precedence(self): with open(resources.find('data/operator_precedence.py')) as f: for code in f: self.check_as_string_ast_equality(code)
def test_module2_as_string(self): """check as_string on a whole module prepared to be returned identically """ module2 = resources.build_file('data/module2.py', 'data.module2') with open(resources.find('data/module2.py'), 'r') as fobj: self.assertMultiLineEqual(module2.as_string(), fobj.read())
def test_file_stream_physical(self): path = resources.find('data/all.py') astroid = builder.AstroidBuilder().file_build(path, 'all') with open(path, 'rb') as file_io: with warnings.catch_warnings(record=True): self.assertEqual(astroid.file_stream.read(), file_io.read())
def test_zip_import_data(self): """check if zip_import_data works""" filepath = resources.find('data/MyPyPa-0.1.0-py2.5.zip/mypypa') ast = self.manager.zip_import_data(filepath) self.assertEqual(ast.name, 'mypypa')
def setUp(self): sys.path.insert(0, resources.find('data')) MANAGER.always_load_extensions = True MANAGER.astroid_cache[BUILTINS] = self._builtins
def project(self): return self.manager.project_from_files( [resources.find('data')], _silent_no_wrap, 'data', black_list=['joined_strings.py'])
def test_find_zipped_module(self): spec = modutils._find_spec( [self.package], [resources.find('data/MyPyPa-0.1.0-py2.5.zip')]) self.assertEqual(spec.type, modutils.ModuleType.PY_ZIPMODULE) self.assertEqual(spec.location.split(os.sep)[-3:], ["data", "MyPyPa-0.1.0-py2.5.zip", self.package])
def test_from_directory(self): self.assertEqual(self.project.name, 'data') self.assertEqual(self.project.path, os.path.abspath(resources.find('data/__init__.py')))
def test_find_egg_module(self): mtype, mfile = modutils._module_file( [self.package], [resources.find('data/MyPyPa-0.1.0-py2.5.egg')]) self.assertEqual(mtype, modutils.PY_ZIPMODULE) self.assertEqual(mfile.split(os.sep)[-3:], ["data", "MyPyPa-0.1.0-py2.5.egg", self.package])
def setUp(self): sys.path.insert(0, resources.find('data')) MANAGER.always_load_extensions = True
def setUp(self): super(LinkerTest, self).setUp() self.project = MANAGER.project_from_files([resources.find('data')], astroid_wrapper) self.linker = inspector.Linker(self.project) self.linker.visit(self.project)
def tearDown(self): MANAGER.always_load_extensions = False sys.path.pop(0) sys.path_importer_cache.pop(resources.find("data"), None)
def setUp(self): resources.SysPathSetup.setUp(self) self.project = MANAGER.project_from_files([resources.find('data')], func_wrapper=_silent_no_wrap) self.linker = inspector.Linker(self.project) self.linker.visit(self.project)
def setUp(self): sys.path.insert(0, resources.find('data'))
def test_module_as_string(self): """check as_string on a whole module prepared to be returned identically """ module = resources.build_file("data/module.py", "data.module") with open(resources.find("data/module.py"), "r") as fobj: self.assertMultiLineEqual(module.as_string(), fobj.read())