Skip to content

Commit

Permalink
Fix issue #1239
Browse files Browse the repository at this point in the history
  • Loading branch information
timothycrosley committed Jul 4, 2020
1 parent 6622018 commit 64ab238
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 28 deletions.
8 changes: 4 additions & 4 deletions isort/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,10 +643,10 @@ def main(argv: Optional[Sequence[str]] = None, stdin: Optional[TextIOWrapper] =
show_diff = config_dict.pop("show_diff", False)
write_to_stdout = config_dict.pop("write_to_stdout", False)

src_paths = config_dict["src_paths"] = set(config_dict.get("src_paths", ()))
for file_name in file_names:
if os.path.isdir(file_name):
src_paths.add(Path(file_name).resolve())
if "src_paths" in config_dict:
config_dict["src_paths"] = {
Path(src_path).resolve() for src_path in config_dict.get("src_paths", ())
}

config = Config(**config_dict)
if show_config:
Expand Down
38 changes: 18 additions & 20 deletions isort/settings.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
"""isort/settings.py.
Defines how the default settings for isort should be loaded
(First from the default setting dictionary at the top of the file, then overridden by any settings
in ~/.isort.cfg or $XDG_CONFIG_HOME/.isort.cfg if there are any)
"""
import configparser
import fnmatch
Expand Down Expand Up @@ -217,15 +214,18 @@ def __init__(
sources: List[Dict[str, Any]] = [_DEFAULT_SETTINGS]

config_settings: Dict[str, Any]
project_root: str
if settings_file:
config_settings = _get_config_data(
settings_file,
CONFIG_SECTIONS.get(os.path.basename(settings_file), FALLBACK_CONFIG_SECTIONS),
)
project_root = os.path.dirname(settings_file)
elif settings_path:
config_settings = _find_config(settings_path)
project_root, config_settings = _find_config(settings_path)
else:
config_settings = {}
project_root = os.getcwd()

profile_name = config_overrides.get("profile", config_settings.get("profile", ""))
profile: Dict[str, Any] = {}
Expand All @@ -241,7 +241,6 @@ def __init__(
sources.append(config_settings)
if config_overrides:
config_overrides["source"] = RUNTIME_SOURCE
config_overrides["runtime_src_paths"] = config_overrides.pop("src_paths", ())
sources.append(config_overrides)

combined_config = {**profile, **config_settings, **config_overrides}
Expand Down Expand Up @@ -277,20 +276,19 @@ def __init__(
combined_config[key] = type(default_value)(value)

if "directory" not in combined_config:
combined_config["directory"] = os.path.basename(
config_settings.get("source", None) or os.getcwd()
combined_config["directory"] = (
os.path.dirname(config_settings["source"])
if config_settings.get("source", None)
else os.getcwd()
)

if "src_paths" not in combined_config and not combined_config.get("runtime_src_paths"):
combined_config["src_paths"] = frozenset((Path.cwd().resolve(),))
path_root = Path(combined_config.get("directory", project_root)).resolve()
path_root = path_root if path_root.is_dir() else path_root.parent
if "src_paths" not in combined_config:
combined_config["src_paths"] = frozenset((path_root, path_root / "src"))
else:
path_root = Path(combined_config.get("directory", Path.cwd())).resolve()
path_root = path_root if path_root.is_dir() else path_root.parent
combined_config["src_paths"] = frozenset(
{path_root / path for path in combined_config.get("src_paths", ())}.union(
Path.cwd().resolve() / path
for path in combined_config.get("runtime_src_paths", ())
)
path_root / path for path in combined_config.get("src_paths", ())
)

# Remove any config values that are used for creating config object but
Expand Down Expand Up @@ -407,7 +405,7 @@ def _abspaths(cwd: str, values: Iterable[str]) -> Set[str]:


@lru_cache()
def _find_config(path: str) -> Dict[str, Any]:
def _find_config(path: str) -> Tuple[str, Dict[str, Any]]:
current_directory = path
tries = 0
while current_directory and tries < MAX_CONFIG_SEARCH_DEPTH:
Expand All @@ -423,11 +421,11 @@ def _find_config(path: str) -> Dict[str, Any]:
warn(f"Failed to pull configuration information from {potential_config_file}")
config_data = {}
if config_data:
return config_data
return (current_directory, config_data)

for stop_dir in STOP_CONFIG_SEARCH_ON_DIRS:
if os.path.isdir(stop_dir):
break
if os.path.isdir(os.path.join(current_directory, stop_dir)):
return (current_directory, {})

new_directory = os.path.split(current_directory)[0]
if new_directory == current_directory:
Expand All @@ -436,7 +434,7 @@ def _find_config(path: str) -> Dict[str, Any]:
current_directory = new_directory
tries += 1

return {}
return (path, {})


@lru_cache()
Expand Down
10 changes: 9 additions & 1 deletion tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,15 @@ def test_is_python_file_fifo(tmpdir):


def test_main(capsys, tmpdir):
base_args = ["--settings-path", str(tmpdir), "--virtual-env", str(tmpdir)]
base_args = [
"--settings-path",
str(tmpdir),
"--virtual-env",
str(tmpdir),
"--src-path",
str(tmpdir),
]
tmpdir.mkdir(".git")

# If no files are passed in the quick guide is returned
main.main(base_args)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ def test_find_config(tmpdir):
""",
"utf8",
)
assert not settings._find_config(str(tmpdir))
assert not settings._find_config(str(tmpdir))[1]

# or if it is malformed
settings._find_config.cache_clear()
settings._get_config_data.cache_clear()
tmp_config.write_text("""arstoyrsyan arienrsaeinrastyngpuywnlguyn354q^%$)(%_)@$""", "utf8")
assert not settings._find_config(str(tmpdir))
assert not settings._find_config(str(tmpdir))[1]

# can when it has either a file format, or generic relevant section
settings._find_config.cache_clear()
Expand All @@ -60,7 +60,7 @@ def test_find_config(tmpdir):
""",
"utf8",
)
assert settings._find_config(str(tmpdir))
assert settings._find_config(str(tmpdir))[1]


def test_get_config_data(tmpdir):
Expand Down

0 comments on commit 64ab238

Please sign in to comment.