Skip to content

Commit

Permalink
patchtest2: decorator and module refactor
Browse files Browse the repository at this point in the history
- 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]>
  • Loading branch information
threexc committed Feb 21, 2025
1 parent e933d82 commit 8104405
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 101 deletions.
113 changes: 14 additions & 99 deletions src/patchtest2/tests/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,100 +2,18 @@
import pyparsing
import re
import unidiff
from patchtest2.tests.results import patchtest_result


class PatchtestResult:
def __init__(self, patch, testname, result, reason):
self.patch = patch
self.testname = testname
self.result = result
self.reason = reason
self.pass_string = f"{self.result}: {self.testname} on {self.patch}"
self.skip_or_fail_string = (
f"{self.result}: {self.testname} on {self.patch} ({self.reason})"
)

def __str__(self):
if self.result == "PASS":
return self.pass_string
else:
return self.skip_or_fail_string


class PatchtestResults:
def __init__(self, target_repo, series):
self.target_repo = target_repo
self.series = series
self.mbox_results = dict(
[
(
"signed_off_by",
self._results(test_mbox_has_signed_off_by),
),
(
"shortlog_format",
self._results(test_mbox_shortlog_format),
),
(
"shortlog_length",
self._results(test_mbox_shortlog_length),
),
(
"has_commit_message",
self._results(test_mbox_has_commit_message),
),
(
"diff_parse",
self._results(test_mbox_unidiff_parse_error),
),
]
)

self.results = dict(
[
(
"mbox",
self.mbox_results,
),
]
)

def _results(self, testname):
return [testname(patch) for patch in self.series.patchdata]

def _print_result(self, category, tag):
for value in self.results[category][tag]:
print(value)

def _print_results(self, category):
for tag in self.results[category].keys():
self._print_result(category, tag)

def print_results(self):
for category in self.results.keys():
self._print_results(category)


# test_for_pattern()
# @pattern: a pyparsing regex object
# @string: a string (patch subject, commit message, author, etc. to
# search for
def test_for_pattern(pattern, string):
if pattern.search_string(string):
return "PASS"
else:
return "FAIL"


@patchtest_result
def test_mbox_has_signed_off_by(target):
test_name = "test_mbox_has_signed_off_by"
result = test_for_pattern(patterns.signed_off_by, target.commit_message)
result = "FAIL"
if patterns.signed_off_by.search_string(target.commit_message):
result = "PASS"
reason = "mbox was missing a signed-off-by tag"
return PatchtestResult(target.subject, test_name, result, reason)

return target.subject, result, reason

@patchtest_result
def test_mbox_shortlog_format(target):
test_name = "test_mbox_shortlog_format"
result = "PASS"
reason = None
if not target.shortlog.strip():
Expand All @@ -112,13 +30,12 @@ def test_mbox_shortlog_format(target):
result = "FAIL"
reason = 'Commit shortlog (first line of commit message) should follow the format "<target>: <summary>"'

return PatchtestResult(target.subject, test_name, result, reason)

return target.subject, result, reason

@patchtest_result
def test_mbox_shortlog_length(target):
shortlog = re.sub("^(\[.*?\])+ ", "", target.shortlog)
shortlog_len = len(shortlog)
test_name = "test_mbox_shortlog_length"
result = "PASS"
reason = f"Edit shortlog so that it is {patterns.mbox_shortlog_maxlength} characters or less (currently {shortlog_len} characters)"

Expand All @@ -130,11 +47,10 @@ def test_mbox_shortlog_length(target):
if shortlog_len > patterns.mbox_shortlog_maxlength:
result = "FAIL"

return PatchtestResult(target.subject, test_name, result, reason)

return target.subject, result, reason

@patchtest_result
def test_mbox_has_commit_message(target):
test_name = "test_mbox_has_commit_message"
result = "PASS"
reason = "Please include a commit message on your patch explaining the change"

Expand All @@ -149,11 +65,10 @@ def test_mbox_has_commit_message(target):
if not commit_lines:
result = "FAIL"

return PatchtestResult(target.subject, test_name, result, reason)

return target.subject, result, reason

@patchtest_result
def test_mbox_unidiff_parse_error(target):
test_name = "test_mbox_unidiff_parse_error"
result = "PASS"
reason = f'Patch "{target.shortlog}" contains malformed diff lines.'

Expand All @@ -162,4 +77,4 @@ def test_mbox_unidiff_parse_error(target):
except unidiff.UnidiffParseError as upe:
result = "FAIL"

return PatchtestResult(target.subject, test_name, result, reason)
return target.subject, result, reason
35 changes: 33 additions & 2 deletions src/patchtest2/tests/patchtest.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,44 @@
import inspect
import patchtest2.tests.core as core
from patchtest2.parser import PatchtestParser
from patchtest2.mbox import PatchSeries, TargetRepo
from patchtest2.tests.core import PatchtestResults

class Patchtest:
def __init__(self, target_repo, series):
self.target_repo = target_repo
self.series = series
self.core_results = {k: self._results(v) for (k, v) in inspect.getmembers(core, inspect.isfunction) if k != "patchtest_result"}

self.results = dict(
[
(
"core",
self.core_results,
),
]
)

def _results(self, testname):
return [testname(patch) for patch in self.series.patchdata]

def _print_result(self, category, tag):
for value in self.results[category][tag]:
print(value)

def _print_results(self, category):
for tag in self.results[category].keys():
self._print_result(category, tag)

def print_results(self):
for category in self.results.keys():
self._print_results(category)


def run():
parser = PatchtestParser.get_parser()
args = parser.parse_args()
target_repo = TargetRepo(args.repodir)
series = PatchSeries(args.patch_path)
results = PatchtestResults(target_repo, series)
results = Patchtest(target_repo, series)

results.print_results()

0 comments on commit 8104405

Please sign in to comment.