import contextlib import pathlib import subprocess import sys import tempfile import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent)) from failure import Failure 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) class StalledOrigin(unittest.TestCase): """A checkout whose origin, run through git's ext transport, answers nothing 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_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) if __name__ == "__main__": unittest.main()