Files
repo-scoping/tests/test_git_ingestion.py
2026-04-26 19:17:36 +02:00

83 lines
2.5 KiB
Python

import subprocess
from repo_registry.repo_ingestion.git import GitIngestionService
def run(command, cwd):
subprocess.run(command, cwd=cwd, check=True, capture_output=True, text=True)
def make_git_repo(path):
path.mkdir()
run(["git", "init", "-b", "main"], path)
run(["git", "config", "user.email", "tests@example.com"], path)
run(["git", "config", "user.name", "Tests"], path)
(path / "README.md").write_text("# Clone Me\n", encoding="utf-8")
(path / "app.py").write_text("print('ok')\n", encoding="utf-8")
run(["git", "add", "."], path)
run(["git", "commit", "-m", "initial"], path)
def test_ingestion_keeps_local_paths_local(tmp_path):
source = tmp_path / "source"
source.mkdir()
checkout = GitIngestionService(tmp_path / "checkouts").resolve(str(source))
assert checkout.source_path == source.resolve()
assert checkout.was_cloned is False
def test_ingestion_clones_file_url(tmp_path):
source = tmp_path / "source"
make_git_repo(source)
checkout = GitIngestionService(tmp_path / "checkouts").resolve(source.as_uri())
assert checkout.was_cloned is True
assert checkout.source_path != source.resolve()
assert (checkout.source_path / "README.md").exists()
branch = subprocess.run(
["git", "branch", "--show-current"],
cwd=checkout.source_path,
check=True,
capture_output=True,
text=True,
).stdout.strip()
assert branch == "main"
def test_git_commands_fail_fast_and_accept_ephemeral_http_credentials(monkeypatch):
calls = []
def fake_run(command, **kwargs):
calls.append((command, kwargs))
return subprocess.CompletedProcess(
command,
128,
stdout="",
stderr="fatal: could not read Username for 'https://example.com': terminal prompts disabled",
)
monkeypatch.setattr(subprocess, "run", fake_run)
service = GitIngestionService()
try:
service._run_git(
["clone", "https://example.com/private.git", "/tmp/private"],
cwd=None,
access_username="user",
access_password="secret",
)
except RuntimeError as exc:
message = str(exc)
else:
raise AssertionError("expected authentication failure")
command, kwargs = calls[0]
assert command[:3] == ["git", "-c", "http.extraHeader=Authorization: Basic dXNlcjpzZWNyZXQ="]
assert kwargs["env"]["GIT_TERMINAL_PROMPT"] == "0"
assert "authentication required" in message
assert "secret" not in message