|
6 | 6 |
|
7 | 7 | import os
|
8 | 8 | import os.path
|
| 9 | +from argparse import Namespace |
9 | 10 | from io import StringIO
|
10 | 11 | from pathlib import Path
|
| 12 | +from typing import Any |
| 13 | +from unittest.mock import patch |
11 | 14 |
|
12 | 15 | import pytest
|
13 | 16 | from pytest import CaptureFixture
|
14 | 17 |
|
15 | 18 | from pylint.lint import Run as LintRun
|
| 19 | +from pylint.lint.pylinter import PyLinter |
16 | 20 | from pylint.testutils._run import _Run as Run
|
17 | 21 | from pylint.testutils.utils import _patch_streams, _test_cwd
|
18 | 22 |
|
@@ -52,7 +56,7 @@ def _create_subconfig_test_fs(tmp_path: Path) -> tuple[Path, ...]:
|
52 | 56 | level1_init.touch()
|
53 | 57 | level1_init2.touch()
|
54 | 58 | level2_init.touch()
|
55 |
| - test_file_text = "#LEVEL1\n#LEVEL2\n#ALL_LEVELS\n#TODO\n" |
| 59 | + test_file_text = "#LEVEL1\n#LEVEL2\n#ALL_LEVELS\n#TODO\nassert (1, None)\ns = 'statement without warnings'\n" |
56 | 60 | test_file1.write_text(test_file_text)
|
57 | 61 | test_file2.write_text(test_file_text)
|
58 | 62 | test_file3.write_text(test_file_text)
|
@@ -250,3 +254,54 @@ def test_local_config_verbose(
|
250 | 254 | LintRun(["--verbose", "--use-local-configs=y", str(tmp_files[1])], exit=False)
|
251 | 255 | output = capsys.readouterr()
|
252 | 256 | assert f"Using config from {level1_dir / 'sub'}" in output.err
|
| 257 | + |
| 258 | + |
| 259 | +def ns_diff(ns1: Namespace, ns2: Namespace) -> str: |
| 260 | + msg = "Namespaces not equal\n" |
| 261 | + for k, v in ns1.__dict__.items(): |
| 262 | + if v != ns2.__dict__[k]: |
| 263 | + msg += f"{v} != {ns2.__dict__[k]}\n" |
| 264 | + return msg |
| 265 | + |
| 266 | + |
| 267 | +generate_reports_orig = PyLinter.generate_reports |
| 268 | + |
| 269 | + |
| 270 | +def generate_reports_spy(self: PyLinter, *args: Any, **kwargs: Any) -> int: |
| 271 | + score = generate_reports_orig(self, *args, **kwargs) |
| 272 | + # check that generate_reports() worked with base config, not config from most recent module |
| 273 | + assert self.config == self._base_config, ns_diff(self.config, self._base_config) |
| 274 | + # level1_dir.a, level1_dir.z, level1_dir.sub.b from _create_subconfig_test_fs |
| 275 | + # each has 2 statements, one of which is warning => score should be 5 |
| 276 | + assert score is not None |
| 277 | + assert 0 < score < 10 |
| 278 | + return score |
| 279 | + |
| 280 | + |
| 281 | +@pytest.mark.parametrize( |
| 282 | + "local_config_args", |
| 283 | + [["--use-local-configs=y"], ["--use-local-configs=y", "--jobs=2"]], |
| 284 | +) |
| 285 | +def test_subconfigs_score( |
| 286 | + _create_subconfig_test_fs: tuple[Path, ...], |
| 287 | + local_config_args: list[str], |
| 288 | +) -> None: |
| 289 | + """Check that statements from all checked modules are accounted in score: |
| 290 | + given stats from many modules such that |
| 291 | + total # of messages > statements in last module, |
| 292 | + check that score is >0 and <10. |
| 293 | + """ |
| 294 | + level1_dir, *_ = _create_subconfig_test_fs |
| 295 | + out = StringIO() |
| 296 | + with patch( |
| 297 | + "pylint.lint.run.Run.LinterClass.generate_reports", |
| 298 | + side_effect=generate_reports_spy, |
| 299 | + autospec=True, |
| 300 | + ) as reports_patch, _patch_streams(out): |
| 301 | + linter = LintRun([*local_config_args, str(level1_dir)], exit=False).linter |
| 302 | + reports_patch.assert_called_once() |
| 303 | + |
| 304 | + # level1_dir.a, level1_dir.z, level1_dir.sub.b from _create_subconfig_test_fs |
| 305 | + # each has 2 statements, one of which is warning, so 3 warnings total |
| 306 | + assert linter.stats.statement == 6 |
| 307 | + assert linter.stats.warning == 3 |
0 commit comments