Answer principles round 1: 1 🔴, 7 🟡 (0 misread); every effect is handed in as an argument, no test patches a global, and the traces principle scopes tools that run on their user's machine
CI / tests (pull_request) Successful in 3m43s
CI / tests (pull_request) Successful in 3m43s
This commit is contained in:
@@ -9,7 +9,6 @@ import sys
|
||||
import tempfile
|
||||
import typing
|
||||
import unittest
|
||||
import unittest.mock
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
||||
|
||||
@@ -982,6 +981,12 @@ class Hook(unittest.TestCase):
|
||||
git(self.repo, "init", "-q", "-b", "main")
|
||||
git(self.repo, "commit", "-q", "--allow-empty", "-m", "base")
|
||||
git(self.repo, "checkout", "-q", "-b", "chunk")
|
||||
self.env: dict[str, str] = {}
|
||||
|
||||
def configured(self) -> pathlib.Path:
|
||||
folder = configured(self, pathlib.Path(self.repo))
|
||||
self.env = {"HOME": str(folder.parents[1])}
|
||||
return folder
|
||||
|
||||
def test_runs_git_guarded(self):
|
||||
git(self.repo, "config", "core.fsmonitor", "/bin/false")
|
||||
@@ -1109,7 +1114,7 @@ class Hook(unittest.TestCase):
|
||||
self.assertEqual(hook(call, self.repo).returncode, 2)
|
||||
|
||||
def round_commit(self, title: str, given: str = "") -> subprocess.CompletedProcess:
|
||||
return hook({"cwd": self.repo, "tool_input": {"command": f"git commit {given}-m '{title}'"}, "tool_name": "Bash"}, self.repo)
|
||||
return hook({"cwd": self.repo, "tool_input": {"command": f"git commit {given}-m '{title}'"}, "tool_name": "Bash"}, self.repo, self.env)
|
||||
|
||||
def test_a_round_commit_whose_counts_differ_from_its_report_is_refused(self):
|
||||
home = tempfile.mkdtemp()
|
||||
@@ -1189,7 +1194,7 @@ class Hook(unittest.TestCase):
|
||||
self.assertEqual(self.round_commit("Answer stability round 1: 0 🔴, 1 🟡 (0 misread); c").returncode, 0, "another chunk's rounds, fast-forwarded into the default branch, are not this chunk's")
|
||||
|
||||
def test_a_commit_is_titled_by_the_configs_template_and_its_round_title_read_through_it(self):
|
||||
config = configured(self, pathlib.Path(self.repo)) / "config.toml"
|
||||
config = self.configured() / "config.toml"
|
||||
config.write_text(config.read_text() + 'commit-title = "PAY-{id} - {summary}"\n')
|
||||
git(self.repo, "checkout", "-q", "-b", "item-7")
|
||||
for title in ["PAY-7 - Answer stability round 1: 1 🔴, 0 🟡 (0 misread); a", "PAY-7 - Answer stability round 2: 0 🔴, 1 🟡 (0 misread); c"]:
|
||||
@@ -1205,7 +1210,7 @@ class Hook(unittest.TestCase):
|
||||
self.assertEqual(self.round_commit("Tidy # rule-override commit-title: the client asked for this title").returncode, 0)
|
||||
|
||||
def test_a_commit_on_a_branch_that_doesnt_fit_the_configs_branch_template_takes_a_bare_title(self):
|
||||
config = configured(self, pathlib.Path(self.repo)) / "config.toml"
|
||||
config = self.configured() / "config.toml"
|
||||
config.write_text(config.read_text() + 'commit-title = "PAY-{id} - {summary}"\nbranch = "feature/PAY-{id}"\n')
|
||||
git(self.repo, "checkout", "-q", "-b", "release-review-0.3.0")
|
||||
self.assertEqual(self.round_commit("Release review of 0.3.0", "--allow-empty ").returncode, 0)
|
||||
@@ -1217,7 +1222,7 @@ class Hook(unittest.TestCase):
|
||||
self.assertEqual(self.round_commit("PAY-7 - Tidy").returncode, 0)
|
||||
|
||||
def test_a_commit_is_refused_naming_the_fault_while_a_config_cannot_be_read(self):
|
||||
config = configured(self, pathlib.Path(self.repo)) / "config.toml"
|
||||
config = self.configured() / "config.toml"
|
||||
config.write_text(config.read_text() + 'commit-title = "PAY-{id}"\n')
|
||||
refused = self.round_commit("PAY-7 - Tidy")
|
||||
self.assertEqual(refused.returncode, 2)
|
||||
@@ -1234,21 +1239,21 @@ class Hook(unittest.TestCase):
|
||||
self.assertEqual(self.round_commit("Tidy").returncode, 0)
|
||||
|
||||
def test_a_commit_while_an_unranked_todo_has_changes_is_blocked(self):
|
||||
configured(self, pathlib.Path(self.repo))
|
||||
self.configured()
|
||||
pathlib.Path(self.repo, "docs").mkdir()
|
||||
for name, content in [("README.md", README), ("docs/decisions.md", DECISIONS), ("todo.md", TODO)]:
|
||||
pathlib.Path(self.repo, name).write_text(content)
|
||||
git(self.repo, "add", "README.md", "docs", "todo.md")
|
||||
git(self.repo, "commit", "-qm", "x", "--no-verify")
|
||||
call = {"cwd": self.repo, "tool_input": {"command": "git add -A && git commit -m x"}, "tool_name": "Bash"}
|
||||
self.assertEqual(hook(call, self.repo).returncode, 0, "todo.md is unchanged")
|
||||
self.assertEqual(hook(call, self.repo, self.env).returncode, 0, "todo.md is unchanged")
|
||||
pathlib.Path(self.repo, "todo.md").write_text(TODO + "\n")
|
||||
ran = hook(call, self.repo)
|
||||
ran = hook(call, self.repo, self.env)
|
||||
self.assertEqual(ran.returncode, 2)
|
||||
self.assertIn("todo.md: not ranked", ran.stderr)
|
||||
|
||||
def test_a_staged_todo_change_is_checked_and_no_clean_filter_runs(self):
|
||||
configured(self, pathlib.Path(self.repo))
|
||||
self.configured()
|
||||
pathlib.Path(self.repo, "docs").mkdir()
|
||||
for name, content in [("README.md", README), ("docs/decisions.md", DECISIONS), ("todo.md", TODO), (".gitattributes", "todo.md filter=planted\n")]:
|
||||
pathlib.Path(self.repo, name).write_text(content)
|
||||
@@ -1257,7 +1262,7 @@ class Hook(unittest.TestCase):
|
||||
pathlib.Path(self.repo, "ran").unlink(missing_ok=True)
|
||||
# A stat-dirty index entry, as extracting an archive leaves, is what makes `git status` re-run the filter.
|
||||
os.utime(pathlib.Path(self.repo, "todo.md"), (1, 1))
|
||||
ran = hook({"cwd": self.repo, "tool_input": {"command": "git commit -m x"}, "tool_name": "Bash"}, self.repo)
|
||||
ran = hook({"cwd": self.repo, "tool_input": {"command": "git commit -m x"}, "tool_name": "Bash"}, self.repo, self.env)
|
||||
self.assertEqual(ran.returncode, 2, ran.stderr)
|
||||
self.assertFalse(pathlib.Path(self.repo, "ran").exists())
|
||||
|
||||
@@ -1316,6 +1321,7 @@ class TodoRankCheck(unittest.TestCase):
|
||||
pathlib.Path(checkout, "bin", "rank_todo.py").write_text(stub)
|
||||
pathlib.Path(checkout, "todo.md").write_text("# todo\n")
|
||||
self.config = configured(self, pathlib.Path(checkout)) / "config.toml"
|
||||
self.home = self.config.parents[2]
|
||||
return checkout
|
||||
|
||||
def test_a_changed_plan_that_cannot_be_read_is_a_breach(self):
|
||||
@@ -1324,16 +1330,16 @@ class TodoRankCheck(unittest.TestCase):
|
||||
self.addCleanup(pathlib.Path(checkout, "todo.md").chmod, 0o600)
|
||||
self.assertEqual(["Permission denied" in detail for detail in self.details(checkout)], [True])
|
||||
|
||||
def details(self, checkout: str) -> list[str]:
|
||||
return [breach.detail for breach in todo_breaches(checkout, pathlib.Path.home(), pathlib.Path(self.own))]
|
||||
def details(self, checkout: str, **limit: float) -> list[str]:
|
||||
return [breach.detail for breach in todo_breaches(checkout, self.home, pathlib.Path(self.own), **limit)]
|
||||
|
||||
def test_a_checkout_of_the_hooks_repo_runs_its_own_rank_todo(self):
|
||||
checkout = self.changed_plan(f"{self.folder}/clone", self.STUB)
|
||||
self.assertEqual(self.details(checkout), [f"own script checked {checkout}/todo.md"])
|
||||
top = str(pathlib.Path(checkout).resolve())
|
||||
self.assertEqual([(breach.rule, breach.directory) for breach in todo_breaches(checkout, pathlib.Path.home(), pathlib.Path(self.own))], [("todo-rank-check", top)])
|
||||
self.assertEqual([(breach.rule, breach.directory) for breach in todo_breaches(checkout, self.home, pathlib.Path(self.own))], [("todo-rank-check", top)])
|
||||
self.config.unlink()
|
||||
self.assertEqual([(breach.rule, breach.directory) for breach in todo_breaches(checkout, pathlib.Path.home(), pathlib.Path(self.own))], [("todo-rank-check", top)], "a todo.md with no config")
|
||||
self.assertEqual([(breach.rule, breach.directory) for breach in todo_breaches(checkout, self.home, pathlib.Path(self.own))], [("todo-rank-check", top)], "a todo.md with no config")
|
||||
|
||||
def test_its_own_rank_todo_passing_passes(self):
|
||||
self.assertEqual(self.details(self.changed_plan(f"{self.folder}/clone", "")), [])
|
||||
@@ -1344,8 +1350,7 @@ class TodoRankCheck(unittest.TestCase):
|
||||
|
||||
def test_its_own_rank_todo_running_past_the_limit_is_named(self):
|
||||
checkout = self.changed_plan(f"{self.folder}/clone", "import signal\nsignal.pause()\n")
|
||||
with unittest.mock.patch("rule_hook.todo_check.OWN_CHECK_SECONDS", 0.2):
|
||||
self.assertEqual(self.details(checkout), [f"{checkout}/bin/rank_todo.py --check did not finish in 0.2 seconds"])
|
||||
self.assertEqual(self.details(checkout, seconds=0.2), [f"{checkout}/bin/rank_todo.py --check did not finish in 0.2 seconds"])
|
||||
|
||||
def test_another_repos_rank_todo_never_runs(self):
|
||||
reasons = self.details(self.changed_plan(self.other, self.STUB))
|
||||
@@ -1375,7 +1380,7 @@ class TodoRankCheck(unittest.TestCase):
|
||||
self.config.write_text(self.config.read_text().replace('"checkout:todo.md"', '"folder:todo.md"'))
|
||||
(self.config.parent / "todo.md").write_text("# todo\n")
|
||||
self.assertEqual(self.details(folder), [f"own script checked {folder}/todo.md"], "checked with the checkout's own rank_todo.py, as a commit there would be")
|
||||
self.assertEqual([(breach.rule, breach.directory) for breach in todo_breaches(folder, pathlib.Path.home(), pathlib.Path(self.own))], [("todo-rank-check", str(pathlib.Path(folder).resolve()))])
|
||||
self.assertEqual([(breach.rule, breach.directory) for breach in todo_breaches(folder, self.home, pathlib.Path(self.own))], [("todo-rank-check", str(pathlib.Path(folder).resolve()))])
|
||||
git(folder, "add", "-A")
|
||||
git(folder, "commit", "-q", "-m", "plan")
|
||||
self.assertEqual(self.details(folder), [], "committed, so unchanged")
|
||||
|
||||
@@ -19,14 +19,14 @@ OWN = pathlib.Path(__file__).resolve().parents[2]
|
||||
OWN_CHECK_SECONDS = 20
|
||||
|
||||
|
||||
def todo_breaches(directory: str, home: pathlib.Path, own: pathlib.Path = OWN) -> list[Breach]:
|
||||
def todo_breaches(directory: str, home: pathlib.Path, own: pathlib.Path = OWN, seconds: float = OWN_CHECK_SECONDS) -> list[Breach]:
|
||||
"""Why the plan of directory's project is not ranked, where the plan has changed; the plan is the todo.md its config names. directory may also be in a config's folder, a git repo of its own."""
|
||||
top = guarded_git(directory, "rev-parse", "--show-toplevel")
|
||||
if isinstance(top, Failure):
|
||||
return []
|
||||
held_here = held(pathlib.Path(top), home)
|
||||
if held_here is not None:
|
||||
return folder_breaches(top, held_here, own)
|
||||
return folder_breaches(top, held_here, own, seconds)
|
||||
config = located(top, home, functools.partial(git, guarded=True))
|
||||
if isinstance(config, Failure):
|
||||
# A checkout whose todo.md changes with no config is one whose config is still to write. An unchanged todo.md may be a third party's, whose clone needs no config.
|
||||
@@ -39,10 +39,10 @@ def todo_breaches(directory: str, home: pathlib.Path, own: pathlib.Path = OWN) -
|
||||
# A plan outside the checkout has no commit in the checkout to differ from, so it is checked on every call.
|
||||
if tracked is not None and not changed(top, todo, tracked):
|
||||
return []
|
||||
return [Breach("todo-rank-check", line, top) for line in check_failures(top, todo, config, own)]
|
||||
return [Breach("todo-rank-check", line, top) for line in check_failures(top, todo, config, own, seconds)]
|
||||
|
||||
|
||||
def folder_breaches(top: str, config: ProjectConfig | Failure, own: pathlib.Path) -> list[Breach]:
|
||||
def folder_breaches(top: str, config: ProjectConfig | Failure, own: pathlib.Path, seconds: float) -> list[Breach]:
|
||||
"""Why the plan kept in top, a config's folder, is not ranked, where the plan has changed in that folder's repo; ranked as for a commit in the project's checkout.
|
||||
|
||||
A config that fails to load doesn't block anything here, so that the commit that fixes it goes through."""
|
||||
@@ -53,18 +53,18 @@ def folder_breaches(top: str, config: ProjectConfig | Failure, own: pathlib.Path
|
||||
return []
|
||||
if not todo.is_file() or not changed(str(config.folder), todo, todo.relative_to(config.folder).as_posix()):
|
||||
return []
|
||||
return [Breach("todo-rank-check", line, top) for line in check_failures(str(config.top), todo, config, own)]
|
||||
return [Breach("todo-rank-check", line, top) for line in check_failures(str(config.top), todo, config, own, seconds)]
|
||||
|
||||
|
||||
def check_failures(top: str, todo: pathlib.Path, config: ProjectConfig, own: pathlib.Path) -> list[str]:
|
||||
def check_failures(top: str, todo: pathlib.Path, config: ProjectConfig, own: pathlib.Path, seconds: float) -> list[str]:
|
||||
"""Why rank_todo.py --check fails on todo. A checkout of the repo the hook runs from checks with its own bin/rank_todo.py, so a branch that changes the table's shape is held to its own rules. Any other checkout checks with the hook's ranking: running its script would run its code before every commit."""
|
||||
script = pathlib.Path(top) / "bin" / "rank_todo.py"
|
||||
if not script.is_file() or not same_repo(top, own):
|
||||
return file_plan(config, todo).check()
|
||||
try:
|
||||
ran = subprocess.run([sys.executable, str(script), "--check", str(todo)], capture_output=True, cwd=top, text=True, timeout=OWN_CHECK_SECONDS)
|
||||
ran = subprocess.run([sys.executable, str(script), "--check", str(todo)], capture_output=True, cwd=top, text=True, timeout=seconds)
|
||||
except subprocess.TimeoutExpired:
|
||||
return [f"{script} --check did not finish in {OWN_CHECK_SECONDS} seconds"]
|
||||
return [f"{script} --check did not finish in {seconds} seconds"]
|
||||
except OSError as failure:
|
||||
return [f"{script} --check could not run: {failure}"]
|
||||
if ran.returncode == 0:
|
||||
|
||||
Reference in New Issue
Block a user