11"""Checks/Validations section."""
22
3+ import datetime
34from pathlib import Path
45
56import tomllib
@@ -22,17 +23,25 @@ def __init__(self, toml_filename: str = None, resources_dir: str = None):
2223 data = tomllib .load (f )
2324 self ._data = data
2425
25- def check (self , checkid ):
26+ def checkup (self , checkup_id ):
2627 """Given an ID for a check, the details"""
27- return self ._data [checkid ]
28+ return self ._data [checkup_id ]
29+
30+ def id_by_pytest_node (self , node_id ):
31+ """Given a PyTest node ID, find the test ID"""
32+ for id_ , checkup in self ._data .items ():
33+ if not isinstance (checkup , dict ):
34+ continue
35+ if checkup .get ("checker" ) == node_id :
36+ return id_
37+ raise AttributeError (f"nodeid { node_id } not found" )
2838
2939
3040class CheckData (JsonSerializable ):
3141 """
3242 The validation data related to a project
3343 """
3444
35- dict_keys = ["id" , "xfailed" , "since" , "details" ]
3645 labels_toml = ChecksToml ()
3746
3847 def __init__ (self , id_ : str , xfailed = None , since = None , details = None , ** _ ):
@@ -42,20 +51,43 @@ def __init__(self, id_: str, xfailed=None, since=None, details=None, **_):
4251 self .details = details
4352
4453 def to_dict (self ) -> dict :
45- ret = {}
46- if self .xfailed :
47- ret ["xfailed" ] = self .xfailed
48- if self .since :
49- ret ["since" ] = self .since
50- if self .details :
51- ret ["details" ] = self .details
52-
54+ ret = super ().to_dict ()
55+ del ret ["id" ]
56+ ret ["importance" ] = self .importance
5357 return ret
5458
59+ @property
60+ def importance (self ):
61+ """get the importance of the check"""
62+ if "importance" in self .labels_toml .checkup (self .id ):
63+ return self .labels_toml .checkup (self .id )["importance" ]
64+ return None
65+
66+ def __repr__ (self ):
67+ return str (self .to_dict ())
68+
5569 def __getattr__ (self , name ):
56- return self .labels_toml .check (self .id )[name ]
70+ return self .labels_toml .checkup (self .id )[name ]
5771
58- def importance (self ):
72+ @classmethod
73+ def from_report (cls , pytest_report ):
74+ """creates a CheckData instance based on a PyTest report"""
75+ assertion_msg = (
76+ pytest_report .longreprtext .partition ("\n " )[0 ].split (":" , 1 )[1 ].strip ()
77+ )
78+ test_id = cls .labels_toml .id_by_pytest_node (pytest_report .nodeid )
79+ if hasattr (pytest_report , "wasxfail" ) and pytest_report .wasxfail :
80+ return CheckData (
81+ test_id , details = assertion_msg , xfailed = pytest_report .wasxfail
82+ )
83+ since = (
84+ pytest_report .previously_failed .kwargs ["since" ]
85+ if hasattr (pytest_report , "previously_failed" )
86+ else datetime .datetime .today ()
87+ )
88+ return CheckData (test_id , details = assertion_msg , since = since )
89+
90+ def importances (self ):
5991 """Returns dict name->description with the possible importance values"""
6092 return {i ["name" ]: i ["description" ] for i in self .labels_toml ["importance" ]}
6193
0 commit comments