def test_application_directories(in_tmpdir, no_empty_path): # Similar to @bukzor's testing setup in_tmpdir.join('tests/testing').ensure_dir().join('__init__.py').ensure() # Should be classified 3rd party without argument ret = classify_import('testing') assert ret is ImportType.THIRD_PARTY # Should be application with extra directories ret = classify_import('testing', application_directories=('.', 'tests')) assert ret is ImportType.APPLICATION
def test_application_directories(in_tmpdir, no_empty_path): # Similar to @bukzor's testing setup in_tmpdir.join('tests/testing').ensure_dir().join('__init__.py').ensure() # Should be classified 3rd party without argument ret = classify_import('testing') assert ret is ImportType.THIRD_PARTY # Should be application with extra directories ret = classify_import('testing', application_directories=('.', 'tests')) assert ret is ImportType.APPLICATION
def test_unclassifiable_application_modules(): # Should be classified 3rd party without argument ret = classify_import('c_module') assert ret is ImportType.THIRD_PARTY # Should be classified application with the override ret = classify_import( 'c_module', unclassifiable_application_modules=('c_module', ), ) assert ret is ImportType.APPLICATION
def test_unclassifiable_application_modules_ignores_future(): # Trying to force __future__ to be APPLICATION shouldn't have any effect ret = classify_import( '__future__', unclassifiable_application_modules=('__future__', ), ) assert ret is ImportType.FUTURE
def test_true_namespace_package(tmpdir): site_packages = tmpdir.join('site-packages') site_packages.join('a').ensure_dir() sys_path = [site_packages.strpath] + sys.path with mock.patch.object(sys, 'path', sys_path): # while this is a py3+ feature, aspy.refactor_imports happens to get # this correct anyway! assert classify_import('a') == ImportType.THIRD_PARTY
def test_true_namespace_package(tmpdir): site_packages = tmpdir.join('site-packages') site_packages.join('a').ensure_dir() sys_path = [site_packages.strpath] + sys.path with mock.patch.object(sys, 'path', sys_path): # while this is a py3+ feature, aspy.refactor_imports happens to get # this correct anyway! assert classify_import('a') == ImportType.THIRD_PARTY
def test_package_existing_is_application_level(in_tmpdir, no_empty_path): in_tmpdir.join('my_package').ensure_dir().join('__init__.py').ensure() ret = classify_import('my_package') assert ret is ImportType.APPLICATION
def test_file_existing_is_application_level(in_tmpdir, no_empty_path): in_tmpdir.join('my_file.py').ensure() ret = classify_import('my_file') assert ret is ImportType.APPLICATION
def _maybe_append_name(self, name): name, _, _ = name.partition('.') imp_type = classify_import(name, self.appdirs) if imp_type == ImportType.THIRD_PARTY: self.third_party.add(name)
def classify_func(obj: AbstractImportObj) -> str: tp = classify_import(obj.import_statement.module, **kwargs) if tp == ImportType.FUTURE: return ImportType.FUTURE else: return '(not future)'
def classify_func(obj: AbstractImportObj) -> str: return classify_import(obj.import_statement.module, **kwargs)
def test_classify_import(module, expected): ret = classify_import(module) assert ret is expected
def test_symlink_path_different(in_tmpdir, no_empty_path): # pragma: no cover # symlink a file, these are likely to not be application files in_tmpdir.join('dest_file.py').ensure() in_tmpdir.join('src_file.py').mksymlinkto('dest-file.py') ret = classify_import('src_file') assert ret is ImportType.THIRD_PARTY
def test_classify_pythonpath_dot_app(in_tmpdir): in_tmpdir.join('f.py').ensure() with in_sys_path_and_pythonpath('.'): assert classify_import('f') is ImportType.APPLICATION
def test_classify_pythonpath_multiple(in_tmpdir): in_tmpdir.join('ppth').ensure_dir().join('f.py').ensure() with in_sys_path_and_pythonpath(os.pathsep.join(('ppth', 'foo'))): assert classify_import('f') is ImportType.THIRD_PARTY
def test_file_existing_is_application_level(in_tmpdir, no_empty_path): in_tmpdir.join('my_file.py').ensure() ret = classify_import('my_file') assert ret is ImportType.APPLICATION
def test_classify_pythonpath_dot_app(in_tmpdir): in_tmpdir.join('f.py').ensure() with in_sys_path_and_pythonpath('.'): assert classify_import('f') is ImportType.APPLICATION
def test_classify_pythonpath_third_party(in_tmpdir): in_tmpdir.join('ppth').ensure_dir().join('f.py').ensure() with in_sys_path_and_pythonpath('ppth'): assert classify_import('f') is ImportType.THIRD_PARTY
def test_symlink_path_different(in_tmpdir, no_empty_path): # pragma: no cover # symlink a file, these are likely to not be application files in_tmpdir.join('dest_file.py').ensure() in_tmpdir.join('src_file.py').mksymlinkto('dest-file.py') ret = classify_import('src_file') assert ret is ImportType.THIRD_PARTY
def test_empty_directory_is_not_package(in_tmpdir, no_empty_path): in_tmpdir.join('my_package').ensure_dir() ret = classify_import('my_package') assert ret is ImportType.THIRD_PARTY
def test_classify_pythonpath_zipimport(in_tmpdir): path_zip = in_tmpdir.join('ppth').ensure_dir().join('fzip.zip') with zipfile.ZipFile(str(path_zip), 'w') as fzip: fzip.writestr('fzip.py', '') with in_sys_path_and_pythonpath('ppth/fzip.zip'): assert classify_import('fzip') is ImportType.THIRD_PARTY
def import_type(self) -> ImportTypeValue: """Return the import type of the import.""" return cast(ImportTypeValue, classify_import(self.full_name))
def classify_func(obj): return ( classify_import(obj.import_statement.module) == ImportType.FUTURE )
def test_package_existing_is_application_level(in_tmpdir, no_empty_path): in_tmpdir.join('my_package').ensure_dir().join('__init__.py').ensure() ret = classify_import('my_package') assert ret is ImportType.APPLICATION
def test_classify_import(module, expected): ret = classify_import(module) assert ret is expected
def test_classify_pythonpath_third_party(in_tmpdir): in_tmpdir.join('ppth').ensure_dir().join('f.py').ensure() with in_sys_path_and_pythonpath('ppth'): assert classify_import('f') is ImportType.THIRD_PARTY
def test_empty_directory_is_not_package(in_tmpdir, no_empty_path): in_tmpdir.join('my_package').ensure_dir() ret = classify_import('my_package') assert ret is ImportType.THIRD_PARTY
def test_classify_pythonpath_multiple(in_tmpdir): in_tmpdir.join('ppth').ensure_dir().join('f.py').ensure() with in_sys_path_and_pythonpath(os.pathsep.join(('ppth', 'foo'))): assert classify_import('f') is ImportType.THIRD_PARTY
def classify_func(obj): return classify_import( obj.import_statement.module, **classify_kwargs ) == ImportType.FUTURE
def test_classify_embedded_builtin(in_tmpdir): path_zip = in_tmpdir.join('ppth').ensure_dir().join('fzip.zip') with zipfile.ZipFile(str(path_zip), 'w') as fzip: fzip.writestr('fzip.py', '') with in_sys_path('ppth/fzip.zip'): assert classify_import('fzip') is ImportType.BUILTIN
def classify_func(obj): return classify_import(obj.import_statement.module)