diff options
author | Pierre Sassoulas <pierre.sassoulas@gmail.com> | 2022-04-22 22:56:12 +0200 |
---|---|---|
committer | Pierre Sassoulas <pierre.sassoulas@gmail.com> | 2022-04-26 09:08:23 +0200 |
commit | 8482d2ee74cdf96daffa1f32b8d04868d8c6c8e6 (patch) | |
tree | a92278d431f2265f37c87872a8535ebea876aa9b /tests/test_self.py | |
parent | c17204d5298ca7a358be78f9b5f880db5f027d59 (diff) | |
download | pylint-git-8482d2ee74cdf96daffa1f32b8d04868d8c6c8e6.tar.gz |
Use an empty pylintrc for tests using project's pylintrc implicitely
We don't want to use the project's pylintrc during tests, because
it means that a change in our config could break tests.
But we want to see if the changes to the default break tests.
Create a private '_Run' class in pylint.testutil._run
Co-authored-by: Daniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>
Diffstat (limited to 'tests/test_self.py')
-rw-r--r-- | tests/test_self.py | 90 |
1 files changed, 45 insertions, 45 deletions
diff --git a/tests/test_self.py b/tests/test_self.py index 70963fe04..233a7d3c1 100644 --- a/tests/test_self.py +++ b/tests/test_self.py @@ -30,11 +30,12 @@ from py._path.local import LocalPath # type: ignore[import] from pylint import extensions, modify_sys_path from pylint.constants import MAIN_CHECKER_NAME, MSG_TYPES_STATUS -from pylint.lint import Run 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._run import _add_rcfile_default_pylintrc +from pylint.testutils._run import _Run as Run from pylint.testutils.utils import _patch_streams from pylint.utils import utils @@ -130,6 +131,7 @@ class TestRunTC: ) -> None: if out is None: out = StringIO() + args = _add_rcfile_default_pylintrc(args) pylint_code = self._run_pylint(args, reporter=reporter, out=out) if reporter: output = reporter.out.getvalue() @@ -144,7 +146,7 @@ class TestRunTC: @staticmethod def _run_pylint(args: list[str], out: TextIO, reporter: Any = None) -> int: - args = args + ["--persistent=no"] + args = _add_rcfile_default_pylintrc(args + ["--persistent=no"]) with _patch_streams(out): with pytest.raises(SystemExit) as cm: with warnings.catch_warnings(): @@ -160,6 +162,7 @@ class TestRunTC: def _test_output(self, args: list[str], expected_output: str) -> None: out = StringIO() + args = _add_rcfile_default_pylintrc(args) self._run_pylint(args, out=out) actual_output = self._clean_paths(out.getvalue()) expected_output = self._clean_paths(expected_output) @@ -172,6 +175,7 @@ class TestRunTC: the ``args`` passed to this method!) and check the file content afterwards. """ out = StringIO() + args = _add_rcfile_default_pylintrc(args) self._run_pylint(args, out=out) cmdline_output = out.getvalue() file_output = self._clean_paths(Path(filename).read_text(encoding="utf-8")) @@ -183,6 +187,7 @@ class TestRunTC: def test_pkginfo(self) -> None: """Make pylint check 'pylint.__pkginfo__.py'.""" + # Disable invalid-name because of invalid argument names args = ["pylint.__pkginfo__", "--disable=invalid-name"] self._runtest(args, reporter=TextReporter(StringIO()), code=0) @@ -1091,13 +1096,13 @@ class TestRunTC: path = join(HERE, "regrtest_data", "fail_on.py") # We set fail-under to be something very low so that even with the warnings # and errors that are generated they don't affect the exit code. - self._runtest([path, "--fail-under=-10"] + args, code=expected) + self._runtest([path, "--fail-under=-10", "--disable=C"] + args, code=expected) def test_one_module_fatal_error(self): """Fatal errors in one of several modules linted still exits non-zero.""" valid_path = join(HERE, "conftest.py") invalid_path = join(HERE, "garbagePath.py") - self._runtest([valid_path, invalid_path], code=1) + self._runtest([valid_path, invalid_path, "--disable=C"], code=1) @pytest.mark.parametrize( "args, expected", @@ -1197,7 +1202,10 @@ class TestRunTC: Reported in https://github.com/PyCQA/pylint/issues/5437 """ with pytest.raises(SystemExit) as ex: - Run(["--ignore-paths", "test", join(HERE, "regrtest_data", "empty.py")]) + args = _add_rcfile_default_pylintrc( + ["--ignore-paths", "test", join(HERE, "regrtest_data", "empty.py")] + ) + Run(args) assert ex.value.code == 0 @staticmethod @@ -1284,7 +1292,7 @@ class TestCallbackOptions: ) def test_output_of_callback_options(command: list[str], expected: str) -> None: """Test whether certain strings are in the output of a callback command.""" - + command = _add_rcfile_default_pylintrc(command) process = subprocess.run( [sys.executable, "-m", "pylint"] + command, capture_output=True, @@ -1294,47 +1302,44 @@ class TestCallbackOptions: assert expected in process.stdout @staticmethod - def test_help_msg() -> None: + @pytest.mark.parametrize( + "args,expected,error", + [ + [["--help-msg", "W0101"], ":unreachable (W0101)", False], + [["--help-msg", "WX101"], "No such message id", False], + [["--help-msg"], "--help-msg: expected at least one argumen", True], + ], + ) + def test_help_msg(args: list[str], expected: str, error: bool) -> None: """Test the --help-msg flag.""" - - process = subprocess.run( - [sys.executable, "-m", "pylint", "--help-msg", "W0101"], - capture_output=True, - encoding="utf-8", - check=False, - ) - assert ":unreachable (W0101)" in process.stdout - - process = subprocess.run( - [sys.executable, "-m", "pylint", "--help-msg", "WX101"], - capture_output=True, - encoding="utf-8", - check=False, - ) - assert "No such message id" in process.stdout - + args = _add_rcfile_default_pylintrc(args) process = subprocess.run( - [sys.executable, "-m", "pylint", "--help-msg"], + [sys.executable, "-m", "pylint"] + args, capture_output=True, encoding="utf-8", check=False, ) - assert "--help-msg: expected at least one argumen" in process.stderr + if error: + result = process.stderr + else: + result = process.stdout + assert expected in result @staticmethod def test_generate_rcfile() -> None: """Test the --generate-rcfile flag.""" + args = _add_rcfile_default_pylintrc(["--generate-rcfile"]) process = subprocess.run( - [sys.executable, "-m", "pylint", "--generate-rcfile"], + [sys.executable, "-m", "pylint"] + args, capture_output=True, encoding="utf-8", check=False, ) assert "[MASTER]" in process.stdout assert "profile" not in process.stdout - + args = _add_rcfile_default_pylintrc(["--generate-rcfile"]) process_two = subprocess.run( - [sys.executable, "-m", "pylint", "--generate-rcfile"], + [sys.executable, "-m", "pylint"] + args, capture_output=True, encoding="utf-8", check=False, @@ -1367,14 +1372,14 @@ class TestCallbackOptions: @staticmethod def test_generate_toml_config() -> None: """Test the --generate-toml-config flag.""" - process = subprocess.run( + args = _add_rcfile_default_pylintrc( [ - sys.executable, - "-m", - "pylint", "--preferred-modules=a:b", "--generate-toml-config", - ], + ] + ) + process = subprocess.run( + [sys.executable, "-m", "pylint"] + args, capture_output=True, encoding="utf-8", check=False, @@ -1384,13 +1389,7 @@ class TestCallbackOptions: assert 'preferred-modules = ["a:b"]' in process.stdout process_two = subprocess.run( - [ - sys.executable, - "-m", - "pylint", - "--preferred-modules=a:b", - "--generate-toml-config", - ], + [sys.executable, "-m", "pylint"] + args, capture_output=True, encoding="utf-8", check=False, @@ -1400,17 +1399,18 @@ class TestCallbackOptions: @staticmethod def test_generate_toml_config_disable_symbolic_names() -> None: """Test that --generate-toml-config puts symbolic names in the --disable option.""" - out = StringIO() - with _patch_streams(out): + output_stream = StringIO() + with _patch_streams(output_stream): with pytest.raises(SystemExit): with warnings.catch_warnings(): warnings.simplefilter("ignore") Run(["--generate-toml-config"]) - bytes_out = BytesIO(out.getvalue().encode("utf-8")) + out = output_stream.getvalue() + bytes_out = BytesIO(out.encode("utf-8")) content = tomllib.load(bytes_out) messages = content["tool"]["pylint"]["messages control"]["disable"] - assert "invalid-name" in messages, out.getvalue() + assert "useless-suppression" in messages, out @staticmethod def test_errors_only() -> None: |