mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
Fix comment confusion bug in update_lib (#7802)
* Use `unittest.expectedSuccess` to mark failing tests * Fix comment find logic * Add test for it
This commit is contained in:
1 parent
ae5c9119c9
commit
002fc1122e
3 files changed
+44
-10
No files matched your search
@@ -190,13 +190,23 @@ class PatchEntry(typing.NamedTuple):
|
||||
if ut_method.has_cond():
|
||||
cond = ast.unparse(dec_node.args[0])
|
||||
else:
|
||||
# Search first on decorator line, then in the line before
|
||||
for line in lines[dec_node.lineno - 1 : dec_node.lineno - 3 : -1]:
|
||||
if found := re.search(rf"{COMMENT}.?(.*)", line):
|
||||
reason = found.group()
|
||||
break
|
||||
pattern = re.compile(rf"{COMMENT}.?(.*)")
|
||||
dec_lineno = dec_node.lineno
|
||||
|
||||
curr_line = lines[dec_lineno - 1]
|
||||
prev_line = lines[dec_lineno - 2]
|
||||
|
||||
# If we see our comment at the decorator line, take it
|
||||
if found := pattern.search(curr_line):
|
||||
reason = found.group()
|
||||
elif prev_line.strip().startswith("#") and (
|
||||
found := pattern.search(prev_line)
|
||||
):
|
||||
# Search the previous line of the decorator,
|
||||
# only take the comment if the line starts with a `#`
|
||||
reason = found.group()
|
||||
else:
|
||||
# Didn't find our `COMMENT` :)
|
||||
# Didn't find our `COMMENT`, so the patch isn't ours :)
|
||||
continue
|
||||
|
||||
reason = reason.removeprefix(COMMENT).strip(";:, ")
|
||||
|
||||
@@ -149,6 +149,30 @@ class TestFoo(unittest.TestCase):
|
||||
specs = patches["TestFoo"]["test_one"]
|
||||
self.assertEqual(len(specs), 2)
|
||||
|
||||
def test_comment_confusion(self):
|
||||
"""
|
||||
Test that we only extract our patches when CPython set one of the UT methods,
|
||||
that we search for
|
||||
"""
|
||||
|
||||
code = f"""
|
||||
class TestFoo(unittest.TestCase):
|
||||
@unittest.expectedSuccess # {COMMENT}; reason
|
||||
@unittest.expectedFailure
|
||||
def test_one(self):
|
||||
pass
|
||||
"""
|
||||
|
||||
patches = extract_patches(code)
|
||||
specs = patches["TestFoo"]["test_one"]
|
||||
self.assertEqual(len(specs), 1)
|
||||
|
||||
spec = specs[0]
|
||||
self.assertEqual(
|
||||
spec,
|
||||
PatchSpec(ut_method=UtMethod.ExpectedSuccess, cond=None, reason="reason"),
|
||||
)
|
||||
|
||||
|
||||
class TestApplyPatches(unittest.TestCase):
|
||||
"""Tests for apply_patches function."""
|
||||
|
||||
Reference in new issue
Block a user