Skip to content

Commit

Permalink
Fixed #1348: --diff works incorrectly with files that have CRLF line …
Browse files Browse the repository at this point in the history
…endings.
  • Loading branch information
timothycrosley committed Jul 27, 2020
1 parent 57eb70a commit 04d00eb
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 12 deletions.
1 change: 1 addition & 0 deletions .isort.cfg
@@ -1,3 +1,4 @@
[settings]
profile=hug
src_paths=isort,test
skip=tests/example_crlf_file.py
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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.
Expand Down
27 changes: 16 additions & 11 deletions isort/api.py
Expand Up @@ -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:
Expand Down
1 change: 0 additions & 1 deletion isort/format.py
Expand Up @@ -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),
Expand Down
10 changes: 10 additions & 0 deletions 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
14 changes: 14 additions & 0 deletions 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


Expand Down Expand Up @@ -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"
)

0 comments on commit 04d00eb

Please sign in to comment.