diff options
Diffstat (limited to 'tests/config/test_config.py')
-rw-r--r-- | tests/config/test_config.py | 104 |
1 files changed, 8 insertions, 96 deletions
diff --git a/tests/config/test_config.py b/tests/config/test_config.py index 8ce5d9b42..f89e62416 100644 --- a/tests/config/test_config.py +++ b/tests/config/test_config.py @@ -1,33 +1,9 @@ -# pylint: disable=missing-module-docstring, missing-function-docstring, protected-access import os -import unittest.mock from pathlib import Path -from typing import Optional, Set, Union +from typing import Optional, Set -import pylint.lint from pylint.lint.run import Run - -# We use an external file and not __file__ or pylint warning in this file -# makes the tests fails because the exit code changes -FILE_TO_LINT = str(Path(__file__).parent / "file_to_lint.py") - - -def get_runner_from_config_file( - config_file: Union[str, Path], expected_exit_code: int = 0 -) -> Run: - """Initialize pylint with the given configuration file and return the Run""" - args = ["--rcfile", str(config_file), FILE_TO_LINT] - # If we used `pytest.raises(SystemExit)`, the `runner` variable - # would not be accessible outside the `with` block. - with unittest.mock.patch("sys.exit") as mocked_exit: - # Do not actually run checks, that could be slow. Do not mock - # `Pylinter.check`: it calls `Pylinter.initialize` which is - # needed to properly set up messages inclusion/exclusion - # in `_msg_states`, used by `is_message_enabled`. - with unittest.mock.patch("pylint.lint.pylinter.check_parallel"): - runner = pylint.lint.Run(args) - mocked_exit.assert_called_once_with(expected_exit_code) - return runner +from pylint.testutils.configuration_test import run_using_a_configuration_file def check_configuration_file_reader( @@ -46,74 +22,7 @@ def check_configuration_file_reader( assert bool(runner.linter.config.reports) == expected_reports_truthey -def test_can_read_ini(tmp_path: Path) -> None: - # Check that we can read the "regular" INI .pylintrc file - config_file = tmp_path / ".pylintrc" - config_file.write_text( - """ -[messages control] -disable = logging-not-lazy,logging-format-interpolation -jobs = 10 -reports = yes -""" - ) - run = get_runner_from_config_file(config_file) - check_configuration_file_reader(run) - - -def test_can_read_setup_cfg(tmp_path: Path) -> None: - # Check that we can read a setup.cfg (which is an INI file where - # section names are prefixed with "pylint." - config_file = tmp_path / "setup.cfg" - config_file.write_text( - """ -[pylint.messages control] -disable = logging-not-lazy,logging-format-interpolation -jobs = 10 -reports = yes -""" - ) - run = get_runner_from_config_file(config_file) - check_configuration_file_reader(run) - - -def test_can_read_toml(tmp_path: Path) -> None: - # Check that we can read a TOML file where lists and integers are - # expressed as strings. - config_file = tmp_path / "pyproject.toml" - config_file.write_text( - """ -[tool.pylint."messages control"] -disable = "logging-not-lazy,logging-format-interpolation" -jobs = "10" -reports = "yes" -""" - ) - run = get_runner_from_config_file(config_file) - check_configuration_file_reader(run) - - -def test_can_read_toml_rich_types(tmp_path: Path) -> None: - # Check that we can read a TOML file where lists, integers and - # booleans are expressed as such (and not as strings), using TOML - # type system. - config_file = tmp_path / "pyproject.toml" - config_file.write_text( - """ -[tool.pylint."messages control"] -disable = [ - "logging-not-lazy", - "logging-format-interpolation", -] -jobs = 10 -reports = true -""" - ) - run = get_runner_from_config_file(config_file) - check_configuration_file_reader(run) - - -def test_can_read_toml_env_variable(tmp_path: Path) -> None: +def test_can_read_toml_env_variable(tmp_path: Path, file_to_lint_path: str) -> None: """We can read and open a properly formatted toml file.""" config_file = tmp_path / "pyproject.toml" config_file.write_text( @@ -126,5 +35,8 @@ reports = "yes" ) env_var = "tmp_path_env" os.environ[env_var] = str(config_file) - run = get_runner_from_config_file(f"${env_var}") - check_configuration_file_reader(run) + mock_exit, _, runner = run_using_a_configuration_file( + f"${env_var}", file_to_lint_path + ) + mock_exit.assert_called_once_with(0) + check_configuration_file_reader(runner) |