| 1 |
import pytest |
| 2 |
from hypothesis import given |
| 3 |
from hypothesis import strategies as st |
| 4 |
|
| 5 |
from isort import _parse_utils, parse |
| 6 |
from isort.settings import Config |
| 7 |
|
| 8 |
TEST_CONTENTS = """ |
| 9 |
import xyz |
| 10 |
import abc |
| 11 |
import (\\ # one |
| 12 |
one as \\ # two |
| 13 |
three) |
| 14 |
import \\ |
| 15 |
zebra as \\ # one |
| 16 |
not_bacon |
| 17 |
from x import (\\ # one |
| 18 |
one as \\ # two |
| 19 |
three) |
| 20 |
|
| 21 |
|
| 22 |
def function(): |
| 23 |
pass |
| 24 |
""" |
| 25 |
|
| 26 |
|
| 27 |
def test_file_contents() -> None: |
| 28 |
( |
| 29 |
in_lines, |
| 30 |
out_lines, |
| 31 |
import_index, |
| 32 |
_, |
| 33 |
_, |
| 34 |
_, |
| 35 |
_, |
| 36 |
_, |
| 37 |
change_count, |
| 38 |
original_line_count, |
| 39 |
_, |
| 40 |
_, |
| 41 |
_, |
| 42 |
_, |
| 43 |
) = parse.file_contents(TEST_CONTENTS, config=Config(default_section="")) |
| 44 |
assert "\n".join(in_lines) == TEST_CONTENTS |
| 45 |
assert "import" not in "\n".join(out_lines) |
| 46 |
assert import_index == 1 |
| 47 |
assert change_count == -11 |
| 48 |
assert original_line_count == len(in_lines) |
| 49 |
|
| 50 |
|
| 51 |
def test_file_contents_empty() -> None: |
| 52 |
parsed = parse.file_contents("", config=Config(default_section="")) |
| 53 |
assert parsed.in_lines == [] |
| 54 |
assert parsed.original_line_count == 0 |
| 55 |
|
| 56 |
|
| 57 |
@pytest.mark.parametrize("line_separator", ["\n", "\r\n", "\r"]) |
| 58 |
def test_file_contents_splits_only_on_newlines(line_separator: str) -> None: |
| 59 |
contents = line_separator.join(["import b", "import a", "\fpass"]) |
| 60 |
parsed = parse.file_contents(contents, config=Config(default_section="")) |
| 61 |
assert parsed.in_lines == ["import b", "import a", "\fpass"] |
| 62 |
|
| 63 |
|
| 64 |
# These tests were written by the `hypothesis.extra.ghostwriter` module |
| 65 |
# and is provided under the Creative Commons Zero public domain dedication. |
| 66 |
|
| 67 |
|
| 68 |
@given(contents=st.text()) |
| 69 |
def test_fuzz__infer_line_separator(contents: str) -> None: |
| 70 |
parse._infer_line_separator(contents=contents, configured="\n") |
| 71 |
|
| 72 |
|
| 73 |
@given(import_string=st.text()) |
| 74 |
def test_fuzz__strip_syntax(import_string: str) -> None: |
| 75 |
_parse_utils.strip_syntax(import_string=import_string) |
| 76 |
|
| 77 |
|
| 78 |
@given(line=st.text(), config=st.builds(Config)) |
| 79 |
def test_fuzz_import_type(line: str, config: Config) -> None: |
| 80 |
_parse_utils.import_type(line=line, config=config) |
| 81 |
|
| 82 |
|
| 83 |
@given(line=st.text(), in_quote=st.text(), needs_import=st.booleans()) |
| 84 |
def test_fuzz_skip_line(line: str, in_quote: str, needs_import: bool) -> None: |
| 85 |
_parse_utils.skip_line(line=line, in_quote=in_quote, needs_import=needs_import) |
| 86 |
|
| 87 |
|
| 88 |
@pytest.mark.parametrize( |
| 89 |
("raw_line", "expected"), |
| 90 |
[ |
| 91 |
("from . cimport a", "from . cimport a"), |
| 92 |
("from.cimport a", "from . cimport a"), |
| 93 |
("from..cimport a", "from .. cimport a"), |
| 94 |
("from . import a", "from . import a"), |
| 95 |
("from.import a", "from . import a"), |
| 96 |
("from..import a", "from .. import a"), |
| 97 |
("import *", "import *"), |
| 98 |
("import*", "import *"), |
| 99 |
("from . import a", "from . import a"), # noqa: PT014 |
| 100 |
("from .import a", "from . import a"), |
| 101 |
("from ..import a", "from .. import a"), |
| 102 |
("from . cimport a", "from . cimport a"), # noqa: PT014 |
| 103 |
("from .cimport a", "from . cimport a"), |
| 104 |
("from ..cimport a", "from .. cimport a"), |
| 105 |
("from\t.\timport a", "from . import a"), |
| 106 |
], |
| 107 |
) |
| 108 |
def test_normalize_line(raw_line: str, expected: str) -> None: |
| 109 |
line, returned_raw_line = _parse_utils.normalize_line(raw_line) |
| 110 |
assert line == expected |
| 111 |
assert returned_raw_line == raw_line |