import datetime from src import cli from src.config import Config, FetchConfig from src.models import GiteaRepository, GiteaUser def make_repo() -> GiteaRepository: return GiteaRepository( ssh_url="ssh://git@example.com/alex/repo.git", clone_url="https://example.com/alex/repo.git", name="repo", id=42, updated_at=datetime.datetime.now(), owner=GiteaUser( id=23, login="alex", email="alex@example.com", ), ) def make_config(tmp_path, fetch: FetchConfig) -> Config: return Config( api_token="api-token", endpoint="https://example.com", repository_format="{owner}/{name}", out_dir=str(tmp_path), fetch=fetch, ) def test_process_repo_clones_with_ssh_url_and_key_env(monkeypatch, tmp_path): calls = [] def fake_git_clone(repository_url, repository, env): calls.append((repository_url, repository, env)) return True monkeypatch.setattr(cli, "git_clone", fake_git_clone) assert cli.process_repo( config=make_config(tmp_path, FetchConfig(ssh_key="/tmp/test_key")), repo=make_repo(), ) assert calls == [ ( "ssh://git@example.com/alex/repo.git", str(tmp_path / "alex" / "repo"), { "GIT_SSH_COMMAND": ( "ssh -i /tmp/test_key -o IdentitiesOnly=yes" ), }, ) ] def test_process_repo_clones_with_https_url_and_auth_env(monkeypatch, tmp_path): calls = [] def fake_git_clone(repository_url, repository, env): calls.append((repository_url, repository, env)) return True monkeypatch.setattr(cli, "git_clone", fake_git_clone) assert cli.process_repo( config=make_config(tmp_path, FetchConfig(user="alex", token="secret")), repo=make_repo(), ) repository_url, repository, env = calls[0] assert repository_url == "https://example.com/alex/repo.git" assert repository == str(tmp_path / "alex" / "repo") assert env["GIT_CONFIG_COUNT"] == "1" assert env["GIT_CONFIG_KEY_0"] == "http.extraHeader" assert env["GIT_CONFIG_VALUE_0"].startswith("Authorization: Basic ") def test_process_repo_updates_existing_mirror_with_fetch_env( monkeypatch, tmp_path, ): calls = [] repo_path = tmp_path / "alex" / "repo" (repo_path / "objects").mkdir(parents=True) (repo_path / "config").write_text("[core]\nrepositoryformatversion = 0\n") def fake_git_update_mirror(repository, env): calls.append((repository, env)) return True monkeypatch.setattr(cli, "git_update_mirror", fake_git_update_mirror) assert cli.process_repo( config=make_config(tmp_path, FetchConfig(ssh_key="/tmp/test_key")), repo=make_repo(), ) assert calls == [ ( str(tmp_path / "alex" / "repo"), { "GIT_SSH_COMMAND": ( "ssh -i /tmp/test_key -o IdentitiesOnly=yes" ), }, ) ] def test_process_repo_returns_false_when_clone_fails(monkeypatch, tmp_path): monkeypatch.setattr(cli, "git_clone", lambda **kwargs: False) assert not cli.process_repo( config=make_config(tmp_path, FetchConfig(ssh_key="/tmp/test_key")), repo=make_repo(), ) def test_process_repo_returns_false_when_update_fails(monkeypatch, tmp_path): repo_path = tmp_path / "alex" / "repo" (repo_path / "objects").mkdir(parents=True) (repo_path / "config").write_text("[core]\nrepositoryformatversion = 0\n") monkeypatch.setattr( cli, "git_update_mirror", lambda repository, env: False, ) assert not cli.process_repo( config=make_config(tmp_path, FetchConfig(ssh_key="/tmp/test_key")), repo=make_repo(), )