test(tests): ✅ Add pytest fixtures to stub external service dependencies and Git utilities for improved test isolation
Co-Authored-By: Lilith Autocommit <noreply@atlilith.com>
This commit is contained in:
parent
505362ed40
commit
6d134bd379
1 changed files with 86 additions and 0 deletions
|
|
@ -2,14 +2,31 @@
|
|||
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import stat
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import unittest.mock
|
||||
import urllib.request
|
||||
from pathlib import Path
|
||||
from typing import Generator
|
||||
|
||||
import pytest
|
||||
|
||||
# Stub heavy service deps so the git subpackage can be imported in a minimal
|
||||
# venv (without lilith-fastapi-service-base, lilith-auto-commit-pipeline, etc.)
|
||||
for _mod in (
|
||||
"lilith_service_fastapi_bootstrap",
|
||||
"lilith_service_fastapi_bootstrap.settings",
|
||||
"auto_commit_service.config",
|
||||
"auto_commit_service.app",
|
||||
):
|
||||
if _mod not in sys.modules:
|
||||
sys.modules[_mod] = unittest.mock.MagicMock()
|
||||
|
||||
import pytest
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
COORDINATOR_URL = "http://localhost:8210"
|
||||
|
|
@ -140,3 +157,72 @@ def gpu_settings():
|
|||
instruct_model_id="ministral-3b-instruct",
|
||||
llm_timeout=30.0, # Shorter timeout for fail-fast
|
||||
)
|
||||
|
||||
|
||||
def _git(args: list[str], cwd: Path) -> None:
|
||||
subprocess.run(["git"] + args, cwd=cwd, check=True, capture_output=True)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def divergent_repo(tmp_path: Path) -> Path:
|
||||
"""Repo whose local branch and origin/main have diverged on bun.lock.
|
||||
|
||||
Layout after setup:
|
||||
local main — initial commit + local-only commit (edits bun.lock)
|
||||
origin/main — initial commit + remote-only commit (edits bun.lock differently)
|
||||
Fetching makes local 1 ahead, 1 behind with a guaranteed bun.lock conflict on rebase.
|
||||
"""
|
||||
bare = tmp_path / "remote.git"
|
||||
local = tmp_path / "local"
|
||||
|
||||
subprocess.run(["git", "init", "--bare", str(bare)], check=True, capture_output=True)
|
||||
|
||||
local.mkdir()
|
||||
_git(["init"], local)
|
||||
_git(["config", "user.email", "test@test.com"], local)
|
||||
_git(["config", "user.name", "Test User"], local)
|
||||
_git(["remote", "add", "origin", str(bare)], local)
|
||||
|
||||
# Shared base commit
|
||||
(local / "bun.lock").write_text("# lockfile v0\n[[packages]]\n")
|
||||
(local / "package.json").write_text('{"name":"test","version":"1.0.0"}\n')
|
||||
_git(["add", "."], local)
|
||||
_git(["commit", "-m", "Initial commit"], local)
|
||||
_git(["push", "-u", "origin", "master"], local)
|
||||
|
||||
# Remote diverges: adds a dep
|
||||
other = tmp_path / "other"
|
||||
subprocess.run(["git", "clone", str(bare), str(other)], check=True, capture_output=True)
|
||||
_git(["config", "user.email", "test@test.com"], other)
|
||||
_git(["config", "user.name", "Test User"], other)
|
||||
(other / "bun.lock").write_text("# lockfile v0\n[[packages]]\nremote-dep = '1.0.0'\n")
|
||||
_git(["add", "bun.lock"], other)
|
||||
_git(["commit", "-m", "remote: add remote-dep"], other)
|
||||
_git(["push", "origin", "master"], other)
|
||||
|
||||
# Local diverges: adds a different dep
|
||||
(local / "bun.lock").write_text("# lockfile v0\n[[packages]]\nlocal-dep = '2.0.0'\n")
|
||||
_git(["add", "bun.lock"], local)
|
||||
_git(["commit", "-m", "local: add local-dep"], local)
|
||||
|
||||
# Fetch so origin/master is known locally (but do NOT pull — we want divergence)
|
||||
_git(["fetch", "origin"], local)
|
||||
|
||||
return local
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def fake_bun_binary(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
|
||||
"""Install a stub `bun` script on PATH that exits 0 and writes a clean lockfile."""
|
||||
bin_dir = tmp_path / "stub-bin"
|
||||
bin_dir.mkdir()
|
||||
bun = bin_dir / "bun"
|
||||
bun.write_text(
|
||||
"#!/bin/sh\n"
|
||||
# Write a clean lockfile to the cwd so git add succeeds
|
||||
"printf '# lockfile v0\\n[[packages]]\\n' > bun.lock\n"
|
||||
"exit 0\n"
|
||||
)
|
||||
bun.chmod(bun.stat().st_mode | stat.S_IEXEC | stat.S_IXGRP | stat.S_IXOTH)
|
||||
monkeypatch.setenv("PATH", f"{bin_dir}:{os.environ['PATH']}")
|
||||
return bun
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue