76 lines
2.8 KiB
Python
76 lines
2.8 KiB
Python
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)
|
|
|
|
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'"))
|
|
|
|
|
|
class Guarded(unittest.TestCase):
|
|
"""A checkout whose own config allows git's ext transport, which runs a program, for its origin."""
|
|
|
|
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)
|
|
|
|
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 test_runs_it_unguarded(self):
|
|
git_repo.git("ls-remote", "origin", cwd=str(self.root))
|
|
self.assertTrue((self.root / "ran").exists())
|
|
|
|
|
|
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()
|