73 lines
2.1 KiB
Python
73 lines
2.1 KiB
Python
import base64
|
|
import os
|
|
import subprocess
|
|
|
|
from src.git import git_clone, git_pull, 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_passes_url_and_env_to_git(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", "ssh://git@example.com/owner/repo.git", "."]
|
|
assert cwd == str(tmp_path)
|
|
assert os.environ.items() <= actual_env.items()
|
|
assert env.items() <= actual_env.items()
|
|
|
|
|
|
def test_git_pull_passes_env_to_git(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_pull(repository=str(tmp_path), env=env)
|
|
|
|
args, cwd, actual_env = calls[0]
|
|
assert args == ["git", "pull"]
|
|
assert cwd == str(tmp_path)
|
|
assert os.environ.items() <= actual_env.items()
|
|
assert env.items() <= actual_env.items()
|