Coverage for src/pytest_gitlab_code_quality/plugin.py: 100%

18 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-03 22:19 +0200

1from pathlib import Path 

2from warnings import WarningMessage 

3 

4from pytest import Config 

5 

6from pytest_gitlab_code_quality.recorder import ViolationRecorder 

7from pytest_gitlab_code_quality.report import Lines, Location, Violation 

8 

9 

10class GitlabCodeQualityReportPlugin: 

11 """ 

12 Orchestrates the test warnings to be recorded. 

13 """ 

14 

15 def __init__( # type: ignore[ignoreMissingSuperCall] 

16 self, 

17 recorder: ViolationRecorder, 

18 root: Path, 

19 ) -> None: 

20 self._recorder = recorder 

21 self._root = root 

22 

23 def pytest_warning_recorded( 

24 self, 

25 warning_message: WarningMessage, 

26 when: str, 

27 nodeid: str, 

28 location: tuple[str, int, str] | None, 

29 ) -> None: 

30 path = warning_message.filename.replace(str(self._root), "") 

31 if path.startswith("/"): 

32 path = path.removeprefix("/") 

33 

34 # TODO: Utilize location 

35 message = warning_message.message 

36 

37 violation = Violation( 

38 description=str(message), 

39 check_name=f"Pytest{warning_message.category.__name__}", 

40 fingerprint=str(hash(f"{nodeid}::{warning_message.lineno}::{message}")), 

41 severity="minor", 

42 location=Location( 

43 path=path, 

44 lines=Lines( 

45 begin=warning_message.lineno, 

46 ), 

47 ), 

48 ) 

49 

50 self._recorder.record(violation) 

51 

52 def pytest_unconfigure(self, config: Config) -> None: 

53 self._recorder.close()