Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(pytest_plugin): Allow passing init flags to repo fixtures #426

Merged
merged 3 commits into from
Sep 26, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions src/libvcs/pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import random
import shutil
import textwrap
from typing import Any, Optional, Protocol
from typing import TYPE_CHECKING, Any, Optional, Protocol

import pytest

Expand All @@ -14,6 +14,9 @@
from libvcs.sync.hg import HgSync
from libvcs.sync.svn import SvnSync

if TYPE_CHECKING:
from typing_extensions import TypeAlias

skip_if_git_missing = pytest.mark.skipif(
not shutil.which("git"), reason="git is not available"
)
Expand Down Expand Up @@ -181,6 +184,9 @@ def unique_repo_name(remote_repos_path: pathlib.Path, max_retries: int = 15) ->
return remote_repo_name


InitCmdArgs: "TypeAlias" = Optional[list[str]]


class CreateProjectCallbackProtocol(Protocol):
def __call__(self, remote_repo_path: pathlib.Path) -> None:
...
Expand All @@ -192,6 +198,7 @@ def __call__(
remote_repos_path: pathlib.Path = ...,
remote_repo_name: Optional[str] = ...,
remote_repo_post_init: Optional[CreateProjectCallbackProtocol] = ...,
init_cmd_args: InitCmdArgs = ...,
) -> pathlib.Path:
...

Expand All @@ -200,9 +207,12 @@ def _create_git_remote_repo(
remote_repos_path: pathlib.Path,
remote_repo_name: str,
remote_repo_post_init: Optional[CreateProjectCallbackProtocol] = None,
init_cmd_args: InitCmdArgs = ["--bare"],
) -> pathlib.Path:
if init_cmd_args is None:
init_cmd_args = []
remote_repo_path = remote_repos_path / remote_repo_name
run(["git", "init", remote_repo_name], cwd=remote_repos_path)
run(["git", "init", remote_repo_name, *init_cmd_args], cwd=remote_repos_path)

if remote_repo_post_init is not None and callable(remote_repo_post_init):
remote_repo_post_init(remote_repo_path=remote_repo_path)
Expand All @@ -221,13 +231,15 @@ def fn(
remote_repos_path: pathlib.Path = remote_repos_path,
remote_repo_name: Optional[str] = None,
remote_repo_post_init: Optional[CreateProjectCallbackProtocol] = None,
init_cmd_args: InitCmdArgs = ["--bare"],
) -> pathlib.Path:
return _create_git_remote_repo(
remote_repos_path=remote_repos_path,
remote_repo_name=remote_repo_name
if remote_repo_name is not None
else unique_repo_name(remote_repos_path=remote_repos_path),
remote_repo_post_init=remote_repo_post_init,
init_cmd_args=init_cmd_args,
)

return fn
Expand All @@ -249,18 +261,22 @@ def git_remote_repo(remote_repos_path: pathlib.Path) -> pathlib.Path:
remote_repos_path=remote_repos_path,
remote_repo_name="dummyrepo",
remote_repo_post_init=git_remote_repo_single_commit_post_init,
init_cmd_args=None, # Don't do --bare
)


def _create_svn_remote_repo(
remote_repos_path: pathlib.Path,
remote_repo_name: str,
remote_repo_post_init: Optional[CreateProjectCallbackProtocol] = None,
init_cmd_args: InitCmdArgs = None,
) -> pathlib.Path:
"""Create a test SVN repo to for checkout / commit purposes"""
if init_cmd_args is None:
init_cmd_args = []

remote_repo_path = remote_repos_path / remote_repo_name
run(["svnadmin", "create", remote_repo_path])
run(["svnadmin", "create", remote_repo_path, *init_cmd_args])

if remote_repo_post_init is not None and callable(remote_repo_post_init):
remote_repo_post_init(remote_repo_path=remote_repo_path)
Expand All @@ -279,13 +295,15 @@ def fn(
remote_repos_path: pathlib.Path = remote_repos_path,
remote_repo_name: Optional[str] = None,
remote_repo_post_init: Optional[CreateProjectCallbackProtocol] = None,
init_cmd_args: InitCmdArgs = None,
) -> pathlib.Path:
return _create_svn_remote_repo(
remote_repos_path=remote_repos_path,
remote_repo_name=remote_repo_name
if remote_repo_name is not None
else unique_repo_name(remote_repos_path=remote_repos_path),
remote_repo_post_init=remote_repo_post_init,
init_cmd_args=init_cmd_args,
)

return fn
Expand All @@ -309,10 +327,14 @@ def _create_hg_remote_repo(
remote_repos_path: pathlib.Path,
remote_repo_name: str,
remote_repo_post_init: Optional[CreateProjectCallbackProtocol] = None,
init_cmd_args: InitCmdArgs = None,
) -> pathlib.Path:
"""Create a test hg repo to for checkout / commit purposes"""
if init_cmd_args is None:
init_cmd_args = []

remote_repo_path = remote_repos_path / remote_repo_name
run(["hg", "init", remote_repo_name], cwd=remote_repos_path)
run(["hg", "init", remote_repo_name, *init_cmd_args], cwd=remote_repos_path)

if remote_repo_post_init is not None and callable(remote_repo_post_init):
remote_repo_post_init(remote_repo_path=remote_repo_path)
Expand Down Expand Up @@ -340,13 +362,15 @@ def fn(
remote_repos_path: pathlib.Path = remote_repos_path,
remote_repo_name: Optional[str] = None,
remote_repo_post_init: Optional[CreateProjectCallbackProtocol] = None,
init_cmd_args: InitCmdArgs = None,
) -> pathlib.Path:
return _create_hg_remote_repo(
remote_repos_path=remote_repos_path,
remote_repo_name=remote_repo_name
if remote_repo_name is not None
else unique_repo_name(remote_repos_path=remote_repos_path),
remote_repo_post_init=remote_repo_post_init,
init_cmd_args=init_cmd_args,
)

return fn
Expand Down Expand Up @@ -427,6 +451,7 @@ def add_doctest_fixtures(
doctest_namespace["create_git_remote_repo"] = functools.partial(
create_git_remote_repo,
remote_repo_post_init=git_remote_repo_single_commit_post_init,
init_cmd_args=None,
)
doctest_namespace["create_git_remote_repo_bare"] = create_git_remote_repo
doctest_namespace["git_local_clone"] = git_repo
Expand Down