Skip to content

Commit

Permalink
Add test for unseekable pipe errors
Browse files Browse the repository at this point in the history
  • Loading branch information
timothycrosley committed Oct 10, 2020
1 parent 756fe46 commit 9a79067
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 19 deletions.
3 changes: 3 additions & 0 deletions isort/api.py
Expand Up @@ -200,6 +200,9 @@ def check_stream(
"""
config = _config(path=file_path, config=config, **config_kwargs)

if show_diff:
input_stream = StringIO(input_stream.read())

changed: bool = sort_stream(
input_stream=input_stream,
output_stream=Empty,
Expand Down
62 changes: 43 additions & 19 deletions tests/unit/test_main.py
Expand Up @@ -14,6 +14,11 @@
from isort.wrap_modes import WrapModes


class UnseekableTextIOWrapper(TextIOWrapper):
def seek(self, *args, **kwargs):
raise ValueError("underlying stream is not seekable")


@given(
file_name=st.text(),
config=st.builds(Config),
Expand Down Expand Up @@ -343,7 +348,7 @@ def test_isort_command():
def test_isort_with_stdin(capsys):
# ensures that isort sorts stdin without any flags

input_content = TextIOWrapper(
input_content = UnseekableTextIOWrapper(
BytesIO(
b"""
import b
Expand All @@ -362,7 +367,7 @@ def test_isort_with_stdin(capsys):
"""
)

input_content_from = TextIOWrapper(
input_content_from = UnseekableTextIOWrapper(
BytesIO(
b"""
import c
Expand All @@ -385,7 +390,7 @@ def test_isort_with_stdin(capsys):

# ensures that isort correctly sorts stdin with --fas flag

input_content = TextIOWrapper(
input_content = UnseekableTextIOWrapper(
BytesIO(
b"""
import sys
Expand All @@ -411,7 +416,7 @@ def test_isort_with_stdin(capsys):

# ensures that isort correctly sorts stdin with --fass flag

input_content = TextIOWrapper(
input_content = UnseekableTextIOWrapper(
BytesIO(
b"""
from a import Path, abc
Expand All @@ -430,7 +435,7 @@ def test_isort_with_stdin(capsys):

# ensures that isort correctly sorts stdin with --ff flag

input_content = TextIOWrapper(
input_content = UnseekableTextIOWrapper(
BytesIO(
b"""
import b
Expand All @@ -453,7 +458,7 @@ def test_isort_with_stdin(capsys):

# ensures that isort correctly sorts stdin with -fss flag

input_content = TextIOWrapper(
input_content = UnseekableTextIOWrapper(
BytesIO(
b"""
import b
Expand All @@ -472,7 +477,7 @@ def test_isort_with_stdin(capsys):
"""
)

input_content = TextIOWrapper(
input_content = UnseekableTextIOWrapper(
BytesIO(
b"""
import a
Expand All @@ -493,7 +498,7 @@ def test_isort_with_stdin(capsys):

# ensures that isort correctly sorts stdin with --ds flag

input_content = TextIOWrapper(
input_content = UnseekableTextIOWrapper(
BytesIO(
b"""
import sys
Expand All @@ -516,7 +521,7 @@ def test_isort_with_stdin(capsys):

# ensures that isort correctly sorts stdin with --cs flag

input_content = TextIOWrapper(
input_content = UnseekableTextIOWrapper(
BytesIO(
b"""
from a import b
Expand All @@ -536,7 +541,7 @@ def test_isort_with_stdin(capsys):

# ensures that isort correctly sorts stdin with --ca flag

input_content = TextIOWrapper(
input_content = UnseekableTextIOWrapper(
BytesIO(
b"""
from a import x as X
Expand All @@ -556,7 +561,7 @@ def test_isort_with_stdin(capsys):

# ensures that isort works consistently with check and ws flags

input_content = TextIOWrapper(
input_content = UnseekableTextIOWrapper(
BytesIO(
b"""
import os
Expand All @@ -570,10 +575,29 @@ def test_isort_with_stdin(capsys):
out, error = capsys.readouterr()

assert not error

# ensures that isort works consistently with check and diff flags

input_content = UnseekableTextIOWrapper(
BytesIO(
b"""
import b
import a
"""
)
)

with pytest.raises(SystemExit):
main.main(["-", "--check", "--diff"], stdin=input_content)
out, error = capsys.readouterr()

assert error
assert not "underlying stream is not seekable" in error
assert not "underlying stream is not seekable" in error

# ensures that isort correctly sorts stdin with --ls flag

input_content = TextIOWrapper(
input_content = UnseekableTextIOWrapper(
BytesIO(
b"""
import abcdef
Expand All @@ -594,7 +618,7 @@ def test_isort_with_stdin(capsys):

# ensures that isort correctly sorts stdin with --nis flag

input_content = TextIOWrapper(
input_content = UnseekableTextIOWrapper(
BytesIO(
b"""
from z import b, c, a
Expand All @@ -613,7 +637,7 @@ def test_isort_with_stdin(capsys):

# ensures that isort correctly sorts stdin with --sl flag

input_content = TextIOWrapper(
input_content = UnseekableTextIOWrapper(
BytesIO(
b"""
from z import b, c, a
Expand All @@ -634,7 +658,7 @@ def test_isort_with_stdin(capsys):

# ensures that isort correctly sorts stdin with --top flag

input_content = TextIOWrapper(
input_content = UnseekableTextIOWrapper(
BytesIO(
b"""
import os
Expand All @@ -655,7 +679,7 @@ def test_isort_with_stdin(capsys):

# ensure that isort correctly sorts stdin with --os flag

input_content = TextIOWrapper(
input_content = UnseekableTextIOWrapper(
BytesIO(
b"""
import sys
Expand All @@ -680,7 +704,7 @@ def test_isort_with_stdin(capsys):
)

# ensures that isort warns with deprecated flags with stdin
input_content = TextIOWrapper(
input_content = UnseekableTextIOWrapper(
BytesIO(
b"""
import sys
Expand All @@ -701,7 +725,7 @@ def test_isort_with_stdin(capsys):
"""
)

input_content = TextIOWrapper(
input_content = UnseekableTextIOWrapper(
BytesIO(
b"""
import sys
Expand All @@ -723,7 +747,7 @@ def test_isort_with_stdin(capsys):
)

# ensures that only-modified flag works with stdin
input_content = TextIOWrapper(
input_content = UnseekableTextIOWrapper(
BytesIO(
b"""
import a
Expand Down

0 comments on commit 9a79067

Please sign in to comment.