import contextlib import pathlib import subprocess import sys import tempfile import time import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent)) from failure import Failure import git_repo 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): started = time.monotonic() ref = git_repo.default_ref(self.root, 0.5) self.assertLess(time.monotonic() - started, 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): started = time.monotonic() with contextlib.chdir(self.root): found = git_repo.default_branch(stale_ok=True, timeout=0.5) self.assertLess(time.monotonic() - started, 5) self.assertIsInstance(found, Failure) self.assertIn("stopped after 0.5 s", found.reason) if __name__ == "__main__": unittest.main()