diff --git a/CHANGELOG.md b/CHANGELOG.md index 2bb54d3cb..53b30a0e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Find out more about isort's release policy [here](https://pycqa.github.io/isort/ ### 5.6.0 TBD - Fixed #1463: Better interactive documentation for future option. + - Fixed #1461: Quiet config option not respected by file API in some circumstances. ### 5.5.1 September 4, 2020 - Fixed #1454: Ensure indented import sections with import heading and a preceding comment don't cause import sorting loops. diff --git a/isort/api.py b/isort/api.py index cbcc3e6ea..f59bc6d91 100644 --- a/isort/api.py +++ b/isort/api.py @@ -299,6 +299,7 @@ def sort_file( """ with io.File.read(filename) as source_file: actual_file_path = file_path or source_file.path + config = _config(path=actual_file_path, config=config, **config_kwargs) changed: bool = False try: if write_to_stdout: @@ -309,7 +310,6 @@ def sort_file( file_path=actual_file_path, disregard_skip=disregard_skip, extension=extension, - **config_kwargs, ) else: tmp_file = source_file.path.with_suffix(source_file.path.suffix + ".isorted") @@ -325,7 +325,6 @@ def sort_file( file_path=actual_file_path, disregard_skip=disregard_skip, extension=extension, - **config_kwargs, ) if changed: if show_diff or ask_to_apply: diff --git a/tests/unit/test_ticketed_features.py b/tests/unit/test_ticketed_features.py index dc5c77814..2fd9073eb 100644 --- a/tests/unit/test_ticketed_features.py +++ b/tests/unit/test_ticketed_features.py @@ -657,3 +657,65 @@ def test_isort_intelligently_places_noqa_comments_issue_1456(): profile="black", show_diff=True, ) + + +def test_isort_respects_quiet_from_sort_file_api_see_1461(capsys, tmpdir): + """Test to ensure isort respects the quiet API parameter when passed in via the API. + See: https://github.com/PyCQA/isort/issues/1461. + """ + settings_file = tmpdir.join(".isort.cfg") + custom_settings_file = tmpdir.join(".custom.isort.cfg") + tmp_file = tmpdir.join("file.py") + tmp_file.write("import b\nimport a\n") + isort.file(tmp_file) + + out, error = capsys.readouterr() + assert not error + assert "Fixing" in out + + # When passed in directly as a setting override + tmp_file.write("import b\nimport a\n") + isort.file(tmp_file, quiet=True) + out, error = capsys.readouterr() + assert not error + assert not out + + # Present in an automatically loaded configuration file + isort.settings._find_config.cache_clear() + settings_file.write( + """ +[isort] +quiet = true +""" + ) + tmp_file.write("import b\nimport a\n") + isort.file(tmp_file) + out, error = capsys.readouterr() + assert not error + assert not out + + # In a custom configuration file + settings_file.write( + """ +[isort] +quiet = false +""" + ) + custom_settings_file.write( + """ +[isort] +quiet = true +""" + ) + tmp_file.write("import b\nimport a\n") + isort.file(tmp_file, settings_file=str(custom_settings_file)) + out, error = capsys.readouterr() + assert not error + assert not out + + # Reused configuration object + custom_config = Config(settings_file=str(custom_settings_file)) + isort.file(tmp_file, config=custom_config) + out, error = capsys.readouterr() + assert not error + assert not out