Answer architecture round 2: 0 🔴, 1 🟡 (0 misread); move task_bench's shared Side and Rust test-module scanner to the package top, rename figures.py to run_figures.py, check for cycles between a package's parts, and move REVIEW_LABEL into the backend protocol
CI / tests (pull_request) Successful in 5m13s
CI / tests (pull_request) Successful in 5m13s
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
import re
|
||||
import typing
|
||||
|
||||
from task_bench.figures.figures import Side
|
||||
from task_bench.tasks import Side
|
||||
|
||||
HUNK = re.compile(r"^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@", re.MULTILINE)
|
||||
|
||||
|
||||
@@ -1,99 +0,0 @@
|
||||
"""The `#[cfg(test)]` modules of a Rust file, which stand in the file of the code they test. Here are where each is, and a file with
|
||||
the modules of another file put in place of its own. A file is bytes: a Rust file is UTF-8, and a module is found by ASCII text alone."""
|
||||
import re
|
||||
|
||||
# What the scan steps over or stops at. A string, a char and a comment may hold a brace.
|
||||
SCAN = re.compile(
|
||||
rb"""//[^\n]*"""
|
||||
rb"""|/\*"""
|
||||
rb"""|(?<![A-Za-z0-9_])b?r(?P<hashes>\#*)\""""
|
||||
rb"""|\""""
|
||||
rb"""|'(?:\\(?:u\{[0-9a-fA-F_]+\}|x[0-9a-fA-F]{2}|.)|[^\\'\n])'"""
|
||||
rb"""|[{}]"""
|
||||
rb"""|\#\[cfg\(test\)\]""",
|
||||
re.DOTALL,
|
||||
)
|
||||
STRING_BODY = re.compile(rb'(?:[^"\\]|\\.)*"', re.DOTALL)
|
||||
COMMENT_EDGE = re.compile(rb"/\*|\*/")
|
||||
# What follows the attribute that starts an inline module: any other attributes, an optional visibility, then `mod name {`.
|
||||
MODULE = re.compile(rb"\s*(?:#\[[^\]]*\]\s*)*(?:pub(?:\([^)]*\))?\s+)?mod\s+(?P<name>\w+)\s*\{")
|
||||
|
||||
|
||||
def tells_apart(path: str, source: bytes) -> bool:
|
||||
"""Whether source, the file at path, is no Rust file, or holds test modules that `modules` can tell apart."""
|
||||
return not path.endswith(".rs") or modules(source) is not None
|
||||
|
||||
|
||||
def modules(source: bytes) -> dict[bytes, tuple[int, int]] | None:
|
||||
"""Each inline `#[cfg(test)] mod name { ... }` of source by its name, with the offsets of its first byte, the attribute's, and of the
|
||||
byte after its closing brace. A module inside a test module and a `#[cfg(test)]` on anything but an inline module are not listed.
|
||||
None where the modules cannot be told apart: two share a name, or a brace, string or comment is never closed."""
|
||||
found: dict[bytes, tuple[int, int]] = {}
|
||||
depth = 0
|
||||
# The module being read: its name, the offset of its attribute and the depth outside it.
|
||||
inside: tuple[bytes, int, int] | None = None
|
||||
position: int | None = 0
|
||||
while position is not None and (match := SCAN.search(source, position)):
|
||||
token, position = match.group(), match.end()
|
||||
if token == b"/*":
|
||||
position = after_comment(source, position)
|
||||
elif token == b'"':
|
||||
position = after_string(source, position)
|
||||
elif match.group("hashes") is not None:
|
||||
position = after_raw_string(source, position, match.group("hashes"))
|
||||
elif token == b"{":
|
||||
depth += 1
|
||||
elif token == b"}":
|
||||
depth -= 1
|
||||
if inside and depth == inside[2]:
|
||||
if inside[0] in found:
|
||||
return None
|
||||
found[inside[0]], inside = (inside[1], position), None
|
||||
elif token.startswith(b"#[") and inside is None and (module := MODULE.match(source, position)):
|
||||
inside, position = (module.group("name"), match.start(), depth), module.end()
|
||||
depth += 1
|
||||
return found if position is not None and depth == 0 and inside is None else None
|
||||
|
||||
|
||||
def after_comment(source: bytes, position: int) -> int | None:
|
||||
"""The offset after the block comment whose `/*` ends at position, None where it is never closed. Block comments nest in Rust."""
|
||||
level = 1
|
||||
while level and (match := COMMENT_EDGE.search(source, position)):
|
||||
level += 1 if match.group() == b"/*" else -1
|
||||
position = match.end()
|
||||
return position if level == 0 else None
|
||||
|
||||
|
||||
def after_string(source: bytes, position: int) -> int | None:
|
||||
"""The offset after the string whose opening quote ends at position, None where it is never closed. A backslash escapes the next byte."""
|
||||
closed = STRING_BODY.match(source, position)
|
||||
return closed.end() if closed else None
|
||||
|
||||
|
||||
def after_raw_string(source: bytes, position: int, hashes: bytes) -> int | None:
|
||||
"""The offset after the raw string, `r` or `br` then hashes and a quote, whose opening ends at position, None where it is never closed.
|
||||
It has no escapes."""
|
||||
closed = re.compile(b'"' + hashes).search(source, position)
|
||||
return closed.end() if closed else None
|
||||
|
||||
|
||||
def with_tests_of(planted: bytes, copy: bytes) -> bytes:
|
||||
"""copy with each of its test modules replaced by the one of that name in planted, and each that planted lacks left out. So a test a
|
||||
fix changed, removed or added doesn't count.
|
||||
|
||||
Where planted's modules cannot be told apart, or planted has none and copy's cannot be told apart, copy is returned as it is. Where
|
||||
planted has some and copy's cannot be told apart, or copy lacks one of planted's, planted is returned: Haiku's file has no module
|
||||
to put them in."""
|
||||
before, after = modules(planted), modules(copy)
|
||||
# Haiku's file stands where planted's modules can't be told apart. The bench never plants a bug in such a file, and a guess at
|
||||
# which module is which could swap one test for another. Haiku's file is also what the check took before the bench put back
|
||||
# planted test modules, so a fix that passed then still passes.
|
||||
if before is None or (after is None and not before):
|
||||
return copy
|
||||
if after is None or before.keys() - after.keys():
|
||||
return planted
|
||||
text, last = b"", 0
|
||||
for (start, end), name in sorted((span, name) for name, span in after.items()):
|
||||
text += copy[last:start] + (planted[before[name][0]:before[name][1]] if name in before else b"")
|
||||
last = end
|
||||
return text + copy[last:]
|
||||
@@ -1,153 +0,0 @@
|
||||
"""The test modules of a Rust file: where they are, and a fixed file with the planted tests put back."""
|
||||
import pathlib
|
||||
import sys
|
||||
import textwrap
|
||||
import unittest
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2]))
|
||||
|
||||
from task_bench.bugs import rust_test_modules
|
||||
from task_bench.bugs.rust_test_modules import tells_apart
|
||||
|
||||
LIB = textwrap.dedent("""\
|
||||
pub fn add(a: i32, b: i32) -> i32 {
|
||||
a + b
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn adds() {
|
||||
assert_eq!(add(1, 2), 3);
|
||||
}
|
||||
}
|
||||
""")
|
||||
|
||||
|
||||
def modules(source: str) -> dict[bytes, tuple[int, int]] | None:
|
||||
return rust_test_modules.modules(source.encode())
|
||||
|
||||
|
||||
def with_tests_of(planted: str, copy: str) -> str:
|
||||
return rust_test_modules.with_tests_of(planted.encode(), copy.encode()).decode()
|
||||
|
||||
|
||||
def spelt(source: str) -> dict[str, str] | None:
|
||||
found = modules(source)
|
||||
return None if found is None else {name.decode(): source[start:end] for name, (start, end) in found.items()}
|
||||
|
||||
|
||||
class ModulesTest(unittest.TestCase):
|
||||
def test_holds_each_test_module_from_its_attribute_to_its_closing_brace(self):
|
||||
self.assertEqual(spelt(LIB), {"tests": LIB[LIB.index("#[cfg(test)]"):LIB.rindex("}") + 1]})
|
||||
|
||||
def test_lists_no_module_where_the_file_has_no_test_module(self):
|
||||
self.assertEqual(modules("fn main() {}\n"), {})
|
||||
|
||||
def test_takes_a_module_with_a_visibility_and_another_attribute(self):
|
||||
source = "#[cfg(test)]\n#[allow(dead_code)]\npub(crate) mod checks {\n}\nfn after() {}\n"
|
||||
self.assertEqual(spelt(source), {"checks": source[:source.index("}") + 1]})
|
||||
|
||||
def test_lists_no_module_where_the_attribute_is_on_anything_but_an_inline_module(self):
|
||||
self.assertEqual(modules("#[cfg(test)]\nmod tests;\n#[cfg(test)]\nuse std::fmt;\n#[cfg(test)]\nfn helper() {}\n"), {})
|
||||
|
||||
def test_lists_no_module_inside_a_test_module(self):
|
||||
source = "#[cfg(test)]\nmod outer {\n #[cfg(test)]\n mod inner {}\n}\n"
|
||||
self.assertEqual(list(spelt(source)), ["outer"])
|
||||
|
||||
def test_reads_past_a_brace_in_a_string_a_char_a_comment_or_an_escape(self):
|
||||
source = textwrap.dedent("""\
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
// }
|
||||
/* } /* } */ } */
|
||||
const A: &str = "}\\"}";
|
||||
const B: &str = r#"}"}"#;
|
||||
const C: char = '}';
|
||||
const D: char = '\\'';
|
||||
fn lifetime<'a>(x: &'a str) -> &'a str { x }
|
||||
}
|
||||
fn after() {}
|
||||
""")
|
||||
self.assertEqual(spelt(source), {"tests": source[:source.index("}\nfn after")+1]})
|
||||
|
||||
def test_reads_a_raw_string_that_ends_in_a_backslash(self):
|
||||
source = "#[cfg(test)]\nmod tests {\n const A: &str = r\"\\\";\n}\nfn after() {}\n"
|
||||
self.assertEqual(list(spelt(source)), ["tests"])
|
||||
|
||||
def test_ends_a_raw_string_at_its_first_closing_quote(self):
|
||||
source = "#[cfg(test)]\nmod tests {\n const A: &str = r#\"x\"#;\n}\nconst B: &str = r#\"y\"#;\n"
|
||||
self.assertEqual(list(spelt(source)), ["tests"])
|
||||
|
||||
def test_reads_a_string_that_ends_in_two_backslashes_or_in_an_escaped_quote(self):
|
||||
for literal in ('"\\\\"', '"a\\""', '"\\}\\}"'):
|
||||
with self.subTest(literal=literal):
|
||||
source = "#[cfg(test)]\nmod tests {\n const A: &str = " + literal + ";\n}\nfn after() {}\n"
|
||||
self.assertEqual(list(spelt(source)), ["tests"])
|
||||
|
||||
def test_lists_no_plain_module_that_follows_a_comment_or_a_char(self):
|
||||
self.assertEqual(modules("// helpers\nmod util {}\nfn a() { let c = 'x'; }\nmod more {}\n"), {})
|
||||
|
||||
def test_lists_a_test_module_inside_a_module_that_is_no_test(self):
|
||||
source = "mod outer {\n #[cfg(test)]\n mod inner {}\n fn after() {}\n}\n"
|
||||
self.assertEqual(spelt(source), {"inner": "#[cfg(test)]\n mod inner {}"})
|
||||
|
||||
def test_cannot_tell_the_modules_apart_where_two_hold_one_name(self):
|
||||
self.assertIsNone(modules("#[cfg(test)]\nmod tests {}\nfn a() { #[cfg(test)] mod tests {} }\n"))
|
||||
|
||||
def test_cannot_tell_the_modules_apart_where_a_brace_string_or_comment_is_never_closed(self):
|
||||
for source in ("#[cfg(test)]\nmod tests {\n", "fn a() {\n", "const A: &str = \"\n", "/* open\n", "const A: &str = r#\"\n"):
|
||||
with self.subTest(source=source):
|
||||
self.assertIsNone(modules(source))
|
||||
|
||||
|
||||
class TellsApartTest(unittest.TestCase):
|
||||
def test_tells_the_modules_of_a_rust_file_apart_where_they_can_be(self):
|
||||
self.assertEqual([tells_apart("lib.rs", b"fn a() {}"), tells_apart("lib.rs", b"fn a() {")], [True, False])
|
||||
|
||||
def test_takes_a_file_of_another_language_whatever_it_holds(self):
|
||||
self.assertTrue(tells_apart("calc.go", b"func a() {"))
|
||||
|
||||
|
||||
class WithTestsOfTest(unittest.TestCase):
|
||||
def test_takes_the_code_as_fixed_and_the_tests_as_planted(self):
|
||||
planted = LIB.replace("a + b", "a - b")
|
||||
fixed = LIB.replace("assert_eq!(add(1, 2), 3);", "assert_eq!(add(1, 2), 0);")
|
||||
self.assertEqual(with_tests_of(planted, fixed), LIB)
|
||||
|
||||
def test_takes_the_planted_tests_where_the_fix_deleted_them(self):
|
||||
planted = LIB.replace("a + b", "a - b")
|
||||
fixed = "pub fn add(a: i32, b: i32) -> i32 {\n a + b\n}\n"
|
||||
self.assertEqual(with_tests_of(planted, fixed), planted)
|
||||
|
||||
def test_leaves_out_a_test_module_the_fix_added(self):
|
||||
planted = "fn a() {}\n"
|
||||
fixed = "fn a() {}\n#[cfg(test)]\nmod mine {}\nfn b() {}\n"
|
||||
self.assertEqual(with_tests_of(planted, fixed), "fn a() {}\n\nfn b() {}\n")
|
||||
|
||||
def test_puts_each_planted_module_where_the_fixed_module_of_its_name_stands(self):
|
||||
planted = "fn a() { 1 }\n#[cfg(test)]\nmod x { fn t() {} }\nfn b() {}\n#[cfg(test)]\nmod y { fn u() {} }\n"
|
||||
fixed = "fn a() { 2 }\n#[cfg(test)]\nmod y { fn changed() {} }\n#[cfg(test)]\nmod x { fn changed() {} }\nfn c() {}\n"
|
||||
self.assertEqual(with_tests_of(planted, fixed), "fn a() { 2 }\n#[cfg(test)]\nmod y { fn u() {} }\n#[cfg(test)]\nmod x { fn t() {} }\nfn c() {}\n")
|
||||
|
||||
def test_puts_in_planted_modules_whose_length_differs_from_the_fixed_ones(self):
|
||||
planted = "#[cfg(test)]\nmod a { 1 }\n#[cfg(test)]\nmod b { 22 }\nfn code() {}\n"
|
||||
fixed = "#[cfg(test)]\nmod a { 333 }\n#[cfg(test)]\nmod b { 4444444 }\nfn fixed() {}\n"
|
||||
self.assertEqual(with_tests_of(planted, fixed), "#[cfg(test)]\nmod a { 1 }\n#[cfg(test)]\nmod b { 22 }\nfn fixed() {}\n")
|
||||
|
||||
def test_takes_the_file_as_fixed_where_the_planted_modules_cannot_be_told_apart(self):
|
||||
self.assertEqual(with_tests_of("#[cfg(test)]\nmod tests {\n", "fixed {}"), "fixed {}")
|
||||
|
||||
def test_takes_the_planted_file_where_the_fixed_modules_cannot_be_told_apart(self):
|
||||
self.assertEqual(with_tests_of(LIB, "fn broken() {"), LIB)
|
||||
|
||||
def test_takes_the_file_as_fixed_where_planted_has_no_module_and_the_fixed_modules_cannot_be_told_apart(self):
|
||||
for planted in ("", "fn a() {}\n"):
|
||||
with self.subTest(planted=planted):
|
||||
self.assertEqual(with_tests_of(planted, "fn broken() {"), "fn broken() {")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user