diff options
author | Pierre Sassoulas <pierre.sassoulas@gmail.com> | 2022-04-07 14:12:51 +0200 |
---|---|---|
committer | Pierre Sassoulas <pierre.sassoulas@gmail.com> | 2022-04-15 15:01:57 +0200 |
commit | 18bf64cb06b1f58c69c074c157b038c106ec0a31 (patch) | |
tree | c866811ba8e4c7d1c59f2690ffb072f3657d7732 | |
parent | 9cfe8366d43713f99ce2242ad4b7cd3e4b97c340 (diff) | |
download | pylint-git-18bf64cb06b1f58c69c074c157b038c106ec0a31.tar.gz |
Move '_patch_stream' to pylint.testutil
-rw-r--r-- | pylint/testutils/utils.py | 15 | ||||
-rw-r--r-- | tests/test_self.py | 11 | ||||
-rw-r--r-- | tests/test_similar.py | 14 |
3 files changed, 17 insertions, 23 deletions
diff --git a/pylint/testutils/utils.py b/pylint/testutils/utils.py index dbd75d6b8..36771ea39 100644 --- a/pylint/testutils/utils.py +++ b/pylint/testutils/utils.py @@ -4,7 +4,22 @@ from __future__ import annotations +import contextlib import os +import sys +from collections.abc import Iterator +from typing import TextIO + + +@contextlib.contextmanager +def _patch_streams(out: TextIO) -> Iterator[None]: + """Patch and subsequently reset a text stream.""" + sys.stderr = sys.stdout = out + try: + yield + finally: + sys.stderr = sys.__stderr__ + sys.stdout = sys.__stdout__ def create_files(paths: list[str], chroot: str = ".") -> None: diff --git a/tests/test_self.py b/tests/test_self.py index 54bbf142a..f7c1ec0e7 100644 --- a/tests/test_self.py +++ b/tests/test_self.py @@ -35,6 +35,7 @@ from pylint.lint.pylinter import PyLinter from pylint.message import Message from pylint.reporters import JSONReporter from pylint.reporters.text import BaseReporter, ColorizedTextReporter, TextReporter +from pylint.testutils.utils import _patch_streams from pylint.utils import utils if sys.version_info >= (3, 11): @@ -54,16 +55,6 @@ UNNECESSARY_LAMBDA = join( @contextlib.contextmanager -def _patch_streams(out: TextIO) -> Iterator: - sys.stderr = sys.stdout = out - try: - yield - finally: - sys.stderr = sys.__stderr__ - sys.stdout = sys.__stdout__ - - -@contextlib.contextmanager def _configure_lc_ctype(lc_ctype: str) -> Iterator: lc_ctype_env = "LC_CTYPE" original_lctype = os.environ.get(lc_ctype_env) diff --git a/tests/test_similar.py b/tests/test_similar.py index 2c258a019..868284e5b 100644 --- a/tests/test_similar.py +++ b/tests/test_similar.py @@ -4,12 +4,9 @@ from __future__ import annotations -import contextlib import os import re -import sys import warnings -from collections.abc import Iterator from io import StringIO from os.path import abspath, dirname, join from typing import TextIO @@ -17,22 +14,13 @@ from typing import TextIO import pytest from pylint.lint import Run +from pylint.testutils.utils import _patch_streams HERE = abspath(dirname(__file__)) DATA = join(HERE, "regrtest_data", "duplicate_code") CLEAN_PATH = re.escape(dirname(dirname(__file__)) + os.path.sep) -@contextlib.contextmanager -def _patch_streams(out: TextIO) -> Iterator: - sys.stderr = sys.stdout = out - try: - yield - finally: - sys.stderr = sys.__stderr__ - sys.stdout = sys.__stdout__ - - class TestSimilarCodeChecker: def _runtest(self, args: list[str], code: int) -> None: """Runs the tests and sees if output code is as expected.""" |