diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ddcead0f..27cb7533a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,9 @@ Changelog NOTE: isort follows the [semver](https://semver.org/) versioning standard. +### 5.0.8 July 11, 2020 + - Fixed #1277 & #1278: New line detection issues on Windows. + ### 5.0.7 July 9, 2020 - Fixed #1306: unexpected --diff behavior. - Fixed #1279: Fixed NOQA comment regression. diff --git a/isort/api.py b/isort/api.py index 572bc5167..58367f3e0 100644 --- a/isort/api.py +++ b/isort/api.py @@ -396,14 +396,14 @@ def _sort_imports( if not line_separator: line_separator = "\n" else: - if not line_separator: - line_separator = line[-1] + stripped_line = line.strip() + if stripped_line and not line_separator: + line_separator = line[len(line.rstrip()):] for file_skip_comment in FILE_SKIP_COMMENTS: if file_skip_comment in line: raise FileSkipComment("Passed in content") - stripped_line = line.strip() if ( (index == 0 or (index in (1, 2) and not contains_imports)) and stripped_line.startswith("#") diff --git a/tests/test_regressions.py b/tests/test_regressions.py index 3e288568e..8b751e318 100644 --- a/tests/test_regressions.py +++ b/tests/test_regressions.py @@ -224,3 +224,24 @@ def test_add_imports_shouldnt_move_lower_comments_issue_1300(): ANSWER = 42 """ assert isort.code(test_input, add_imports=["from os import path"]) == test_input + + +def test_windows_newline_issue_1277(): + """Test to ensure windows new lines are correctly handled within indented scopes. + See: https://github.com/timothycrosley/isort/issues/1277 + """ + assert ( + isort.code("\ndef main():\r\n import time\r\n\n import sys\r\n") == + "\ndef main():\r\n import sys\r\n import time\r\n" + ) + + +def test_windows_newline_issue_1278(): + """Test to ensure windows new lines are correctly handled within indented scopes. + See: https://github.com/timothycrosley/isort/issues/1278 + """ + assert isort.check_code( + "\ntry:\r\n import datadog_agent\r\n\r\n " + "from ..log import CheckLoggingAdapter, init_logging\r\n\r\n init_logging()\r\n" + "except ImportError:\r\n pass\r\n" + )