Skip to content

Commit

Permalink
Minor tests/base_tester.py improvements
Browse files Browse the repository at this point in the history
* `IMPACTED_CHECKER_CLASSES` is a `type[BaseChecker]`,
  not a `BaseChecker` (we are instantiating it)
* Replace `sys._getframe` "implementation detail"
  with a standard `pytest` autouse `fixture`
* Make sure our test corpus does not raise any warnings
  (Deus Ex Machina from the future)

Signed-off-by: Stavros Ntentos <[email protected]>
  • Loading branch information
stdedos committed Jul 13, 2024
1 parent 9e899ef commit c6279d3
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions tests/base_tester.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from __future__ import annotations

import logging
import os
import sys
from abc import ABC
from pathlib import Path
from pprint import pprint

import astroid
import pytest
from pylint.checkers import BaseChecker
from pylint.testutils import MessageTest, UnittestLinter
from pylint.utils import ASTWalker
Expand All @@ -28,28 +29,41 @@ class BasePytestTester(ABC):
MSG_ID: str
msgs: list[MessageTest] = []

# Set by ``test_name_fixture``
test_name: str = ""

@pytest.fixture(autouse=True)
def test_name_fixture(self, request):
self.test_name = request.node.originalname.replace("test_", "", 1)

def __init_subclass__(cls, **kwargs):
super().__init_subclass__(**kwargs)
if not hasattr(cls, "MSG_ID") or not isinstance(cls.MSG_ID, str) or not cls.MSG_ID:
raise TypeError("Subclasses must define a non-empty MSG_ID of type str")

enable_plugin = True

@pytest.fixture(autouse=True)
def with_no_warnings(self, caplog): # pylint: disable=no-self-use
with caplog.at_level(logging.WARNING):
yield

records = caplog.get_records("call")
assert not records, records

def run_linter(self, enable_plugin):
self.enable_plugin = enable_plugin

# pylint: disable-next=protected-access
target_test_file = sys._getframe(1).f_code.co_name.replace("test_", "", 1)
file_path = os.path.join(
get_test_root_path(),
"input",
self.MSG_ID,
target_test_file + ".py",
self.test_name + ".py",
)

with open(file_path) as fin:
content = fin.read()
module = astroid.parse(content, module_name=target_test_file)
module = astroid.parse(content, module_name=self.test_name)
module.file = fin.name

self.walk(module) # run all checkers
Expand Down

0 comments on commit c6279d3

Please sign in to comment.