Skip to content

Commit 0e50a71

Browse files
committed
pytest_plugin(feat): Add async_git_repo fixture
why: Provide async fixture for testing with AsyncGitSync. what: - Add async_git_repo fixture using @pytest_asyncio.fixture - Conditional import for backward compatibility without pytest-asyncio
1 parent 391a418 commit 0e50a71

1 file changed

Lines changed: 53 additions & 0 deletions

File tree

src/libvcs/pytest_plugin.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@
1818
from libvcs.sync.hg import HgSync
1919
from libvcs.sync.svn import SvnSync
2020

21+
# Async support - conditional import
22+
try:
23+
import pytest_asyncio
24+
25+
from libvcs.sync._async.git import AsyncGitSync
26+
27+
HAS_PYTEST_ASYNCIO = True
28+
except ImportError:
29+
HAS_PYTEST_ASYNCIO = False
30+
2131

2232
class MaxUniqueRepoAttemptsExceeded(exc.LibVCSException):
2333
"""Raised when exceeded threshold of attempts to find a unique repo destination."""
@@ -780,6 +790,49 @@ def svn_repo(
780790
return svn_repo
781791

782792

793+
# =============================================================================
794+
# Async Fixtures
795+
# =============================================================================
796+
797+
if HAS_PYTEST_ASYNCIO:
798+
799+
@pytest_asyncio.fixture
800+
@skip_if_git_missing
801+
async def async_git_repo(
802+
remote_repos_path: pathlib.Path,
803+
projects_path: pathlib.Path,
804+
git_remote_repo: pathlib.Path,
805+
set_gitconfig: pathlib.Path,
806+
) -> t.AsyncGenerator[AsyncGitSync, None]:
807+
"""Pre-made async git clone of remote repo checked out to user's projects dir.
808+
809+
Async equivalent of :func:`git_repo` fixture.
810+
811+
Examples
812+
--------
813+
>>> @pytest.mark.asyncio
814+
... async def test_git_operations(async_git_repo):
815+
... revision = await async_git_repo.get_revision()
816+
... assert revision is not None
817+
"""
818+
remote_repo_name = unique_repo_name(remote_repos_path=projects_path)
819+
new_checkout_path = projects_path / remote_repo_name
820+
821+
repo = AsyncGitSync(
822+
url=f"file://{git_remote_repo}",
823+
path=new_checkout_path,
824+
remotes={
825+
"origin": GitRemote(
826+
name="origin",
827+
push_url=f"file://{git_remote_repo}",
828+
fetch_url=f"file://{git_remote_repo}",
829+
),
830+
},
831+
)
832+
await repo.obtain()
833+
yield repo
834+
835+
783836
@pytest.fixture
784837
def add_doctest_fixtures(
785838
request: pytest.FixtureRequest,

0 commit comments

Comments
 (0)