示例#1
0
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
示例#5
0
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
示例#7
0
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
示例#8
0
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)
示例#10
0
 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)'
示例#11
0
 def classify_func(obj: AbstractImportObj) -> str:
     return classify_import(obj.import_statement.module, **kwargs)
示例#12
0
def test_classify_import(module, expected):
    ret = classify_import(module)
    assert ret is expected
示例#13
0
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
示例#14
0
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
示例#20
0
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
示例#21
0
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
示例#22
0
 def import_type(self) -> ImportTypeValue:
     """Return the import type of the import."""
     return cast(ImportTypeValue, classify_import(self.full_name))
示例#23
0
 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
示例#26
0
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
示例#28
0
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
示例#29
0
 def classify_func(obj):
     return classify_import(
         obj.import_statement.module, **classify_kwargs
     ) == ImportType.FUTURE
示例#30
0
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
示例#31
0
 def classify_func(obj):
     return classify_import(obj.import_statement.module)