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
+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):