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
CI / tests (pull_request) Successful in 3m46s
This commit is contained in:
@@ -1,13 +1,23 @@
|
||||
import contextlib
|
||||
import functools
|
||||
import io
|
||||
import os
|
||||
import pathlib
|
||||
import subprocess
|
||||
import sys
|
||||
import unittest
|
||||
import unittest.mock
|
||||
|
||||
with unittest.mock.patch.dict(os.environ, {"BENCH_SOURCES": "/nonexistent"}):
|
||||
import bench
|
||||
import bench
|
||||
|
||||
|
||||
def answering(*answers):
|
||||
"""A run that gives the next of answers on each call, raising the one that is a failure."""
|
||||
left = list(answers)
|
||||
|
||||
def run(name):
|
||||
answer = left.pop(0)
|
||||
if isinstance(answer, Exception):
|
||||
raise answer
|
||||
return answer
|
||||
return run
|
||||
|
||||
|
||||
class Main(unittest.TestCase):
|
||||
@@ -17,27 +27,27 @@ class Main(unittest.TestCase):
|
||||
raise KeyError("a")
|
||||
return f"{name}: done"
|
||||
out = io.StringIO()
|
||||
with unittest.mock.patch.object(bench, "run", side_effect=run), unittest.mock.patch.object(sys, "argv", ["bench.py", "a-v-1", "b-v-1"]), contextlib.redirect_stdout(out):
|
||||
bench.main()
|
||||
with contextlib.redirect_stdout(out):
|
||||
bench.main(["a-v-1", "b-v-1"], run)
|
||||
self.assertEqual(out.getvalue().splitlines(), ["a-v-1: FAILED: KeyError: 'a'", "b-v-1: done"])
|
||||
|
||||
|
||||
class Attempted(unittest.TestCase):
|
||||
def test_returns_the_run_result_of_a_run_that_finished(self):
|
||||
with unittest.mock.patch.object(bench, "run", return_value="a-v-1: done"):
|
||||
self.assertEqual(bench.attempted("a-v-1"), "a-v-1: done")
|
||||
self.assertEqual(bench.attempted("a-v-1", answering("a-v-1: done")), "a-v-1: done")
|
||||
|
||||
def test_returns_a_failed_run_as_a_line_naming_the_run_and_its_error_so_later_runs_still_report(self):
|
||||
failures = [KeyError("nope"), ValueError("not enough values to unpack"), subprocess.CalledProcessError(128, ["git", "clone"]), FileNotFoundError("claude")]
|
||||
for failure in failures:
|
||||
with self.subTest(failure=type(failure).__name__), unittest.mock.patch.object(bench, "run", side_effect=[failure, "b-v-1: done"]):
|
||||
results = [bench.attempted("a-v-1"), bench.attempted("b-v-1")]
|
||||
with self.subTest(failure=type(failure).__name__):
|
||||
run = answering(failure, "b-v-1: done")
|
||||
results = [bench.attempted("a-v-1", run), bench.attempted("b-v-1", run)]
|
||||
self.assertTrue(results[0].startswith("a-v-1: FAILED: "), results[0])
|
||||
self.assertIn(type(failure).__name__, results[0])
|
||||
self.assertEqual(results[1], "b-v-1: done")
|
||||
|
||||
def test_a_run_of_an_unknown_case_fails_as_a_line(self):
|
||||
self.assertRegex(bench.attempted("nope-v-1"), r"^nope-v-1: FAILED: KeyError")
|
||||
self.assertRegex(bench.attempted("nope-v-1", functools.partial(bench.run, sources=pathlib.Path("/nonexistent"))), r"^nope-v-1: FAILED: KeyError")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user