Answer principles round 1: 0 🔴, 1 🟡 (0 misread); bound and stream a patch cut's ci.sh, keep a trace of each deploy-release run, fail on file errors and a missing Unreleased heading, read the plan once per cut, one home for the review title, inject task-bench's container names, one spelling of its task kind
CI / tests (pull_request) Has been cancelled
CI / tests (pull_request) Has been cancelled
This commit is contained in:
@@ -1,16 +1,19 @@
|
||||
import datetime
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
import pathlib
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import time
|
||||
import unittest
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
||||
|
||||
from deploy_release import ci_script, cut, patch, trace
|
||||
from failure import Failure
|
||||
from test_env import isolated_env
|
||||
from deploy_release import patch
|
||||
|
||||
TOOL = pathlib.Path(__file__).resolve().parents[1] / "deploy-release"
|
||||
|
||||
@@ -244,7 +247,7 @@ class Cut(unittest.TestCase):
|
||||
(fake / name).write_text(FAKE_FORGE)
|
||||
(fake / name).chmod(0o755)
|
||||
(self.temp / "gitconfig").write_text("[user]\n\tname = t\n\temail = t@t\n")
|
||||
self.env = {**isolated_env(), "GIT_CONFIG_GLOBAL": str(self.temp / "gitconfig"), "GIT_CONFIG_NOSYSTEM": "1", "PATH": f"{fake}:{os.environ['PATH']}"}
|
||||
self.env = {**isolated_env(), "GIT_CONFIG_GLOBAL": str(self.temp / "gitconfig"), "GIT_CONFIG_NOSYSTEM": "1", "PATH": f"{fake}:{os.environ['PATH']}", "XDG_STATE_HOME": str(self.temp / "state")}
|
||||
self.forge_state({"origin": str(self.origin), "pulls": [], "checks": "none", "calls": []})
|
||||
self.git(self.temp, "init", "-q", "--bare", "-b", "main", str(self.origin))
|
||||
self.clone = self.temp / "claude-config"
|
||||
@@ -470,6 +473,43 @@ class Cut(unittest.TestCase):
|
||||
self.assertEqual(self.cut().returncode, 1, "0.2.0 is cut, so a second run refuses")
|
||||
self.assertEqual(self.git(self.clone, "tag", "-l", "v0.2.0", "v9.9.9").split(), ["v0.2.0", "v9.9.9"])
|
||||
|
||||
def traces(self) -> list[str]:
|
||||
"""The text of each run's trace, oldest first."""
|
||||
return [file.read_text() for file in sorted((self.temp / "state" / "deploy-release").glob("*.log")) if not file.name.endswith("-ci.log")]
|
||||
|
||||
def test_keeps_each_step_of_a_cut_with_its_time_in_a_file_of_the_run(self):
|
||||
self.assert_cut(self.cut())
|
||||
[kept] = self.traces()
|
||||
lines = kept.splitlines()
|
||||
self.assertRegex(lines[0], rf"^[0-9]{{4}}-[0-9]{{2}}-[0-9]{{2}}T[0-9:]{{8}}\S* deploy-release --cut {self.target} in {self.clone}$")
|
||||
self.assertRegex(kept, r"(?m)^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9:]{8}\S* pushed the tag v0\.2\.0$")
|
||||
self.assertIn("Done: v0.2.0 is cut and pushed", lines[-1])
|
||||
|
||||
def test_keeps_the_reason_a_cut_stopped_in_its_trace_and_a_rerun_writes_its_own(self):
|
||||
self.assert_cut(self.cut())
|
||||
self.assertEqual(self.cut().returncode, 1)
|
||||
first, second = self.traces()
|
||||
self.assertTrue(second.endswith(" release 0.3.0 still holds items in todo.md\n"), second)
|
||||
self.assertNotIn("still holds items", first)
|
||||
|
||||
def test_reads_whether_the_next_releases_review_landed_beside_the_release(self):
|
||||
self.assertEqual(cut.ready_release(str(self.clone), "origin/main"), cut.Ready((0, 2, 0), reviewed=True))
|
||||
self.plan_patch()
|
||||
self.assertEqual(cut.ready_release(str(self.clone), "origin/main"), cut.Ready((0, 1, 1), reviewed=False))
|
||||
|
||||
def test_refuses_before_any_change_where_mains_changelog_has_no_unreleased_heading(self):
|
||||
ran = self.cut_patch(self.plan_patch({"CHANGELOG.md": "# Changelog\n\n- Second.\n- Fixed.\n\n## 0.1.0\n\n- First.\n"}))
|
||||
self.assertEqual(ran.returncode, 1)
|
||||
self.assertIn("CHANGELOG.md on origin/main has no ## Unreleased section", ran.stderr)
|
||||
self.assertEqual(self.origin_git("tag", "-l", "v0.1.1"), "")
|
||||
|
||||
def test_says_where_ci_shs_output_goes_and_keeps_it_there(self):
|
||||
ran = self.cut_patch(self.plan_patch({"ci.sh": "echo the suite ran\nexit 0\n"}))
|
||||
self.assertEqual(ran.returncode, 0, ran.stderr)
|
||||
[log] = (self.temp / "state" / "deploy-release").glob("*-ci.log")
|
||||
self.assertEqual(log.read_text(), "the suite ran\n")
|
||||
self.assertIn(f"ci.sh's output goes to {log}", ran.stderr)
|
||||
|
||||
def plan_patch(self, fix: dict[str, str] | None = None) -> str:
|
||||
"""On top of setUp's main: a feature no patch may ship, item 3 filed under 0.1.1, then the commit that fixes tool.txt and finishes item 3, pushed. The revision range that names only the fix."""
|
||||
self.commit({"feature.txt": "new\n"}, "Add a feature")
|
||||
@@ -678,6 +718,149 @@ class Cut(unittest.TestCase):
|
||||
|
||||
|
||||
|
||||
def still_runs(pid: int) -> bool:
|
||||
"""Whether the process pid runs: one that ended and that nobody has waited for yet doesn't."""
|
||||
try:
|
||||
return pathlib.Path(f"/proc/{pid}/stat").read_text().rsplit(")", 1)[1].split()[0] != "Z"
|
||||
except OSError:
|
||||
return False
|
||||
|
||||
|
||||
class PatchCi(unittest.TestCase):
|
||||
"""The patch's ci.sh run, read in-process with the runner handed in, and the runner itself on a real ci.sh."""
|
||||
|
||||
def setUp(self):
|
||||
self.worktree = pathlib.Path(tempfile.mkdtemp())
|
||||
self.addCleanup(subprocess.run, ["rm", "-rf", str(self.worktree)])
|
||||
self.log = self.worktree / "ci.log"
|
||||
self.said: list[str] = []
|
||||
|
||||
def test_passes_where_ci_sh_exits_zero(self):
|
||||
(self.worktree / "ci.sh").write_text("exit 0\n")
|
||||
seen: list[pathlib.Path] = []
|
||||
self.assertIsNone(patch.run_ci(self.worktree, "origin/main", lambda worktree: seen.append(worktree) or patch.CiRan(0, "fine\n")))
|
||||
self.assertEqual(seen, [self.worktree])
|
||||
|
||||
def test_a_failed_ci_sh_gives_the_last_30_lines_of_its_output(self):
|
||||
(self.worktree / "ci.sh").write_text("exit 1\n")
|
||||
failed = patch.run_ci(self.worktree, "origin/main", lambda worktree: patch.CiRan(1, "".join(f"line {number}\n" for number in range(1, 41))))
|
||||
self.assertIsInstance(failed, Failure)
|
||||
self.assertIn("ci.sh failed on the patch, so nothing is tagged. Fix it on origin/main", failed.reason)
|
||||
self.assertTrue(failed.reason.endswith("\nline 11\n" + "\n".join(f"line {number}" for number in range(12, 41))), failed.reason)
|
||||
self.assertNotIn("line 10\n", failed.reason)
|
||||
|
||||
def test_a_ci_sh_that_could_not_run_or_was_stopped_tags_nothing_and_gives_the_runners_reason(self):
|
||||
(self.worktree / "ci.sh").write_text("exit 0\n")
|
||||
failed = patch.run_ci(self.worktree, "origin/main", lambda worktree: Failure("ci.sh still ran after 30 minutes and was stopped"))
|
||||
self.assertEqual(failed, Failure("ci.sh still ran after 30 minutes and was stopped, so nothing is tagged. Rerun once that is fixed."))
|
||||
|
||||
def test_refuses_a_patch_without_ci_sh_before_running_anything(self):
|
||||
self.assertEqual(patch.run_ci(self.worktree, "origin/main", lambda worktree: self.fail("ran")), Failure("the patch holds no ci.sh to test it with"))
|
||||
|
||||
def test_the_runner_writes_ci_shs_output_to_the_log_as_it_runs_and_gives_its_exit_code(self):
|
||||
(self.worktree / "ci.sh").write_text("echo out\necho err >&2\nexit 3\n")
|
||||
ran = ci_script.ci_runner(self.log, 30, self.said.append)(self.worktree)
|
||||
self.assertEqual(ran, patch.CiRan(3, "out\nerr\n"))
|
||||
self.assertEqual(self.log.read_text(), "out\nerr\n")
|
||||
self.assertEqual(self.said, [f"ci.sh's output goes to {self.log}; it is stopped where it still runs after 30 seconds"])
|
||||
|
||||
def test_the_runner_stops_a_ci_sh_that_hangs_with_what_it_started_and_says_so(self):
|
||||
(self.worktree / "ci.sh").write_text("echo started\nsleep 60 &\necho $! >child.pid\nwait\n")
|
||||
began = time.monotonic()
|
||||
ran = ci_script.ci_runner(self.log, 0.5, self.said.append, grace=0.2)(self.worktree)
|
||||
self.assertLess(time.monotonic() - began, 20)
|
||||
self.assertEqual(ran, Failure(f"ci.sh still ran after 0.5 seconds and was stopped; {self.log} holds its output"))
|
||||
self.assertEqual(self.log.read_text(), "started\n")
|
||||
child = int((self.worktree / "child.pid").read_text())
|
||||
deadline = time.monotonic() + 5
|
||||
while still_runs(child) and time.monotonic() < deadline:
|
||||
time.sleep(0.05)
|
||||
self.assertFalse(still_runs(child), "the process ci.sh started still runs")
|
||||
|
||||
def test_the_runner_gives_a_failure_where_ci_sh_cannot_start(self):
|
||||
ran = ci_script.ci_runner(self.log, 30, self.said.append)(self.worktree / "gone")
|
||||
self.assertIsInstance(ran, Failure)
|
||||
self.assertIn("ci.sh could not run: ", ran.reason)
|
||||
self.assertIn(str(self.worktree / "gone"), ran.reason)
|
||||
|
||||
def test_the_runner_gives_a_failure_where_the_log_cannot_be_written(self):
|
||||
(self.worktree / "ci.sh").write_text("exit 0\n")
|
||||
ran = ci_script.ci_runner(self.worktree / "no-folder" / "ci.log", 30, self.said.append)(self.worktree)
|
||||
self.assertIsInstance(ran, Failure)
|
||||
self.assertIn(str(self.worktree / "no-folder" / "ci.log"), ran.reason)
|
||||
|
||||
|
||||
class Trace(unittest.TestCase):
|
||||
"""What a cut says, in-process, with the clock and stderr handed in."""
|
||||
|
||||
def setUp(self):
|
||||
self.temp = pathlib.Path(tempfile.mkdtemp())
|
||||
self.addCleanup(subprocess.run, ["rm", "-rf", str(self.temp)])
|
||||
self.now = lambda: datetime.datetime(2026, 11, 27, 9, 30, 5)
|
||||
|
||||
def test_keeps_traces_under_the_state_folder_xdg_names_else_under_homes(self):
|
||||
self.assertEqual(trace.trace_folder({"XDG_STATE_HOME": "/x/state"}, pathlib.Path("/home/u")), pathlib.Path("/x/state/deploy-release"))
|
||||
self.assertEqual(trace.trace_folder({}, pathlib.Path("/home/u")), pathlib.Path("/home/u/.local/state/deploy-release"))
|
||||
|
||||
def test_names_a_run_by_its_start_and_its_process(self):
|
||||
self.assertEqual(trace.run_name(self.now(), 4711), "2026-11-27T09-30-05-4711")
|
||||
|
||||
def test_starts_a_trace_with_the_command_then_appends_each_step_with_its_time_and_says_it(self):
|
||||
file = trace.started(self.temp / "state" / "deploy-release", "run", "deploy-release --cut", self.now)
|
||||
self.assertEqual(file, self.temp / "state" / "deploy-release" / "run.log")
|
||||
out = io.StringIO()
|
||||
say = trace.saying(file, self.now, out)
|
||||
say("pushed the tag v0.2.0")
|
||||
say("merged pull request 3")
|
||||
self.assertEqual(out.getvalue(), "deploy-release: pushed the tag v0.2.0\ndeploy-release: merged pull request 3\n")
|
||||
self.assertEqual(file.read_text(), "2026-11-27T09:30:05 deploy-release --cut\n2026-11-27T09:30:05 pushed the tag v0.2.0\n2026-11-27T09:30:05 merged pull request 3\n")
|
||||
|
||||
def test_a_trace_that_cannot_be_started_is_a_failure_naming_the_file(self):
|
||||
(self.temp / "state").write_text("a file where the folder goes\n")
|
||||
made = trace.started(self.temp / "state" / "deploy-release", "run", "deploy-release --cut", self.now)
|
||||
self.assertIsInstance(made, Failure)
|
||||
self.assertIn(str(self.temp / "state" / "deploy-release" / "run.log"), made.reason)
|
||||
|
||||
def test_still_says_each_step_where_there_is_no_trace_or_it_cannot_be_written(self):
|
||||
out = io.StringIO()
|
||||
trace.saying(None, self.now, out)("pushed the tag v0.2.0")
|
||||
trace.saying(self.temp / "gone" / "run.log", self.now, out)("merged pull request 3")
|
||||
self.assertEqual(out.getvalue().splitlines()[:2], ["deploy-release: pushed the tag v0.2.0", "deploy-release: merged pull request 3"])
|
||||
self.assertIn(f"warning: the trace {self.temp / 'gone' / 'run.log'} could not be written", out.getvalue().splitlines()[2])
|
||||
|
||||
|
||||
class Rewritten(unittest.TestCase):
|
||||
"""A cut's file edits, where the filesystem refuses one."""
|
||||
|
||||
def setUp(self):
|
||||
self.temp = pathlib.Path(tempfile.mkdtemp())
|
||||
self.addCleanup(subprocess.run, ["rm", "-rf", str(self.temp)])
|
||||
|
||||
def test_replaces_a_files_text_and_hands_none_for_a_file_that_is_not_there(self):
|
||||
file = self.temp / "CHANGELOG.md"
|
||||
self.assertIsNone(cut.rewritten(file, "head CHANGELOG.md", lambda text: f"{text!r} new\n"))
|
||||
self.assertEqual(file.read_text(), "None new\n")
|
||||
self.assertIsNone(cut.rewritten(file, "head CHANGELOG.md", lambda text: text.upper()))
|
||||
self.assertEqual(file.read_text(), "NONE NEW\n")
|
||||
|
||||
def test_a_file_that_cannot_be_read_or_written_is_a_failure_naming_the_step_and_the_file(self):
|
||||
folder = self.temp / "CHANGELOG.md"
|
||||
folder.mkdir()
|
||||
# A folder can't be read as a file, and a file in a folder that isn't there can't be written.
|
||||
for file in (folder, self.temp / "gone" / "commit.patch"):
|
||||
with self.subTest(file=file):
|
||||
failed = cut.rewritten(file, "head CHANGELOG.md with ## 0.1.1, so nothing is tagged", lambda text: "new\n")
|
||||
self.assertIsInstance(failed, Failure)
|
||||
self.assertTrue(failed.reason.startswith("could not head CHANGELOG.md with ## 0.1.1, so nothing is tagged: "), failed.reason)
|
||||
self.assertIn(str(file), failed.reason)
|
||||
|
||||
def test_gives_the_rewrites_own_failure_and_leaves_the_file(self):
|
||||
file = self.temp / "CHANGELOG.md"
|
||||
file.write_text("old\n")
|
||||
self.assertEqual(cut.rewritten(file, "x", lambda text: Failure("no heading")), Failure("no heading"))
|
||||
self.assertEqual(file.read_text(), "old\n")
|
||||
|
||||
|
||||
class PatchChangelog(unittest.TestCase):
|
||||
"""The changelog edits of a patch cut, read in-process, so a mutation tester reaches them."""
|
||||
|
||||
@@ -698,6 +881,9 @@ class PatchChangelog(unittest.TestCase):
|
||||
def test_puts_the_section_last_where_unreleased_is_the_last_section(self):
|
||||
self.assertEqual(patch.moved_entries("# Changelog\n\n## Unreleased\n\n- Fixed.\n", ["- Fixed."], "0.1.1"), "# Changelog\n\n## Unreleased\n\n## 0.1.1\n\n- Fixed.\n")
|
||||
|
||||
def test_a_changelog_without_an_unreleased_heading_is_a_failure_that_names_it(self):
|
||||
self.assertEqual(patch.moved_entries("# Changelog\n\n## 0.1.0\n\n- First.\n", ["- Fixed."], "0.1.1"), Failure("CHANGELOG.md has no ## Unreleased heading to move the entries of 0.1.1 from"))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user