import base64 import os import subprocess from src.git import git_clone, git_update_mirror, http_git_env, ssh_git_env def test_ssh_git_env_uses_requested_identity_file(): env = ssh_git_env("/tmp/test_key") assert env == { "GIT_SSH_COMMAND": "ssh -i /tmp/test_key -o IdentitiesOnly=yes" } def test_ssh_git_env_quotes_identity_file_path(): env = ssh_git_env("/tmp/test key") assert env == { "GIT_SSH_COMMAND": "ssh -i '/tmp/test key' -o IdentitiesOnly=yes" } def test_http_git_env_uses_basic_auth_header_without_url_credentials(): env = http_git_env(user="alex", token="secret") expected_credentials = base64.b64encode(b"alex:secret").decode("ascii") assert env == { "GIT_CONFIG_COUNT": "1", "GIT_CONFIG_KEY_0": "http.extraHeader", "GIT_CONFIG_VALUE_0": f"Authorization: Basic {expected_credentials}", } def test_git_clone_uses_mirror_clone(monkeypatch, tmp_path): calls = [] env = {"GIT_SSH_COMMAND": "ssh -i /tmp/test_key -o IdentitiesOnly=yes"} def fake_check_call(args, cwd, env): calls.append((args, cwd, env)) monkeypatch.setattr(subprocess, "check_call", fake_check_call) assert git_clone( repository_url="ssh://git@example.com/owner/repo.git", repository=str(tmp_path), env=env, ) args, cwd, actual_env = calls[0] assert args == [ "git", "clone", "--mirror", "ssh://git@example.com/owner/repo.git", str(tmp_path), ] assert cwd is None assert os.environ.items() <= actual_env.items() assert env.items() <= actual_env.items() def test_git_update_mirror_prunes_remote_refs(monkeypatch, tmp_path): calls = [] env = {"GIT_CONFIG_COUNT": "1"} def fake_check_call(args, cwd, env): calls.append((args, cwd, env)) monkeypatch.setattr(subprocess, "check_call", fake_check_call) assert git_update_mirror(repository=str(tmp_path), env=env) args, cwd, actual_env = calls[0] assert args == ["git", "remote", "update", "--prune"] assert cwd == str(tmp_path) assert os.environ.items() <= actual_env.items() assert env.items() <= actual_env.items()