22import pyparsing
33import re
44import unidiff
5+ from patchtest2 .tests .results import patchtest_result
56
6-
7- class PatchtestResult :
8- def __init__ (self , patch , testname , result , reason ):
9- self .patch = patch
10- self .testname = testname
11- self .result = result
12- self .reason = reason
13- self .pass_string = f"{ self .result } : { self .testname } on { self .patch } "
14- self .skip_or_fail_string = (
15- f"{ self .result } : { self .testname } on { self .patch } ({ self .reason } )"
16- )
17-
18- def __str__ (self ):
19- if self .result == "PASS" :
20- return self .pass_string
21- else :
22- return self .skip_or_fail_string
23-
24-
25- class PatchtestResults :
26- def __init__ (self , target_repo , series ):
27- self .target_repo = target_repo
28- self .series = series
29- self .mbox_results = dict (
30- [
31- (
32- "signed_off_by" ,
33- self ._results (test_mbox_has_signed_off_by ),
34- ),
35- (
36- "shortlog_format" ,
37- self ._results (test_mbox_shortlog_format ),
38- ),
39- (
40- "shortlog_length" ,
41- self ._results (test_mbox_shortlog_length ),
42- ),
43- (
44- "has_commit_message" ,
45- self ._results (test_mbox_has_commit_message ),
46- ),
47- (
48- "diff_parse" ,
49- self ._results (test_mbox_unidiff_parse_error ),
50- ),
51- ]
52- )
53-
54- self .results = dict (
55- [
56- (
57- "mbox" ,
58- self .mbox_results ,
59- ),
60- ]
61- )
62-
63- def _results (self , testname ):
64- return [testname (patch ) for patch in self .series .patchdata ]
65-
66- def _print_result (self , category , tag ):
67- for value in self .results [category ][tag ]:
68- print (value )
69-
70- def _print_results (self , category ):
71- for tag in self .results [category ].keys ():
72- self ._print_result (category , tag )
73-
74- def print_results (self ):
75- for category in self .results .keys ():
76- self ._print_results (category )
77-
78-
79- # test_for_pattern()
80- # @pattern: a pyparsing regex object
81- # @string: a string (patch subject, commit message, author, etc. to
82- # search for
83- def test_for_pattern (pattern , string ):
84- if pattern .search_string (string ):
85- return "PASS"
86- else :
87- return "FAIL"
88-
89-
7+ @patchtest_result
908def test_mbox_has_signed_off_by (target ):
91- test_name = "test_mbox_has_signed_off_by"
92- result = test_for_pattern (patterns .signed_off_by , target .commit_message )
9+ result = "FAIL"
10+ if patterns .signed_off_by .search_string (target .commit_message ):
11+ result = "PASS"
9312 reason = "mbox was missing a signed-off-by tag"
94- return PatchtestResult (target .subject , test_name , result , reason )
95-
13+ return target .subject , result , reason
9614
15+ @patchtest_result
9716def test_mbox_shortlog_format (target ):
98- test_name = "test_mbox_shortlog_format"
9917 result = "PASS"
10018 reason = None
10119 if not target .shortlog .strip ():
@@ -112,13 +30,12 @@ def test_mbox_shortlog_format(target):
11230 result = "FAIL"
11331 reason = 'Commit shortlog (first line of commit message) should follow the format "<target>: <summary>"'
11432
115- return PatchtestResult (target .subject , test_name , result , reason )
116-
33+ return target .subject , result , reason
11734
35+ @patchtest_result
11836def test_mbox_shortlog_length (target ):
11937 shortlog = re .sub ("^(\[.*?\])+ " , "" , target .shortlog )
12038 shortlog_len = len (shortlog )
121- test_name = "test_mbox_shortlog_length"
12239 result = "PASS"
12340 reason = f"Edit shortlog so that it is { patterns .mbox_shortlog_maxlength } characters or less (currently { shortlog_len } characters)"
12441
@@ -130,11 +47,10 @@ def test_mbox_shortlog_length(target):
13047 if shortlog_len > patterns .mbox_shortlog_maxlength :
13148 result = "FAIL"
13249
133- return PatchtestResult (target .subject , test_name , result , reason )
134-
50+ return target .subject , result , reason
13551
52+ @patchtest_result
13653def test_mbox_has_commit_message (target ):
137- test_name = "test_mbox_has_commit_message"
13854 result = "PASS"
13955 reason = "Please include a commit message on your patch explaining the change"
14056
@@ -149,11 +65,10 @@ def test_mbox_has_commit_message(target):
14965 if not commit_lines :
15066 result = "FAIL"
15167
152- return PatchtestResult (target .subject , test_name , result , reason )
153-
68+ return target .subject , result , reason
15469
70+ @patchtest_result
15571def test_mbox_unidiff_parse_error (target ):
156- test_name = "test_mbox_unidiff_parse_error"
15772 result = "PASS"
15873 reason = f'Patch "{ target .shortlog } " contains malformed diff lines.'
15974
@@ -162,4 +77,4 @@ def test_mbox_unidiff_parse_error(target):
16277 except unidiff .UnidiffParseError as upe :
16378 result = "FAIL"
16479
165- return PatchtestResult ( target .subject , test_name , result , reason )
80+ return target .subject , result , reason
0 commit comments