diff options
author | Timothy Crosley <timothy.crosley@gmail.com> | 2020-09-03 17:18:32 -0700 |
---|---|---|
committer | Timothy Crosley <timothy.crosley@gmail.com> | 2020-09-03 17:18:32 -0700 |
commit | 0c34b9845434bd18b3abc31364242405de74da74 (patch) | |
tree | 0d761ad5de6b0cea7d155587a6b5e32239ba3fc7 | |
parent | ff0d4365d8f66e7c0f4b2c4994cd05645b41934f (diff) | |
download | isort-issue/1453/float-to-top-errors-with-empty-file.tar.gz |
Resolve #1453: isort error when float to top on almost empty file. Ensure errors return back file name when they occur.issue/1453/float-to-top-errors-with-empty-file
-rw-r--r-- | isort/core.py | 2 | ||||
-rw-r--r-- | isort/main.py | 9 | ||||
-rw-r--r-- | tests/unit/test_main.py | 11 | ||||
-rw-r--r-- | tests/unit/test_regressions.py | 15 |
4 files changed, 36 insertions, 1 deletions
diff --git a/isort/core.py b/isort/core.py index 46900e02..08adc239 100644 --- a/isort/core.py +++ b/isort/core.py @@ -86,7 +86,7 @@ def process( if current: parsed = parse.file_contents(current, config=config) extra_space = "" - while current[-1] == "\n": + while current and current[-1] == "\n": extra_space += "\n" current = current[:-1] extra_space = extra_space.replace("\n", "", 1) diff --git a/isort/main.py b/isort/main.py index 990e1218..e7dc61ad 100644 --- a/isort/main.py +++ b/isort/main.py @@ -11,6 +11,7 @@ from warnings import warn from . import __version__, api, sections from .exceptions import FileSkipped +from .format import create_terminal_printer from .logo import ASCII_ART from .profiles import profiles from .settings import VALID_PY_TARGETS, Config, WrapModes @@ -103,6 +104,14 @@ def sort_imports( except (OSError, ValueError) as error: warn(f"Unable to parse file {file_name} due to {error}") return None + except Exception: + printer = create_terminal_printer(color=config.color_output) + printer.error( + f"Unrecoverable exception thrown when parsing {file_name}! " + "This should NEVER happen.\n" + "If encountered, please open an issue: https://github.com/PyCQA/isort/issues/new" + ) + raise def iter_source_code(paths: Iterable[str], config: Config, skipped: List[str]) -> Iterator[str]: diff --git a/tests/unit/test_main.py b/tests/unit/test_main.py index 3b7dd768..ae63fd60 100644 --- a/tests/unit/test_main.py +++ b/tests/unit/test_main.py @@ -35,6 +35,17 @@ def test_sort_imports(tmpdir): assert main.sort_imports(str(tmp_file), config=skip_config, disregard_skip=False).skipped +def test_sort_imports_error_handling(tmpdir, mocker, capsys): + tmp_file = tmpdir.join("file.py") + tmp_file.write("import os, sys\n") + mocker.patch("isort.core.process").side_effect = IndexError("Example unhandled exception") + with pytest.raises(IndexError): + main.sort_imports(str(tmp_file), DEFAULT_CONFIG, check=True).incorrectly_sorted + + out, error = capsys.readouterr() + assert "Unrecoverable exception thrown when parsing" in error + + def test_parse_args(): assert main.parse_args([]) == {} assert main.parse_args(["--multi-line", "1"]) == {"multi_line_output": WrapModes.VERTICAL} diff --git a/tests/unit/test_regressions.py b/tests/unit/test_regressions.py index a6e377ea..5feaef6f 100644 --- a/tests/unit/test_regressions.py +++ b/tests/unit/test_regressions.py @@ -911,3 +911,18 @@ def bar(): pass ''' ) + + +def test_empty_float_to_top_shouldnt_error_issue_1453(): + """isort shouldn't error when float to top is set with a mostly empty file""" + assert isort.check_code( + """ +""", + show_diff=True, + float_to_top=True, + ) + assert isort.check_code( + """ +""", + show_diff=True, + ) |