Skip to content

Commit

Permalink
Merge pull request #1512 from PyCQA/issue/1511/show-files-via-cli-sup…
Browse files Browse the repository at this point in the history
…port

Implemented #1511: Support for easily seeing all files isort will be …
  • Loading branch information
timothycrosley committed Oct 1, 2020
2 parents c2473cf + a5df07f commit 44db9ff
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,7 @@ Find out more about isort's release policy [here](https://pycqa.github.io/isort/
- Implemented #1433: Provide helpful feedback in case a custom config file is specified without a configuration.
- Implemented #1494: Default to sorting imports within `.pxd` files.
- Implemented #1502: Improved float-to-top behavior when there is an existing import section present at top-of-file.
- Implemented #1511: Support for easily seeing all files isort will be ran against using `isort . --show-files`.
- Improved handling of unsupported configuration option errors (see #1475).
- Fixed #1463: Better interactive documentation for future option.
- Fixed #1461: Quiet config option not respected by file API in some circumstances.
Expand Down
16 changes: 16 additions & 0 deletions isort/main.py
Expand Up @@ -638,6 +638,12 @@ def _build_arg_parser() -> argparse.ArgumentParser:
action="store_true",
help="See isort's determined config, as well as sources of config options.",
)
parser.add_argument(
"--show-files",
dest="show_files",
action="store_true",
help="See the files isort will be ran against with the current config options.",
)
parser.add_argument(
"--honor-noqa",
dest="honor_noqa",
Expand Down Expand Up @@ -805,6 +811,9 @@ def main(argv: Optional[Sequence[str]] = None, stdin: Optional[TextIOWrapper] =
return

show_config: bool = arguments.pop("show_config", False)
show_files: bool = arguments.pop("show_files", False)
if show_config and show_files:
sys.exit("Error: either specify show-config or show-files not both.")

if "settings_path" in arguments:
if os.path.isfile(arguments["settings_path"]):
Expand Down Expand Up @@ -854,6 +863,9 @@ def main(argv: Optional[Sequence[str]] = None, stdin: Optional[TextIOWrapper] =
print(json.dumps(config.__dict__, indent=4, separators=(",", ": "), default=_preconvert))
return
elif file_names == ["-"]:
if show_files:
sys.exit("Error: can't show files for streaming input.")

if check:
incorrectly_sorted = not api.check_stream(
input_stream=sys.stdin if stdin is None else stdin,
Expand Down Expand Up @@ -883,6 +895,10 @@ def main(argv: Optional[Sequence[str]] = None, stdin: Optional[TextIOWrapper] =
file_names = filtered_files

file_names = iter_source_code(file_names, config, skipped, broken)
if show_files:
for file_name in file_names:
print(file_name)
return
num_skipped = 0
num_broken = 0
if config.verbose:
Expand Down
24 changes: 20 additions & 4 deletions tests/unit/test_main.py
Expand Up @@ -13,9 +13,6 @@
from isort.settings import DEFAULT_CONFIG, Config
from isort.wrap_modes import WrapModes

# This test code was written by the `hypothesis.extra.ghostwriter` module
# and is provided under the Creative Commons Zero public domain dedication.


@given(
file_name=st.text(),
Expand Down Expand Up @@ -104,6 +101,26 @@ def test_preconvert():
main._preconvert(datetime.now())


def test_show_files(capsys, tmpdir):
tmpdir.join("a.py").write("import a")
tmpdir.join("b.py").write("import b")

# show files should list the files isort would sort
main.main([str(tmpdir), "--show-files"])
out, error = capsys.readouterr()
assert "a.py" in out
assert "b.py" in out
assert not error

# can not be used for stream
with pytest.raises(SystemExit):
main.main(["-", "--show-files"])

# can not be used with show-config
with pytest.raises(SystemExit):
main.main([str(tmpdir), "--show-files", "--show-config"])


def test_main(capsys, tmpdir):
base_args = [
"-sp",
Expand Down Expand Up @@ -661,7 +678,6 @@ def test_isort_with_stdin(capsys):
)

# ensures that isort warns with deprecated flags with stdin

input_content = TextIOWrapper(
BytesIO(
b"""
Expand Down

0 comments on commit 44db9ff

Please sign in to comment.