Exemplo n.º 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
Exemplo n.º 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
Exemplo n.º 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
Exemplo n.º 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
Exemplo n.º 9
0
 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)
Exemplo n.º 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)'
Exemplo n.º 11
0
 def classify_func(obj: AbstractImportObj) -> str:
     return classify_import(obj.import_statement.module, **kwargs)
Exemplo n.º 12
0
def test_classify_import(module, expected):
    ret = classify_import(module)
    assert ret is expected
Exemplo n.º 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
Exemplo n.º 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
Exemplo n.º 15
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
Exemplo n.º 16
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
Exemplo n.º 17
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
Exemplo n.º 18
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
Exemplo n.º 19
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
Exemplo n.º 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
Exemplo n.º 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
Exemplo n.º 22
0
 def import_type(self) -> ImportTypeValue:
     """Return the import type of the import."""
     return cast(ImportTypeValue, classify_import(self.full_name))
Exemplo n.º 23
0
 def classify_func(obj):
     return (
         classify_import(obj.import_statement.module) ==
         ImportType.FUTURE
     )
Exemplo n.º 24
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
Exemplo n.º 25
0
def test_classify_import(module, expected):
    ret = classify_import(module)
    assert ret is expected
Exemplo n.º 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
Exemplo n.º 27
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
Exemplo n.º 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
Exemplo n.º 29
0
 def classify_func(obj):
     return classify_import(
         obj.import_statement.module, **classify_kwargs
     ) == ImportType.FUTURE
Exemplo n.º 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
Exemplo n.º 31
0
 def classify_func(obj):
     return classify_import(obj.import_statement.module)