| 1 |
import pytest |
| 2 |
|
| 3 |
from isort import code, wrap |
| 4 |
from isort.settings import Config |
| 5 |
from isort.wrap_modes import WrapModes |
| 6 |
|
| 7 |
|
| 8 |
def test_import_statement() -> None: |
| 9 |
assert wrap.import_statement("", [], []) == "" |
| 10 |
assert ( |
| 11 |
wrap.import_statement("from x import ", ["y"], [], config=Config(balanced_wrapping=True)) |
| 12 |
== "from x import (y)" |
| 13 |
) |
| 14 |
assert ( |
| 15 |
wrap.import_statement("from long_import ", ["verylong"] * 10, []) |
| 16 |
== """from long_import (verylong, verylong, verylong, verylong, verylong, verylong, |
| 17 |
verylong, verylong, verylong, verylong)""" |
| 18 |
) |
| 19 |
assert wrap.import_statement("from x import ", ["y", "z"], [], explode=True) == ( |
| 20 |
"from x import (\n y,\n z,\n)" |
| 21 |
) |
| 22 |
|
| 23 |
|
| 24 |
@pytest.mark.parametrize( |
| 25 |
("multi_line_output", "expected"), |
| 26 |
[ |
| 27 |
( |
| 28 |
WrapModes.VERTICAL_HANGING_INDENT, |
| 29 |
"""from a import ( |
| 30 |
b as c # comment that is long enough that this import doesn't fit in one line (parens) |
| 31 |
)""", |
| 32 |
), |
| 33 |
( |
| 34 |
WrapModes.VERTICAL, |
| 35 |
"""from a import ( |
| 36 |
b as c) # comment that is long enough that this import doesn't fit in one line (parens)""", |
| 37 |
), |
| 38 |
], |
| 39 |
) |
| 40 |
def test_line__comment_with_brackets__expects_unchanged_comment( |
| 41 |
multi_line_output: WrapModes, expected: str |
| 42 |
) -> None: |
| 43 |
content = ( |
| 44 |
"from a import b as c " |
| 45 |
"# comment that is long enough that this import doesn't fit in one line (parens)" |
| 46 |
) |
| 47 |
config = Config( |
| 48 |
multi_line_output=multi_line_output, |
| 49 |
use_parentheses=True, |
| 50 |
) |
| 51 |
|
| 52 |
assert wrap.line(content=content, line_separator="\n", config=config) == expected |
| 53 |
|
| 54 |
|
| 55 |
def test_line_star_import_wrapped_with_backslash() -> None: |
| 56 |
"""Star imports should use backslashes when parentheses were not requested.""" |
| 57 |
content = "from very.very.very.very.very.very.very.very.very.long.line import *" |
| 58 |
expected = "from very.very.very.very.very.very.very.very.very.long.line import \\\n *" |
| 59 |
config = Config(line_length=20) |
| 60 |
assert wrap.line(content=content, line_separator="\n", config=config) == expected |
| 61 |
|
| 62 |
|
| 63 |
def test_line_star_import_is_not_wrapped_with_parentheses() -> None: |
| 64 |
"""Star imports cannot use parenthesized wrapping (issue #2649).""" |
| 65 |
content = "from very.very.very.very.very.very.very.very.very.long.line import *" |
| 66 |
config = Config(line_length=20, use_parentheses=True) |
| 67 |
assert wrap.line(content=content, line_separator="\n", config=config) == content |
| 68 |
|
| 69 |
|
| 70 |
def test_line_star_import_is_not_wrapped_in_vertical_mode() -> None: |
| 71 |
"""Vertical modes use parentheses even without use_parentheses.""" |
| 72 |
content = "from very.very.very.very.very.very.very.very.very.long.line import *" |
| 73 |
config = Config(line_length=20, multi_line_output=WrapModes.VERTICAL_HANGING_INDENT) |
| 74 |
assert wrap.line(content=content, line_separator="\n", config=config) == content |
| 75 |
|
| 76 |
|
| 77 |
def test_line_star_cimport_wrapped_with_backslash() -> None: |
| 78 |
"""Star cimports should also use backslashes.""" |
| 79 |
content = "from very.very.very.very.very.very.very.very.very.long.line cimport *" |
| 80 |
expected = "from very.very.very.very.very.very.very.very.very.long.line cimport \\\n *" |
| 81 |
config = Config(line_length=20) |
| 82 |
assert wrap.line(content=content, line_separator="\n", config=config) == expected |
| 83 |
|
| 84 |
|
| 85 |
def test_line_star_import_with_comment_wrapped_with_backslash() -> None: |
| 86 |
"""When falling back to backslashes for start imports, comments should be preserved.""" |
| 87 |
content = "from very.very.very.very.very.very.very.very.very.long.line import * # noqa: F401" |
| 88 |
config = Config(line_length=20) |
| 89 |
expected = ( |
| 90 |
"from very.very.very.very.very.very.very.very.very.long.line import \\\n * # noqa: F401" |
| 91 |
) |
| 92 |
assert wrap.line(content=content, line_separator="\n", config=config) == expected |
| 93 |
|
| 94 |
|
| 95 |
def test_line_star_import_in_noqa_mode_is_not_backslash_wrapped() -> None: |
| 96 |
"""NOQA mode should prevent backslashes getting inserted for too long star imports.""" |
| 97 |
content = "from very.very.very.very.very.very.very.very.very.very.very.long.line import *" |
| 98 |
config = Config(line_length=20, multi_line_output=WrapModes.NOQA) |
| 99 |
assert wrap.line(content=content, line_separator="\n", config=config) == ( |
| 100 |
"from very.very.very.very.very.very.very.very.very.very.very.long.line import * # NOQA" |
| 101 |
) |
| 102 |
|
| 103 |
|
| 104 |
def test_star_import_wrapped_end_to_end() -> None: |
| 105 |
"""New lines should be preserved at the end of too long start imports.""" |
| 106 |
source = "from very.very.very.very.very.very.very.very.very.long.line import *\n" |
| 107 |
expected = "from very.very.very.very.very.very.very.very.very.long.line import \\\n *\n" |
| 108 |
assert code(source, line_length=20, force_single_line=True) == expected |