summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorDaniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>2021-10-07 09:40:31 +0200
committerGitHub <noreply@github.com>2021-10-07 09:40:31 +0200
commit16c09cb48a3c66f453faccdf380aea6969d61202 (patch)
tree9cd442a7c64913f9815f6bf140ed39d0e24ac5fd /tests
parentd98c29d888fbfd11aa42da593c6cfc6812434ee0 (diff)
downloadpylint-git-16c09cb48a3c66f453faccdf380aea6969d61202.tar.gz
Refactor ``LinterStats`` (#5074)
* Refactor ``self.stats`` on linter and checker This adds a new class ``LinterStats`` which is used to store all kinds of stats during a run of ``pylint``. Tests have been changed slightly to be able to use the new class. Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/lint/unittest_lint.py6
-rw-r--r--tests/test_check_parallel.py251
-rw-r--r--tests/test_import_graph.py4
-rw-r--r--tests/test_regr.py6
4 files changed, 128 insertions, 139 deletions
diff --git a/tests/lint/unittest_lint.py b/tests/lint/unittest_lint.py
index 277799676..79437e987 100644
--- a/tests/lint/unittest_lint.py
+++ b/tests/lint/unittest_lint.py
@@ -48,7 +48,7 @@ from io import StringIO
from os import chdir, getcwd
from os.path import abspath, basename, dirname, isdir, join, sep
from shutil import rmtree
-from typing import Dict, Iterable, Iterator, List, Optional, Tuple
+from typing import Iterable, Iterator, List, Optional, Tuple
import platformdirs
import pytest
@@ -870,7 +870,7 @@ def test_by_module_statement_value(init_linter: PyLinter) -> None:
linter = init_linter
linter.check([os.path.join(os.path.dirname(__file__), "data")])
- by_module_stats: Dict[str, Dict[str, int]] = linter.stats["by_module"] # type: ignore
+ by_module_stats = linter.stats.by_module
for module, module_stats in by_module_stats.items():
linter2 = init_linter
@@ -881,4 +881,4 @@ def test_by_module_statement_value(init_linter: PyLinter) -> None:
# Check that the by_module "statement" is equal to the global "statement"
# computed for that module
- assert module_stats["statement"] == linter2.stats["statement"]
+ assert module_stats["statement"] == linter2.stats.statement
diff --git a/tests/test_check_parallel.py b/tests/test_check_parallel.py
index c0fbe00fd..be250d892 100644
--- a/tests/test_check_parallel.py
+++ b/tests/test_check_parallel.py
@@ -10,7 +10,6 @@
# pylint: disable=protected-access,missing-function-docstring,no-self-use
-import collections
import os
from typing import List
@@ -25,7 +24,8 @@ from pylint.lint.parallel import _worker_check_single_file as worker_check_singl
from pylint.lint.parallel import _worker_initialize as worker_initialize
from pylint.lint.parallel import check_parallel
from pylint.testutils import GenericTestReporter as Reporter
-from pylint.typing import CheckerStats, FileItem
+from pylint.typing import FileItem
+from pylint.utils import LinterStats, ModuleStats
def _gen_file_data(idx: int = 0) -> FileItem:
@@ -103,11 +103,10 @@ class ParallelTestChecker(BaseChecker):
super().__init__(linter)
self.data: List[str] = []
self.linter = linter
- self.stats: CheckerStats = {}
def open(self) -> None:
"""init the checkers: reset statistics information"""
- self.stats = self.linter.add_stats()
+ self.linter.stats.reset_node_count()
self.data = []
def close(self) -> None:
@@ -202,26 +201,24 @@ class TestCheckParallelFramework:
no_errors_status = 0
assert no_errors_status == msg_status
assert {
- "by_module": {
- "--test-file_data-name-0--": {
- "convention": 0,
- "error": 0,
- "fatal": 0,
- "info": 0,
- "refactor": 0,
- "statement": 18,
- "warning": 0,
- }
- },
- "by_msg": {},
- "convention": 0,
- "error": 0,
- "fatal": 0,
- "info": 0,
- "refactor": 0,
- "statement": 18,
- "warning": 0,
- } == stats
+ "--test-file_data-name-0--": {
+ "convention": 0,
+ "error": 0,
+ "fatal": 0,
+ "info": 0,
+ "refactor": 0,
+ "statement": 18,
+ "warning": 0,
+ }
+ } == stats.by_module
+ assert {} == stats.by_msg
+ assert stats.convention == 0
+ assert stats.error == 0
+ assert stats.fatal == 0
+ assert stats.info == 0
+ assert stats.refactor == 0
+ assert stats.statement == 18
+ assert stats.warning == 0
def test_worker_check_sequential_checker(self) -> None:
"""Same as test_worker_check_single_file_no_checkers with SequentialTestChecker"""
@@ -248,26 +245,24 @@ class TestCheckParallelFramework:
no_errors_status = 0
assert no_errors_status == msg_status
assert {
- "by_module": {
- "--test-file_data-name-0--": {
- "convention": 0,
- "error": 0,
- "fatal": 0,
- "info": 0,
- "refactor": 0,
- "statement": 18,
- "warning": 0,
- }
- },
- "by_msg": {},
- "convention": 0,
- "error": 0,
- "fatal": 0,
- "info": 0,
- "refactor": 0,
- "statement": 18,
- "warning": 0,
- } == stats
+ "--test-file_data-name-0--": {
+ "convention": 0,
+ "error": 0,
+ "fatal": 0,
+ "info": 0,
+ "refactor": 0,
+ "statement": 18,
+ "warning": 0,
+ }
+ } == stats.by_module
+ assert {} == stats.by_msg
+ assert stats.convention == 0
+ assert stats.error == 0
+ assert stats.fatal == 0
+ assert stats.info == 0
+ assert stats.refactor == 0
+ assert stats.statement == 18
+ assert stats.warning == 0
class TestCheckParallel:
@@ -299,52 +294,48 @@ class TestCheckParallel:
"checkers registered"
)
assert {
- "by_module": {
- "--test-file_data-name-0--": {
- "convention": 0,
- "error": 0,
- "fatal": 0,
- "info": 0,
- "refactor": 0,
- "statement": 18,
- "warning": 0,
- }
- },
- "by_msg": {},
- "convention": 0,
- "error": 0,
- "fatal": 0,
- "info": 0,
- "refactor": 0,
- "statement": 18,
- "warning": 0,
- } == linter.stats
+ "--test-file_data-name-0--": {
+ "convention": 0,
+ "error": 0,
+ "fatal": 0,
+ "info": 0,
+ "refactor": 0,
+ "statement": 18,
+ "warning": 0,
+ }
+ } == linter.stats.by_module
+ assert linter.stats.by_msg == {}
+ assert linter.stats.convention == 0
+ assert linter.stats.error == 0
+ assert linter.stats.fatal == 0
+ assert linter.stats.info == 0
+ assert linter.stats.refactor == 0
+ assert linter.stats.statement == 18
+ assert linter.stats.warning == 0
# now run the regular mode of checking files and check that, in this proc, we
# collect the right data
filepath = [single_file_container[0][1]] # get the filepath element
linter.check(filepath)
assert {
- "by_module": {
- "input.similar1": { # module is the only change from previous
- "convention": 0,
- "error": 0,
- "fatal": 0,
- "info": 0,
- "refactor": 0,
- "statement": 18,
- "warning": 0,
- }
- },
- "by_msg": {},
- "convention": 0,
- "error": 0,
- "fatal": 0,
- "info": 0,
- "refactor": 0,
- "statement": 18,
- "warning": 0,
- } == linter.stats
+ "input.similar1": { # module is the only change from previous
+ "convention": 0,
+ "error": 0,
+ "fatal": 0,
+ "info": 0,
+ "refactor": 0,
+ "statement": 18,
+ "warning": 0,
+ }
+ } == linter.stats.by_module
+ assert linter.stats.by_msg == {}
+ assert linter.stats.convention == 0
+ assert linter.stats.error == 0
+ assert linter.stats.fatal == 0
+ assert linter.stats.info == 0
+ assert linter.stats.refactor == 0
+ assert linter.stats.statement == 18
+ assert linter.stats.warning == 0
def test_invoke_single_job(self) -> None:
"""Tests basic checkers functionality using just a single workderdo
@@ -365,26 +356,24 @@ class TestCheckParallel:
)
assert {
- "by_module": {
- "--test-file_data-name-0--": {
- "convention": 0,
- "error": 0,
- "fatal": 0,
- "info": 0,
- "refactor": 0,
- "statement": 18,
- "warning": 0,
- }
- },
- "by_msg": collections.Counter(),
- "convention": 0,
- "error": 0,
- "fatal": 0,
- "info": 0,
- "refactor": 0,
- "statement": 18,
- "warning": 0,
- } == linter.stats
+ "--test-file_data-name-0--": {
+ "convention": 0,
+ "error": 0,
+ "fatal": 0,
+ "info": 0,
+ "refactor": 0,
+ "statement": 18,
+ "warning": 0,
+ }
+ } == linter.stats.by_module
+ assert linter.stats.by_msg == {}
+ assert linter.stats.convention == 0
+ assert linter.stats.error == 0
+ assert linter.stats.fatal == 0
+ assert linter.stats.info == 0
+ assert linter.stats.refactor == 0
+ assert linter.stats.statement == 18
+ assert linter.stats.warning == 0
assert linter.msg_status == 0, "We expect a single-file check to exit cleanly"
@pytest.mark.parametrize(
@@ -425,30 +414,30 @@ class TestCheckParallel:
# define the stats we expect to get back from the runs, these should only vary
# with the number of files.
- expected_stats = {
- "by_module": {
+ expected_stats = LinterStats(
+ by_module={
# pylint: disable-next=consider-using-f-string
"--test-file_data-name-%d--"
- % idx: {
- "convention": 0,
- "error": 0,
- "fatal": 0,
- "info": 0,
- "refactor": 0,
- "statement": 18,
- "warning": 0,
- }
+ % idx: ModuleStats(
+ convention=0,
+ error=0,
+ fatal=0,
+ info=0,
+ refactor=0,
+ statement=18,
+ warning=0,
+ )
for idx in range(num_files)
- },
- "by_msg": {},
- "convention": 0,
- "error": 0,
- "fatal": 0,
- "info": 0,
- "refactor": 0,
- "statement": 18 * num_files,
- "warning": 0,
- }
+ }
+ )
+ expected_stats.by_msg = {}
+ expected_stats.convention = 0
+ expected_stats.error = 0
+ expected_stats.fatal = 0
+ expected_stats.info = 0
+ expected_stats.refactor = 0
+ expected_stats.statement = 18 * num_files
+ expected_stats.warning = 0
file_infos = _gen_file_datas(num_files)
@@ -482,11 +471,11 @@ class TestCheckParallel:
stats_check_parallel = linter.stats
assert linter.msg_status == 0, "We should not fail the lint"
- assert (
- stats_single_proc == stats_check_parallel
+ assert str(stats_single_proc) == str(
+ stats_check_parallel
), "Single-proc and check_parallel() should return the same thing"
- assert (
- stats_check_parallel == expected_stats
+ assert str(stats_check_parallel) == str(
+ expected_stats
), "The lint is returning unexpected results, has something changed?"
@pytest.mark.parametrize(
@@ -551,6 +540,6 @@ class TestCheckParallel:
arguments=None,
)
stats_check_parallel = linter.stats
- assert (
- stats_single_proc["by_msg"] == stats_check_parallel["by_msg"]
+ assert str(stats_single_proc.by_msg) == str(
+ stats_check_parallel.by_msg
), "Single-proc and check_parallel() should return the same thing"
diff --git a/tests/test_import_graph.py b/tests/test_import_graph.py
index 42e3baf56..6035601bf 100644
--- a/tests/test_import_graph.py
+++ b/tests/test_import_graph.py
@@ -48,7 +48,7 @@ POSSIBLE_DOT_FILENAMES = ["foo.dot", "foo.gv", "tests/regrtest_data/foo.dot"]
@pytest.mark.parametrize("dest", POSSIBLE_DOT_FILENAMES, indirect=True)
def test_dependencies_graph(dest: str) -> None:
"""DOC files are correctly generated, and the graphname is the basename"""
- imports._dependencies_graph(dest, {"labas": ["hoho", "yep"], "hoho": ["yep"]})
+ imports._dependencies_graph(dest, {"labas": {"hoho", "yep"}, "hoho": {"yep"}})
with open(dest, encoding="utf-8") as stream:
assert (
stream.read().strip()
@@ -75,7 +75,7 @@ URL="." node[shape="box"]
def test_missing_graphviz(filename: str) -> None:
"""Raises if graphviz is not installed, and defaults to png if no extension given"""
with pytest.raises(RuntimeError, match=r"Cannot generate `graph\.png`.*"):
- imports._dependencies_graph(filename, {"a": ["b", "c"], "b": ["c"]})
+ imports._dependencies_graph(filename, {"a": {"b", "c"}, "b": {"c"}})
@pytest.fixture
diff --git a/tests/test_regr.py b/tests/test_regr.py
index 07385a64d..6dadcf2c6 100644
--- a/tests/test_regr.py
+++ b/tests/test_regr.py
@@ -114,12 +114,12 @@ def modify_path() -> Iterator:
def test_check_package___init__(finalize_linter: PyLinter) -> None:
filename = ["package.__init__"]
finalize_linter.check(filename)
- checked = list(finalize_linter.stats["by_module"].keys()) # type: ignore # Refactor of PyLinter.stats necessary
- assert checked == filename
+ checked = list(finalize_linter.stats.by_module.keys())
+ assert sorted(checked) == sorted(filename)
os.chdir(join(REGR_DATA, "package"))
finalize_linter.check(["__init__"])
- checked = list(finalize_linter.stats["by_module"].keys()) # type: ignore
+ checked = list(finalize_linter.stats.by_module.keys())
assert checked == ["__init__"]