Skip to content

Commit 71d2be2

Browse files
committed
patchtest2: add logging support
This adds two mutually-exclusive flags: - --log-results, for writing the same results as seen in STDOUT to file - --log-json, for printing the Patchtest class' results member to file in json format Also do a bit of reformatting and cleanup using black. Signed-off-by: Trevor Gamblin <[email protected]>
1 parent 8823a51 commit 71d2be2

File tree

4 files changed

+56
-4
lines changed

4 files changed

+56
-4
lines changed

src/patchtest2/parser.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,19 @@ def get_parser(cls):
2323
parser = argparse.ArgumentParser()
2424

2525
target_patch_group = parser.add_mutually_exclusive_group(required=True)
26+
log_type_group = parser.add_mutually_exclusive_group(required=False)
2627

2728
target_patch_group.add_argument(
28-
"--patch", metavar="PATCH", dest="patch_path", help="The patch to be tested"
29+
"--patch",
30+
action="store",
31+
metavar="PATCH",
32+
dest="patch_path",
33+
help="The patch to be tested",
2934
)
3035

3136
target_patch_group.add_argument(
3237
"--directory",
38+
action="store",
3339
metavar="DIRECTORY",
3440
dest="patch_path",
3541
help="The directory containing patches to be tested",
@@ -60,10 +66,18 @@ def get_parser(cls):
6066
"--debug", "-d", action="store_true", help="Enable debug output"
6167
)
6268

63-
parser.add_argument(
69+
log_type_group.add_argument(
6470
"--log-results",
71+
dest="log_results",
6572
action="store_true",
6673
help='Enable logging to a file matching the target patch name with ".testresult" appended',
6774
)
6875

76+
log_type_group.add_argument(
77+
"--log-json",
78+
dest="log_json",
79+
action="store_true",
80+
help='Enable logging to a file matching the target patch name with ".testresult" appended, in json format',
81+
)
82+
6983
return parser

src/patchtest2/tests/core.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import unidiff
55
from patchtest2.tests.results import patchtest_result
66

7+
78
@patchtest_result
89
def test_mbox_has_signed_off_by(target):
910
result = "FAIL"
@@ -12,6 +13,7 @@ def test_mbox_has_signed_off_by(target):
1213
reason = "mbox was missing a signed-off-by tag"
1314
return target.subject, result, reason
1415

16+
1517
@patchtest_result
1618
def test_mbox_shortlog_format(target):
1719
result = "PASS"
@@ -32,6 +34,7 @@ def test_mbox_shortlog_format(target):
3234

3335
return target.subject, result, reason
3436

37+
3538
@patchtest_result
3639
def test_mbox_shortlog_length(target):
3740
shortlog = re.sub("^(\[.*?\])+ ", "", target.shortlog)
@@ -48,6 +51,7 @@ def test_mbox_shortlog_length(target):
4851

4952
return target.subject, result, reason
5053

54+
5155
@patchtest_result
5256
def test_mbox_has_commit_message(target):
5357
result = "PASS"
@@ -66,6 +70,7 @@ def test_mbox_has_commit_message(target):
6670

6771
return target.subject, result, reason
6872

73+
6974
@patchtest_result
7075
def test_mbox_unidiff_parse_error(target):
7176
result = "PASS"

src/patchtest2/tests/patchtest.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
import inspect
2+
import json
3+
import os
24
import patchtest2.tests.core as core
35
from patchtest2.parser import PatchtestParser
46
from patchtest2.mbox import PatchSeries, TargetRepo
57

8+
69
class Patchtest:
710
def __init__(self, target_repo, series):
811
self.target_repo = target_repo
912
self.series = series
10-
self.core_results = {k: self._results(v) for (k, v) in inspect.getmembers(core, inspect.isfunction) if k != "patchtest_result"}
13+
self.core_results = {
14+
k: self._results(v)
15+
for (k, v) in inspect.getmembers(core, inspect.isfunction)
16+
if k != "patchtest_result"
17+
}
1118

1219
self.results = dict(
1320
[
@@ -33,6 +40,26 @@ def print_results(self):
3340
for category in self.results.keys():
3441
self._print_results(category)
3542

43+
def _log_results(self, logfile):
44+
result_str = ""
45+
for category in self.results.keys():
46+
for tag in self.results[category].keys():
47+
for value in self.results[category][tag]:
48+
result_str += value + "\n"
49+
50+
with open(logfile + ".testresult", "w") as f:
51+
f.write(result_str)
52+
53+
def _log_json(self, logfile):
54+
with open(logfile + ".testresult", "w") as f:
55+
f.write(json.dumps(self.results, indent=4, sort_keys=True))
56+
57+
def log_results(self, logfile, mode=None):
58+
if mode == "json":
59+
self._log_json(logfile)
60+
else:
61+
self._log_results(logfile)
62+
3663

3764
def run():
3865
parser = PatchtestParser.get_parser()
@@ -42,3 +69,8 @@ def run():
4269
results = Patchtest(target_repo, series)
4370

4471
results.print_results()
72+
if args.log_json:
73+
results.log_results(os.path.basename(args.patch_path), mode="json")
74+
75+
if args.log_results:
76+
results.log_results(os.path.basename(args.patch_path))

src/patchtest2/tests/results.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import inspect
22
import functools
33

4+
45
def patchtest_result(func):
56
@functools.wraps(func)
67
def wrapper(*args, **kwargs):
@@ -9,5 +10,5 @@ def wrapper(*args, **kwargs):
910
return f"{result}: {func.__name__} on {subject}"
1011
else:
1112
return f"{result}: {func.__name__} on {subject} ({reason})"
12-
return wrapper
1313

14+
return wrapper

0 commit comments

Comments
 (0)