This commit is contained in:
@@ -1,19 +1,10 @@
|
||||
import datetime
|
||||
import importlib.util
|
||||
from pathlib import Path
|
||||
|
||||
from src import cli
|
||||
from src.config import Config, FetchConfig
|
||||
from src.models import GiteaRepository, GiteaUser
|
||||
|
||||
|
||||
def load_mirror_module():
|
||||
module_path = Path(__file__).parent.parent / "gitea-mirror.py"
|
||||
spec = importlib.util.spec_from_file_location("gitea_mirror", module_path)
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
return module
|
||||
|
||||
|
||||
def make_repo() -> GiteaRepository:
|
||||
return GiteaRepository(
|
||||
ssh_url="ssh://git@example.com/alex/repo.git",
|
||||
@@ -40,16 +31,15 @@ def make_config(tmp_path, fetch: FetchConfig) -> Config:
|
||||
|
||||
|
||||
def test_process_repo_clones_with_ssh_url_and_key_env(monkeypatch, tmp_path):
|
||||
mirror = load_mirror_module()
|
||||
calls = []
|
||||
|
||||
def fake_git_clone(repository_url, repository, env):
|
||||
calls.append((repository_url, repository, env))
|
||||
return True
|
||||
|
||||
monkeypatch.setattr(mirror, "git_clone", fake_git_clone)
|
||||
monkeypatch.setattr(cli, "git_clone", fake_git_clone)
|
||||
|
||||
mirror.process_repo(
|
||||
assert cli.process_repo(
|
||||
config=make_config(tmp_path, FetchConfig(ssh_key="/tmp/test_key")),
|
||||
repo=make_repo(),
|
||||
)
|
||||
@@ -68,16 +58,15 @@ def test_process_repo_clones_with_ssh_url_and_key_env(monkeypatch, tmp_path):
|
||||
|
||||
|
||||
def test_process_repo_clones_with_https_url_and_auth_env(monkeypatch, tmp_path):
|
||||
mirror = load_mirror_module()
|
||||
calls = []
|
||||
|
||||
def fake_git_clone(repository_url, repository, env):
|
||||
calls.append((repository_url, repository, env))
|
||||
return True
|
||||
|
||||
monkeypatch.setattr(mirror, "git_clone", fake_git_clone)
|
||||
monkeypatch.setattr(cli, "git_clone", fake_git_clone)
|
||||
|
||||
mirror.process_repo(
|
||||
assert cli.process_repo(
|
||||
config=make_config(tmp_path, FetchConfig(user="alex", token="secret")),
|
||||
repo=make_repo(),
|
||||
)
|
||||
@@ -91,7 +80,6 @@ def test_process_repo_clones_with_https_url_and_auth_env(monkeypatch, tmp_path):
|
||||
|
||||
|
||||
def test_process_repo_pulls_existing_repo_with_fetch_env(monkeypatch, tmp_path):
|
||||
mirror = load_mirror_module()
|
||||
calls = []
|
||||
repo_path = tmp_path / "alex" / "repo" / ".git"
|
||||
repo_path.mkdir(parents=True)
|
||||
@@ -100,9 +88,9 @@ def test_process_repo_pulls_existing_repo_with_fetch_env(monkeypatch, tmp_path):
|
||||
calls.append((repository, env))
|
||||
return True
|
||||
|
||||
monkeypatch.setattr(mirror, "git_pull", fake_git_pull)
|
||||
monkeypatch.setattr(cli, "git_pull", fake_git_pull)
|
||||
|
||||
mirror.process_repo(
|
||||
assert cli.process_repo(
|
||||
config=make_config(tmp_path, FetchConfig(ssh_key="/tmp/test_key")),
|
||||
repo=make_repo(),
|
||||
)
|
||||
@@ -117,3 +105,23 @@ def test_process_repo_pulls_existing_repo_with_fetch_env(monkeypatch, tmp_path):
|
||||
},
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
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_pull_fails(monkeypatch, tmp_path):
|
||||
repo_path = tmp_path / "alex" / "repo" / ".git"
|
||||
repo_path.mkdir(parents=True)
|
||||
monkeypatch.setattr(cli, "git_pull", lambda repository, env: False)
|
||||
|
||||
assert not cli.process_repo(
|
||||
config=make_config(tmp_path, FetchConfig(ssh_key="/tmp/test_key")),
|
||||
repo=make_repo(),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user