diff --git a/CHANGELOG.md b/CHANGELOG.md index 87baa08a3..712e554d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/isort/main.py b/isort/main.py index fb5f3200e..c518a92dc 100644 --- a/isort/main.py +++ b/isort/main.py @@ -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", @@ -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"]): @@ -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, @@ -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: diff --git a/tests/unit/test_main.py b/tests/unit/test_main.py index ddca98dd4..ef837b614 100644 --- a/tests/unit/test_main.py +++ b/tests/unit/test_main.py @@ -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(), @@ -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", @@ -661,7 +678,6 @@ def test_isort_with_stdin(capsys): ) # ensures that isort warns with deprecated flags with stdin - input_content = TextIOWrapper( BytesIO( b"""