Files
claude-config/bin/git_cli/test_git_repo.py
T

146 lines
6.4 KiB
Python

import contextlib
import os
import pathlib
import subprocess
import sys
import tempfile
import unittest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from failure import Failure
from git_cli import git_repo
class Git(unittest.TestCase):
# Each command is a shell alias, so it touches no repository, whatever GIT_DIR the test runs under.
def test_stops_git_still_running_after_the_timeout_and_says_so(self):
done = git_repo.git("-c", "alias.hang=!sleep 5", "hang", timeout=0.2)
self.assertIsInstance(done, Failure)
self.assertIn("hang", done.reason)
self.assertIn("0.2 s", done.reason)
def test_says_git_printed_text_that_is_not_utf_8(self):
done = git_repo.git("-c", "alias.latin=!printf 'caf\\351'", "latin")
self.assertIsInstance(done, Failure)
self.assertIn("latin", done.reason)
self.assertIn("UTF-8", done.reason)
def test_reads_each_byte_that_is_not_utf_8_as_a_replacement_character_where_asked(self):
self.assertEqual(git_repo.git("-c", "alias.latin=!printf 'caf\\351'", "latin", errors="replace"), "caf�")
def test_says_which_command_could_not_start_in_a_directory_that_is_gone(self):
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)
def test_runs_git_in_the_environment_given_and_only_guarded_stops_the_fetch_of_missing_objects(self):
shown = ("-c", "alias.shown=!echo \"$SHOWN_TO_GIT/$GIT_NO_LAZY_FETCH\"", "shown")
given = {**{name: value for name, value in os.environ.items() if name != "GIT_NO_LAZY_FETCH"}, "SHOWN_TO_GIT": "given"}
self.assertEqual(git_repo.git(*shown, env=given), "given/\n")
self.assertEqual(git_repo.git(*shown, env=given, guarded=True), "given/1\n")
self.assertNotIn("SHOWN_TO_GIT", os.environ)
self.assertEqual(git_repo.git(*shown, guarded=True), "/1\n")
class Guarded(unittest.TestCase):
"""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 = temp.name
self.config("init", "--quiet")
def config(self, *args):
subprocess.run(["git", *args], capture_output=True, check=True, cwd=self.root)
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):
"""A checkout whose origin, run through git's ext transport, doesn't answer for 10 s, and which records no default branch for it."""
def setUp(self):
temp = tempfile.TemporaryDirectory()
self.addCleanup(temp.cleanup)
self.root = temp.name
for args in (("init", "--quiet"), ("config", "protocol.ext.allow", "always"), ("remote", "add", "origin", "ext::sleep 10")):
subprocess.run(["git", *args], capture_output=True, check=True, cwd=self.root)
def test_reading_the_default_branch_fails_after_the_timeout(self):
ref = git_repo.default_ref(self.root, 0.5)
self.assertIsInstance(ref, Failure)
self.assertIn("stopped after 0.5 s", ref.reason)
def test_reading_the_default_branch_guarded_asks_no_one_and_fails(self):
ref = git_repo.default_ref(self.root, 0.5, guarded=True)
self.assertIsInstance(ref, Failure)
self.assertIn("origin records no default branch", ref.reason)
def test_fetching_the_default_branch_fails_after_the_timeout(self):
with contextlib.chdir(self.root):
found = git_repo.default_branch(stale_ok=True, timeout=0.5)
self.assertIsInstance(found, Failure)
self.assertIn("stopped after 0.5 s", found.reason)
class TextAt(unittest.TestCase):
def test_reads_the_file_the_revision_lists(self):
answers = {"ls-tree": "todo.md\n", "show": "# todo\n"}
self.assertEqual(git_repo.text_at("r", "todo.md", lambda *args: answers[args[0]]), "# todo\n")
def test_reads_empty_where_the_revision_lists_no_such_file(self):
self.assertEqual(git_repo.text_at("r", "todo.md", lambda *args: "" if args[0] == "ls-tree" else Failure("no such file")), "")
def test_fails_where_git_cannot_list_the_revision(self):
self.assertEqual(git_repo.text_at("r", "todo.md", lambda *args: Failure("bad revision")), Failure("bad revision"))
class FileAt(unittest.TestCase):
def setUp(self):
temp = tempfile.TemporaryDirectory()
self.addCleanup(temp.cleanup)
self.root = temp.name
self.run_git("init", "--quiet")
pathlib.Path(self.root, "todo.md").write_text("# todo\n")
self.run_git("add", "todo.md")
self.run_git("-c", "user.name=t", "-c", "user.email=t@t", "-c", "commit.gpgSign=false", "commit", "--quiet", "-m", "Start")
def run_git(self, *args):
subprocess.run(["git", *args], capture_output=True, check=True, cwd=self.root)
def test_reads_the_file_in_the_checkout_cwd_names_while_running_in_another_directory(self):
with tempfile.TemporaryDirectory() as elsewhere, contextlib.chdir(elsewhere):
self.assertEqual(git_repo.file_at("HEAD", "todo.md", self.root), "# todo\n")
if __name__ == "__main__":
unittest.main()