Answer principles round 1: 0 🔴, 2 🟡; test every guard, name a non-UTF-8 file name as an exit 1, fail on a malformed rev-parse

This commit is contained in:
2026-10-21 20:05:36 +02:00
parent 3fc6790bee
commit 3ae89c3acf
4 changed files with 44 additions and 18 deletions
+1 -1
View File
@@ -327,7 +327,7 @@ class Premises(unittest.TestCase):
(checkout.temp / "fake-bin" / "git").unlink()
run = checkout.run([worker()])
self.assertEqual((run.code, run.calls), (1, []))
self.assertEqual(run.stderr.strip(), "git rev-parse --show-toplevel could not start: [Errno 2] No such file or directory: 'git'")
self.assertTrue(run.stderr.startswith("git rev-parse --show-toplevel could not start: "), run.stderr)
def test_makes_no_worktree_for_a_readme_without_premises(self):
checkout = Checkout(self)
+6 -5
View File
@@ -25,11 +25,11 @@ single tracked file.
Below the table it names:
- each run it skipped because its commit is not in the checkout;
- each run it could not score because its places name no measured unit, or every one;
- each file left out of its run because git cannot show it, such as a file that is not UTF-8, or
it does not parse on this Python.
- each file left out of its run because git's output for it is not UTF-8, because git cannot show
it, or because it does not parse on this Python.
Exit 1, saying why, on unknown arguments, or when git cannot start or finds no checkout, the history
cannot be read, or no scoring run could be scored.
Exit 1, saying why, on unknown arguments, or when git cannot start or finds no checkout, a tracked
file's name is not UTF-8, the history cannot be read, or no scoring run could be scored.
"""
import pathlib
import statistics
@@ -110,7 +110,8 @@ def noted(notes: list[str]) -> str:
def run_scores(run: Entry, top: str) -> tuple[RunScores | None, list[str]] | Failure:
"""The run's scores, and why each file git cannot show or that does not parse was left out."""
"""The run's scores, and why each file left out was: git could not show it as UTF-8, or it does not parse."""
# A tracked name that is not UTF-8 fails the run rather than reading with its bytes replaced: every tool reads git's output through git_repo.git's one contract.
listed = git("ls-tree", "-r", "-z", "--name-only", run.commit, cwd=top)
if isinstance(listed, Failure):
return listed
+3 -1
View File
@@ -115,7 +115,9 @@ def located() -> Checkout | Failure:
found = git("rev-parse", "--path-format=absolute", "--show-toplevel", "--git-common-dir")
if isinstance(found, Failure):
return found
top, common = found.splitlines()
if len(lines := found.splitlines()) != 2:
return Failure(f"git rev-parse printed {found!r}, not a checkout's top folder and git directory on two lines")
top, common = lines
listed = git("ls-files", "-z", cwd=top)
if isinstance(listed, Failure):
return listed
+34 -11
View File
@@ -27,26 +27,49 @@ class Git(unittest.TestCase):
self.assertIn("UTF-8", done.reason)
def test_says_which_command_could_not_start_in_a_directory_that_is_gone(self):
self.assertEqual(git_repo.git("log", cwd="/code/gone"), Failure("git log could not start: [Errno 2] No such file or directory: '/code/gone'"))
done = git_repo.git("log", cwd="/code/gone")
self.assertIsInstance(done, Failure)
self.assertTrue(done.reason.startswith("git log could not start: "), done.reason)
self.assertIn("/code/gone", done.reason)
class Guarded(unittest.TestCase):
"""A checkout whose own config allows git's ext transport, which runs a program, for its origin."""
"""A checkout whose own config may name a program for git to run."""
def setUp(self):
temp = tempfile.TemporaryDirectory()
self.addCleanup(temp.cleanup)
self.root = pathlib.Path(temp.name)
for args in (("init", "--quiet"), ("config", "protocol.ext.allow", "always"), ("remote", "add", "origin", "ext::touch ran")):
subprocess.run(["git", *args], capture_output=True, check=True, cwd=self.root)
self.root = temp.name
self.config("init", "--quiet")
def test_runs_no_transport_the_checkout_allows(self):
self.assertIsInstance(git_repo.git("ls-remote", "origin", cwd=str(self.root), guarded=True), Failure)
self.assertFalse((self.root / "ran").exists())
def config(self, *args):
subprocess.run(["git", *args], capture_output=True, check=True, cwd=self.root)
def test_runs_it_unguarded(self):
git_repo.git("ls-remote", "origin", cwd=str(self.root))
self.assertTrue((self.root / "ran").exists())
def test_runs_a_command_that_needs_no_transport(self):
self.assertEqual(git_repo.git("rev-parse", "--is-inside-work-tree", cwd=self.root, guarded=True), "true\n")
def test_refuses_each_transport_the_checkout_allows_by_name(self):
# Port 1 refuses a connection, so a transport let through fails without the refusal named.
for name, url in (("ext", "ext::true"), ("file", f"file://{self.root}"), ("git", "git://127.0.0.1:1/x"), ("http", "http://127.0.0.1:1/x"), ("https", "https://127.0.0.1:1/x"), ("ssh", "ssh://127.0.0.1:1/x")):
with self.subTest(name):
self.config("config", f"protocol.{name}.allow", "always")
done = git_repo.git("ls-remote", url, cwd=self.root, guarded=True)
self.assertIsInstance(done, Failure)
self.assertIn(f"transport '{name}' not allowed", done.reason)
def test_refuses_a_transport_it_does_not_name(self):
done = git_repo.git("ls-remote", "ftp://127.0.0.1:1/x", cwd=self.root, guarded=True)
self.assertIsInstance(done, Failure)
self.assertIn("transport 'ftp' not allowed", done.reason)
def test_turns_off_the_fsmonitor_and_signature_check_the_checkout_turns_on(self):
self.config("config", "core.fsmonitor", "/bin/false")
self.config("config", "log.showSignature", "true")
self.assertEqual(git_repo.git("config", "core.fsmonitor", cwd=self.root, guarded=True), "false\n")
self.assertEqual(git_repo.git("config", "log.showSignature", cwd=self.root, guarded=True), "false\n")
def test_stops_a_partial_clone_fetching_its_missing_objects(self):
self.assertEqual(git_repo.git("-c", "alias.lazy=!printenv GIT_NO_LAZY_FETCH", "lazy", cwd=self.root, guarded=True), "1\n")
class StalledOrigin(unittest.TestCase):