Answer stability round 1: 1 🔴, 12 🟡 (0 misread); wait-for waits out a half-written line, prepare-scoring honours claudeMdExcludes, and a merge by another method waits for the checks
CI / tests (pull_request) Successful in 2m38s
CI / tests (pull_request) Successful in 2m38s
This commit is contained in:
+22
-9
@@ -27,14 +27,19 @@ class Clock(typing.Protocol):
|
||||
def sleep(self, seconds: float) -> None: ...
|
||||
|
||||
|
||||
def line_in(path: pathlib.Path, pattern: re.Pattern[str] | None) -> str | None | Failure:
|
||||
"""The first line matching pattern, or without one the last line, where the file holds anything; else None."""
|
||||
def text_of(path: pathlib.Path) -> str | None | Failure:
|
||||
"""What the file holds; None where it doesn't exist."""
|
||||
try:
|
||||
lines = path.read_text(errors="replace").splitlines()
|
||||
return path.read_text(errors="replace")
|
||||
except FileNotFoundError:
|
||||
return None
|
||||
except OSError as error:
|
||||
return Failure(f"cannot read {path}: {error.strerror}")
|
||||
|
||||
|
||||
def line_in(text: str, pattern: re.Pattern[str] | None) -> str | None:
|
||||
"""The first line of text matching pattern, or without one its last line; None where there is none."""
|
||||
lines = text.splitlines()
|
||||
if pattern is None:
|
||||
return lines[-1] if lines else None
|
||||
return next((line for line in lines if pattern.search(line)), None)
|
||||
@@ -44,18 +49,26 @@ def waited(file: str, pattern: str | None, timeout: float, clock: Clock = time)
|
||||
path = pathlib.Path(file)
|
||||
compiled = re.compile(pattern) if pattern is not None else None
|
||||
deadline = clock.monotonic() + timeout
|
||||
while (found := line_in(path, compiled)) is None:
|
||||
before = None
|
||||
while True:
|
||||
text = text_of(path)
|
||||
if isinstance(text, Failure):
|
||||
return text
|
||||
# A last line without its newline may still be being written: it counts once a poll finds it unchanged.
|
||||
settled = text is None or text.endswith("\n") or text == before
|
||||
whole = text if settled or text is None else text[:text.rfind("\n") + 1]
|
||||
# Without a pattern the last line is wanted, so an unsettled one waits.
|
||||
if whole is not None and (settled or compiled is not None) and (found := line_in(whole, compiled)) is not None:
|
||||
return found
|
||||
before = text
|
||||
left = deadline - clock.monotonic()
|
||||
if left <= 0:
|
||||
if not path.exists():
|
||||
if text is None:
|
||||
return Failure(f"{file} did not appear in {timeout:g} s")
|
||||
last = line_in(path, None)
|
||||
if isinstance(last, Failure):
|
||||
return last
|
||||
last = line_in(text, None)
|
||||
wanted = f"no line matching `{pattern}`" if pattern is not None else "nothing"
|
||||
return Failure(f"{file} held {wanted} after {timeout:g} s" + (f"; its last line: {last}" if last is not None else ""))
|
||||
clock.sleep(min(POLL_SECONDS, left))
|
||||
return found
|
||||
|
||||
|
||||
def arguments(argv: list[str]) -> tuple[str, str | None, float] | Failure:
|
||||
|
||||
@@ -4,7 +4,6 @@ import pathlib
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import threading
|
||||
import unittest
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
||||
@@ -59,6 +58,17 @@ class WaitFor(unittest.TestCase):
|
||||
def test_gives_up_on_a_file_that_never_appears(self):
|
||||
self.assertEqual(main.waited(str(self.file), None, 5, Clock()), Failure(f"{self.file} did not appear in 5 s"))
|
||||
|
||||
def test_waits_for_a_half_written_last_line_to_end(self):
|
||||
self.file.write_text("running\n== 3 passed in")
|
||||
clock = Clock(lambda: self.file.write_text("running\n== 3 passed in 1s ==\n"))
|
||||
self.assertEqual(main.waited(str(self.file), "passed", 60, clock), "== 3 passed in 1s ==")
|
||||
|
||||
def test_takes_a_last_line_without_a_newline_once_a_poll_finds_it_unchanged(self):
|
||||
self.file.write_text("# Report\nNo findings.")
|
||||
clock = Clock()
|
||||
self.assertEqual(main.waited(str(self.file), None, 60, clock), "No findings.")
|
||||
self.assertEqual(clock.now, main.POLL_SECONDS)
|
||||
|
||||
def test_reads_a_line_that_is_not_utf_8(self):
|
||||
self.file.write_bytes(b"caf\xe9 passed\n")
|
||||
self.assertEqual(main.waited(str(self.file), "passed", 60, Clock()), "caf\ufffd passed")
|
||||
@@ -85,6 +95,8 @@ class WaitFor(unittest.TestCase):
|
||||
for argv, expected in (
|
||||
([str(self.file), "passed", "--timeout", "5"], (0, "== 1 passed ==\n", "")),
|
||||
(["--help"], (0, main.__doc__.strip() + "\n", "")),
|
||||
(["-h"], (0, main.__doc__.strip() + "\n", "")),
|
||||
(["--help", str(self.file)], (0, main.__doc__.strip() + "\n", "")),
|
||||
([str(self.file), "("], (1, "", "wait-for: cannot read the pattern `(`: missing ), unterminated subpattern at position 0\n")),
|
||||
):
|
||||
with self.subTest(argv=argv):
|
||||
@@ -96,6 +108,7 @@ class WaitFor(unittest.TestCase):
|
||||
def test_arguments_take_the_timeout_anywhere_and_refuse_a_bad_one(self):
|
||||
self.assertEqual(main.arguments(["--timeout", "1.5", "f", "p"]), ("f", "p", 1.5))
|
||||
self.assertEqual(main.arguments(["f"]), ("f", None, 540.0))
|
||||
self.assertEqual(main.arguments(["f", "--timeout", "0.5"]), ("f", None, 0.5))
|
||||
for timeout in ("0", "-1", "x", "1.", None):
|
||||
with self.subTest(timeout=timeout):
|
||||
argv = ["f", "--timeout"] + ([timeout] if timeout else [])
|
||||
@@ -104,11 +117,9 @@ class WaitFor(unittest.TestCase):
|
||||
with self.subTest(argv=argv):
|
||||
self.assertEqual(main.arguments(argv), Failure(main.USAGE))
|
||||
|
||||
def test_the_command_waits_for_a_file_another_process_writes(self):
|
||||
writer = threading.Timer(0.5, lambda: self.file.write_text("== 1 passed in 0.1s ==\n"))
|
||||
writer.start()
|
||||
self.addCleanup(writer.cancel)
|
||||
result = subprocess.run([sys.executable, str(TOOL), str(self.file), "passed", "--timeout", "20"], capture_output=True, text=True)
|
||||
def test_the_command_prints_the_line_a_file_holds(self):
|
||||
self.file.write_text("== 1 passed in 0.1s ==\n")
|
||||
result = subprocess.run([sys.executable, str(TOOL), str(self.file), "passed", "--timeout", "20"], capture_output=True, text=True, timeout=10)
|
||||
self.assertEqual((result.returncode, result.stdout, result.stderr), (0, "== 1 passed in 0.1s ==\n", ""))
|
||||
|
||||
def test_refuses_a_bad_pattern_timeout_or_argument_count_waiting_for_nothing(self):
|
||||
|
||||
Reference in New Issue
Block a user