Answer principles round 2: 0 🔴, 2 🟡 (0 misread); workflow-bench hands its effects in as arguments, and no Python file of the repo patches a global
CI / tests (pull_request) Successful in 3m46s

This commit is contained in:
2026-11-19 12:55:35 +01:00
parent cb3135bc8c
commit 762614353c
18 changed files with 335 additions and 231 deletions
+45 -46
View File
@@ -13,7 +13,6 @@ Exit 0 on the line, 1 on the timeout, bad arguments or a file it cannot read, sa
import pathlib
import re
import sys
import time
import typing
from failure import Failure
@@ -28,25 +27,41 @@ class Clock(typing.Protocol):
def sleep(self, seconds: float) -> None: ...
def text_of(path: pathlib.Path) -> str | None | Failure:
"""What the file holds; None where it doesn't exist."""
try:
return path.read_text(errors="replace")
except FileNotFoundError:
return None
except OSError as error:
return Failure(f"cannot read {path}: {error.strerror}")
def main(argv: list[str], clock: Clock) -> int:
if argv[:1] in (["-h"], ["--help"]):
print(__doc__.strip())
return 0
parsed = arguments(argv)
done = parsed if isinstance(parsed, Failure) else waited(*parsed, clock)
if isinstance(done, Failure):
print(f"wait-for: {done.reason}", file=sys.stderr)
return 1
print(done)
return 0
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)
def arguments(argv: list[str]) -> tuple[str, str | None, float] | Failure:
operands = list(argv)
timeout = float(TIMEOUT_SECONDS)
if "--timeout" in operands:
at = operands.index("--timeout")
given = operands[at + 1:at + 2]
if not given or not re.fullmatch(r"[0-9]+(\.[0-9]+)?", given[0]) or float(given[0]) <= 0:
return Failure(f"give --timeout a number of seconds above 0; {USAGE}")
timeout = float(given[0])
del operands[at:at + 2]
if not 1 <= len(operands) <= 2:
return Failure(USAGE)
pattern = operands[1] if len(operands) == 2 else None
if pattern is not None:
try:
re.compile(pattern)
except re.error as error:
return Failure(f"cannot read the pattern `{pattern}`: {error}")
return operands[0], pattern, timeout
def waited(file: str, pattern: str | None, timeout: float, clock: Clock = time) -> str | Failure:
def waited(file: str, pattern: str | None, timeout: float, clock: Clock) -> str | Failure:
path = pathlib.Path(file)
compiled = re.compile(pattern) if pattern is not None else None
deadline = clock.monotonic() + timeout
@@ -72,35 +87,19 @@ def waited(file: str, pattern: str | None, timeout: float, clock: Clock = time)
clock.sleep(min(POLL_SECONDS, left))
def arguments(argv: list[str]) -> tuple[str, str | None, float] | Failure:
operands = list(argv)
timeout = float(TIMEOUT_SECONDS)
if "--timeout" in operands:
at = operands.index("--timeout")
given = operands[at + 1:at + 2]
if not given or not re.fullmatch(r"[0-9]+(\.[0-9]+)?", given[0]) or float(given[0]) <= 0:
return Failure(f"give --timeout a number of seconds above 0; {USAGE}")
timeout = float(given[0])
del operands[at:at + 2]
if not 1 <= len(operands) <= 2:
return Failure(USAGE)
pattern = operands[1] if len(operands) == 2 else None
if pattern is not None:
try:
re.compile(pattern)
except re.error as error:
return Failure(f"cannot read the pattern `{pattern}`: {error}")
return operands[0], pattern, timeout
def text_of(path: pathlib.Path) -> str | None | Failure:
"""What the file holds; None where it doesn't exist."""
try:
return path.read_text(errors="replace")
except FileNotFoundError:
return None
except OSError as error:
return Failure(f"cannot read {path}: {error.strerror}")
def main(argv: list[str]) -> int:
if argv[:1] in (["-h"], ["--help"]):
print(__doc__.strip())
return 0
parsed = arguments(argv)
done = parsed if isinstance(parsed, Failure) else waited(*parsed)
if isinstance(done, Failure):
print(f"wait-for: {done.reason}", file=sys.stderr)
return 1
print(done)
return 0
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)
+1 -1
View File
@@ -116,7 +116,7 @@ class WaitFor(unittest.TestCase):
with self.subTest(argv=argv):
out, err = io.StringIO(), io.StringIO()
with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err):
code = main.main(argv)
code = main.main(argv, Clock())
self.assertEqual((code, out.getvalue(), err.getvalue()), expected)
def test_arguments_take_the_timeout_anywhere_and_refuse_a_bad_one(self):