summaryrefslogtreecommitdiff
path: root/tests/unit/utils.py
blob: 3b00ced938a009d4276c3e56de974e77aab6fc3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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 across multiple runs, and if an expected_output
    is given - that it matches that.
    """
    expected_output = expected_output or code

    output = isort.code(code, **config)
    assert output == expected_output

    assert output == isort.code(output, **config)