|
| 1 | +# pylint: disable=missing-module-docstring, missing-function-docstring, protected-access |
1 | 2 | import unittest.mock
|
2 | 3 |
|
3 | 4 | import pylint.lint
|
4 | 5 |
|
5 | 6 |
|
6 |
| -def test_can_read_toml(tmp_path): |
7 |
| - config_file = tmp_path / "pyproject.toml" |
| 7 | +def check_configuration_file_reader(config_file): |
| 8 | + """Initialize pylint with the given configuration file and check that |
| 9 | + what we initialized the linter with what was expected. |
| 10 | + """ |
| 11 | + args = ["--rcfile", str(config_file), __file__] |
| 12 | + # If we used `pytest.raises(SystemExit)`, the `runner` variable |
| 13 | + # would not be accessible outside the `with` block. |
| 14 | + with unittest.mock.patch("sys.exit") as mocked_exit: |
| 15 | + # Do not actually run checks, that could be slow. Do not mock |
| 16 | + # `Pylinter.check`: it calls `Pylinter.initialize` which is |
| 17 | + # needed to properly set up messages inclusion/exclusion |
| 18 | + # in `_msg_states`, used by `is_message_enabled`. |
| 19 | + with unittest.mock.patch("pylint.lint.pylinter.check_parallel"): |
| 20 | + runner = pylint.lint.Run(args) |
| 21 | + |
| 22 | + # "logging-not-lazy" and "logging-format-interpolation" |
| 23 | + expected_disabled = {"W1201", "W1202"} |
| 24 | + for msgid in expected_disabled: |
| 25 | + assert not runner.linter.is_message_enabled(msgid) |
| 26 | + assert runner.linter.config.jobs == 10 |
| 27 | + assert runner.linter.config.reports |
| 28 | + |
| 29 | + mocked_exit.assert_called_once_with(0) |
| 30 | + return runner |
| 31 | + |
| 32 | + |
| 33 | +def test_can_read_ini(tmp_path): |
| 34 | + # Check that we can read the "regular" INI .pylintrc file |
| 35 | + config_file = tmp_path / ".pylintrc" |
8 | 36 | config_file.write_text(
|
9 |
| - "[tool.pylint.'messages control']\n" |
10 |
| - "disable='all'\n" |
11 |
| - "enable='missing-module-docstring'\n" |
12 |
| - "jobs=10\n" |
| 37 | + """ |
| 38 | +[messages control] |
| 39 | +disable = logging-not-lazy,logging-format-interpolation |
| 40 | +jobs = 10 |
| 41 | +reports = yes |
| 42 | +""" |
13 | 43 | )
|
14 |
| - linter = pylint.lint.PyLinter() |
15 |
| - linter.global_set_option = unittest.mock.MagicMock() |
16 |
| - linter.read_config_file(str(config_file)) |
17 |
| - |
18 |
| - assert linter.global_set_option.called_with("disable", "all") |
19 |
| - assert linter.global_set_option.called_with("enable", "missing-module-docstring") |
20 |
| - assert linter.global_set_option.called_with("jobs", 10) |
| 44 | + check_configuration_file_reader(config_file) |
21 | 45 |
|
22 | 46 |
|
23 | 47 | def test_can_read_setup_cfg(tmp_path):
|
| 48 | + # Check that we can read a setup.cfg (which is an INI file where |
| 49 | + # section names are prefixed with "pylint." |
24 | 50 | config_file = tmp_path / "setup.cfg"
|
25 | 51 | config_file.write_text(
|
26 |
| - "[pylint.messages control]\n" |
27 |
| - "disable=all\n" |
28 |
| - "enable=missing-module-docstring\n" |
29 |
| - "jobs=10\n" |
| 52 | + """ |
| 53 | +[pylint.messages control] |
| 54 | +disable = logging-not-lazy,logging-format-interpolation |
| 55 | +jobs = 10 |
| 56 | +reports = yes |
| 57 | +""" |
30 | 58 | )
|
31 |
| - linter = pylint.lint.PyLinter() |
32 |
| - linter.global_set_option = unittest.mock.MagicMock() |
33 |
| - linter.read_config_file(str(config_file)) |
| 59 | + check_configuration_file_reader(config_file) |
34 | 60 |
|
35 |
| - assert linter.global_set_option.called_with("disable", "all") |
36 |
| - assert linter.global_set_option.called_with("enable", "missing-module-docstring") |
37 |
| - assert linter.global_set_option.called_with("jobs", 10) |
| 61 | + |
| 62 | +def test_can_read_toml(tmp_path): |
| 63 | + # Check that we can read a TOML file where lists and integers are |
| 64 | + # expressed as strings. |
| 65 | + config_file = tmp_path / "pyproject.toml" |
| 66 | + config_file.write_text( |
| 67 | + """ |
| 68 | +[tool.pylint."messages control"] |
| 69 | +disable = "logging-not-lazy,logging-format-interpolation" |
| 70 | +jobs = "10" |
| 71 | +reports = "yes" |
| 72 | +""" |
| 73 | + ) |
| 74 | + check_configuration_file_reader(config_file) |
| 75 | + |
| 76 | + |
| 77 | +def test_can_read_toml_rich_types(tmp_path): |
| 78 | + # Check that we can read a TOML file where lists, integers and |
| 79 | + # booleans are expressed as such (and not as strings), using TOML |
| 80 | + # type system. |
| 81 | + config_file = tmp_path / "pyproject.toml" |
| 82 | + config_file.write_text( |
| 83 | + """ |
| 84 | +[tool.pylint."messages control"] |
| 85 | +disable = [ |
| 86 | + "logging-not-lazy", |
| 87 | + "logging-format-interpolation", |
| 88 | +] |
| 89 | +jobs = 10 |
| 90 | +reports = true |
| 91 | +""" |
| 92 | + ) |
| 93 | + check_configuration_file_reader(config_file) |
0 commit comments