Files
aios/tests/test_gitea_transport_consistency_provider.py
T

82 lines
2.8 KiB
Python

from __future__ import annotations
import tempfile
import unittest
from pathlib import Path
from gitea_transport_consistency_provider import (
GitCommandResult,
GiteaProviderError,
collect_gitea_transport_consistency,
repository_identity_from_remote,
)
SRC = "a"*40
TREE = "b"*40
BRANCH = "refs/heads/agent/v2r2c2-p0-ttl-captrace-contract-20260909"
def mapping(remote_oid=SRC, url="https://g.cnnetmkt.top/git/aios/aios.git"):
return {
("rev-parse", "--is-inside-work-tree"): "true\n",
("rev-parse", "HEAD^{commit}"): SRC+"\n",
("rev-parse", f"{SRC}^{{tree}}"): TREE+"\n",
("symbolic-ref", "--quiet", "HEAD"): BRANCH+"\n",
("remote", "get-url", "origin"): url+"\n",
("ls-remote", "--exit-code", "origin", BRANCH): f"{remote_oid}\t{BRANCH}\n",
}
def runner(mp):
def run(args, cwd):
val = mp[tuple(args[1:])]
if isinstance(val, GitCommandResult):
return val
return GitCommandResult(0, val, "")
return run
class Tests(unittest.TestCase):
def call(self, mp):
with tempfile.TemporaryDirectory() as d:
return collect_gitea_transport_consistency(
repo_root=Path(d),
source_oid=SRC,
tree_oid=TREE,
git_runner=runner(mp),
)
def test_identity_parsing(self):
self.assertEqual(repository_identity_from_remote(
"https://user:secret@g.cnnetmkt.top/git/aios/aios.git?x=y"
), "aios/aios")
self.assertEqual(repository_identity_from_remote(
"git@g.cnnetmkt.top:aios/aios.git"
), "aios/aios")
def test_exact_remote_ref_passes(self):
r = self.call(mapping())
self.assertTrue(r["consistent"])
self.assertEqual(r["remote_ref_oid"], SRC)
self.assertFalse(r["api_verified"])
self.assertEqual(r["transport"], "git-ls-remote")
def test_remote_oid_mismatch_denied(self):
with self.assertRaisesRegex(GiteaProviderError, "GITEA_REMOTE_REF_OID_MISMATCH"):
self.call(mapping(remote_oid="c"*40))
def test_repository_mismatch_denied(self):
with self.assertRaisesRegex(GiteaProviderError, "GITEA_REPOSITORY_IDENTITY_MISMATCH"):
self.call(mapping(url="https://g.cnnetmkt.top/git/other/repo.git"))
def test_local_source_mismatch_denied(self):
mp = mapping()
mp[("rev-parse", "HEAD^{commit}")] = "d"*40+"\n"
with self.assertRaisesRegex(GiteaProviderError, "GITEA_LOCAL_SOURCE_OID_MISMATCH"):
self.call(mp)
def test_remote_failure_denied(self):
mp = mapping()
mp[("ls-remote", "--exit-code", "origin", BRANCH)] = GitCommandResult(2,"","down")
with self.assertRaisesRegex(GiteaProviderError, "GITEA_REMOTE_REF_UNAVAILABLE"):
self.call(mp)
if __name__ == "__main__":
unittest.main()