mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 09:08:38 +00:00
Keep feature-gated Rust changes out of red-side tests
This commit is contained in:
@ -179,15 +179,53 @@ def is_test_file(path: str) -> bool:
|
||||
|
||||
|
||||
CFG = re.compile(r"^\s*#\[cfg\((.*)\)\]\s*$")
|
||||
CFG_TOKEN = re.compile(r'r(?P<hashes>#{0,255})".*?"(?P=hashes)|"(?:[^"\\]|\\.)*"|/\*|\w+|\S')
|
||||
CFG_COMMENT_MARKER = re.compile(r"/\*|\*/")
|
||||
RAW_OPEN = re.compile(r'b?r(#*)"')
|
||||
CHAR = re.compile(r"b?'(?:\\(?:x[0-9a-fA-F]{2}|u\{[0-9a-fA-F]+\}|.)|[^\\'])'")
|
||||
|
||||
|
||||
def is_test_cfg(expr: str) -> bool:
|
||||
expr = expr.replace(" ", "")
|
||||
if "not(test" in expr:
|
||||
"""Recognize predicates that structurally require the bare 'test' option."""
|
||||
tokens = []
|
||||
end = 0
|
||||
while token := CFG_TOKEN.search(expr, end):
|
||||
end = token.end()
|
||||
if token[0] != "/*":
|
||||
tokens.append(token[0])
|
||||
continue
|
||||
# Rust permits nested block comments, with no special treatment of quotes inside them.
|
||||
depth = 1
|
||||
while depth and (marker := CFG_COMMENT_MARKER.search(expr, end)):
|
||||
end = marker.end()
|
||||
depth += 1 if marker[0] == "/*" else -1
|
||||
if depth:
|
||||
return False
|
||||
return cfg_requires_test(tokens)
|
||||
|
||||
|
||||
def cfg_requires_test(tokens: list[str]) -> bool:
|
||||
if tokens == ["test"]:
|
||||
return True
|
||||
if len(tokens) < 3 or tokens[0] not in ("all", "any") or tokens[1] != "(" or tokens[-1] != ")":
|
||||
return False
|
||||
return expr == "test" or (expr.startswith("all(") and re.search(r"[(,]test[,)]", expr) is not None)
|
||||
body = tokens[2:-1]
|
||||
operands = []
|
||||
start = depth = 0
|
||||
for i, token in enumerate(body):
|
||||
match token:
|
||||
case "(":
|
||||
depth += 1
|
||||
case ")":
|
||||
depth -= 1
|
||||
case "," if depth == 0:
|
||||
operands.append(body[start:i])
|
||||
start = i + 1
|
||||
if start < len(body):
|
||||
operands.append(body[start:])
|
||||
requires_test = map(cfg_requires_test, operands)
|
||||
# One required conjunct suffices. For alternatives, each must require test.
|
||||
return any(requires_test) if tokens[0] == "all" else bool(operands) and all(requires_test)
|
||||
|
||||
|
||||
def item_end(lines: list[str], start: int) -> int | None:
|
||||
|
||||
@ -689,6 +689,35 @@ class Hunks(unittest.TestCase):
|
||||
new = old.replace(" fn x() {}\n}", " fn x() {}\n fn y() { let _ = '}'; }\n}")
|
||||
self.assertEqual(self.diff(old, new).decode(), new)
|
||||
|
||||
def test_feature_gated_code_is_not_carried_into_red(self):
|
||||
old = (
|
||||
'#[cfg(all(windows, any(test, feature = "host")))]\n'
|
||||
'fn normal_code() -> u32 { 1 }\n\n'
|
||||
'#[cfg(all(windows, all(not(unix), test)))]\n'
|
||||
'fn test_helper() -> u32 { 1 }\n'
|
||||
)
|
||||
new = old.replace("{ 1 }", "{ 2 }")
|
||||
self.assertEqual(self.diff(old, new).decode(),
|
||||
old.replace("test_helper() -> u32 { 1 }", "test_helper() -> u32 { 2 }"))
|
||||
|
||||
def test_cfg_classification_respects_grouping_and_literals(self):
|
||||
for expr in ('all(test, any(windows, not(test)))',
|
||||
'any(all(test, windows), all(test, unix),)',
|
||||
'all(feature = r#"a,(b,c)"#, test)'):
|
||||
with self.subTest(expr=expr):
|
||||
self.assertTrue(rg.is_test_cfg(expr))
|
||||
for expr in ('any(test, windows)', 'any()',
|
||||
'all(feature = "a,test,b", windows)',
|
||||
'all(feature = r#"a",test,"b"#, windows)',
|
||||
'all(windows /* ,test, */)'):
|
||||
with self.subTest(expr=expr):
|
||||
self.assertFalse(rg.is_test_cfg(expr))
|
||||
|
||||
def test_cfg_block_comments_are_whitespace(self):
|
||||
self.assertTrue(rg.is_test_cfg('all(test, windows /* platform note */)'))
|
||||
self.assertTrue(rg.is_test_cfg('all(test /* " /* nested */ */ , windows)'))
|
||||
self.assertFalse(rg.is_test_cfg('all(windows /* /* nested */ ,test, */)'))
|
||||
|
||||
def test_all_with_test_counts_and_test_files_are_whole(self):
|
||||
self.assertTrue(rg.is_test_cfg("all(test, windows)"))
|
||||
self.assertFalse(rg.is_test_cfg("all(not(test), windows)"))
|
||||
|
||||
Reference in New Issue
Block a user