diff --git a/.isort.cfg b/.isort.cfg index 428419bc1..3f3b28ad5 100644 --- a/.isort.cfg +++ b/.isort.cfg @@ -1,3 +1,4 @@ [settings] profile=hug src_paths=isort,test +skip=tests/example_crlf_file.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e1096e81..1e23349f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ NOTE: isort follows the [semver](https://semver.org/) versioning standard. - Implemented #941: Added an additional `multi_line_output` mode for more compact formatting (Thanks @sztamas!) - `# isort: split` can now be used at the end of an import line. - Fixed #1339: Extra indent is not preserved when isort:skip is used in nested imports. + - Fixed #1348: `--diff` works incorrectly with files that have CRLF line endings. ### 5.1.4 July 19, 2020 - Fixed issue #1333: Use of wrap_length raises an exception about it not being lower or equal to line_length. diff --git a/isort/api.py b/isort/api.py index edebf529d..3e484a519 100644 --- a/isort/api.py +++ b/isort/api.py @@ -335,17 +335,22 @@ def sort_file( if changed: if show_diff or ask_to_apply: source_file.stream.seek(0) - show_unified_diff( - file_input=source_file.stream.read(), - file_output=tmp_file.read_text(encoding=source_file.encoding), - file_path=file_path or source_file.path, - output=None if show_diff is True else cast(TextIO, show_diff), - ) - if show_diff or ( - ask_to_apply - and not ask_whether_to_apply_changes_to_file(str(source_file.path)) - ): - return + with tmp_file.open( + encoding=source_file.encoding, newline="" + ) as tmp_out: + show_unified_diff( + file_input=source_file.stream.read(), + file_output=tmp_out.read(), + file_path=file_path or source_file.path, + output=None if show_diff is True else cast(TextIO, show_diff), + ) + if show_diff or ( + ask_to_apply + and not ask_whether_to_apply_changes_to_file( + str(source_file.path) + ) + ): + return source_file.stream.close() tmp_file.replace(source_file.path) if not config.quiet: diff --git a/isort/format.py b/isort/format.py index 15ad6233a..cdcc5a00b 100644 --- a/isort/format.py +++ b/isort/format.py @@ -43,7 +43,6 @@ def show_unified_diff( file_mtime = str( datetime.now() if file_path is None else datetime.fromtimestamp(file_path.stat().st_mtime) ) - unified_diff_lines = unified_diff( file_input.splitlines(keepends=True), file_output.splitlines(keepends=True), diff --git a/tests/example_crlf_file.py b/tests/example_crlf_file.py new file mode 100644 index 000000000..78db95188 --- /dev/null +++ b/tests/example_crlf_file.py @@ -0,0 +1,10 @@ +import b +import a + + +def func(): + x = 1 + y = 2 + z = 3 + c = 4 + return x + y + z + c diff --git a/tests/test_regressions.py b/tests/test_regressions.py index b62173883..5d1d0413a 100644 --- a/tests/test_regressions.py +++ b/tests/test_regressions.py @@ -1,4 +1,6 @@ """A growing set of tests designed to ensure isort doesn't have regressions in new versions""" +from io import StringIO + import isort @@ -478,3 +480,15 @@ def import_test(): """, show_diff=True, ) + + +def test_windows_diff_too_large_misrepresentative_issue_1348(test_path): + """Ensure isort handles windows files correctly when it come to producing a diff with --diff. + See: https://github.com/timothycrosley/isort/issues/1348 + """ + diff_output = StringIO() + isort.file(test_path / "example_crlf_file.py", show_diff=diff_output) + diff_output.seek(0) + assert diff_output.read().endswith( + "-1,5 +1,5 @@\n+import a\r\n import b\r\n" "-import a\r\n \r\n \r\n def func():\r\n" + )