summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorTimothy Crosley <timothy.crosley@gmail.com>2021-06-17 01:21:52 -0700
committerTimothy Crosley <timothy.crosley@gmail.com>2021-06-17 01:21:52 -0700
commit90fde3ef126fdccbb947bb59129b63b4cfa7c348 (patch)
treec993f408088c6abca430e89d4492f6a4a041e472 /tests
parent9b7b136dad3bbefcf9f4c0257715abb49f7da695 (diff)
downloadisort-90fde3ef126fdccbb947bb59129b63b4cfa7c348.tar.gz
Fix missing coverage
Diffstat (limited to 'tests')
-rw-r--r--tests/unit/test_isort.py9
-rw-r--r--tests/unit/test_main.py25
-rw-r--r--tests/unit/utils.py16
3 files changed, 40 insertions, 10 deletions
diff --git a/tests/unit/test_isort.py b/tests/unit/test_isort.py
index 2cbc93a4..ec2fd5d6 100644
--- a/tests/unit/test_isort.py
+++ b/tests/unit/test_isort.py
@@ -19,6 +19,7 @@ from isort import api, sections, files
from isort.settings import WrapModes, Config
from isort.utils import exists_case_sensitive
from isort.exceptions import FileSkipped, ExistingSyntaxErrors
+from .utils import as_stream, UnreadableStream
TEST_DEFAULT_CONFIG = """
[*.{py,pyi}]
@@ -1368,6 +1369,7 @@ def test_single_multiline() -> None:
def test_atomic_mode() -> None:
+ """With atomic mode isort should be able to automatically detect and stop syntax errors"""
# without syntax error, everything works OK
test_input = "from b import d, c\nfrom a import f, e\n"
assert isort.code(test_input, atomic=True) == ("from a import e, f\nfrom b import c, d\n")
@@ -1377,6 +1379,13 @@ def test_atomic_mode() -> None:
with pytest.raises(ExistingSyntaxErrors):
isort.code(test_input, atomic=True)
+ # ensure atomic works with streams
+ test_input = as_stream("from b import d, c\nfrom a import f, e\n")
+ test_output = UnreadableStream()
+ isort.stream(test_input, test_output, atomic=True)
+ test_output.seek(0)
+ assert test_output.read() == "from a import e, f\nfrom b import c, d\n"
+
def test_order_by_type() -> None:
test_input = "from module import Class, CONSTANT, function"
diff --git a/tests/unit/test_main.py b/tests/unit/test_main.py
index ccf51c0e..207f19dc 100644
--- a/tests/unit/test_main.py
+++ b/tests/unit/test_main.py
@@ -2,7 +2,6 @@ import json
import os
import subprocess
from datetime import datetime
-from io import BytesIO, TextIOWrapper
import py
import pytest
@@ -14,15 +13,8 @@ from isort._version import __version__
from isort.exceptions import InvalidSettingsPath
from isort.settings import DEFAULT_CONFIG, Config
from isort.wrap_modes import WrapModes
-
-
-class UnseekableTextIOWrapper(TextIOWrapper):
- def seek(self, *args, **kwargs):
- raise ValueError("underlying stream is not seekable")
-
-
-def as_stream(text: str) -> UnseekableTextIOWrapper:
- return UnseekableTextIOWrapper(BytesIO(text.encode("utf8")))
+from .utils import as_stream
+from io import BytesIO, TextIOWrapper
@given(
@@ -910,6 +902,19 @@ import b
"""
)
+ atomic_input_without_skip = input_with_skip.replace("isort: skip_file", "generic comment")
+ stream_without_skip = as_stream(atomic_input_without_skip)
+ main.main(["-", "--atomic"], stdin=stream_without_skip)
+ out, error = capsys.readouterr()
+ assert (
+ out
+ == """
+# generic comment
+import a
+import b
+"""
+ )
+
def test_only_modified_flag(tmpdir, capsys):
# ensures there is no verbose output for correct files with only-modified flag
diff --git a/tests/unit/utils.py b/tests/unit/utils.py
index 9c963d63..b87e7a45 100644
--- a/tests/unit/utils.py
+++ b/tests/unit/utils.py
@@ -1,6 +1,22 @@
+from io import BytesIO, StringIO, TextIOWrapper
+
import isort
+class UnseekableTextIOWrapper(TextIOWrapper):
+ def seek(self, *args, **kwargs):
+ raise ValueError("underlying stream is not seekable")
+
+
+class UnreadableStream(StringIO):
+ def readable(self, *args, **kwargs) -> bool:
+ return False
+
+
+def as_stream(text: str) -> UnseekableTextIOWrapper:
+ return UnseekableTextIOWrapper(BytesIO(text.encode("utf8")))
+
+
def isort_test(code: str, expected_output: str = "", **config):
"""Runs isort against the given code snippet and ensures that it
gives consistent output accross multiple runs, and if an expected_output