| 1 |
import json |
| 2 |
import os |
| 3 |
import pathlib |
| 4 |
import shutil |
| 5 |
import subprocess |
| 6 |
from datetime import datetime |
| 7 |
from pathlib import Path |
| 8 |
import unittest.mock |
| 9 |
|
| 10 |
import pytest |
| 11 |
from hypothesis import given |
| 12 |
from hypothesis import strategies as st |
| 13 |
|
| 14 |
from isort import main |
| 15 |
from isort._version import _VERSION_STRING, _IS_COMPILED |
| 16 |
from isort.exceptions import InvalidSettingsPath |
| 17 |
from isort.settings import DEFAULT_CONFIG, Config |
| 18 |
from isort.wrap_modes import WrapModes |
| 19 |
from .utils import as_stream |
| 20 |
from io import BytesIO, TextIOWrapper |
| 21 |
|
| 22 |
|
| 23 |
@given( |
| 24 |
file_name=st.text(), |
| 25 |
config=st.builds(Config), |
| 26 |
check=st.booleans(), |
| 27 |
ask_to_apply=st.booleans(), |
| 28 |
write_to_stdout=st.booleans(), |
| 29 |
) |
| 30 |
def test_fuzz_sort_imports( |
| 31 |
file_name: str, config: Config, check: bool, ask_to_apply: bool, write_to_stdout: bool |
| 32 |
) -> None: |
| 33 |
main.sort_imports( |
| 34 |
file_name=file_name, |
| 35 |
config=config, |
| 36 |
check=check, |
| 37 |
ask_to_apply=ask_to_apply, |
| 38 |
write_to_stdout=write_to_stdout, |
| 39 |
) |
| 40 |
|
| 41 |
|
| 42 |
def test_sort_imports(tmp_path: Path) -> None: |
| 43 |
tmp_file = tmp_path / "file.py" |
| 44 |
tmp_file.write_text("import os, sys\n") |
| 45 |
assert main.sort_imports(str(tmp_file), DEFAULT_CONFIG, check=True).incorrectly_sorted # type: ignore # noqa |
| 46 |
main.sort_imports(str(tmp_file), DEFAULT_CONFIG) |
| 47 |
assert not main.sort_imports(str(tmp_file), DEFAULT_CONFIG, check=True).incorrectly_sorted # type: ignore # noqa |
| 48 |
|
| 49 |
skip_config = Config(skip=["file.py"]) |
| 50 |
assert main.sort_imports( # type: ignore |
| 51 |
str(tmp_file), config=skip_config, check=True, disregard_skip=False |
| 52 |
).skipped |
| 53 |
assert main.sort_imports(str(tmp_file), config=skip_config, disregard_skip=False).skipped # type: ignore # noqa |
| 54 |
|
| 55 |
|
| 56 |
@pytest.mark.skipif(reason="Can't use these mocks in mypyc-compiled code.", condition=_IS_COMPILED) |
| 57 |
def test_sort_imports_error_handling(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None: |
| 58 |
tmp_file = tmp_path / "file.py" |
| 59 |
tmp_file.write_text("import os, sys\n") |
| 60 |
with ( |
| 61 |
unittest.mock.patch( |
| 62 |
"isort.core.process", side_effect=IndexError("Example unhandled exception") |
| 63 |
), |
| 64 |
pytest.raises(IndexError), |
| 65 |
): |
| 66 |
main.sort_imports(str(tmp_file), DEFAULT_CONFIG, check=True).incorrectly_sorted # type: ignore # noqa |
| 67 |
|
| 68 |
_, error = capsys.readouterr() |
| 69 |
assert "Unrecoverable exception thrown when parsing" in error |
| 70 |
|
| 71 |
|
| 72 |
def test_parse_args() -> None: |
| 73 |
assert main.parse_args([]) == {} |
| 74 |
assert main.parse_args(["--multi-line", "1"]) == {"multi_line_output": WrapModes.VERTICAL} |
| 75 |
assert main.parse_args(["--multi-line", "GRID"]) == {"multi_line_output": WrapModes.GRID} |
| 76 |
assert main.parse_args(["--dont-order-by-type"]) == {"order_by_type": False} |
| 77 |
assert main.parse_args(["--dt"]) == {"order_by_type": False} |
| 78 |
assert main.parse_args(["--only-sections"]) == {"only_sections": True} |
| 79 |
assert main.parse_args(["--os"]) == {"only_sections": True} |
| 80 |
assert main.parse_args(["--om"]) == {"only_modified": True} |
| 81 |
assert main.parse_args(["--only-modified"]) == {"only_modified": True} |
| 82 |
assert main.parse_args(["--csi"]) == {"combine_straight_imports": True} |
| 83 |
assert main.parse_args(["--combine-straight-imports"]) == {"combine_straight_imports": True} |
| 84 |
assert main.parse_args(["--dont-follow-links"]) == {"follow_links": False} |
| 85 |
assert main.parse_args(["--overwrite-in-place"]) == {"overwrite_in_place": True} |
| 86 |
assert main.parse_args(["--from-first"]) == {"from_first": True} |
| 87 |
assert main.parse_args(["--resolve-all-configs"]) == {"resolve_all_configs": True} |
| 88 |
|
| 89 |
|
| 90 |
def test_ascii_art(capsys: pytest.CaptureFixture[str]) -> None: |
| 91 |
main.main(["--version"]) |
| 92 |
out, error = capsys.readouterr() |
| 93 |
assert ( |
| 94 |
out |
| 95 |
== f""" |
| 96 |
_ _ |
| 97 |
(_) ___ ___ _ __| |_ |
| 98 |
| |/ _/ / _ \\/ '__ _/ |
| 99 |
| |\\__ \\/\\_\\/| | | |_ |
| 100 |
|_|\\___/\\___/\\_/ \\_/ |
| 101 |
|
| 102 |
isort your imports, so you don't have to. |
| 103 |
|
| 104 |
VERSION {_VERSION_STRING} |
| 105 |
|
| 106 |
""" |
| 107 |
) |
| 108 |
assert error == "" |
| 109 |
|
| 110 |
|
| 111 |
def test_preconvert() -> None: |
| 112 |
assert main._preconvert(frozenset([1, 1, 2])) == [1, 2] |
| 113 |
assert main._preconvert(WrapModes.GRID) == "GRID" |
| 114 |
assert main._preconvert(main._preconvert) == "_preconvert" |
| 115 |
with pytest.raises(TypeError): |
| 116 |
main._preconvert(datetime.now()) |
| 117 |
|
| 118 |
|
| 119 |
def test_show_files(capsys: pytest.CaptureFixture[str], tmp_path: Path) -> None: |
| 120 |
(tmp_path / "a.py").write_text("import a") |
| 121 |
(tmp_path / "b.py").write_text("import b") |
| 122 |
|
| 123 |
# show files should list the files isort would sort |
| 124 |
main.main([str(tmp_path), "--show-files"]) |
| 125 |
out, error = capsys.readouterr() |
| 126 |
assert "a.py" in out |
| 127 |
assert "b.py" in out |
| 128 |
assert not error |
| 129 |
|
| 130 |
# can not be used for stream |
| 131 |
with pytest.raises(SystemExit): |
| 132 |
main.main(["-", "--show-files"]) |
| 133 |
|
| 134 |
# can not be used with show-config |
| 135 |
with pytest.raises(SystemExit): |
| 136 |
main.main([str(tmp_path), "--show-files", "--show-config"]) |
| 137 |
|
| 138 |
|
| 139 |
def test_missing_default_section(tmp_path: Path) -> None: |
| 140 |
config_file = tmp_path / ".isort.cfg" |
| 141 |
config_file.write_text( |
| 142 |
""" |
| 143 |
[settings] |
| 144 |
sections=MADEUP |
| 145 |
""" |
| 146 |
) |
| 147 |
|
| 148 |
python_file = tmp_path / "file.py" |
| 149 |
python_file.write_text("import os") |
| 150 |
|
| 151 |
with pytest.raises(SystemExit): |
| 152 |
main.main([str(python_file)]) |
| 153 |
|
| 154 |
|
| 155 |
def test_ran_against_root() -> None: |
| 156 |
with pytest.raises(SystemExit): |
| 157 |
main.main(["/"]) |
| 158 |
|
| 159 |
|
| 160 |
def test_config_root_interaction_with_resolve_all_configs(tmp_path: Path) -> None: |
| 161 |
python_file = tmp_path / "file.py" |
| 162 |
python_file.write_text("import os\n") |
| 163 |
|
| 164 |
with pytest.raises(SystemExit) as exc_info: |
| 165 |
main.main([str(python_file), "--config-root", str(tmp_path)]) |
| 166 |
assert "--resolve-all-configs" in str(exc_info.value) |
| 167 |
|
| 168 |
main.main([str(python_file), "--config-root", str(tmp_path), "--resolve-all-configs"]) |
| 169 |
|
| 170 |
|
| 171 |
def test_main(capsys: pytest.CaptureFixture[str], tmp_path: Path) -> None: |
| 172 |
base_args = [ |
| 173 |
"--sp", |
| 174 |
str(tmp_path), |
| 175 |
"--virtual-env", |
| 176 |
str(tmp_path), |
| 177 |
"--src-path", |
| 178 |
str(tmp_path), |
| 179 |
] |
| 180 |
(tmp_path / ".git").mkdir() |
| 181 |
|
| 182 |
# If nothing is passed in the quick guide is returned without erroring |
| 183 |
main.main([]) |
| 184 |
out, error = capsys.readouterr() |
| 185 |
assert main.QUICK_GUIDE in out |
| 186 |
assert not error |
| 187 |
|
| 188 |
# If no files are passed in but arguments are the quick guide is returned, alongside an error. |
| 189 |
with pytest.raises(SystemExit): |
| 190 |
main.main(base_args) |
| 191 |
out, error = capsys.readouterr() |
| 192 |
assert main.QUICK_GUIDE in out |
| 193 |
|
| 194 |
# Unless the config is requested, in which case it will be returned alone as JSON |
| 195 |
main.main([*base_args, "--show-config"]) |
| 196 |
out, error = capsys.readouterr() |
| 197 |
returned_config = json.loads(out) |
| 198 |
assert returned_config |
| 199 |
assert returned_config["virtual_env"] == str(tmp_path) |
| 200 |
|
| 201 |
# This should work even if settings path is not provided |
| 202 |
main.main([*base_args[2:], "--show-config"]) |
| 203 |
out, error = capsys.readouterr() |
| 204 |
assert json.loads(out)["virtual_env"] == str(tmp_path) |
| 205 |
|
| 206 |
# This should raise an error if an invalid settings path is provided |
| 207 |
with pytest.raises(InvalidSettingsPath): |
| 208 |
main.main( |
| 209 |
[ |
| 210 |
*base_args[2:], |
| 211 |
"--show-config", |
| 212 |
"--settings-path", |
| 213 |
"/random-root-folder-that-cant-exist-right?", |
| 214 |
] |
| 215 |
) |
| 216 |
|
| 217 |
# Should be able to set settings path to a file |
| 218 |
config_file = tmp_path / ".isort.cfg" |
| 219 |
config_file.write_text( |
| 220 |
""" |
| 221 |
[settings] |
| 222 |
profile=hug |
| 223 |
verbose=true |
| 224 |
""" |
| 225 |
) |
| 226 |
config_args = ["--settings-path", str(config_file)] |
| 227 |
main.main( |
| 228 |
[ |
| 229 |
*config_args, |
| 230 |
"--virtual-env", |
| 231 |
"/random-root-folder-that-cant-exist-right?", |
| 232 |
"--show-config", |
| 233 |
] |
| 234 |
) |
| 235 |
out, error = capsys.readouterr() |
| 236 |
assert json.loads(out)["profile"] == "hug" |
| 237 |
|
| 238 |
# Should be able to stream in content to sort |
| 239 |
input_content = TextIOWrapper( |
| 240 |
BytesIO( |
| 241 |
b""" |
| 242 |
import b |
| 243 |
import a |
| 244 |
""" |
| 245 |
) |
| 246 |
) |
| 247 |
main.main([*config_args, "-"], stdin=input_content) |
| 248 |
out, error = capsys.readouterr() |
| 249 |
assert ( |
| 250 |
out |
| 251 |
== f""" |
| 252 |
else-type place_module for b returned {DEFAULT_CONFIG.default_section} |
| 253 |
else-type place_module for a returned {DEFAULT_CONFIG.default_section} |
| 254 |
import a |
| 255 |
import b |
| 256 |
""" |
| 257 |
) |
| 258 |
|
| 259 |
# Should be able to stream diff |
| 260 |
input_content = TextIOWrapper( |
| 261 |
BytesIO( |
| 262 |
b""" |
| 263 |
import b |
| 264 |
import a |
| 265 |
""" |
| 266 |
) |
| 267 |
) |
| 268 |
main.main([*config_args, "-", "--diff"], stdin=input_content) |
| 269 |
out, error = capsys.readouterr() |
| 270 |
assert not error |
| 271 |
assert "+" in out |
| 272 |
assert "-" in out |
| 273 |
assert "import a" in out |
| 274 |
assert "import b" in out |
| 275 |
|
| 276 |
# check should work with stdin |
| 277 |
|
| 278 |
input_content_check = TextIOWrapper( |
| 279 |
BytesIO( |
| 280 |
b""" |
| 281 |
import b |
| 282 |
import a |
| 283 |
""" |
| 284 |
) |
| 285 |
) |
| 286 |
|
| 287 |
with pytest.raises(SystemExit): |
| 288 |
main.main([*config_args, "-", "--check-only"], stdin=input_content_check) |
| 289 |
out, error = capsys.readouterr() |
| 290 |
assert error == "ERROR: Imports are incorrectly sorted and/or formatted.\n" |
| 291 |
|
| 292 |
# Should be able to run with just a file |
| 293 |
python_file = tmp_path / "has_imports.py" |
| 294 |
python_file.write_text( |
| 295 |
""" |
| 296 |
import b |
| 297 |
import a |
| 298 |
""" |
| 299 |
) |
| 300 |
main.main([str(python_file), "--filter-files", "--verbose"]) |
| 301 |
assert python_file.read_text().lstrip() == "import a\nimport b\n" |
| 302 |
|
| 303 |
# Add a file to skip |
| 304 |
should_skip = tmp_path / "should_skip.py" |
| 305 |
should_skip.write_text("import nothing") |
| 306 |
main.main( |
| 307 |
[ |
| 308 |
str(python_file), |
| 309 |
str(should_skip), |
| 310 |
"--filter-files", |
| 311 |
"--verbose", |
| 312 |
"--skip", |
| 313 |
str(should_skip), |
| 314 |
] |
| 315 |
) |
| 316 |
|
| 317 |
# Should raise a system exit if check only, with broken file |
| 318 |
python_file.write_text( |
| 319 |
""" |
| 320 |
import b |
| 321 |
import a |
| 322 |
""" |
| 323 |
) |
| 324 |
with pytest.raises(SystemExit): |
| 325 |
main.main( |
| 326 |
[ |
| 327 |
str(python_file), |
| 328 |
str(should_skip), |
| 329 |
"--filter-files", |
| 330 |
"--verbose", |
| 331 |
"--check-only", |
| 332 |
"--skip", |
| 333 |
str(should_skip), |
| 334 |
] |
| 335 |
) |
| 336 |
|
| 337 |
# Should have same behavior if full directory is skipped |
| 338 |
with pytest.raises(SystemExit): |
| 339 |
main.main( |
| 340 |
[ |
| 341 |
str(tmp_path), |
| 342 |
"--filter-files", |
| 343 |
"--verbose", |
| 344 |
"--check-only", |
| 345 |
"--skip", |
| 346 |
str(should_skip), |
| 347 |
] |
| 348 |
) |
| 349 |
|
| 350 |
# Nested files should be skipped without needing --filter-files |
| 351 |
nested_dir = tmp_path / "nested_dir" |
| 352 |
nested_dir.mkdir() |
| 353 |
nested_file = nested_dir / "skip.py" |
| 354 |
nested_file.write_text("import b;import a") |
| 355 |
python_file.write_text( |
| 356 |
""" |
| 357 |
import a |
| 358 |
import b |
| 359 |
""" |
| 360 |
) |
| 361 |
main.main([str(tmp_path), "--extend-skip", "skip.py", "--check"]) |
| 362 |
|
| 363 |
# without filter options passed in should successfully sort files |
| 364 |
main.main([str(python_file), str(should_skip), "--verbose", "--atomic"]) |
| 365 |
|
| 366 |
# Should raise a system exit if all passed path is broken |
| 367 |
with pytest.raises(SystemExit): |
| 368 |
main.main(["not-exist", "--check-only"]) |
| 369 |
|
| 370 |
# Should not raise system exit if any of passed path is not broken |
| 371 |
main.main([str(python_file), "not-exist", "--verbose", "--check-only"]) |
| 372 |
out, error = capsys.readouterr() |
| 373 |
assert "Broken" in out |
| 374 |
|
| 375 |
|
| 376 |
def test_isort_filename_overrides(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None: |
| 377 |
"""Tests isorts available approaches for overriding filename and extension based behavior""" |
| 378 |
input_text = """ |
| 379 |
import b |
| 380 |
import a |
| 381 |
|
| 382 |
def function(): |
| 383 |
pass |
| 384 |
""" |
| 385 |
|
| 386 |
def build_input_content() -> TextIOWrapper: |
| 387 |
return as_stream(input_text) |
| 388 |
|
| 389 |
main.main(["-"], stdin=build_input_content()) |
| 390 |
out, error = capsys.readouterr() |
| 391 |
assert not error |
| 392 |
assert out == ( |
| 393 |
""" |
| 394 |
import a |
| 395 |
import b |
| 396 |
|
| 397 |
|
| 398 |
def function(): |
| 399 |
pass |
| 400 |
""" |
| 401 |
) |
| 402 |
|
| 403 |
# if file is skipped it should output unchanged. |
| 404 |
main.main( |
| 405 |
["-", "--filename", "x.py", "--skip", "x.py", "--filter-files"], |
| 406 |
stdin=build_input_content(), |
| 407 |
) |
| 408 |
out, error = capsys.readouterr() |
| 409 |
assert not error |
| 410 |
assert out == ( |
| 411 |
""" |
| 412 |
import b |
| 413 |
import a |
| 414 |
|
| 415 |
def function(): |
| 416 |
pass |
| 417 |
""" |
| 418 |
) |
| 419 |
|
| 420 |
main.main(["-", "--ext-format", "pyi"], stdin=build_input_content()) |
| 421 |
out, error = capsys.readouterr() |
| 422 |
assert not error |
| 423 |
assert out == ( |
| 424 |
""" |
| 425 |
import a |
| 426 |
import b |
| 427 |
|
| 428 |
def function(): |
| 429 |
pass |
| 430 |
""" |
| 431 |
) |
| 432 |
|
| 433 |
tmp_file = tmp_path / "tmp.pyi" |
| 434 |
tmp_file.write_text(input_text, encoding="utf8") |
| 435 |
main.main(["-", "--filename", str(tmp_file)], stdin=build_input_content()) |
| 436 |
out, error = capsys.readouterr() |
| 437 |
assert not error |
| 438 |
assert out == ( |
| 439 |
""" |
| 440 |
import a |
| 441 |
import b |
| 442 |
|
| 443 |
def function(): |
| 444 |
pass |
| 445 |
""" |
| 446 |
) |
| 447 |
|
| 448 |
# setting a filename override when file is passed in as non-stream is not supported. |
| 449 |
with pytest.raises(SystemExit): |
| 450 |
main.main([str(tmp_file), "--filename", str(tmp_file)], stdin=build_input_content()) |
| 451 |
|
| 452 |
|
| 453 |
def test_isort_float_to_top_overrides(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None: |
| 454 |
"""Tests isorts supports overriding float to top from CLI""" |
| 455 |
test_input = """ |
| 456 |
import b |
| 457 |
|
| 458 |
|
| 459 |
def function(): |
| 460 |
pass |
| 461 |
|
| 462 |
|
| 463 |
import a |
| 464 |
""" |
| 465 |
config_file = tmp_path / ".isort.cfg" |
| 466 |
config_file.write_text( |
| 467 |
""" |
| 468 |
[settings] |
| 469 |
float_to_top=True |
| 470 |
""" |
| 471 |
) |
| 472 |
python_file = tmp_path / "file.py" |
| 473 |
python_file.write_text(test_input) |
| 474 |
|
| 475 |
main.main([str(python_file)]) |
| 476 |
out, error = capsys.readouterr() |
| 477 |
assert not error |
| 478 |
assert "Fixing" in out |
| 479 |
assert python_file.read_text(encoding="utf8") == ( |
| 480 |
""" |
| 481 |
import a |
| 482 |
import b |
| 483 |
|
| 484 |
|
| 485 |
def function(): |
| 486 |
pass |
| 487 |
""" |
| 488 |
) |
| 489 |
|
| 490 |
python_file.write_text(test_input) |
| 491 |
main.main([str(python_file), "--dont-float-to-top"]) |
| 492 |
_, error = capsys.readouterr() |
| 493 |
assert not error |
| 494 |
assert python_file.read_text(encoding="utf8") == test_input |
| 495 |
|
| 496 |
with pytest.raises(SystemExit): |
| 497 |
main.main([str(python_file), "--float-to-top", "--dont-float-to-top"]) |
| 498 |
|
| 499 |
|
| 500 |
def test_isort_with_stdin(capsys: pytest.CaptureFixture[str]) -> None: |
| 501 |
# ensures that isort sorts stdin without any flags |
| 502 |
|
| 503 |
input_content = as_stream( |
| 504 |
""" |
| 505 |
import b |
| 506 |
import a |
| 507 |
""" |
| 508 |
) |
| 509 |
|
| 510 |
main.main(["-"], stdin=input_content) |
| 511 |
out, error = capsys.readouterr() |
| 512 |
|
| 513 |
assert out == ( |
| 514 |
""" |
| 515 |
import a |
| 516 |
import b |
| 517 |
""" |
| 518 |
) |
| 519 |
|
| 520 |
input_content_from = as_stream( |
| 521 |
""" |
| 522 |
import c |
| 523 |
import b |
| 524 |
from a import z, y, x |
| 525 |
""" |
| 526 |
) |
| 527 |
|
| 528 |
main.main(["-"], stdin=input_content_from) |
| 529 |
out, error = capsys.readouterr() |
| 530 |
|
| 531 |
assert out == ( |
| 532 |
""" |
| 533 |
import b |
| 534 |
import c |
| 535 |
from a import x, y, z |
| 536 |
""" |
| 537 |
) |
| 538 |
|
| 539 |
# ensures that isort correctly sorts stdin with --fas flag |
| 540 |
|
| 541 |
input_content = as_stream( |
| 542 |
""" |
| 543 |
import sys |
| 544 |
import pandas |
| 545 |
from z import abc |
| 546 |
from a import xyz |
| 547 |
""" |
| 548 |
) |
| 549 |
|
| 550 |
main.main(["-", "--fas"], stdin=input_content) |
| 551 |
out, error = capsys.readouterr() |
| 552 |
|
| 553 |
assert out == ( |
| 554 |
""" |
| 555 |
from a import xyz |
| 556 |
from z import abc |
| 557 |
|
| 558 |
import pandas |
| 559 |
import sys |
| 560 |
""" |
| 561 |
) |
| 562 |
|
| 563 |
# ensures that isort correctly sorts stdin with --fass flag |
| 564 |
|
| 565 |
input_content = as_stream( |
| 566 |
""" |
| 567 |
from a import Path, abc |
| 568 |
""" |
| 569 |
) |
| 570 |
|
| 571 |
main.main(["-", "--fass"], stdin=input_content) |
| 572 |
out, error = capsys.readouterr() |
| 573 |
|
| 574 |
assert out == ( |
| 575 |
""" |
| 576 |
from a import abc, Path |
| 577 |
""" |
| 578 |
) |
| 579 |
|
| 580 |
# ensures that isort correctly sorts stdin with --ff flag |
| 581 |
|
| 582 |
input_content = as_stream( |
| 583 |
""" |
| 584 |
import b |
| 585 |
from c import x |
| 586 |
from a import y |
| 587 |
""" |
| 588 |
) |
| 589 |
|
| 590 |
main.main(["-", "--ff"], stdin=input_content) |
| 591 |
out, error = capsys.readouterr() |
| 592 |
|
| 593 |
assert out == ( |
| 594 |
""" |
| 595 |
from a import y |
| 596 |
from c import x |
| 597 |
import b |
| 598 |
""" |
| 599 |
) |
| 600 |
|
| 601 |
# ensures that isort correctly sorts stdin with -fss flag |
| 602 |
|
| 603 |
input_content = as_stream( |
| 604 |
""" |
| 605 |
import b |
| 606 |
from a import a |
| 607 |
""" |
| 608 |
) |
| 609 |
|
| 610 |
main.main(["-", "--fss"], stdin=input_content) |
| 611 |
out, error = capsys.readouterr() |
| 612 |
|
| 613 |
assert out == ( |
| 614 |
""" |
| 615 |
from a import a |
| 616 |
import b |
| 617 |
""" |
| 618 |
) |
| 619 |
|
| 620 |
input_content = as_stream( |
| 621 |
""" |
| 622 |
import a |
| 623 |
from b import c |
| 624 |
""" |
| 625 |
) |
| 626 |
|
| 627 |
main.main(["-", "--fss"], stdin=input_content) |
| 628 |
out, error = capsys.readouterr() |
| 629 |
|
| 630 |
assert out == ( |
| 631 |
""" |
| 632 |
import a |
| 633 |
from b import c |
| 634 |
""" |
| 635 |
) |
| 636 |
|
| 637 |
# ensures that isort correctly sorts stdin with --ds flag |
| 638 |
|
| 639 |
input_content = as_stream( |
| 640 |
""" |
| 641 |
import sys |
| 642 |
import pandas |
| 643 |
import a |
| 644 |
""" |
| 645 |
) |
| 646 |
|
| 647 |
main.main(["-", "--ds"], stdin=input_content) |
| 648 |
out, error = capsys.readouterr() |
| 649 |
|
| 650 |
assert out == ( |
| 651 |
""" |
| 652 |
import a |
| 653 |
import pandas |
| 654 |
import sys |
| 655 |
""" |
| 656 |
) |
| 657 |
|
| 658 |
# ensures that isort correctly sorts stdin with --cs flag |
| 659 |
|
| 660 |
input_content = as_stream( |
| 661 |
""" |
| 662 |
from a import b |
| 663 |
from a import * |
| 664 |
""" |
| 665 |
) |
| 666 |
|
| 667 |
main.main(["-", "--cs"], stdin=input_content) |
| 668 |
out, error = capsys.readouterr() |
| 669 |
|
| 670 |
assert out == ( |
| 671 |
""" |
| 672 |
from a import * |
| 673 |
""" |
| 674 |
) |
| 675 |
|
| 676 |
# ensures that isort correctly sorts stdin with --ca flag |
| 677 |
|
| 678 |
input_content = as_stream( |
| 679 |
""" |
| 680 |
from a import x as X |
| 681 |
from a import y as Y |
| 682 |
""" |
| 683 |
) |
| 684 |
|
| 685 |
main.main(["-", "--ca"], stdin=input_content) |
| 686 |
out, error = capsys.readouterr() |
| 687 |
|
| 688 |
assert out == ( |
| 689 |
""" |
| 690 |
from a import x as X, y as Y |
| 691 |
""" |
| 692 |
) |
| 693 |
|
| 694 |
# ensures that isort works consistently with check and ws flags |
| 695 |
|
| 696 |
input_content = as_stream( |
| 697 |
""" |
| 698 |
import os |
| 699 |
import a |
| 700 |
import b |
| 701 |
""" |
| 702 |
) |
| 703 |
|
| 704 |
main.main(["-", "--check-only", "--ws"], stdin=input_content) |
| 705 |
out, error = capsys.readouterr() |
| 706 |
|
| 707 |
assert not error |
| 708 |
|
| 709 |
# ensures that isort works consistently with check and diff flags |
| 710 |
|
| 711 |
input_content = as_stream( |
| 712 |
""" |
| 713 |
import b |
| 714 |
import a |
| 715 |
""" |
| 716 |
) |
| 717 |
|
| 718 |
with pytest.raises(SystemExit): |
| 719 |
main.main(["-", "--check", "--diff"], stdin=input_content) |
| 720 |
out, error = capsys.readouterr() |
| 721 |
|
| 722 |
assert error |
| 723 |
assert "underlying stream is not seekable" not in error |
| 724 |
assert "underlying stream is not seekable" not in error |
| 725 |
|
| 726 |
# ensures that isort correctly sorts stdin with --ls flag |
| 727 |
|
| 728 |
input_content = as_stream( |
| 729 |
""" |
| 730 |
import abcdef |
| 731 |
import x |
| 732 |
""" |
| 733 |
) |
| 734 |
|
| 735 |
main.main(["-", "--ls"], stdin=input_content) |
| 736 |
out, error = capsys.readouterr() |
| 737 |
|
| 738 |
assert out == ( |
| 739 |
""" |
| 740 |
import x |
| 741 |
import abcdef |
| 742 |
""" |
| 743 |
) |
| 744 |
|
| 745 |
# ensures that isort correctly sorts stdin with --nis flag |
| 746 |
|
| 747 |
input_content = as_stream( |
| 748 |
""" |
| 749 |
from z import b, c, a |
| 750 |
""" |
| 751 |
) |
| 752 |
|
| 753 |
main.main(["-", "--nis"], stdin=input_content) |
| 754 |
out, error = capsys.readouterr() |
| 755 |
|
| 756 |
assert out == ( |
| 757 |
""" |
| 758 |
from z import b, c, a |
| 759 |
""" |
| 760 |
) |
| 761 |
|
| 762 |
# ensures that isort correctly sorts stdin with --sl flag |
| 763 |
|
| 764 |
input_content = as_stream( |
| 765 |
""" |
| 766 |
from z import b, c, a |
| 767 |
""" |
| 768 |
) |
| 769 |
|
| 770 |
main.main(["-", "--sl"], stdin=input_content) |
| 771 |
out, error = capsys.readouterr() |
| 772 |
|
| 773 |
assert out == ( |
| 774 |
""" |
| 775 |
from z import a |
| 776 |
from z import b |
| 777 |
from z import c |
| 778 |
""" |
| 779 |
) |
| 780 |
|
| 781 |
# ensures that isort correctly sorts stdin with --top flag |
| 782 |
|
| 783 |
input_content = as_stream( |
| 784 |
""" |
| 785 |
import os |
| 786 |
import sys |
| 787 |
""" |
| 788 |
) |
| 789 |
main.main(["-", "--top", "sys"], stdin=input_content) |
| 790 |
out, error = capsys.readouterr() |
| 791 |
|
| 792 |
assert out == ( |
| 793 |
""" |
| 794 |
import sys |
| 795 |
import os |
| 796 |
""" |
| 797 |
) |
| 798 |
|
| 799 |
# ensure that isort correctly sorts stdin with --os flag |
| 800 |
|
| 801 |
input_content = as_stream( |
| 802 |
""" |
| 803 |
import sys |
| 804 |
import os |
| 805 |
import z |
| 806 |
from a import b, e, c |
| 807 |
""" |
| 808 |
) |
| 809 |
main.main(["-", "--os"], stdin=input_content) |
| 810 |
out, error = capsys.readouterr() |
| 811 |
|
| 812 |
assert out == ( |
| 813 |
""" |
| 814 |
import sys |
| 815 |
import os |
| 816 |
|
| 817 |
import z |
| 818 |
from a import b, e, c |
| 819 |
""" |
| 820 |
) |
| 821 |
|
| 822 |
# ensures that only-modified flag works with stdin |
| 823 |
input_content = as_stream( |
| 824 |
""" |
| 825 |
import a |
| 826 |
import b |
| 827 |
""" |
| 828 |
) |
| 829 |
|
| 830 |
main.main(["-", "--verbose", "--only-modified"], stdin=input_content) |
| 831 |
out, error = capsys.readouterr() |
| 832 |
|
| 833 |
assert "else-type place_module for a returned THIRDPARTY" not in out |
| 834 |
assert "else-type place_module for b returned THIRDPARTY" not in out |
| 835 |
|
| 836 |
# ensures that combine-straight-imports flag works with stdin |
| 837 |
input_content = as_stream( |
| 838 |
""" |
| 839 |
import a |
| 840 |
import b |
| 841 |
""" |
| 842 |
) |
| 843 |
|
| 844 |
main.main(["-", "--combine-straight-imports"], stdin=input_content) |
| 845 |
out, error = capsys.readouterr() |
| 846 |
|
| 847 |
assert out == ( |
| 848 |
""" |
| 849 |
import a, b |
| 850 |
""" |
| 851 |
) |
| 852 |
|
| 853 |
|
| 854 |
def test_unsupported_encodings(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None: |
| 855 |
tmp_file = tmp_path / "file.py" |
| 856 |
# fmt: off |
| 857 |
tmp_file.write_text( |
| 858 |
''' |
| 859 |
# [syntax-error]\ |
| 860 |
# -*- coding: IBO-8859-1 -*- |
| 861 |
""" check correct unknown encoding declaration |
| 862 |
""" |
| 863 |
__revision__ = '×™×™×™×™' |
| 864 |
''', |
| 865 |
encoding="utf8" |
| 866 |
) |
| 867 |
# fmt: on |
| 868 |
|
| 869 |
# should throw an error if only unsupported encoding provided |
| 870 |
with pytest.raises(SystemExit): |
| 871 |
main.main([str(tmp_file)]) |
| 872 |
_, error = capsys.readouterr() |
| 873 |
|
| 874 |
assert "No valid encodings." in error |
| 875 |
|
| 876 |
# should not throw an error if at least one valid encoding found |
| 877 |
normal_file = tmp_path / "file1.py" |
| 878 |
normal_file.write_text("import os\nimport sys") |
| 879 |
|
| 880 |
main.main([str(tmp_file), str(normal_file), "--verbose"]) |
| 881 |
_, error = capsys.readouterr() |
| 882 |
|
| 883 |
|
| 884 |
def test_stream_skip_file(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None: |
| 885 |
input_with_skip = """ |
| 886 |
# isort: skip_file |
| 887 |
import b |
| 888 |
import a |
| 889 |
""" |
| 890 |
stream_with_skip = as_stream(input_with_skip) |
| 891 |
main.main(["-"], stdin=stream_with_skip) |
| 892 |
out, _ = capsys.readouterr() |
| 893 |
assert out == input_with_skip |
| 894 |
|
| 895 |
input_without_skip = input_with_skip.replace("isort: skip_file", "generic comment") |
| 896 |
stream_without_skip = as_stream(input_without_skip) |
| 897 |
main.main(["-"], stdin=stream_without_skip) |
| 898 |
out, _ = capsys.readouterr() |
| 899 |
assert ( |
| 900 |
out |
| 901 |
== """ |
| 902 |
# generic comment |
| 903 |
import a |
| 904 |
import b |
| 905 |
""" |
| 906 |
) |
| 907 |
|
| 908 |
atomic_input_without_skip = input_with_skip.replace("isort: skip_file", "generic comment") |
| 909 |
stream_without_skip = as_stream(atomic_input_without_skip) |
| 910 |
main.main(["-", "--atomic"], stdin=stream_without_skip) |
| 911 |
out, _ = capsys.readouterr() |
| 912 |
assert ( |
| 913 |
out |
| 914 |
== """ |
| 915 |
# generic comment |
| 916 |
import a |
| 917 |
import b |
| 918 |
""" |
| 919 |
) |
| 920 |
|
| 921 |
|
| 922 |
def test_only_modified_flag(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None: |
| 923 |
# ensures there is no verbose output for correct files with only-modified flag |
| 924 |
|
| 925 |
file1 = tmp_path / "file1.py" |
| 926 |
file1.write_text( |
| 927 |
""" |
| 928 |
import a |
| 929 |
import b |
| 930 |
""" |
| 931 |
) |
| 932 |
|
| 933 |
file2 = tmp_path / "file2.py" |
| 934 |
file2.write_text( |
| 935 |
""" |
| 936 |
import math |
| 937 |
|
| 938 |
import pandas as pd |
| 939 |
""" |
| 940 |
) |
| 941 |
|
| 942 |
main.main([str(file1), str(file2), "--verbose", "--only-modified"]) |
| 943 |
out, error = capsys.readouterr() |
| 944 |
|
| 945 |
assert ( |
| 946 |
out |
| 947 |
== f""" |
| 948 |
_ _ |
| 949 |
(_) ___ ___ _ __| |_ |
| 950 |
| |/ _/ / _ \\/ '__ _/ |
| 951 |
| |\\__ \\/\\_\\/| | | |_ |
| 952 |
|_|\\___/\\___/\\_/ \\_/ |
| 953 |
|
| 954 |
isort your imports, so you don't have to. |
| 955 |
|
| 956 |
VERSION {_VERSION_STRING} |
| 957 |
|
| 958 |
""" |
| 959 |
) |
| 960 |
|
| 961 |
assert not error |
| 962 |
|
| 963 |
# ensures that verbose output is only for modified file(s) with only-modified flag |
| 964 |
|
| 965 |
file3 = tmp_path / "file3.py" |
| 966 |
file3.write_text( |
| 967 |
""" |
| 968 |
import sys |
| 969 |
import os |
| 970 |
""" |
| 971 |
) |
| 972 |
|
| 973 |
main.main([str(file1), str(file2), str(file3), "--verbose", "--only-modified"]) |
| 974 |
out, error = capsys.readouterr() |
| 975 |
|
| 976 |
assert "else-type place_module for sys returned STDLIB" in out |
| 977 |
assert "else-type place_module for os returned STDLIB" in out |
| 978 |
assert "else-type place_module for math returned STDLIB" not in out |
| 979 |
assert "else-type place_module for pandas returned THIRDPARTY" not in out |
| 980 |
|
| 981 |
assert not error |
| 982 |
|
| 983 |
# ensures that the behaviour is consistent for check flag with only-modified flag |
| 984 |
|
| 985 |
main.main([str(file1), str(file2), "--check-only", "--verbose", "--only-modified"]) |
| 986 |
out, error = capsys.readouterr() |
| 987 |
|
| 988 |
assert ( |
| 989 |
out |
| 990 |
== f""" |
| 991 |
_ _ |
| 992 |
(_) ___ ___ _ __| |_ |
| 993 |
| |/ _/ / _ \\/ '__ _/ |
| 994 |
| |\\__ \\/\\_\\/| | | |_ |
| 995 |
|_|\\___/\\___/\\_/ \\_/ |
| 996 |
|
| 997 |
isort your imports, so you don't have to. |
| 998 |
|
| 999 |
VERSION {_VERSION_STRING} |
| 1000 |
|
| 1001 |
""" |
| 1002 |
) |
| 1003 |
|
| 1004 |
assert not error |
| 1005 |
|
| 1006 |
file4 = tmp_path / "file4.py" |
| 1007 |
file4.write_text( |
| 1008 |
""" |
| 1009 |
import sys |
| 1010 |
import os |
| 1011 |
""" |
| 1012 |
) |
| 1013 |
|
| 1014 |
with pytest.raises(SystemExit): |
| 1015 |
main.main([str(file2), str(file4), "--check-only", "--verbose", "--only-modified"]) |
| 1016 |
out, error = capsys.readouterr() |
| 1017 |
|
| 1018 |
assert "else-type place_module for sys returned STDLIB" in out |
| 1019 |
assert "else-type place_module for os returned STDLIB" in out |
| 1020 |
assert "else-type place_module for math returned STDLIB" not in out |
| 1021 |
assert "else-type place_module for pandas returned THIRDPARTY" not in out |
| 1022 |
|
| 1023 |
|
| 1024 |
def test_identify_imports_main(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None: |
| 1025 |
file_content = "import mod2\nimport mod2\na = 1\nimport mod1\n" |
| 1026 |
some_file = tmp_path / "some_file.py" |
| 1027 |
some_file.write_text(file_content) |
| 1028 |
file_imports = f"{some_file}:1 import mod2\n{some_file}:4 import mod1\n" |
| 1029 |
file_imports_with_dupes = ( |
| 1030 |
f"{some_file}:1 import mod2\n{some_file}:2 import mod2\n{some_file}:4 import mod1\n" |
| 1031 |
) |
| 1032 |
|
| 1033 |
main.identify_imports_main([str(some_file), "--unique"]) |
| 1034 |
out, error = capsys.readouterr() |
| 1035 |
assert out.replace("\r\n", "\n") == file_imports |
| 1036 |
assert not error |
| 1037 |
|
| 1038 |
main.identify_imports_main([str(some_file)]) |
| 1039 |
out, error = capsys.readouterr() |
| 1040 |
assert out.replace("\r\n", "\n") == file_imports_with_dupes |
| 1041 |
assert not error |
| 1042 |
|
| 1043 |
main.identify_imports_main(["-", "--unique"], stdin=as_stream(file_content)) |
| 1044 |
out, error = capsys.readouterr() |
| 1045 |
assert out.replace("\r\n", "\n") == file_imports.replace(str(some_file), "") |
| 1046 |
|
| 1047 |
main.identify_imports_main(["-"], stdin=as_stream(file_content)) |
| 1048 |
out, error = capsys.readouterr() |
| 1049 |
assert out.replace("\r\n", "\n") == file_imports_with_dupes.replace(str(some_file), "") |
| 1050 |
|
| 1051 |
main.identify_imports_main([str(tmp_path)]) |
| 1052 |
|
| 1053 |
main.identify_imports_main(["-", "--packages"], stdin=as_stream(file_content)) |
| 1054 |
out, error = capsys.readouterr() |
| 1055 |
assert len(out.split("\n")) == 6 |
| 1056 |
|
| 1057 |
main.identify_imports_main(["-", "--modules"], stdin=as_stream(file_content)) |
| 1058 |
out, error = capsys.readouterr() |
| 1059 |
assert len(out.split("\n")) == 3 |
| 1060 |
|
| 1061 |
main.identify_imports_main(["-", "--attributes"], stdin=as_stream(file_content)) |
| 1062 |
out, error = capsys.readouterr() |
| 1063 |
assert len(out.split("\n")) == 3 |
| 1064 |
|
| 1065 |
|
| 1066 |
def test_gitignore(capsys: pytest.CaptureFixture[str], tmp_path: pathlib.Path) -> None: |
| 1067 |
import_content = """ |
| 1068 |
import b |
| 1069 |
import a |
| 1070 |
""" |
| 1071 |
|
| 1072 |
def main_check(args: list[str]) -> tuple[str, str]: |
| 1073 |
try: |
| 1074 |
main.main(args) |
| 1075 |
except SystemExit: |
| 1076 |
pass |
| 1077 |
return capsys.readouterr() |
| 1078 |
|
| 1079 |
subprocess.run(["git", "init", str(tmp_path)]) |
| 1080 |
python_file = tmp_path / "has_imports.py" |
| 1081 |
python_file.write_text(import_content) |
| 1082 |
(tmp_path / "no_imports.py").write_text("...") |
| 1083 |
|
| 1084 |
out, error = main_check([str(python_file), "--skip-gitignore", "--filter-files", "--check"]) |
| 1085 |
assert "has_imports.py" in error |
| 1086 |
assert "no_imports.py" not in error |
| 1087 |
|
| 1088 |
(tmp_path / ".gitignore").write_text("has_imports.py") |
| 1089 |
|
| 1090 |
out, error = main_check([str(python_file), "--check"]) |
| 1091 |
assert "has_imports.py" in error |
| 1092 |
assert "no_imports.py" not in error |
| 1093 |
|
| 1094 |
out, error = main_check([str(python_file), "--skip-gitignore", "--filter-files", "--check"]) |
| 1095 |
assert "Skipped" in out |
| 1096 |
|
| 1097 |
# Should work with nested directories |
| 1098 |
(tmp_path / "nested_dir").mkdir() |
| 1099 |
(tmp_path / ".gitignore").write_text("nested_dir/has_imports.py") |
| 1100 |
subfolder_file = tmp_path / "nested_dir/has_imports.py" |
| 1101 |
subfolder_file.write_text(import_content) |
| 1102 |
|
| 1103 |
out, error = main_check([str(tmp_path), "--skip-gitignore", "--filter-files", "--check"]) |
| 1104 |
assert "has_imports.py" in error |
| 1105 |
assert "nested_dir/has_imports.py" not in error |
| 1106 |
|
| 1107 |
# Should work with relative path |
| 1108 |
currentdir = os.getcwd() |
| 1109 |
os.chdir(tmp_path) |
| 1110 |
|
| 1111 |
out, error = main_check([".", "--skip-gitignore", "--filter-files", "--check"]) |
| 1112 |
assert "has_imports.py" in error |
| 1113 |
assert "nested_dir/has_imports.py" not in error |
| 1114 |
|
| 1115 |
(tmp_path / ".gitignore").write_text( |
| 1116 |
""" |
| 1117 |
nested_dir/has_imports.py |
| 1118 |
has_imports.py |
| 1119 |
""" |
| 1120 |
) |
| 1121 |
out, error = main_check([".", "--skip-gitignore", "--filter-files", "--check"]) |
| 1122 |
assert "Skipped" in out |
| 1123 |
|
| 1124 |
os.chdir(currentdir) |
| 1125 |
|
| 1126 |
# Should work with multiple git projects |
| 1127 |
|
| 1128 |
shutil.rmtree(tmp_path / ".git") |
| 1129 |
(tmp_path / ".gitignore").unlink() |
| 1130 |
|
| 1131 |
# git_project0 |
| 1132 |
# | has_imports_ignored.py ignored |
| 1133 |
# | has_imports.py should check |
| 1134 |
git_project0 = tmp_path / "git_project0" |
| 1135 |
git_project0.mkdir() |
| 1136 |
subprocess.run(["git", "init", str(git_project0)]) |
| 1137 |
(git_project0 / ".gitignore").write_text("has_imports_ignored.py") |
| 1138 |
(git_project0 / "has_imports_ignored.py").write_text(import_content) |
| 1139 |
(git_project0 / "has_imports.py").write_text(import_content) |
| 1140 |
|
| 1141 |
# git_project1 |
| 1142 |
# | has_imports.py should check |
| 1143 |
# | nested_dir |
| 1144 |
# | has_imports_ignored.py ignored |
| 1145 |
# | has_imports.py should check |
| 1146 |
# | nested_dir_ignored ignored |
| 1147 |
# | has_imports.py ignored from folder |
| 1148 |
git_project1 = tmp_path / "git_project1" |
| 1149 |
git_project1.mkdir() |
| 1150 |
subprocess.run(["git", "init", str(git_project1)]) |
| 1151 |
(git_project1 / ".gitignore").write_text( |
| 1152 |
""" |
| 1153 |
nested_dir/has_imports_ignored.py |
| 1154 |
nested_dir_ignored |
| 1155 |
""" |
| 1156 |
) |
| 1157 |
(git_project1 / "has_imports.py").write_text(import_content) |
| 1158 |
nested_dir = git_project1 / "nested_dir" |
| 1159 |
nested_dir.mkdir() |
| 1160 |
(nested_dir / "has_imports.py").write_text(import_content) |
| 1161 |
(nested_dir / "has_imports_ignored.py").write_text(import_content) |
| 1162 |
nested_ignored_dir = git_project1 / "nested_dir_ignored" |
| 1163 |
nested_ignored_dir.mkdir() |
| 1164 |
(nested_ignored_dir / "has_imports.py").write_text(import_content) |
| 1165 |
|
| 1166 |
should_check = [ |
| 1167 |
"/has_imports.py", |
| 1168 |
"/nested_dir/has_imports.py", |
| 1169 |
"/git_project0/has_imports.py", |
| 1170 |
"/git_project1/has_imports.py", |
| 1171 |
"/git_project1/nested_dir/has_imports.py", |
| 1172 |
] |
| 1173 |
|
| 1174 |
out, error = main_check([str(tmp_path), "--skip-gitignore", "--filter-files", "--check"]) |
| 1175 |
|
| 1176 |
if os.name == "nt": |
| 1177 |
should_check = [sc.replace("/", "\\") for sc in should_check] |
| 1178 |
|
| 1179 |
assert all(f"{tmp_path}{file}" in error for file in should_check) |
| 1180 |
|
| 1181 |
out, error = main_check([str(tmp_path), "--skip-gitignore", "--filter-files"]) |
| 1182 |
|
| 1183 |
assert all(f"{tmp_path}{file}" in out for file in should_check) |
| 1184 |
|
| 1185 |
# Should work when git project contains symlinks |
| 1186 |
|
| 1187 |
if os.name != "nt": |
| 1188 |
(git_project0 / "has_imports_ignored.py").write_text(import_content) |
| 1189 |
(git_project0 / "has_imports.py").write_text(import_content) |
| 1190 |
(tmp_path / "has_imports.py").write_text(import_content) |
| 1191 |
(tmp_path / "nested_dir" / "has_imports.py").write_text(import_content) |
| 1192 |
(git_project0 / "ignore_link.py").symlink_to(tmp_path / "has_imports.py") |
| 1193 |
(git_project0 / "ignore_link").symlink_to(tmp_path / "nested_dir") |
| 1194 |
gitignore = git_project0 / ".gitignore" |
| 1195 |
gitignore.write_text(gitignore.read_text() + "ignore_link.py\nignore_link") |
| 1196 |
|
| 1197 |
out, error = main_check( |
| 1198 |
[str(git_project0), "--skip-gitignore", "--filter-files", "--check"] |
| 1199 |
) |
| 1200 |
|
| 1201 |
should_check = ["/git_project0/has_imports.py"] |
| 1202 |
|
| 1203 |
assert all(f"{tmp_path}{file}" in error for file in should_check) |
| 1204 |
|
| 1205 |
out, error = main_check([str(git_project0), "--skip-gitignore", "--filter-files"]) |
| 1206 |
|
| 1207 |
assert all(f"{tmp_path}{file}" in out for file in should_check) |
| 1208 |
|
| 1209 |
|
| 1210 |
def test_multiple_configs(capsys: pytest.CaptureFixture[str], tmp_path: Path) -> None: |
| 1211 |
# Ensure that --resolve-all-configs flag resolves multiple configs correctly |
| 1212 |
# and sorts files corresponding to their nearest config |
| 1213 |
|
| 1214 |
setup_cfg = """ |
| 1215 |
[isort] |
| 1216 |
from_first=True |
| 1217 |
""" |
| 1218 |
|
| 1219 |
pyproject_toml = """ |
| 1220 |
[tool.isort] |
| 1221 |
no_inline_sort = \"True\" |
| 1222 |
""" |
| 1223 |
|
| 1224 |
isort_cfg = """ |
| 1225 |
[settings] |
| 1226 |
force_single_line=True |
| 1227 |
""" |
| 1228 |
|
| 1229 |
broken_isort_cfg = """ |
| 1230 |
[iaort_confg] |
| 1231 |
force_single_line=True |
| 1232 |
""" |
| 1233 |
|
| 1234 |
dir1 = tmp_path / "subdir1" |
| 1235 |
dir2 = tmp_path / "subdir2" |
| 1236 |
dir3 = tmp_path / "subdir3" |
| 1237 |
dir4 = tmp_path / "subdir4" |
| 1238 |
|
| 1239 |
dir1.mkdir() |
| 1240 |
dir2.mkdir() |
| 1241 |
dir3.mkdir() |
| 1242 |
dir4.mkdir() |
| 1243 |
|
| 1244 |
setup_cfg_file = dir1 / "setup.cfg" |
| 1245 |
setup_cfg_file.write_text(setup_cfg, "utf-8") |
| 1246 |
|
| 1247 |
pyproject_toml_file = dir2 / "pyproject.toml" |
| 1248 |
pyproject_toml_file.write_text(pyproject_toml, "utf-8") |
| 1249 |
|
| 1250 |
isort_cfg_file = dir3 / ".isort.cfg" |
| 1251 |
isort_cfg_file.write_text(isort_cfg, "utf-8") |
| 1252 |
|
| 1253 |
broken_isort_cfg_file = dir4 / ".isort.cfg" |
| 1254 |
broken_isort_cfg_file.write_text(broken_isort_cfg, "utf-8") |
| 1255 |
|
| 1256 |
import_section = """ |
| 1257 |
from a import y, z, x |
| 1258 |
import b |
| 1259 |
""" |
| 1260 |
|
| 1261 |
file1 = dir1 / "file1.py" |
| 1262 |
file1.write_text(import_section, "utf-8") |
| 1263 |
|
| 1264 |
file2 = dir2 / "file2.py" |
| 1265 |
file2.write_text(import_section, "utf-8") |
| 1266 |
|
| 1267 |
file3 = dir3 / "file3.py" |
| 1268 |
file3.write_text(import_section, "utf-8") |
| 1269 |
|
| 1270 |
file4 = dir4 / "file4.py" |
| 1271 |
file4.write_text(import_section, "utf-8") |
| 1272 |
|
| 1273 |
file5 = tmp_path / "file5.py" |
| 1274 |
file5.write_text(import_section, "utf-8") |
| 1275 |
|
| 1276 |
main.main([str(tmp_path), "--resolve-all-configs", "--cr", str(tmp_path), "--verbose"]) |
| 1277 |
out, _ = capsys.readouterr() |
| 1278 |
|
| 1279 |
assert f"{setup_cfg_file} used for file {file1}" in out |
| 1280 |
assert f"{pyproject_toml_file} used for file {file2}" in out |
| 1281 |
assert f"{isort_cfg_file} used for file {file3}" in out |
| 1282 |
assert f"default used for file {file4}" in out |
| 1283 |
assert f"default used for file {file5}" in out |
| 1284 |
|
| 1285 |
assert ( |
| 1286 |
file1.read_text() |
| 1287 |
== """ |
| 1288 |
from a import x, y, z |
| 1289 |
import b |
| 1290 |
""" |
| 1291 |
) |
| 1292 |
|
| 1293 |
assert ( |
| 1294 |
file2.read_text() |
| 1295 |
== """ |
| 1296 |
import b |
| 1297 |
from a import y, z, x |
| 1298 |
""" |
| 1299 |
) |
| 1300 |
assert ( |
| 1301 |
file3.read_text() |
| 1302 |
== """ |
| 1303 |
import b |
| 1304 |
from a import x |
| 1305 |
from a import y |
| 1306 |
from a import z |
| 1307 |
""" |
| 1308 |
) |
| 1309 |
assert ( |
| 1310 |
file4.read_text() |
| 1311 |
== """ |
| 1312 |
import b |
| 1313 |
from a import x, y, z |
| 1314 |
""" |
| 1315 |
) |
| 1316 |
|
| 1317 |
assert ( |
| 1318 |
file5.read_text() |
| 1319 |
== """ |
| 1320 |
import b |
| 1321 |
from a import x, y, z |
| 1322 |
""" |
| 1323 |
) |
| 1324 |
|
| 1325 |
# Ensure that --resolve-all-config flags works with --check |
| 1326 |
|
| 1327 |
file6 = dir1 / "file6.py" |
| 1328 |
file6.write_text( |
| 1329 |
""" |
| 1330 |
import b |
| 1331 |
from a import x, y, z |
| 1332 |
""" |
| 1333 |
) |
| 1334 |
|
| 1335 |
with pytest.raises(SystemExit): |
| 1336 |
main.main([str(tmp_path), "--resolve-all-configs", "--cr", str(tmp_path), "--check"]) |
| 1337 |
|
| 1338 |
_, err = capsys.readouterr() |
| 1339 |
|
| 1340 |
assert f"{file6} Imports are incorrectly sorted and/or formatted" in err |
| 1341 |
|
| 1342 |
|
| 1343 |
def test_multiple_src_paths(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None: |
| 1344 |
""" |
| 1345 |
Ensure that isort has consistent behavior with multiple source paths |
| 1346 |
""" |
| 1347 |
|
| 1348 |
tests_module = tmp_path / "tests" |
| 1349 |
app_module = tmp_path / "app" |
| 1350 |
|
| 1351 |
tests_module.mkdir() |
| 1352 |
app_module.mkdir() |
| 1353 |
|
| 1354 |
pyproject_toml = tmp_path / "pyproject.toml" |
| 1355 |
pyproject_toml.write_text( |
| 1356 |
""" |
| 1357 |
[tool.isort] |
| 1358 |
profile = "black" |
| 1359 |
src_paths = ["app", "tests"] |
| 1360 |
auto_identify_namespace_packages = false |
| 1361 |
""", |
| 1362 |
"utf-8", |
| 1363 |
) |
| 1364 |
file = tmp_path / "file.py" |
| 1365 |
file.write_text( |
| 1366 |
""" |
| 1367 |
from app.something import something |
| 1368 |
from tests.something import something_else |
| 1369 |
""", |
| 1370 |
"utf-8", |
| 1371 |
) |
| 1372 |
|
| 1373 |
for _ in range(10): # To ensure isort has consistent results in multiple runs |
| 1374 |
main.main([str(tmp_path), "--verbose"]) |
| 1375 |
out, _ = capsys.readouterr() |
| 1376 |
|
| 1377 |
assert ( |
| 1378 |
file.read_text() |
| 1379 |
== """ |
| 1380 |
from app.something import something |
| 1381 |
from tests.something import something_else |
| 1382 |
""" |
| 1383 |
) |
| 1384 |
assert "from-type place_module for tests.something returned FIRSTPARTY" in out |
| 1385 |
|
| 1386 |
|
| 1387 |
def test_cli_src_path_glob_pattern( |
| 1388 |
tmp_path: Path, capsys: pytest.CaptureFixture[str], monkeypatch: pytest.MonkeyPatch |
| 1389 |
) -> None: |
| 1390 |
service_a_src = tmp_path / "service_a" / "src" |
| 1391 |
service_a_src.mkdir(parents=True) |
| 1392 |
service_b_src = tmp_path / "service_b" / "src" |
| 1393 |
service_b_src.mkdir(parents=True) |
| 1394 |
(tmp_path / "file.py").write_text("import os\n", "utf-8") |
| 1395 |
|
| 1396 |
monkeypatch.chdir(tmp_path) |
| 1397 |
|
| 1398 |
main.main([".", "--src", "*/src/", "--show-config"]) |
| 1399 |
out, _ = capsys.readouterr() |
| 1400 |
config = json.loads(out) |
| 1401 |
src_paths = set(config["src_paths"]) |
| 1402 |
|
| 1403 |
assert str(service_a_src) in src_paths |
| 1404 |
assert str(service_b_src) in src_paths |