From 9a79067846cff03aca6b7460056aaacffec6a36c Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Sat, 10 Oct 2020 03:41:32 -0700 Subject: [PATCH] Add test for unseekable pipe errors --- isort/api.py | 3 ++ tests/unit/test_main.py | 62 ++++++++++++++++++++++++++++------------- 2 files changed, 46 insertions(+), 19 deletions(-) diff --git a/isort/api.py b/isort/api.py index a8cecb623..5a2df6af3 100644 --- a/isort/api.py +++ b/isort/api.py @@ -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, diff --git a/tests/unit/test_main.py b/tests/unit/test_main.py index caf20e7ed..b7bc7a00e 100644 --- a/tests/unit/test_main.py +++ b/tests/unit/test_main.py @@ -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), @@ -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 @@ -362,7 +367,7 @@ def test_isort_with_stdin(capsys): """ ) - input_content_from = TextIOWrapper( + input_content_from = UnseekableTextIOWrapper( BytesIO( b""" import c @@ -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 @@ -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 @@ -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 @@ -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 @@ -472,7 +477,7 @@ def test_isort_with_stdin(capsys): """ ) - input_content = TextIOWrapper( + input_content = UnseekableTextIOWrapper( BytesIO( b""" import a @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -701,7 +725,7 @@ def test_isort_with_stdin(capsys): """ ) - input_content = TextIOWrapper( + input_content = UnseekableTextIOWrapper( BytesIO( b""" import sys @@ -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