Skip to content

Commit

Permalink
Fixed issue #1189: '--diff' broken when reading from standard input.
Browse files Browse the repository at this point in the history
  • Loading branch information
timothycrosley committed Jul 16, 2020
1 parent b7be8c8 commit cde7ec8
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -4,7 +4,8 @@ Changelog
NOTE: isort follows the [semver](https://semver.org/) versioning standard.

### 5.1.1 July 15, 2020
- Fix issue #1322: Occasionally two extra newlines before comment with `-n` & `--fss`.
- Fixed issue #1322: Occasionally two extra newlines before comment with `-n` & `--fss`.
- Fixed issue #1189: `--diff` broken when reading from standard input.

### 5.1.0 July 14, 2020
- isort now throws an exception if an invalid settings path is given (issue #1174).
Expand Down
23 changes: 23 additions & 0 deletions isort/api.py
Expand Up @@ -100,6 +100,7 @@ def sort_stream(
config: Config = DEFAULT_CONFIG,
file_path: Optional[Path] = None,
disregard_skip: bool = False,
show_diff: bool = False,
**config_kwargs,
):
"""Sorts any imports within the provided code stream, outputs to the provided output stream.
Expand All @@ -113,6 +114,28 @@ def sort_stream(
- **disregard_skip**: set to `True` if you want to ignore a skip set in config for this file.
- ****config_kwargs**: Any config modifications.
"""
if show_diff:
_output_stream = StringIO()
_input_stream = StringIO(input_stream.read())
changed = sort_stream(
input_stream=_input_stream,
output_stream=_output_stream,
extension=extension,
config=config,
file_path=file_path,
disregard_skip=disregard_skip,
**config_kwargs,
)
_output_stream.seek(0)
_input_stream.seek(0)
show_unified_diff(
file_input=_input_stream.read(),
file_output=_output_stream.read(),
file_path=file_path,
output=output_stream,
)
return changed

config = _config(path=file_path, config=config, **config_kwargs)
content_source = str(file_path or "Passed in content")
if not disregard_skip:
Expand Down
6 changes: 4 additions & 2 deletions isort/format.py
Expand Up @@ -28,7 +28,9 @@ def format_natural(import_line: str) -> str:
return import_line


def show_unified_diff(*, file_input: str, file_output: str, file_path: Optional[Path]):
def show_unified_diff(
*, file_input: str, file_output: str, file_path: Optional[Path], output=sys.stdout
):
file_name = "" if file_path is None else str(file_path)
file_mtime = str(
datetime.now() if file_path is None else datetime.fromtimestamp(file_path.stat().st_mtime)
Expand All @@ -43,7 +45,7 @@ def show_unified_diff(*, file_input: str, file_output: str, file_path: Optional[
tofiledate=str(datetime.now()),
)
for line in unified_diff_lines:
sys.stdout.write(line)
output.write(line)


def ask_whether_to_apply_changes_to_file(file_path: str) -> bool:
Expand Down
8 changes: 8 additions & 0 deletions tests/test_api.py
@@ -1,4 +1,5 @@
"""Tests the isort API module"""
from io import StringIO
from unittest.mock import MagicMock, patch

import pytest
Expand Down Expand Up @@ -41,3 +42,10 @@ def test_check_file(tmpdir) -> None:
def test_sorted_imports_multiple_configs() -> None:
with pytest.raises(ValueError):
api.sort_code_string("import os", config=Config(line_length=80), line_length=80)


def test_diff_stream() -> None:
output = StringIO()
assert api.sort_stream(StringIO("import b\nimport a\n"), output, show_diff=True)
output.seek(0)
assert "import a\n import b\n" in output.read()

0 comments on commit cde7ec8

Please sign in to comment.