Skip to content

Commit 8104405

Browse files
committed
patchtest2: decorator and module refactor
- Make core.py exclusive to "core" (i.e. mbox) tests - Use new patchtest_result decorator in core.py - Move PatchtestResults class to patchtest.py, rename to "Patchtest" - Use inspect module to dynamically get test function handles and names from core.py Signed-off-by: Trevor Gamblin <[email protected]>
1 parent e933d82 commit 8104405

File tree

2 files changed

+47
-101
lines changed

2 files changed

+47
-101
lines changed

src/patchtest2/tests/core.py

Lines changed: 14 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -2,100 +2,18 @@
22
import pyparsing
33
import re
44
import 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
908
def 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
9716
def 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
11836
def 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
13653
def 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
15571
def 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

src/patchtest2/tests/patchtest.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,44 @@
1+
import inspect
2+
import patchtest2.tests.core as core
13
from patchtest2.parser import PatchtestParser
24
from patchtest2.mbox import PatchSeries, TargetRepo
3-
from patchtest2.tests.core import PatchtestResults
5+
6+
class Patchtest:
7+
def __init__(self, target_repo, series):
8+
self.target_repo = target_repo
9+
self.series = series
10+
self.core_results = {k: self._results(v) for (k, v) in inspect.getmembers(core, inspect.isfunction) if k != "patchtest_result"}
11+
12+
self.results = dict(
13+
[
14+
(
15+
"core",
16+
self.core_results,
17+
),
18+
]
19+
)
20+
21+
def _results(self, testname):
22+
return [testname(patch) for patch in self.series.patchdata]
23+
24+
def _print_result(self, category, tag):
25+
for value in self.results[category][tag]:
26+
print(value)
27+
28+
def _print_results(self, category):
29+
for tag in self.results[category].keys():
30+
self._print_result(category, tag)
31+
32+
def print_results(self):
33+
for category in self.results.keys():
34+
self._print_results(category)
435

536

637
def run():
738
parser = PatchtestParser.get_parser()
839
args = parser.parse_args()
940
target_repo = TargetRepo(args.repodir)
1041
series = PatchSeries(args.patch_path)
11-
results = PatchtestResults(target_repo, series)
42+
results = Patchtest(target_repo, series)
1243

1344
results.print_results()

0 commit comments

Comments
 (0)