Skip to content

Commit

Permalink
Add test for allowing root, switch to allow root command
Browse files Browse the repository at this point in the history
  • Loading branch information
timothycrosley committed Mar 1, 2021
1 parent 747a08c commit 3de261f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
12 changes: 5 additions & 7 deletions isort/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,10 @@ def _build_arg_parser() -> argparse.ArgumentParser:
help="Provide the filename associated with a stream.",
)
target_group.add_argument(
"--no-preserve-root",
"--allow-root",
action="store_true",
default=False,
help="Tells isort not to treat / specially, allowing it to be ran on the root dir.",
help="Tells isort not to treat / specially, allowing it to be ran against the root dir.",
)

output_group.add_argument(
Expand Down Expand Up @@ -1006,7 +1006,7 @@ def main(argv: Optional[Sequence[str]] = None, stdin: Optional[TextIOWrapper] =
remapped_deprecated_args = config_dict.pop("remapped_deprecated_args", False)
stream_filename = config_dict.pop("filename", None)
ext_format = config_dict.pop("ext_format", None)
no_preserve_root = config_dict.pop("no_preserve_root", None)
allow_root = config_dict.pop("allow_root", None)
wrong_sorted_files = False
all_attempt_broken = False
no_valid_encodings = False
Expand Down Expand Up @@ -1044,18 +1044,16 @@ def main(argv: Optional[Sequence[str]] = None, stdin: Optional[TextIOWrapper] =
file_path=file_path,
extension=ext_format,
)
elif "/" in file_names and not no_preserve_root:
elif "/" in file_names and not allow_root:
printer = create_terminal_printer(color=config.color_output)
printer.error("it is dangerous to operate recursively on '/'")
printer.error("use --no-preserve-root to override this failsafe")
printer.error("use --allow-root to override this failsafe")
sys.exit(1)
else:
if stream_filename:
printer = create_terminal_printer(color=config.color_output)
printer.error("Filename override is intended only for stream (-) sorting.")
sys.exit(1)
if file_names == ["/"]:
input("You've requested to run isort against your entire computer (/) are you sure? ")
skipped: List[str] = []
broken: List[str] = []

Expand Down
5 changes: 5 additions & 0 deletions tests/unit/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ def test_missing_default_section(tmpdir):
main.main([str(python_file)])


def test_ran_against_root():
with pytest.raises(SystemExit):
main.main(["/"])


def test_main(capsys, tmpdir):
base_args = [
"-sp",
Expand Down

0 comments on commit 3de261f

Please sign in to comment.