File: utils.py

package info (click to toggle)
isort 7.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,548 kB
  • sloc: python: 15,337; javascript: 42; makefile: 28; sh: 22
file content (30 lines) | stat: -rw-r--r-- 866 bytes parent folder | download | duplicates (2)
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)