From ca084113ad381db3a9497bdabdf5c1de39706cb1 Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Mon, 8 Nov 2021 21:21:51 -0800 Subject: [PATCH] Fixed #1819: Occasional inconsistency with multiple src paths. --- isort/settings.py | 5 +++-- tests/unit/test_main.py | 44 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/isort/settings.py b/isort/settings.py index d0060d056..45e0ecb94 100644 --- a/isort/settings.py +++ b/isort/settings.py @@ -458,13 +458,14 @@ def __init__( if "src_paths" not in combined_config: combined_config["src_paths"] = (path_root / "src", path_root) else: - src_paths: Set[Path] = set() + src_paths: List[Path] = [] for src_path in combined_config.get("src_paths", ()): full_paths = ( path_root.glob(src_path) if "*" in str(src_path) else [path_root / src_path] ) for path in full_paths: - src_paths.add(path) + if not path in src_paths: + src_paths.append(path) combined_config["src_paths"] = tuple(src_paths) diff --git a/tests/unit/test_main.py b/tests/unit/test_main.py index 66ac0b02c..4c16cf518 100644 --- a/tests/unit/test_main.py +++ b/tests/unit/test_main.py @@ -1345,3 +1345,47 @@ def test_multiple_configs(capsys, tmpdir): _, err = capsys.readouterr() assert f"{str(file6)} Imports are incorrectly sorted and/or formatted" in err + + +def test_multiple_src_paths(tmpdir, capsys): + """ + Ensure that isort has consistent behavior with multiple source paths + """ + + tests_module = tmpdir / "tests" + app_module = tmpdir / "app" + + tests_module.mkdir() + app_module.mkdir() + + pyproject_toml = tmpdir / "pyproject.toml" + pyproject_toml.write_text( + """ +[tool.isort] +profile = "black" +src_paths = ["app", "tests"] +auto_identify_namespace_packages = false +""", + "utf-8", + ) + file = tmpdir / "file.py" + file.write_text( + """ +from app.something import something +from tests.something import something_else +""", + "utf-8", + ) + + for _ in range(10): # To ensure isort has consistent results in multiple runs + main.main([str(tmpdir), "--verbose"]) + out, _ = capsys.readouterr() + + assert ( + file.read() + == """ +from app.something import something +from tests.something import something_else +""" + ) + assert "from-type place_module for tests.something returned FIRSTPARTY" in out