| 1 |
"""Tests for the isort import placement module""" |
| 2 |
|
| 3 |
from functools import partial |
| 4 |
from pathlib import Path |
| 5 |
|
| 6 |
from isort import place, sections |
| 7 |
from isort.settings import Config |
| 8 |
|
| 9 |
|
| 10 |
def test_module(src_path: Path) -> None: |
| 11 |
place_tester = partial(place.module, config=Config(src_paths=[src_path])) |
| 12 |
assert place_tester("isort") == sections.FIRSTPARTY |
| 13 |
assert place_tester("os") == sections.STDLIB |
| 14 |
assert place_tester(".deprecated") == sections.LOCALFOLDER |
| 15 |
assert place_tester("__future__") == sections.FUTURE |
| 16 |
assert place_tester("hug") == sections.THIRDPARTY |
| 17 |
|
| 18 |
|
| 19 |
def test_extra_standard_library(src_path: Path) -> None: |
| 20 |
place_tester = partial( |
| 21 |
place.module, config=Config(src_paths=[src_path], extra_standard_library=["hug"]) |
| 22 |
) |
| 23 |
assert place_tester("os") == sections.STDLIB |
| 24 |
assert place_tester("hug") == sections.STDLIB |
| 25 |
|
| 26 |
|
| 27 |
def test_no_standard_library_placement() -> None: |
| 28 |
assert place.module_with_reason( |
| 29 |
"pathlib", config=Config(sections=["THIRDPARTY"], default_section="THIRDPARTY") |
| 30 |
) == ("THIRDPARTY", "Default option in Config or universal default.") |
| 31 |
assert place.module("pathlib") == "STDLIB" |
| 32 |
|
| 33 |
|
| 34 |
def test_namespace_package_placement(examples_path: Path) -> None: |
| 35 |
namespace_examples = examples_path / "namespaces" |
| 36 |
|
| 37 |
implicit = namespace_examples / "implicit" |
| 38 |
pkg_resource = namespace_examples / "pkg_resource" |
| 39 |
pkgutil = namespace_examples / "pkgutil" |
| 40 |
for namespace_test in (implicit, pkg_resource, pkgutil): |
| 41 |
print(namespace_test) |
| 42 |
config = Config(settings_path=namespace_test) |
| 43 |
no_namespaces = Config(settings_path=namespace_test, auto_identify_namespace_packages=False) |
| 44 |
namespace_override = Config(settings_path=namespace_test, known_firstparty=["root.name"]) |
| 45 |
assert place.module("root.name", config=config) == "THIRDPARTY" |
| 46 |
assert place.module("root.nested", config=config) == "FIRSTPARTY" |
| 47 |
assert place.module("root.name", config=no_namespaces) == "FIRSTPARTY" |
| 48 |
assert place.module("root.name", config=namespace_override) == "FIRSTPARTY" |
| 49 |
|
| 50 |
no_namespace = namespace_examples / "none" |
| 51 |
almost_implicit = namespace_examples / "almost-implicit" |
| 52 |
weird_encoding = namespace_examples / "weird_encoding" |
| 53 |
for lacks_namespace in (no_namespace, almost_implicit, weird_encoding): |
| 54 |
config = Config(settings_path=lacks_namespace) |
| 55 |
manual_namespace = Config(settings_path=lacks_namespace, namespace_packages=["root"]) |
| 56 |
assert place.module("root.name", config=config) == "FIRSTPARTY" |
| 57 |
assert place.module("root.nested", config=config) == "FIRSTPARTY" |
| 58 |
assert place.module("root.name", config=manual_namespace) == "THIRDPARTY" |
| 59 |
assert place.module("root.nested", config=config) == "FIRSTPARTY" |