| 1 |
"""Tests the isort API module""" |
| 2 |
|
| 3 |
import os |
| 4 |
from io import StringIO |
| 5 |
from pathlib import Path |
| 6 |
from unittest.mock import MagicMock, patch |
| 7 |
|
| 8 |
import pytest |
| 9 |
|
| 10 |
from isort import ImportKey, api |
| 11 |
from isort.settings import Config |
| 12 |
|
| 13 |
imperfect_content = "import b\nimport a\n" |
| 14 |
fixed_content = "import a\nimport b\n" |
| 15 |
fixed_diff = "+import a\n import b\n-import a\n" |
| 16 |
|
| 17 |
|
| 18 |
@pytest.fixture |
| 19 |
def imperfect(tmp_path: Path) -> Path: |
| 20 |
imperfect_file = tmp_path / "test_needs_changes.py" |
| 21 |
imperfect_file.write_text(imperfect_content, "utf8") |
| 22 |
return imperfect_file |
| 23 |
|
| 24 |
|
| 25 |
def test_sort_file_with_bad_syntax(tmp_path: Path) -> None: |
| 26 |
tmp_file = tmp_path / "test_bad_syntax.py" |
| 27 |
tmp_file.write_text("""print('mismatching quotes")""", "utf8") |
| 28 |
with pytest.warns(UserWarning): |
| 29 |
api.sort_file(tmp_file, atomic=True) |
| 30 |
with pytest.warns(UserWarning): |
| 31 |
api.sort_file(tmp_file, atomic=True, write_to_stdout=True) |
| 32 |
|
| 33 |
|
| 34 |
def test_sort_file(imperfect: Path) -> None: |
| 35 |
assert api.sort_file(imperfect) |
| 36 |
assert imperfect.read_text() == fixed_content |
| 37 |
|
| 38 |
|
| 39 |
def test_sort_file_in_place(imperfect: Path) -> None: |
| 40 |
assert api.sort_file(imperfect, overwrite_in_place=True) |
| 41 |
assert imperfect.read_text() == fixed_content |
| 42 |
|
| 43 |
|
| 44 |
def test_sort_file_to_stdout(capsys: pytest.CaptureFixture[str], imperfect: Path) -> None: |
| 45 |
assert api.sort_file(imperfect, write_to_stdout=True) |
| 46 |
out, _ = capsys.readouterr() |
| 47 |
assert out == fixed_content.replace("\n", os.linesep) |
| 48 |
|
| 49 |
|
| 50 |
def test_other_ask_to_apply(imperfect: Path) -> None: |
| 51 |
# First show diff, but ensure change won't get written by asking to apply |
| 52 |
# and ensuring answer is no. |
| 53 |
with patch("builtins.input", MagicMock(return_value="n")): |
| 54 |
assert not api.sort_file(imperfect, ask_to_apply=True) |
| 55 |
assert imperfect.read_text() == imperfect_content |
| 56 |
|
| 57 |
# Then run again, but apply the change (answer is yes) |
| 58 |
with patch("builtins.input", MagicMock(return_value="y")): |
| 59 |
assert api.sort_file(imperfect, ask_to_apply=True) |
| 60 |
assert imperfect.read_text() == fixed_content |
| 61 |
|
| 62 |
|
| 63 |
def test_check_file_no_changes(capsys: pytest.CaptureFixture[str], tmp_path: Path) -> None: |
| 64 |
perfect = tmp_path / "test_no_changes.py" |
| 65 |
perfect.write_text("import a\nimport b\n", "utf8") |
| 66 |
assert api.check_file(perfect, show_diff=True) |
| 67 |
out, _ = capsys.readouterr() |
| 68 |
assert not out |
| 69 |
|
| 70 |
|
| 71 |
def test_check_file_with_changes(capsys: pytest.CaptureFixture[str], imperfect: Path) -> None: |
| 72 |
assert not api.check_file(imperfect, show_diff=True) |
| 73 |
out, _ = capsys.readouterr() |
| 74 |
assert fixed_diff.replace("\n", os.linesep) in out |
| 75 |
|
| 76 |
|
| 77 |
def test_sorted_imports_multiple_configs() -> None: |
| 78 |
with pytest.raises(ValueError, match="You can either specify custom configuration options"): |
| 79 |
api.sort_code_string("import os", config=Config(line_length=80), line_length=80) |
| 80 |
|
| 81 |
|
| 82 |
def test_diff_stream() -> None: |
| 83 |
output = StringIO() |
| 84 |
assert api.sort_stream(StringIO("import b\nimport a\n"), output, show_diff=True) |
| 85 |
output.seek(0) |
| 86 |
assert fixed_diff in output.read() |
| 87 |
|
| 88 |
|
| 89 |
def test_sort_code_string_mixed_newlines() -> None: |
| 90 |
assert api.sort_code_string("import A\n\r\nimportA\n\n") == "import A\r\n\r\nimportA\r\n\n" |
| 91 |
|
| 92 |
|
| 93 |
def test_find_imports_in_file(imperfect: Path) -> None: |
| 94 |
found_imports = list(api.find_imports_in_file(imperfect)) |
| 95 |
assert "b" in [found_import.module for found_import in found_imports] |
| 96 |
|
| 97 |
|
| 98 |
def test_find_imports_in_file_error(tmp_path: Path) -> None: |
| 99 |
test_path = tmp_path / "test_path.py" |
| 100 |
test_path.mkdir() |
| 101 |
with pytest.warns(UserWarning): |
| 102 |
assert not list(api.find_imports_in_file(test_path)) |
| 103 |
|
| 104 |
|
| 105 |
def test_find_imports_in_code() -> None: |
| 106 |
code = """ |
| 107 |
from x.y import z as a |
| 108 |
from x.y import z as a |
| 109 |
from x.y import z |
| 110 |
import x.y |
| 111 |
import x |
| 112 |
""" |
| 113 |
assert len(list(api.find_imports_in_code(code))) == 5 |
| 114 |
assert len(list(api.find_imports_in_code(code, unique=True))) == 4 |
| 115 |
assert len(list(api.find_imports_in_code(code, unique=ImportKey.ATTRIBUTE))) == 3 |
| 116 |
assert len(list(api.find_imports_in_code(code, unique=ImportKey.MODULE))) == 2 |
| 117 |
assert len(list(api.find_imports_in_code(code, unique=ImportKey.PACKAGE))) == 1 |