| 1 |
from io import BytesIO, StringIO, TextIOWrapper |
| 2 |
from typing import Any |
| 3 |
|
| 4 |
import isort |
| 5 |
|
| 6 |
|
| 7 |
class UnseekableTextIOWrapper(TextIOWrapper): |
| 8 |
def seek(self, offset: int, whence: int = 0, /) -> int: |
| 9 |
raise ValueError("underlying stream is not seekable") |
| 10 |
|
| 11 |
|
| 12 |
class UnreadableStream(StringIO): |
| 13 |
def readable(self, *args: object, **kwargs: object) -> bool: |
| 14 |
return False |
| 15 |
|
| 16 |
|
| 17 |
def as_stream(text: str) -> UnseekableTextIOWrapper: |
| 18 |
return UnseekableTextIOWrapper(BytesIO(text.encode("utf8"))) |
| 19 |
|
| 20 |
|
| 21 |
def isort_test(code: str, expected_output: str = "", **config: Any) -> None: |
| 22 |
"""Runs isort against the given code snippet and ensures that it |
| 23 |
gives consistent output across multiple runs, and if an expected_output |
| 24 |
is given - that it matches that. |
| 25 |
""" |
| 26 |
expected_output = expected_output or code |
| 27 |
|
| 28 |
output = isort.code(code, **config) |
| 29 |
assert output == expected_output |
| 30 |
|
| 31 |
assert output == isort.code(output, **config) |